PL0 whitespace handling improvement
parent
bbcc84862f
commit
34a1505d95
|
@ -4,40 +4,40 @@
|
||||||
USING: kernel tools.test peg peg.pl0 multiline sequences words assocs ;
|
USING: kernel tools.test peg peg.pl0 multiline sequences words assocs ;
|
||||||
IN: peg.pl0.tests
|
IN: peg.pl0.tests
|
||||||
|
|
||||||
{ f } [
|
{ t } [
|
||||||
"CONST foo = 1;" \ pl0 "ebnf-parser" word-prop "block" swap at parse not
|
"CONST foo = 1;" \ pl0 "ebnf-parser" word-prop "block" swap at parse parse-result-remaining empty?
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ f } [
|
{ t } [
|
||||||
"VAR foo;" \ pl0 "ebnf-parser" word-prop "block" swap at parse not
|
"VAR foo;" \ pl0 "ebnf-parser" word-prop "block" swap at parse parse-result-remaining empty?
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ f } [
|
{ t } [
|
||||||
"VAR foo,bar , baz;" \ pl0 "ebnf-parser" word-prop "block" swap at parse not
|
"VAR foo,bar , baz;" \ pl0 "ebnf-parser" word-prop "block" swap at parse parse-result-remaining empty?
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ f } [
|
{ t } [
|
||||||
"foo := 5;" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not
|
"foo := 5" \ pl0 "ebnf-parser" word-prop "statement" swap at parse parse-result-remaining empty?
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ f } [
|
{ t } [
|
||||||
"BEGIN foo := 5; END" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not
|
"BEGIN foo := 5 END" \ pl0 "ebnf-parser" word-prop "statement" swap at parse parse-result-remaining empty?
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ f } [
|
{ t } [
|
||||||
"IF 1=1 THEN foo := 5; END" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not
|
"IF 1=1 THEN foo := 5" \ pl0 "ebnf-parser" word-prop "statement" swap at parse parse-result-remaining empty?
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ f } [
|
{ t } [
|
||||||
"WHILE 1=1 DO foo := 5; END" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not
|
"WHILE 1=1 DO foo := 5" \ pl0 "ebnf-parser" word-prop "statement" swap at parse parse-result-remaining empty?
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ f } [
|
{ t } [
|
||||||
"WHILE ODD 1=1 DO foo := 5; END" \ pl0 "ebnf-parser" word-prop "statement" swap at parse not
|
"WHILE ODD 1 DO foo := 5" \ pl0 "ebnf-parser" word-prop "statement" swap at parse parse-result-remaining empty?
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ f } [
|
{ t } [
|
||||||
"PROCEDURE square; BEGIN squ=x*x END" \ pl0 "ebnf-parser" word-prop "block" swap at parse not
|
"PROCEDURE square; BEGIN squ:=x*x END" \ pl0 "ebnf-parser" word-prop "block" swap at parse parse-result-remaining empty?
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ f } [
|
{ f } [
|
||||||
|
|
|
@ -7,22 +7,52 @@ IN: peg.pl0
|
||||||
#! Grammar for PL/0 based on http://en.wikipedia.org/wiki/PL/0
|
#! Grammar for PL/0 based on http://en.wikipedia.org/wiki/PL/0
|
||||||
|
|
||||||
EBNF: pl0
|
EBNF: pl0
|
||||||
- = (" " | "\t" | "\n")+ => [[ drop ignore ]]
|
|
||||||
_ = (" " | "\t" | "\n")* => [[ drop ignore ]]
|
_ = (" " | "\t" | "\n")* => [[ drop ignore ]]
|
||||||
block = ( _ "CONST" - ident _ "=" _ number ( _ "," _ ident _ "=" _ number )* _ ";" )?
|
|
||||||
( _ "VAR" - ident ( _ "," _ ident )* _ ";" )?
|
BEGIN = "BEGIN" _
|
||||||
( _ "PROCEDURE" - ident _ ";" ( _ block _ ";" )? )* _ statement
|
CALL = "CALL" _
|
||||||
statement = ( ident _ ":=" _ expression | "CALL" - ident |
|
CONST = "CONST" _
|
||||||
"BEGIN" - statement ( _ ";" _ statement )* _ "END" |
|
DO = "DO" _
|
||||||
"IF" - condition _ "THEN" - statement |
|
END = "END" _
|
||||||
"WHILE" - condition _ "DO" - statement )?
|
IF = "IF" _
|
||||||
condition = "ODD" - expression |
|
THEN = "THEN" _
|
||||||
expression _ ("=" | "#" | "<=" | "<" | ">=" | ">") _ expression
|
ODD = "ODD" _
|
||||||
expression = ("+" | "-")? term ( _ ("+" | "-") _ term )*
|
PROCEDURE = "PROCEDURE" _
|
||||||
term = factor ( _ ("*" | "/") _ factor )*
|
VAR = "VAR" _
|
||||||
factor = ident | number | "(" _ expression _ ")"
|
WHILE = "WHILE" _
|
||||||
ident = (([a-zA-Z])+) [[ >string ]]
|
EQ = "=" _
|
||||||
digit = ([0-9]) [[ digit> ]]
|
LTEQ = "<=" _
|
||||||
number = ((digit)+) [[ unclip [ swap 10 * + ] reduce ]]
|
LT = "<" _
|
||||||
program = block "."
|
GT = ">" _
|
||||||
|
GTEQ = ">=" _
|
||||||
|
NEQ = "#" _
|
||||||
|
COMMA = "," _
|
||||||
|
SEMICOLON = ";" _
|
||||||
|
ASSIGN = ":=" _
|
||||||
|
|
||||||
|
ADD = "+" _
|
||||||
|
SUBTRACT = "-" _
|
||||||
|
MULTIPLY = "*" _
|
||||||
|
DIVIDE = "/" _
|
||||||
|
|
||||||
|
LPAREN = "(" _
|
||||||
|
RPAREN = ")" _
|
||||||
|
|
||||||
|
block = ( CONST ident EQ number ( COMMA ident EQ number )* SEMICOLON )?
|
||||||
|
( VAR ident ( COMMA ident )* SEMICOLON )?
|
||||||
|
( PROCEDURE ident SEMICOLON ( block SEMICOLON )? )* statement
|
||||||
|
statement = ( ident ASSIGN expression
|
||||||
|
| CALL ident
|
||||||
|
| BEGIN statement ( SEMICOLON statement )* END
|
||||||
|
| IF condition THEN statement
|
||||||
|
| WHILE condition DO statement )?
|
||||||
|
condition = ODD expression
|
||||||
|
| expression (EQ | NEQ | LTEQ | LT | GTEQ | GT) expression
|
||||||
|
expression = (ADD | SUBTRACT)? term ( (ADD | SUBTRACT) term )* _
|
||||||
|
term = factor ( (MULTIPLY | DIVIDE) factor )*
|
||||||
|
factor = ident | number | LPAREN expression RPAREN
|
||||||
|
ident = (([a-zA-Z])+) _ => [[ >string ]]
|
||||||
|
digit = ([0-9]) => [[ digit> ]]
|
||||||
|
number = ((digit)+) _ => [[ 10 digits>integer ]]
|
||||||
|
program = _ block "."
|
||||||
;EBNF
|
;EBNF
|
||||||
|
|
Loading…
Reference in New Issue