module Parsem

Overview

Parsec-like parser combinators for Crystal.

https://github.com/ThatsJustCheesy/parsem

Extended Modules

Defined in:

character_classes.cr
infer.cr
parsem.cr
parser.cr

Constant Summary

VERSION = "1.1.2"

Instance Method Summary

Macro Summary

Instance Method Detail

def 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.


[View source]
def any(type : Token.class) : Parser(Token, Token) forall Token #

Parses any token of type type.


[View source]
def concat(array_1 : Array(Value), array_2 : Array(Value)) : Array(Value) forall Value #

[View source]
def concat_string(string_1 : String, string_2 : String) : String #

[View source]
def digit : Parser(Char, Char) #

Parses a decimal digit (0–9).

TODO Unicode support


[View source]
def extend(value : Value, array : Array(Value)) : Array(Value) forall Value #

[View source]
def extend(array : Array(Value), value : Value) : Array(Value) forall Value #

[View source]
def extend_string(char : Char, string : String) : String #

[View source]
def extend_string(string : String, char : Char) : String #

[View source]
def letter : Parser(Char, Char) #

Parses a letter of the Latin alphabet (a–z, A–Z).

TODO Unicode support


[View source]
def none_of(tokens : Array(Token)) : Parser(Token, Token) forall Token #

Parses any single token not in tokens.


[View source]
def not(token : Token) : Parser(Token, Token) forall Token #

Parses any token except #token.


[View source]
def one_of(tokens : Array(Token)) : Parser(Token, Token) forall Token #

Parses any single token of tokens.


[View source]
def string(string : String) : Parser(Char, String) #

[View source]
def token(token : Token) : Parser(Token, Token) forall Token #

Parses #token.


[View source]
def whitespace : Parser(Char, Char) #

Parses a whitespace character (space, \t, \r, \n).

TODO Unicode support


[View source]
def ws #

Shortcut for zero or more #whitespace characters.


[View source]

Macro Detail

macro 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.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 just B)
    • Generic type parameters cannot be used

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).


[View source]
macro 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.


[View source]