Permalink
Browse files

Trying a grammar with error reporting

  • Loading branch information...
1 parent cc425ac commit d1b59fd16badda2e0aec260581d7083efa9d8271 @pstuifzand committed Mar 24, 2012
Showing with 13 additions and 11 deletions.
  1. +10 −9 examples/lisp/lisp.mp
  2. +1 −1 examples/lisp/lisp.pl
  3. +2 −1 examples/lisp/test.lsp
View
@@ -1,16 +1,17 @@
-String = /"([^"]+)"/
+String = /"([^"]*)"/
Number = /(\d+)/
Symbol = /([a-z]+)/
Space = /[ \r\n\t]+/
-Parser ::= Expression+ {{ shift; return \@_; }}
-Expression ::= WS $( WS Parser WS $) WS {{ shift; return $_[3]; }}
-Expression ::= WS $( WS Expression WS $. WS Expression WS $) WS {{ shift; return [ $_[3], $_[7] ]; }}
-Expression ::= WS $( WS $) WS {{ shift; return 'nil'; }}
-Expression ::= WS String WS {{ shift; return { string => $_[1] }; }}
-Expression ::= WS Number WS {{ shift; return { number => $_[1] }; }}
-Expression ::= WS Symbol WS {{ shift; return $_[1]; }}
-Expression ::= WS $' Expression WS {{ shift; return [ 'quote', $_[2] ]; }}
+Parser ::= Expression+ {{ my $lsp = shift; return $lsp if $lsp->{error}; return \@_; }}
+Expression ::= WS $( WS Parser WS $) WS {{ shift; return $_[3]; }}
+Expression ::= WS $( WS Parser WS {{ my $lisp = shift; $lisp->{error} = "Expression is not closed"; return;}}
+Expression ::= WS $( WS Expression WS $. WS Expression WS $) WS {{ shift; return [ $_[3], $_[7] ]; }}
+Expression ::= WS $( WS $) WS {{ shift; return []; }}
+Expression ::= WS String WS {{ shift; return { type => 'string', val => $_[1] }; }}
+Expression ::= WS Number WS {{ shift; return { type => 'number', val => $_[1] }; }}
+Expression ::= WS Symbol WS {{ shift; return { type => 'symbol', val => $_[1] }; }}
+Expression ::= WS $' Expression WS {{ shift; return [ 'quote', $_[2] ]; }}
WS ::= Space
WS ::= Null
View
@@ -13,5 +13,5 @@
my $parser = MarpaX::Parser::Lisp->new;
my $parse_tree = $parser->parse($fh);
-$codegen->generate_code($parse_tree);
+$codegen->generate_code(*STDOUT, $parse_tree);
View
@@ -1 +1,2 @@
-3 ((1) . 5) 4
+(while t
+ (message "Hello World"

0 comments on commit d1b59fd

Please sign in to comment.