Skip to content

Commit

Permalink
Test using subparsers to implement ASCII escape
Browse files Browse the repository at this point in the history
Requested as a feature to the main parser in #1.  With this subparser
architecture, it can be specified as its own module like this.
  • Loading branch information
anko committed Jan 18, 2016
1 parent 371feaa commit bb1fc8e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test.ls
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,34 @@ test-with-modifications do
string \]
p.clone p.sub.composite.atom.sub.charNeedingEscape

test-with-modifications do
"Transformer can add ASCII char escape type to strings"
'"\\t\\x20"' # "\x20" is an ASCII hex escape for the space character
[ (new String "\t ") ]
(p) ->

ascii-escape =
p.sub.basic.escape-char # Expect to see the escape character
# ("\" unless another transformer has
# changed it)
.then p.parsimmon.string \x # ... then an "x"
.then p.parsimmon.regex /[0-9a-f]+/ # ... then a char code in hex
.map ->
# The string parser expects its sub-parsers to return strings, so we
# turn the hex code into the corresponding real character.
it
|> parseInt _, 16
|> String.from-char-code

# We then replace the string parser's "escapedCharacter" sub-parser with an
# "alt" (alternative) between our new kind of escaped-character parser and
# itself.
p.replace do
p.sub.composite.string.sub.escaped-character
p.parsimmon.alt do
ascii-escape
p.clone p.sub.composite.string.sub.escaped-character

#
# Location information
#
Expand Down

0 comments on commit bb1fc8e

Please sign in to comment.