Parsem
Parsec-like parser combinators for Crystal.
Constants
VERSION = "1.1.2"
Instance methods
alternatives(parsers : Array(Parser(Token, Output))) : Parser(Token, Output) forall Token, Output
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.
concat(array_1 : Array(Value), array_2 : Array(Value)) : Array(Value) forall Value
Sourceconcat_string(string_1 : String, string_2 : String) : String
Sourceextend(value : Value, array : Array(Value)) : Array(Value) forall Value
Sourceextend(array : Array(Value), value : Value) : Array(Value) forall Value
Sourceextend_string(char : Char, string : String) : String
Sourceextend_string(string : String, char : Char) : String
Sourcenone_of(tokens : Array(Token)) : Parser(Token, Token) forall Token
Parses any single token not in tokens.
one_of(tokens : Array(Token)) : Parser(Token, Token) forall Token
Parses any single token of tokens.
string(string : String) : Parser(Char, String)
SourceMacros
infer(proc_pointer, file = __FILE__, line = __LINE__)
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.bor->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).
lazy(parser)
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.