Parser

interface for all parser combinators parse must be implemented.

Members

Functions

opBinary
Parser opBinary(Variant[] function(Variant[] objects) toCall)

dsl for transforming results of a parser

opBinary
Parser opBinary(Parser rhs)

dsl for alternatives e.g. match("abc") | match("def") matches "abc" or "def"

opBinary
Parser opBinary(Parser rhs)

dsl for sequences e.g. match("a") ~ match("b") matches "ab"

opUnary
Parser opUnary()

dsl for repetition of a parser e.g. (*match("a")) matches sequences of a

opUnary
Parser opUnary()

dsl for optional parser e.g. (-match("abc")) matches "abc" and "efg"

parse
ParseResult!(T) parse(T[] input)

this must be implemented by subclasses

parseAll
ParseResult!(T) parseAll(T[] s)
Undocumented in source. Be warned that the author may not have intended to support it.
setCallback
Parser setCallback(Variant[] function(Variant[] objects) tocall)
Undocumented in source. Be warned that the author may not have intended to support it.
setCallback
Parser setCallback(Variant[] delegate(Variant[] objects) tocall)
Undocumented in source. Be warned that the author may not have intended to support it.
transform
ParseResult!(T) transform(ParseResult!(T) result)
Undocumented in source. Be warned that the author may not have intended to support it.

Static functions

success
success(T[] rest, U args)

helper to create a successfull result

Variables

fCallable
Variant[] delegate(Variant[]) fCallable;
Undocumented in source.

Examples

transforming from regexp string to integer

import std.conv;

auto res = (regex("\\d+") ^^ (input) {
    return variantArray(input[0].get!string
        .to!int);
}).parse("123");
res.success.should == true;
res.results[0].should == 123;

the pc4d.alternative parser and its dsl '|'

auto parser = match("abc") | match("def");
auto res = parser.parse("abc");
res.success.should == true;

res = parser.parse("def");
res.success.should == true;

res = parser.parse("ghi");
res.success.should == false;

trying to parse all of the input

auto parser = match("test");
auto res = parser.parseAll("test");

res.success.should == true;
res.rest.length.should == 0;

res = parser.parseAll("test1");
res.success.should == false;

trying to parse part of the input

auto parser = match("test");
auto res = parser.parse("test");

res.success.should == true;
res.rest.length.should == 0;

res = parser.parse("test1");
res.success.should == true;

res.rest.should == "1";

Meta