module Parsem
Overview
Parsec-like parser combinators for Crystal.
https://github.com/ThatsJustCheesy/parsem
Extended Modules
Defined in:
character_classes.crinfer.cr
parsem.cr
parser.cr
Constant Summary
-
VERSION =
"1.1.2"
Instance Method Summary
-
#alternatives(parsers : Array(Parser(Token, Output))) : Parser(Token, Output) forall Token, Output
Folds the choice operator (
Parser#|
) overparsers
. -
#any(type : Token.class) : Parser(Token, Token) forall Token
Parses any token of type
type
. - #concat(array_1 : Array(Value), array_2 : Array(Value)) : Array(Value) forall Value
- #concat_string(string_1 : String, string_2 : String) : String
-
#digit : Parser(Char, Char)
Parses a decimal digit (0–9).
- #extend(value : Value, array : Array(Value)) : Array(Value) forall Value
- #extend(array : Array(Value), value : Value) : Array(Value) forall Value
- #extend_string(char : Char, string : String) : String
- #extend_string(string : String, char : Char) : String
-
#letter : Parser(Char, Char)
Parses a letter of the Latin alphabet (a–z, A–Z).
-
#none_of(tokens : Array(Token)) : Parser(Token, Token) forall Token
Parses any single token not in
tokens
. -
#not(token : Token) : Parser(Token, Token) forall Token
Parses any token except
#token
. -
#one_of(tokens : Array(Token)) : Parser(Token, Token) forall Token
Parses any single token of
tokens
. - #string(string : String) : Parser(Char, String)
-
#token(token : Token) : Parser(Token, Token) forall Token
Parses
#token
. -
#whitespace : Parser(Char, Char)
Parses a whitespace character (space,
\t
,\r
,\n
). -
#ws
Shortcut for zero or more
#whitespace
characters.
Macro Summary
-
infer(proc_pointer, file = __FILE__, line = __LINE__)
Attempts to infer the parameter types of a
->A.b
proc literal. -
lazy(parser)
Defers the creation of
parser
until it is used.
Instance Method Detail
Folds the choice operator (Parser#|
) over parsers
.
That is, alternatives([parser_a, parser_b, parser_c])
is the same as parser_a | parser_b | parser_c
.
Fails unconditionally if parsers
is empty.
Parses any token of type type
.
Parses a letter of the Latin alphabet (a–z, A–Z).
TODO Unicode support
Parses any single token not in tokens
.
Parses any token except #token
.
Parses any single token of tokens
.
Parses a whitespace character (space, \t
, \r
, \n
).
TODO Unicode support
Macro Detail
Attempts to infer the parameter types of a ->A.b
proc literal.
For example:
record Foo, int32 : Int32
make_a_foo = infer(->Foo.new)
make_a_foo.call(123) # => Foo(@int32 = 123)
This macro has some finicky requirements:
- The proc literal must have a type name as receiver (like
->A.b
, not->a.b
or->b
) - The receiver must have exactly one method definition with the given name (no overloads)
- That method definition must have type restrictions on all of its parameters
- Those type restrictions must resolve correctly from the caller's context
- Nested types (
A::B
) should be fully qualified (not written as justB
) - Generic type parameters cannot be used
- Nested types (
If you can't satisfy these requirements, you can't use this macro.
In that case, specify the parameter types manually as usual, like ->A.b(Int32)
.
Defers the creation of parser
until it is used.
This allows parsers to be composed in terms of each other.
WARNING May incur a performance penalty.