vektah/parser-combinator

0.2.4 2014-02-12 06:49 UTC

This package is not auto-updated.

Last update: 2024-12-02 15:40:50 UTC


README

A simple parser combinator framework written in PHP.

Google protobuffer parser:

$parser = new ProtoParser();
$out = $parser->parse('
    message Bar {
        required MessageType type = 0;
        required bytes data = 1;

        enum Foo {
            asdf = 0;
            qwer = 1;
        }
    }
');

// $out will contain return a parse tree that looks like this:
[
    new Message('Bar', [
        new Field('required', 'MessageType', 'type', 0),
        new Field('required', 'bytes', 'data', 1),
        new Enum('Foo', [
            new EnumValue('asdf', 0),
            new EnumValue('qwer', 1)
        ])
    ])
];

Json parser:

The only reason you would use this parser is for error messages. It is much slower then the C library used by php to parse json. It could be used to automatically reparse json in failure cases and display helpful error messages.

$parser = new JsonParser();
$out = $parser->parse('{"asdf": { "foo" : "bar" }}');

// $out will contain a parse tree that looks like this:
[
  'asdf' => [
    'foo' => 'bar'
  ]
]