4 API Reference
A parser is a value that represents a method of turning a syntax object or sequence of syntax objects an arbitrary Racket value. Parsers can be created using various primitives, then sequenced together using parser combinators to create larger parsers.
Parsers are functors, applicative functors, and monads, which allows them to be mapped over and sequenced together using the corresponding generic interfaces.
4.1 Primitives
procedure
parser : parser? boxes : (listof syntax-box?)
procedure
(parse-error->string message) → string?
message : message?
procedure
(parse-result! result) → any/c
result : (either/c message? any/c)
struct
(struct exn:fail:read:megaparsack exn:fail:read ( unexpected expected) #:transparent) unexpected : any/c expected : (listof string?)
struct
(struct syntax-box (datum srcloc) #:transparent) datum : any/c srcloc : srcloc?
The datum can be anything at all, but usually it is either a character or some token produced as the result of lexing. It is unlikely that you will need to create syntax-box values yourself; rather, use higher-level functions like parse-string that create these values for you.
struct
(struct message (srcloc unexpected expected) #:transparent) srcloc : srcloc? unexpected : any/c expected : (listof string?)
If no parsers consume input, then the first successful empty parser is used instead. If all parsers fail, the result will be a failure that combines failure information from each parser attempted.
The syntax/p combinator makes source location wrapping opt-in, which is desirable since it is often useful to return values from combinators that are intermediate values not intended to be wrapped in syntax (for example, many/p returns a list of results, not a syntax list).
procedure
(syntax-box/p parser) → (parser/c any/c syntax-box?)
parser : parser?
procedure
(many/p parser [ #:sep sep #:min min-count #:max max-count]) → (parser/c any/c list?) parser : parser? sep : parser? = void/p min-count : exact-nonnegative-integer? = 0 max-count : (or/c exact-nonnegative-integer? +inf.0) = +inf.0
> (define letters/p (many/p letter/p)) > (parse-result! (parse-string letters/p "abc")) '(#\a #\b #\c)
> (define dotted-letters/p (many/p letter/p #:sep (char/p #\.) #:min 2 #:max 4)) > (parse-result! (parse-string dotted-letters/p "a.b.c")) '(#\a #\b #\c)
> (parse-result! (parse-string dotted-letters/p "abc")) string:1:0: parse error
unexpected: b
expected: '.'
> (parse-result! (parse-string dotted-letters/p "a")) string:1:0: parse error
unexpected: end of input
expected: '.'
> (parse-result! (parse-string dotted-letters/p "a.b.c.d.e")) '(#\a #\b #\c #\d)
Added in version 1.1 of package megaparsack-lib.
procedure
(many+/p parser [#:sep sep #:max max-count])
→ (parser/c any/c list?) parser : parser? sep : parser? = void/p max-count : (or/c exact-nonnegative-integer? +inf.0) = +inf.0
Changed in version 1.1 of package megaparsack-lib: Added support for #:sep and #:max keyword arguments for consistency with many/p.
procedure
n : exact-nonnegative-integer? parser : parser?
> (parse-result! (parse-string (one-of/p '(#\a #\b)) "a")) #\a
> (parse-result! (parse-string (one-of/p '(#\a #\b)) "b")) #\b
> (parse-result! (parse-string (one-of/p '(#\a #\b)) "c")) string:1:0: parse error
unexpected: c
expected: a or b
Added in version 1.2 of package megaparsack-lib.
procedure
(guard/p parser pred? [ expected make-unexpected]) → parser? parser : parser? pred? : (any/c . -> . any/c) expected : (or/c string? #f) = #f make-unexpected : (any/c . -> . any/c) = identity
If the parser fails and expected is a string, then expected is used to add expected information to the parser error. Additionally, the make-unexpected function is applied to the result of parser to produce the unexpected field of the parse error.
> (define small-integer/p (guard/p integer/p (λ (x) (<= x 100)) "integer in range [0,100]")) > (parse-result! (parse-string small-integer/p "42")) 42
> (parse-result! (parse-string small-integer/p "300")) string:1:0: parse error
unexpected: 300
expected: integer in range [0,100]
> (define dotted-let-digit-let/p (list/p letter/p digit/p letter/p #:sep (char/p #\.))) > (parse-result! (parse-string dotted-let-digit-let/p "a.1.b")) '(#\a #\1 #\b)
> (parse-result! (parse-string dotted-let-digit-let/p "a1c")) string:1:0: parse error
unexpected: 1
expected: '.'
> (parse-result! (parse-string dotted-let-digit-let/p "a.1")) string:1:2: parse error
unexpected: end of input
expected: '.'
Using a separator parser that consumes no input (such as the default separator, void/p) is equivalent to not using a separator at all.
> (define let-digit-let/p (list/p letter/p digit/p letter/p)) > (parse-result! (parse-string let-digit-let/p "a1b")) '(#\a #\1 #\b)
4.2 Parsing Text
(require megaparsack/text) | package: megaparsack-lib |
procedure
(parse-string parser str [src-name]) → (either/c message? any/c)
parser : (parser/c char? any/c) str : string? src-name : any/c = 'string
procedure
(parse-syntax-string parser stx-str) → (either/c message? any/c)
parser : (parser/c char? any/c) stx-str : (syntax/c string?)
procedure
(char-not/p c) → (parser/c char? char?)
c : char?
Added in version 1.3 of package megaparsack-lib.
> (parse-result! (parse-string (char-between/p #\a #\z) "d")) #\d
> (parse-result! (parse-string (char-between/p #\a #\z) "D")) string:1:0: parse error
unexpected: D
expected: a character between 'a' and 'z'
Added in version 1.2 of package megaparsack-lib.
> (parse-result! (parse-string (char-in/p "aeiou") "i")) #\i
> (parse-result! (parse-string (char-in/p "aeiou") "z")) string:1:0: parse error
unexpected: z
expected: 'a', 'e', 'i', 'o', or 'u'
Added in version 1.2 of package megaparsack-lib.
procedure
(char-not-in/p alphabet) → (parser/c char? char?)
alphabet : string?
Added in version 1.3 of package megaparsack-lib.
value
any-char/p : (parser/c char? char?)
Added in version 1.3 of package megaparsack-lib.
value
symbolic/p : (parser/c char? char?)
procedure
(string-ci/p str) → (parser/c char? string?)
str : string?
Added in version 1.3 of package megaparsack-lib.
4.3 Parsing with parser-tools/lex
(require megaparsack/parser-tools/lex) | |
package: megaparsack-parser-tools |
Sometimes it is useful to run a lexing pass over an input stream before parsing, in which case megaparsack/text is not appropriate. The parser-tools package provides the parser-tools/lex library, which implements a lexer that produces tokens.
When using parser-tools/lex, use lexer-src-pos instead of lexer to enable the built-in source location tracking. This will produce a sequence of position-token elements, which can then be passed to parse-tokens and detected with token/p.
procedure
(parse-tokens parser tokens [source-name]) → syntax?
parser : parser? tokens : (listof position-token?) source-name : any/c = 'tokens
4.4 Deprecated Forms and Functions
NOTE: This function is deprecated; use many/p, instead.
procedure
(many/sep*/p parser sep) → parser?
parser : parser? sep : parser?
NOTE: This function is deprecated; use (many/p parser #:sep sep), instead.
procedure
(many/sep+/p parser sep) → parser?
parser : parser? sep : parser?
NOTE: This function is deprecated; use (many+/p parser #:sep sep), instead.