From 1b58ba404ec22cef9d8713369c6aa4fa47387864 Mon Sep 17 00:00:00 2001 From: Chris Double Date: Wed, 2 Apr 2008 13:50:29 +1300 Subject: [PATCH] Fix peg.pl0 test failures --- extra/peg/pl0/pl0-tests.factor | 47 +++++++++++++++++++++++++++++++++- extra/peg/pl0/pl0.factor | 26 ++++++++++--------- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/extra/peg/pl0/pl0-tests.factor b/extra/peg/pl0/pl0-tests.factor index b3d2135da7..1ed528d05d 100644 --- a/extra/peg/pl0/pl0-tests.factor +++ b/extra/peg/pl0/pl0-tests.factor @@ -1,9 +1,54 @@ ! Copyright (C) 2007 Chris Double. ! See http://factorcode.org/license.txt for BSD license. ! -USING: kernel tools.test peg peg.pl0 multiline sequences ; +USING: kernel tools.test peg peg.pl0 multiline sequences words assocs ; IN: peg.pl0.tests +{ f } [ + "CONST foo = 1;" \ pl0 "ebnf-parser" word-prop "block" swap at parse not +] unit-test + +{ f } [ + "VAR foo;" \ pl0 "ebnf-parser" word-prop "block" swap at parse not +] unit-test + +{ f } [ + "VAR foo,bar , baz;" \ pl0 "ebnf-parser" word-prop "block" swap at parse not +] unit-test + +{ f } [ + "foo := 5;" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not +] unit-test + +{ f } [ + "BEGIN foo := 5; END" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not +] unit-test + +{ f } [ + "IF 1=1 THEN foo := 5; END" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not +] unit-test + +{ f } [ + "WHILE 1=1 DO foo := 5; END" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not +] unit-test + +{ f } [ + "WHILE ODD 1=1 DO foo := 5; END" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not +] unit-test + +{ f } [ + "PROCEDURE square; BEGIN squ=x*x END" \ pl0 "ebnf-parser" word-prop "block" swap at parse not +] unit-test + +{ f } [ + <" +PROCEDURE square; +BEGIN + squ := x * x +END; +"> \ pl0 "ebnf-parser" word-prop "block" swap at parse not +] unit-test + { t } [ <" VAR x, squ; diff --git a/extra/peg/pl0/pl0.factor b/extra/peg/pl0/pl0.factor index f7eb3cad23..8025728285 100644 --- a/extra/peg/pl0/pl0.factor +++ b/extra/peg/pl0/pl0.factor @@ -7,18 +7,20 @@ IN: peg.pl0 #! Grammar for PL/0 based on http://en.wikipedia.org/wiki/PL/0 EBNF: pl0 -block = ( "CONST" ident "=" number ( "," ident "=" number )* ";" )? - ( "VAR" ident ( "," ident )* ";" )? - ( "PROCEDURE" ident ";" ( block ";" )? )* statement -statement = ( ident ":=" expression | "CALL" ident | - "BEGIN" statement (";" statement )* "END" | - "IF" condition "THEN" statement | - "WHILE" condition "DO" statement )? -condition = "ODD" expression | - expression ("=" | "#" | "<=" | "<" | ">=" | ">") expression -expression = ("+" | "-")? term (("+" | "-") term )* -term = factor (("*" | "/") factor )* -factor = ident | number | "(" expression ")" +- = (" " | "\t" | "\n")+ => [[ drop ignore ]] +_ = (" " | "\t" | "\n")* => [[ drop ignore ]] +block = ( _ "CONST" - ident _ "=" _ number ( _ "," _ ident _ "=" _ number )* _ ";" )? + ( _ "VAR" - ident ( _ "," _ ident )* _ ";" )? + ( _ "PROCEDURE" - ident _ ";" ( _ block _ ";" )? )* _ statement +statement = ( ident _ ":=" _ expression | "CALL" - ident | + "BEGIN" - statement ( _ ";" _ statement )* _ "END" | + "IF" - condition _ "THEN" - statement | + "WHILE" - condition _ "DO" - statement )? +condition = "ODD" - expression | + expression _ ("=" | "#" | "<=" | "<" | ">=" | ">") _ expression +expression = ("+" | "-")? term ( _ ("+" | "-") _ term )* +term = factor ( _ ("*" | "/") _ factor )* +factor = ident | number | "(" _ expression _ ")" ident = (([a-zA-Z])+) [[ >string ]] digit = ([0-9]) [[ digit> ]] number = ((digit)+) [[ unclip [ swap 10 * + ] reduce ]]