Last active
December 15, 2015 00:29
-
-
Save jddurand/5173485 to your computer and use it in GitHub Desktop.
Tentative answer to https://groups.google.com/d/msg/marpa-parser/8QK1x69MmXA/SyjyeryTOa8J
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!env perl | |
use strict; | |
use diagnostics; | |
use Marpa::R2; | |
use POSIX; | |
use Data::Dumper; | |
my $source = do { local $/; <DATA> }; | |
my $grammar = Marpa::R2::Scanless::G->new( { bless_package => 'test', source => \$source }); | |
my @input; | |
push(@input, "first; second; if(a) b; else c; d;"); | |
push(@input, " | |
Some \x{263a}Unicode\x{05d0} in\x{10a0}put; | |
// comment which needs to be parsed, not ignored | |
Second expression; | |
if(Something) | |
First thing | |
else | |
Other thing; | |
End | |
"); | |
print "Rules:\n" . $grammar->show_rules() . "\n"; | |
foreach (@input) { | |
print "Input:\n<CUT HERE>\n$_</CUT HERE>\n"; | |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); | |
$recce->read(\$_); | |
my $value_ref = $recce->value(); | |
print Dumper($value_ref); | |
} | |
exit(EXIT_SUCCESS); | |
__DATA__ | |
#################################### | |
# Defaults | |
#################################### | |
:default ::= action => [values] bless => ::lhs | |
#################################### | |
# G1 rules | |
#################################### | |
:start ::= instructions | |
expression ::= text3 ';' | |
| text3 | |
elseblock ::= 'else' instruction | |
ifblock ::= 'if' '(' text ')' instruction | |
ifschain ::= ifblock elseblock | |
| ifblock | |
instruction ::= comment | |
| ifschain | |
| expression | |
instructions ::= instruction+ | |
#################################### | |
# G0 rules | |
#################################### | |
text_0 ~ '\\'| '\)'| '\(' | [\w] | [^\s()] | |
text ::= text_0+ | |
text3_0 ~ '\\' | '\[' | '\}' | '\{' | '\;' | [\w] | [^\s\[;{}] | |
text3 ::= text3_0+ | |
# | |
## C comment, c.f. https://gist.github.com/jeffreykegler/5015057 | |
# ------------------------------------------------------------- | |
<C style comment> ~ '/*' <comment interior> '*/' | |
<comment interior> ~ | |
<optional non stars> | |
<optional star prefixed segments> | |
<optional pre final stars> | |
<optional non stars> ~ [^*]* | |
<optional star prefixed segments> ~ <star prefixed segment>* | |
<star prefixed segment> ~ <stars> [^/*] <optional star free text> | |
<stars> ~ [*]+ | |
<optional star free text> ~ [^*]* | |
<optional pre final stars> ~ [*]* | |
comment ~ <C style comment> | |
# | |
## C++ comment | |
# ----------- | |
<Cplusplus style comment> ~ '//' <Cplusplus comment interior> | |
<Cplusplus comment interior> ~ [^\n]* | |
comment ~ <Cplusplus style comment> | |
# | |
## Generic discard | |
# --------------- | |
ws ~ [\s]+ | |
:discard ~ ws |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment