factor/basis/peg/peg-tests.factor

219 lines
4.2 KiB
Factor
Raw Normal View History

2007-11-19 22:36:38 -05:00
! Copyright (C) 2007 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
!
USING: continuations kernel tools.test strings namespaces make arrays
sequences peg peg.private peg.parsers words math accessors ;
2008-03-01 17:00:45 -05:00
IN: peg.tests
2007-11-19 22:36:38 -05:00
{ } [ reset-pegs ] unit-test
2008-07-03 00:52:22 -04:00
[
2013-07-24 17:52:09 -04:00
"endbegin" "begin" token parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-19 22:36:38 -05:00
2007-11-20 21:31:23 -05:00
{ "begin" "end" } [
2013-07-24 17:52:09 -04:00
"beginend" "begin" token (parse)
[ ast>> ] [ remaining>> ] bi
>string
2007-11-19 22:36:38 -05:00
] unit-test
2008-07-03 00:52:22 -04:00
[
2013-07-24 17:52:09 -04:00
"" CHAR: a CHAR: z range parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-19 22:36:38 -05:00
2008-07-03 00:52:22 -04:00
[
2013-07-24 17:52:09 -04:00
"1bcd" CHAR: a CHAR: z range parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-19 22:36:38 -05:00
{ CHAR: a } [
2013-07-24 17:52:09 -04:00
"abcd" CHAR: a CHAR: z range parse
2007-11-19 22:36:38 -05:00
] unit-test
{ CHAR: z } [
2013-07-24 17:52:09 -04:00
"zbcd" CHAR: a CHAR: z range parse
2007-11-19 22:36:38 -05:00
] unit-test
2008-07-03 00:52:22 -04:00
[
2013-07-24 17:52:09 -04:00
"bad" "a" token "b" token 2array seq parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-19 22:36:38 -05:00
2007-11-20 21:31:23 -05:00
{ V{ "g" "o" } } [
2013-07-24 17:52:09 -04:00
"good" "g" token "o" token 2array seq parse
2007-11-19 22:36:38 -05:00
] unit-test
2007-11-19 23:58:11 -05:00
{ "a" } [
2013-07-24 17:52:09 -04:00
"abcd" "a" token "b" token 2array choice parse
2007-11-19 23:58:11 -05:00
] unit-test
{ "b" } [
2013-07-24 17:52:09 -04:00
"bbcd" "a" token "b" token 2array choice parse
2007-11-19 23:58:11 -05:00
] unit-test
2008-07-03 00:52:22 -04:00
[
"cbcd" "a" token "b" token 2array choice parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-19 23:58:11 -05:00
2008-07-03 00:52:22 -04:00
[
"" "a" token "b" token 2array choice parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-20 21:01:44 -05:00
{ 0 } [
2013-07-24 17:52:09 -04:00
"" "a" token repeat0 parse length
2007-11-20 21:01:44 -05:00
] unit-test
{ 0 } [
2013-07-24 17:52:09 -04:00
"b" "a" token repeat0 parse length
2007-11-20 21:01:44 -05:00
] unit-test
2007-11-20 21:31:23 -05:00
{ V{ "a" "a" "a" } } [
2013-07-24 17:52:09 -04:00
"aaab" "a" token repeat0 parse
2007-11-20 21:01:44 -05:00
] unit-test
2008-07-03 00:52:22 -04:00
[
2013-07-24 17:52:09 -04:00
"" "a" token repeat1 parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-20 21:01:44 -05:00
2008-07-03 00:52:22 -04:00
[
2013-07-24 17:52:09 -04:00
"b" "a" token repeat1 parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-20 21:01:44 -05:00
2007-11-20 21:31:23 -05:00
{ V{ "a" "a" "a" } } [
2013-07-24 17:52:09 -04:00
"aaab" "a" token repeat1 parse
2007-11-20 21:01:44 -05:00
] unit-test
2007-11-20 21:50:47 -05:00
2013-07-24 17:52:09 -04:00
{ V{ "a" "b" } } [
"ab" "a" token optional "b" token 2array seq parse
2007-11-20 21:50:47 -05:00
] unit-test
2013-07-24 17:52:09 -04:00
{ V{ f "b" } } [
"b" "a" token optional "b" token 2array seq parse
2007-11-20 21:50:47 -05:00
] unit-test
2013-07-24 17:52:09 -04:00
[
"cb" "a" token optional "b" token 2array seq parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-20 22:06:02 -05:00
{ V{ CHAR: a CHAR: b } } [
2013-07-24 17:52:09 -04:00
"ab" "a" token ensure CHAR: a CHAR: z range dup 3array seq parse
2007-11-20 22:06:02 -05:00
] unit-test
2008-07-03 00:52:22 -04:00
[
"bb" "a" token ensure CHAR: a CHAR: z range 2array seq parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-20 22:11:49 -05:00
{ t } [
2013-07-24 17:52:09 -04:00
"a+b"
"a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
parse [ t ] [ f ] if
2007-11-20 22:11:49 -05:00
] unit-test
{ t } [
2013-07-24 17:52:09 -04:00
"a++b"
"a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
parse [ t ] [ f ] if
2007-11-20 22:11:49 -05:00
] unit-test
{ t } [
2013-07-24 17:52:09 -04:00
"a+b"
"a" token "+" token "++" token 2array choice "b" token 3array seq
parse [ t ] [ f ] if
2007-11-20 22:11:49 -05:00
] unit-test
2008-07-03 00:52:22 -04:00
[
2013-07-24 17:52:09 -04:00
"a++b"
"a" token "+" token "++" token 2array choice "b" token 3array seq
parse [ t ] [ f ] if
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-20 22:21:23 -05:00
{ 1 } [
2013-07-24 17:52:09 -04:00
"a" "a" token [ drop 1 ] action parse
2007-11-20 22:21:23 -05:00
] unit-test
{ V{ 1 1 } } [
2013-07-24 17:52:09 -04:00
"aa" "a" token [ drop 1 ] action dup 2array seq parse
2007-11-20 22:21:23 -05:00
] unit-test
2008-07-03 00:52:22 -04:00
[
2013-07-24 17:52:09 -04:00
"b" "a" token [ drop 1 ] action parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-26 21:08:16 -05:00
2013-07-24 17:52:09 -04:00
[
"b" [ CHAR: a = ] satisfy parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-26 21:08:16 -05:00
2013-07-24 17:52:09 -04:00
{ CHAR: a } [
"a" [ CHAR: a = ] satisfy parse
2007-11-26 21:45:00 -05:00
] unit-test
{ "a" } [
2013-07-24 17:52:09 -04:00
" a" "a" token sp parse
2007-11-26 21:45:00 -05:00
] unit-test
{ "a" } [
2013-07-24 17:52:09 -04:00
"a" "a" token sp parse
2007-11-26 21:45:00 -05:00
] unit-test
{ V{ "a" } } [
2013-07-24 17:52:09 -04:00
"[a]" "[" token hide "a" token "]" token hide 3array seq parse
2007-11-26 21:45:00 -05:00
] unit-test
2008-07-03 00:52:22 -04:00
[
"a]" "[" token hide "a" token "]" token hide 3array seq parse
2008-07-03 00:52:22 -04:00
] must-fail
2007-11-26 21:45:00 -05:00
2008-03-27 00:45:59 -04:00
{ V{ "1" "-" "1" } V{ "1" "+" "1" } } [
2013-07-24 17:52:09 -04:00
[
[ "1" token , "-" token , "1" token , ] seq* ,
[ "1" token , "+" token , "1" token , ] seq* ,
] choice*
"1-1" over parse swap
"1+1" swap parse
2008-03-27 00:45:59 -04:00
] unit-test
: expr ( -- parser )
2015-09-08 19:15:10 -04:00
! Test direct left recursion. Currently left recursion should cause a
! failure of that parser.
2013-07-24 17:52:09 -04:00
[ expr ] delay "+" token "1" token 3seq "1" token 2choice ;
2008-03-27 06:54:34 -04:00
2008-03-28 09:47:03 -04:00
{ V{ V{ "1" "+" "1" } "+" "1" } } [
2013-07-24 17:52:09 -04:00
"1+1+1" expr parse
2008-03-27 06:54:34 -04:00
] unit-test
2008-03-27 21:10:33 -04:00
{ t } [
2015-09-08 19:15:10 -04:00
! Ensure a circular parser doesn't loop infinitely
2013-07-24 17:52:09 -04:00
[ f , "a" token , ] seq*
dup peg>> parsers>>
dupd 0 swap set-nth compile word?
] unit-test
2008-07-03 00:52:22 -04:00
[
"A" [ drop t ] satisfy [ 66 >= ] semantic parse
2008-07-03 00:52:22 -04:00
] must-fail
{ CHAR: B } [
2013-07-24 17:52:09 -04:00
"B" [ drop t ] satisfy [ 66 >= ] semantic parse
] unit-test
{ f } [ \ + T{ parser f f f } equal? ] unit-test
USE: compiler
{ } [ disable-optimizer ] unit-test
{ } [ "" epsilon parse drop ] unit-test
{ } [ enable-optimizer ] unit-test
{ [ ] } [ "" epsilon [ drop [ [ ] ] call ] action parse ] unit-test
{
T{ parse-error
{ position 0 }
{ got "fbcd" }
{ messages V{ "'a'" "'b'" } }
}
} [
[ "fbcd" "a" token "b" token 2array choice parse ] [ ] recover
] unit-test