Skip to content

Commit

Permalink
Fix the try combinator/lookahead at EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
drewc committed Sep 1, 2024
1 parent fd188f1 commit 36a2d91
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 113 deletions.
56 changes: 33 additions & 23 deletions src/std/parsec-test.ss
Original file line number Diff line number Diff line change
Expand Up @@ -267,49 +267,59 @@

(test-inline
test-case: "Dot tests"
> (caar (do-parse (.run (.return 42) "")))
> (caar (do-parsec (.run (.return 42) "")))
42
> (def-parse FourTwo (.char #\4) (.char #\2) (.return 42))
> (caar (do-parse (.run FourTwo "42")))
> (def-parsec FourTwo (.char #\4) (.char #\2) (.return 42))
> (caar (do-parsec (.run FourTwo "42")))
42

)
(test-inline
test-case: "Character Parsing tests"
> (caar (do-parse (.run (.string "asd") "asdfjkl;")))
> (caar (do-parsec (.run (.string "asd") "asdfjkl;")))
"asd"
> (caar (do-parse (.run (.string "asd" char-ci=?) "AsDfjkl;")))
> (caar (do-parsec (.run (.string "asd" char-ci=?) "AsDfjkl;")))
"AsD"

)
(test-inline
test-case: "Org Syntax Parsing tests"
> (def-parse EOL (.or (.eof) (.newline)))
> (def-parsec-bnf
EOL ::= (.or (.eof) (.newline))

> (def-parse KEY
(.>> (.string "#+")
(.many-till
(.satisfy (? (not char-whitespace?)))
(.string ": "))))
KEY ::= (.>> (.string "#+")
(.many-till
(.satisfy (? (not char-whitespace?)))
(.string ": ")))

> (def-parse VALUE (.many-till (.any-token) EOL))
VALUE ::= (.many-till (.any-token) EOL)

> (def-parse KEYWORD
key <- (.liftM list->string KEY)
value <- (.liftM list->string VALUE)
(.return ['keyword key: key value: value]))
KEYWORD ::= key <- (.liftM list->string KEY)
value <- (.liftM list->string VALUE)
(.return ['keyword key: key value: value]))

> (run-parser KEYWORD "#+TITLE: Org Mode keyword!")
(keyword key: "TITLE" value: "Org Mode keyword!")

> (def-parsec-bnf
GENERIC-LINE ::= (.many-till (.any-token) EOL)

LINE-NO-TRY ::= (.or KEYWORD GENERIC-LINE))



> (run-parser LINE-NO-TRY "asd\njkl")
(#\a #\s #\d)
> (run-parser LINE-NO-TRY "#+TITLE: Org Mode keyword!")
(keyword key: "TITLE" value: "Org Mode keyword!")
> (run-parser LINE-NO-TRY "#+heh yeah!")
#f
> (def-parsec-bnf
LINE ::= (.or (.try KEYWORD) GENERIC-LINE))





> (run-parser LINE "asd\njkl")
(#\a #\s #\d)
> (run-parser LINE "#+TITLE: Org Mode keyword!")
(keyword key: "TITLE" value: "Org Mode keyword!")
> (list->string (run-parser LINE "#+heh yeah!"))
"#+heh yeah!"

)

Expand Down
Loading

0 comments on commit 36a2d91

Please sign in to comment.