Skip to content

Instantly share code, notes, and snippets.

@jddurand
Last active December 15, 2015 00:29
Show Gist options
  • Save jddurand/5173485 to your computer and use it in GitHub Desktop.
Save jddurand/5173485 to your computer and use it in GitHub Desktop.
#!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