Fix peg.pl0 test failures

db4
Chris Double 2008-04-02 13:50:29 +13:00
parent 9d0485cd8a
commit 1b58ba404e
2 changed files with 60 additions and 13 deletions

View File

@ -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;

View File

@ -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 ]]