-
I've written a scanner for Postscript using a few of the examples as a guide, and I have the following code, but somehow, the escape characters are not working. Parser string_() =>
ref(bracket, '()', (ref(characterNoString) | ref(string_)).star().flatten())
.pick(1)
.map((s) {
print(s);
print(s.runes);
return PSString.from(s);
});
// Parser character() => ref(characterEscape) | ref(characterRaw);
Parser characterNoString() => (ref(characterEscape) | ref(characterRawNoString)).map((s) { print("Adding $s ${s.runes}"); return s; });
Parser characterEscape() => (char('\\') & pattern(escapeCharacters.keys.join())).map((s) => escapeCharacters[s[1]]);
Parser characterRaw() => pattern('^)');
Parser characterRawNoString() => pattern('^(^)^\\');
Parser name() => ref(literal) | ref(name_);
Parser literal() => (char('/') & name_())
.flatten("literal expected")
.map((i) => PSName.Literal(i.substring(1)));
static final Map<String, String> escapeCharacters = {
'\\': '\\',
'/': '/',
'(': '(',
')': ')',
'b': '\b',
'f': '\f',
'n': '\n',
'r': '\r',
't': '\t'
};
When I run the scanner, I see this:
Somehow, instead of the "\n" turning into charcode What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I think the problem is here: Probably something like |
Beta Was this translation helpful? Give feedback.
-
Oh! Thanks so much for the help, that did the trick! |
Beta Was this translation helpful? Give feedback.
I think the problem is here:
(ref(characterNoString) | ref(string_)).star().flatten()
. flatten returns the consumed sub-string, so it ignores the decoded characters you return.Probably something like
... .star().map((s) => s.join())
is what you want ...