factor/basis/peg/ebnf/ebnf-tests.factor

523 lines
12 KiB
Factor
Raw Normal View History

2007-11-27 00:13:36 -05:00
! Copyright (C) 2007 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
!
2008-04-28 22:19:14 -04:00
USING: kernel tools.test peg peg.ebnf words math math.parser
2008-06-20 08:28:13 -04:00
sequences accessors peg.parsers parser namespaces arrays
strings eval ;
2008-03-01 17:00:45 -05:00
IN: peg.ebnf.tests
2007-11-27 00:13:36 -05:00
{ T{ ebnf-non-terminal f "abc" } } [
2008-07-03 22:20:19 -04:00
"abc" 'non-terminal' parse
2007-11-27 00:13:36 -05:00
] unit-test
{ T{ ebnf-terminal f "55" } } [
2008-07-03 22:20:19 -04:00
"'55'" 'terminal' parse
2007-11-27 00:13:36 -05:00
] unit-test
2007-11-27 16:28:28 -05:00
{
T{ ebnf-rule f
2007-11-27 19:05:53 -05:00
"digit"
T{ ebnf-choice f
V{ T{ ebnf-terminal f "1" } T{ ebnf-terminal f "2" } }
2007-11-27 16:28:28 -05:00
}
}
} [
2008-07-03 22:20:19 -04:00
"digit = '1' | '2'" 'rule' parse
2007-11-27 16:28:28 -05:00
] unit-test
{
T{ ebnf-rule f
"digit"
T{ ebnf-sequence f
V{ T{ ebnf-terminal f "1" } T{ ebnf-terminal f "2" } }
2007-11-27 16:28:28 -05:00
}
2007-11-27 19:05:53 -05:00
}
2007-11-27 16:28:28 -05:00
} [
2008-07-03 22:20:19 -04:00
"digit = '1' '2'" 'rule' parse
] unit-test
{
T{ ebnf-choice f
V{
T{ ebnf-sequence f
V{ T{ ebnf-non-terminal f "one" } T{ ebnf-non-terminal f "two" } }
}
T{ ebnf-non-terminal f "three" }
}
}
} [
2008-07-03 22:20:19 -04:00
"one two | three" 'choice' parse
2007-11-27 21:26:25 -05:00
] unit-test
{
T{ ebnf-sequence f
V{
T{ ebnf-non-terminal f "one" }
2008-04-28 22:15:05 -04:00
T{ ebnf-whitespace f
T{ ebnf-choice f
V{ T{ ebnf-non-terminal f "two" } T{ ebnf-non-terminal f "three" } }
}
2007-11-27 21:26:25 -05:00
}
}
}
} [
2008-07-03 22:20:19 -04:00
"one {two | three}" 'choice' parse
2007-11-27 21:32:04 -05:00
] unit-test
{
T{ ebnf-sequence f
V{
T{ ebnf-non-terminal f "one" }
T{ ebnf-repeat0 f
T{ ebnf-sequence f
V{
T{ ebnf-choice f
V{ T{ ebnf-non-terminal f "two" } T{ ebnf-non-terminal f "three" } }
}
T{ ebnf-non-terminal f "four" }
}
}
}
}
}
} [
2008-07-03 22:20:19 -04:00
"one ((two | three) four)*" 'choice' parse
2007-11-27 21:49:14 -05:00
] unit-test
{
T{ ebnf-sequence f
V{
T{ ebnf-non-terminal f "one" }
T{ ebnf-optional f T{ ebnf-non-terminal f "two" } }
T{ ebnf-non-terminal f "three" }
}
}
} [
2008-07-03 22:20:19 -04:00
"one ( two )? three" 'choice' parse
2007-11-27 21:49:14 -05:00
] unit-test
2008-03-19 00:34:28 -04:00
{ "foo" } [
2008-07-03 22:20:19 -04:00
"\"foo\"" 'identifier' parse
2008-03-19 00:34:28 -04:00
] unit-test
{ "foo" } [
2008-07-03 22:20:19 -04:00
"'foo'" 'identifier' parse
2008-03-19 00:34:28 -04:00
] unit-test
{ "foo" } [
2008-09-02 16:13:01 -04:00
"foo" 'non-terminal' parse symbol>>
2008-03-19 00:34:28 -04:00
] unit-test
{ "foo" } [
2008-09-02 16:13:01 -04:00
"foo]" 'non-terminal' parse symbol>>
2008-03-19 00:34:28 -04:00
] unit-test
{ V{ "a" "b" } } [
2008-07-03 23:48:52 -04:00
"ab" [EBNF foo='a' 'b' EBNF]
] unit-test
{ V{ 1 "b" } } [
2008-07-03 23:48:52 -04:00
"ab" [EBNF foo=('a')[[ drop 1 ]] 'b' EBNF]
] unit-test
{ V{ 1 2 } } [
2008-07-03 23:48:52 -04:00
"ab" [EBNF foo=('a') [[ drop 1 ]] ('b') [[ drop 2 ]] EBNF]
] unit-test
{ CHAR: A } [
2008-07-03 23:48:52 -04:00
"A" [EBNF foo=[A-Z] EBNF]
] unit-test
{ CHAR: Z } [
2008-07-03 23:48:52 -04:00
"Z" [EBNF foo=[A-Z] EBNF]
] unit-test
2008-06-29 22:32:20 -04:00
[
"0" [EBNF foo=[A-Z] EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
{ CHAR: 0 } [
2008-07-03 23:48:52 -04:00
"0" [EBNF foo=[^A-Z] EBNF]
] unit-test
2008-06-29 22:32:20 -04:00
[
"A" [EBNF foo=[^A-Z] EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-06-29 22:32:20 -04:00
[
"Z" [EBNF foo=[^A-Z] EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-03-27 06:54:34 -04:00
{ V{ "1" "+" "foo" } } [
2008-07-03 23:48:52 -04:00
"1+1" [EBNF foo='1' '+' '1' [[ drop "foo" ]] EBNF]
] unit-test
{ "foo" } [
2008-07-03 23:48:52 -04:00
"1+1" [EBNF foo='1' '+' '1' => [[ drop "foo" ]] EBNF]
] unit-test
{ "foo" } [
2008-07-03 23:48:52 -04:00
"1+1" [EBNF foo='1' '+' '1' => [[ drop "foo" ]] | '1' '-' '1' => [[ drop "bar" ]] EBNF]
] unit-test
{ "bar" } [
2008-07-03 23:48:52 -04:00
"1-1" [EBNF foo='1' '+' '1' => [[ drop "foo" ]] | '1' '-' '1' => [[ drop "bar" ]] EBNF]
] unit-test
{ 6 } [
2008-07-03 23:48:52 -04:00
"4+2" [EBNF num=[0-9] => [[ digit> ]] foo=num:x '+' num:y => [[ x y + ]] EBNF]
] unit-test
{ 6 } [
2008-07-03 23:48:52 -04:00
"4+2" [EBNF foo=[0-9]:x '+' [0-9]:y => [[ x digit> y digit> + ]] EBNF]
] unit-test
{ 10 } [
2008-07-03 23:48:52 -04:00
{ 1 2 3 4 } [EBNF num=. ?[ number? ]? list=list:x num:y => [[ x y + ]] | num EBNF]
] unit-test
2008-06-29 22:32:20 -04:00
[
{ "a" 2 3 4 } [EBNF num=. ?[ number? ]? list=list:x num:y => [[ x y + ]] | num EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
{ 3 } [
2008-07-03 23:48:52 -04:00
{ 1 2 "a" 4 } [EBNF num=. ?[ number? ]? list=list:x num:y => [[ x y + ]] | num EBNF]
] unit-test
2008-06-29 22:32:20 -04:00
[
"ab" [EBNF -=" " | "\t" | "\n" foo="a" - "b" EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
{ V{ "a" " " "b" } } [
2008-07-03 23:48:52 -04:00
"a b" [EBNF -=" " | "\t" | "\n" foo="a" - "b" EBNF]
] unit-test
{ V{ "a" "\t" "b" } } [
2008-07-03 23:48:52 -04:00
"a\tb" [EBNF -=" " | "\t" | "\n" foo="a" - "b" EBNF]
] unit-test
{ V{ "a" "\n" "b" } } [
2008-07-03 23:48:52 -04:00
"a\nb" [EBNF -=" " | "\t" | "\n" foo="a" - "b" EBNF]
] unit-test
{ V{ "a" f "b" } } [
2008-07-03 23:48:52 -04:00
"ab" [EBNF -=" " | "\t" | "\n" foo="a" (-)? "b" EBNF]
] unit-test
{ V{ "a" " " "b" } } [
2008-07-03 23:48:52 -04:00
"a b" [EBNF -=" " | "\t" | "\n" foo="a" (-)? "b" EBNF]
] unit-test
{ V{ "a" "\t" "b" } } [
2008-07-03 23:48:52 -04:00
"a\tb" [EBNF -=" " | "\t" | "\n" foo="a" (-)? "b" EBNF]
] unit-test
{ V{ "a" "\n" "b" } } [
2008-07-03 23:48:52 -04:00
"a\nb" [EBNF -=" " | "\t" | "\n" foo="a" (-)? "b" EBNF]
] unit-test
{ V{ "a" "b" } } [
2008-07-03 23:48:52 -04:00
"ab" [EBNF -=(" " | "\t" | "\n")? => [[ drop ignore ]] foo="a" - "b" EBNF]
] unit-test
{ V{ "a" "b" } } [
2008-07-03 23:48:52 -04:00
"a\tb" [EBNF -=(" " | "\t" | "\n")? => [[ drop ignore ]] foo="a" - "b" EBNF]
] unit-test
{ V{ "a" "b" } } [
2008-07-03 23:48:52 -04:00
"a\nb" [EBNF -=(" " | "\t" | "\n")? => [[ drop ignore ]] foo="a" - "b" EBNF]
] unit-test
2008-06-29 22:32:20 -04:00
[
"axb" [EBNF -=(" " | "\t" | "\n")? => [[ drop ignore ]] foo="a" - "b" EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-03-28 09:51:49 -04:00
{ V{ V{ 49 } "+" V{ 49 } } } [
#! Test direct left recursion.
2008-03-28 07:49:39 -04:00
#! Using packrat, so first part of expr fails, causing 2nd choice to be used
2008-07-03 23:48:52 -04:00
"1+1" [EBNF num=([0-9])+ expr=expr "+" num | num EBNF]
2008-03-28 07:49:39 -04:00
] unit-test
2008-03-27 06:54:34 -04:00
2008-03-28 09:51:49 -04:00
{ V{ V{ V{ 49 } "+" V{ 49 } } "+" V{ 49 } } } [
#! Test direct left recursion.
2008-03-28 07:41:41 -04:00
#! Using packrat, so first part of expr fails, causing 2nd choice to be used
2008-07-03 23:48:52 -04:00
"1+1+1" [EBNF num=([0-9])+ expr=expr "+" num | num EBNF]
2008-03-28 09:51:49 -04:00
] unit-test
{ V{ V{ V{ 49 } "+" V{ 49 } } "+" V{ 49 } } } [
#! Test indirect left recursion.
#! Using packrat, so first part of expr fails, causing 2nd choice to be used
2008-07-03 23:48:52 -04:00
"1+1+1" [EBNF num=([0-9])+ x=expr expr=x "+" num | num EBNF]
2008-03-28 07:49:39 -04:00
] unit-test
2008-03-27 06:54:34 -04:00
2008-04-02 23:09:03 -04:00
{ t } [
2008-07-03 22:20:19 -04:00
"abcd='9' | ('8'):x => [[ x ]]" 'ebnf' (parse) remaining>> empty?
2008-04-02 23:09:03 -04:00
] unit-test
EBNF: primary
Primary = PrimaryNoNewArray
PrimaryNoNewArray = ClassInstanceCreationExpression
| MethodInvocation
| FieldAccess
| ArrayAccess
| "this"
ClassInstanceCreationExpression = "new" ClassOrInterfaceType "(" ")"
| Primary "." "new" Identifier "(" ")"
MethodInvocation = Primary "." MethodName "(" ")"
| MethodName "(" ")"
FieldAccess = Primary "." Identifier
| "super" "." Identifier
ArrayAccess = Primary "[" Expression "]"
| ExpressionName "[" Expression "]"
ClassOrInterfaceType = ClassName | InterfaceTypeName
ClassName = "C" | "D"
InterfaceTypeName = "I" | "J"
Identifier = "x" | "y" | ClassOrInterfaceType
MethodName = "m" | "n"
ExpressionName = Identifier
Expression = "i" | "j"
main = Primary
;EBNF
{ "this" } [
2008-07-03 23:48:52 -04:00
"this" primary
] unit-test
{ V{ "this" "." "x" } } [
2008-07-03 23:48:52 -04:00
"this.x" primary
] unit-test
{ V{ V{ "this" "." "x" } "." "y" } } [
2008-07-03 23:48:52 -04:00
"this.x.y" primary
] unit-test
{ V{ V{ "this" "." "x" } "." "m" "(" ")" } } [
2008-07-03 23:48:52 -04:00
"this.x.m()" primary
] unit-test
{ V{ V{ V{ "x" "[" "i" "]" } "[" "j" "]" } "." "y" } } [
2008-07-03 23:48:52 -04:00
"x[i][j].y" primary
] unit-test
2008-04-14 06:42:45 -04:00
'ebnf' compile must-infer
{ V{ V{ "a" "b" } "c" } } [
2008-07-03 23:48:52 -04:00
"abc" [EBNF a="a" "b" foo=(a "c") EBNF]
] unit-test
2008-04-28 22:15:05 -04:00
{ V{ V{ "a" "b" } "c" } } [
2008-07-03 23:48:52 -04:00
"abc" [EBNF a="a" "b" foo={a "c"} EBNF]
2008-04-28 22:15:05 -04:00
] unit-test
{ V{ V{ "a" "b" } "c" } } [
2008-07-03 23:48:52 -04:00
"abc" [EBNF a="a" "b" foo=a "c" EBNF]
2008-04-28 22:15:05 -04:00
] unit-test
2008-06-29 22:32:20 -04:00
[
"a bc" [EBNF a="a" "b" foo=(a "c") EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-06-29 22:32:20 -04:00
[
"a bc" [EBNF a="a" "b" foo=a "c" EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-06-29 22:32:20 -04:00
[
"a bc" [EBNF a="a" "b" foo={a "c"} EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-06-29 22:32:20 -04:00
[
"ab c" [EBNF a="a" "b" foo=a "c" EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
{ V{ V{ "a" "b" } "c" } } [
2008-07-03 23:48:52 -04:00
"ab c" [EBNF a="a" "b" foo={a "c"} EBNF]
2008-04-28 22:15:05 -04:00
] unit-test
2008-06-29 22:32:20 -04:00
[
"ab c" [EBNF a="a" "b" foo=(a "c") EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-04-28 22:15:05 -04:00
2008-06-29 22:32:20 -04:00
[
"a b c" [EBNF a="a" "b" foo=a "c" EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-04-28 22:15:05 -04:00
2008-06-29 22:32:20 -04:00
[
"a b c" [EBNF a="a" "b" foo=(a "c") EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-06-29 22:32:20 -04:00
[
"a b c" [EBNF a="a" "b" foo={a "c"} EBNF]
2008-06-29 22:32:20 -04:00
] must-fail
2008-04-28 22:15:05 -04:00
{ V{ V{ V{ "a" "b" } "c" } V{ V{ "a" "b" } "c" } } } [
2008-07-03 23:48:52 -04:00
"ab cab c" [EBNF a="a" "b" foo={a "c"}* EBNF]
] unit-test
2008-04-28 22:15:05 -04:00
{ V{ } } [
2008-07-03 23:48:52 -04:00
"ab cab c" [EBNF a="a" "b" foo=(a "c")* EBNF]
2008-04-28 22:15:05 -04:00
] unit-test
{ V{ V{ V{ "a" "b" } "c" } V{ V{ "a" "b" } "c" } } } [
2008-07-03 23:48:52 -04:00
"ab c ab c" [EBNF a="a" "b" foo={a "c"}* EBNF]
2008-04-28 22:15:05 -04:00
] unit-test
{ V{ } } [
2008-07-03 23:48:52 -04:00
"ab c ab c" [EBNF a="a" "b" foo=(a "c")* EBNF]
2008-04-28 22:15:05 -04:00
] unit-test
2008-06-16 01:39:14 -04:00
{ V{ "a" "a" "a" } } [
2008-07-03 23:48:52 -04:00
"aaa" [EBNF a=('a')* b=!('b') a:x => [[ x ]] EBNF]
2008-06-16 01:39:14 -04:00
] unit-test
{ t } [
2008-07-03 23:48:52 -04:00
"aaa" [EBNF a=('a')* b=!('b') a:x => [[ x ]] EBNF]
"aaa" [EBNF a=('a')* b=!('b') (a):x => [[ x ]] EBNF] =
2008-06-16 01:39:14 -04:00
] unit-test
{ V{ "a" "a" "a" } } [
2008-07-03 23:48:52 -04:00
"aaa" [EBNF a=('a')* b=a:x => [[ x ]] EBNF]
2008-06-16 01:39:14 -04:00
] unit-test
{ t } [
2008-07-03 23:48:52 -04:00
"aaa" [EBNF a=('a')* b=a:x => [[ x ]] EBNF]
"aaa" [EBNF a=('a')* b=(a):x => [[ x ]] EBNF] =
2008-06-16 01:39:14 -04:00
] unit-test
{ t } [
2008-07-03 22:20:19 -04:00
"number=(digit)+:n 'a'" 'ebnf' (parse) remaining>> length zero?
2008-06-16 01:39:14 -04:00
] unit-test
{ t } [
2008-07-03 22:20:19 -04:00
"number=(digit)+ 'a'" 'ebnf' (parse) remaining>> length zero?
2008-06-16 01:39:14 -04:00
] unit-test
{ t } [
2008-07-03 22:20:19 -04:00
"number=digit+ 'a'" 'ebnf' (parse) remaining>> length zero?
2008-06-16 01:39:14 -04:00
] unit-test
{ t } [
2008-07-03 22:20:19 -04:00
"number=digit+:n 'a'" 'ebnf' (parse) remaining>> length zero?
] unit-test
2008-06-18 09:10:44 -04:00
{ t } [
2008-07-03 22:20:19 -04:00
"foo=(name):n !(keyword) => [[ n ]]" 'rule' parse
"foo=name:n !(keyword) => [[ n ]]" 'rule' parse =
2008-06-18 09:10:44 -04:00
] unit-test
{ t } [
2008-07-03 22:20:19 -04:00
"foo=!(keyword) (name):n => [[ n ]]" 'rule' parse
"foo=!(keyword) name:n => [[ n ]]" 'rule' parse =
2008-06-18 09:10:44 -04:00
] unit-test
<<
EBNF: parser1
foo='a'
;EBNF
>>
EBNF: parser2
foo=<foreign parser1 foo> 'b'
;EBNF
EBNF: parser3
foo=<foreign parser1> 'c'
;EBNF
EBNF: parser4
foo=<foreign any-char> 'd'
;EBNF
{ "a" } [
2008-07-03 23:48:52 -04:00
"a" parser1
] unit-test
{ V{ "a" "b" } } [
2008-07-03 23:48:52 -04:00
"ab" parser2
] unit-test
{ V{ "a" "c" } } [
2008-07-03 23:48:52 -04:00
"ac" parser3
] unit-test
{ V{ CHAR: a "d" } } [
2008-07-03 23:48:52 -04:00
"ad" parser4
2008-06-18 09:10:44 -04:00
] unit-test
2008-06-19 19:49:08 -04:00
2008-06-19 20:35:33 -04:00
{ t } [
"USING: kernel peg.ebnf ; \"a\\n\" [EBNF foo='a' '\n' => [[ drop \"\n\" ]] EBNF]" eval drop t
] unit-test
2008-06-19 20:35:33 -04:00
[
"USING: peg.ebnf ; <EBNF foo='a' foo='b' EBNF>" eval drop
2008-06-19 20:35:33 -04:00
] must-fail
{ t } [
2008-06-19 20:35:33 -04:00
#! Rule lookup occurs in a namespace. This causes an incorrect duplicate rule
#! if a var in a namespace is set. This unit test is to remind me to fix this.
2008-07-03 22:20:19 -04:00
[ "fail" "foo" set "foo='a'" 'ebnf' parse transform drop t ] with-scope
2008-06-20 08:28:13 -04:00
] unit-test
#! Tokenizer tests
{ V{ "a" CHAR: b } } [
2008-07-03 23:48:52 -04:00
"ab" [EBNF tokenizer=default foo="a" . EBNF]
2008-06-20 08:28:13 -04:00
] unit-test
TUPLE: ast-number value ;
EBNF: a-tokenizer
Letter = [a-zA-Z]
Digit = [0-9]
Digits = Digit+
SingleLineComment = "//" (!("\n") .)* "\n" => [[ ignore ]]
MultiLineComment = "/*" (!("*/") .)* "*/" => [[ ignore ]]
Space = " " | "\t" | "\r" | "\n" | SingleLineComment | MultiLineComment
Spaces = Space* => [[ ignore ]]
Number = Digits:ws '.' Digits:fs => [[ ws "." fs 3array concat >string string>number ast-number boa ]]
| Digits => [[ >string string>number ast-number boa ]]
Special = "(" | ")" | "{" | "}" | "[" | "]" | "," | ";"
| "?" | ":" | "!==" | "~=" | "===" | "==" | "=" | ">="
| ">" | "<=" | "<" | "++" | "+=" | "+" | "--" | "-="
| "-" | "*=" | "*" | "/=" | "/" | "%=" | "%" | "&&="
| "&&" | "||=" | "||" | "." | "!"
Tok = Spaces (Number | Special )
;EBNF
{ V{ CHAR: 1 T{ ast-number f 23 } ";" CHAR: x } } [
"123;x" [EBNF bar = .
tokenizer = <foreign a-tokenizer Tok> foo=.
tokenizer=default baz=.
main = bar foo foo baz
2008-07-03 23:48:52 -04:00
EBNF]
2008-06-20 08:28:13 -04:00
] unit-test
{ V{ CHAR: 5 "+" CHAR: 2 } } [
"5+2" [EBNF
space=(" " | "\n")
number=[0-9]
operator=("*" | "+")
spaces=space* => [[ ignore ]]
tokenizer=spaces (number | operator)
main= . . .
2008-07-03 23:48:52 -04:00
EBNF]
2008-06-20 08:28:13 -04:00
] unit-test
{ V{ CHAR: 5 "+" CHAR: 2 } } [
"5 + 2" [EBNF
space=(" " | "\n")
number=[0-9]
operator=("*" | "+")
spaces=space* => [[ ignore ]]
tokenizer=spaces (number | operator)
main= . . .
2008-07-03 23:48:52 -04:00
EBNF]
] unit-test
{ "++" } [
2008-07-03 23:48:52 -04:00
"++--" [EBNF tokenizer=("++" | "--") main="++" EBNF]
2008-06-29 22:32:20 -04:00
] unit-test
{ "\\" } [
2008-07-03 23:48:52 -04:00
"\\" [EBNF foo="\\" EBNF]
] unit-test