Skip to content

Commit ee8da84

Browse files
committed
fixup! fix: properly parse octal escape sequences
1 parent 7656aa3 commit ee8da84

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

tests/parse/parse.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,23 @@ R(["pyret-base/js/pyret-tokenizer", "pyret-base/js/pyret-parser", "fs"], functio
226226
expect(parse("```asd``\\````")).not.toBe(false);
227227
expect(parse("```asd```asd```")).toBe(false);
228228
});
229+
230+
it('should lex octal escape sequences', function() {
231+
const escapeSequences = ['\\0', '\\77', '\\101'];
232+
const expectedSequences = ['0', '77', '101'];
233+
for (let i = 0; i < escapeSequences.length; ++i) {
234+
const expectedValues = ['\\', expectedSequences[i], undefined];
235+
236+
const lexedValues = lex(escapeSequences[i]).map(token => token.value);
237+
expect(lexedValues).toEqual(expectedValues);
238+
239+
const parseStr = `str = "${escapeSequences[i]}"`;
240+
expect(parse(parseStr)).not.toBe(false);
241+
}
242+
243+
// invalid escape sequence
244+
expect(parse('str = \'\\8\'')).toBe(false);
245+
});
229246
});
230247
describe("parsing", function() {
231248
it("should parse lets and letrecs", function() {
@@ -762,19 +779,6 @@ R(["pyret-base/js/pyret-tokenizer", "pyret-base/js/pyret-parser", "fs"], functio
762779
expect(parse("spy \"five\": x end")).not.toBe(false);
763780
expect(parse("spy \"five\": x: 5 end")).not.toBe(false);
764781
});
765-
766-
it("should parse octal escape squences", function() {
767-
expect(parse("a = '\\0'").toString()).toContain(stringAst('\\u0000'));
768-
expect(parse("a = '\\101'").toString()).toContain(stringAst('A'));
769-
expect(parse("a = '\\101bc'").toString()).toContain(stringAst('Abc'));
770-
expect(parse("a = '\\77'").toString()).toContain(stringAst('?'));
771-
772-
expect(parse("a = '\\88'")).toBe(false);
773-
774-
function stringAst(str) {
775-
return `'STRING "'${str}'"`;
776-
}
777-
});
778782
});
779783

780784
jazz.execute();

tests/pyret/tests/test-strings.arr

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ check:
137137
string-to-code-points("abcd") is [list: 97, 98, 99, 100]
138138
string-to-code-points("") is [list:]
139139

140+
# octal escape sequences
141+
string-to-code-point("\0") is 0
142+
string-to-code-point("\77") is 63
143+
string-to-code-point("\101") is 65
144+
140145
string-from-code-points([list: 955, 97, 10]) is "λa\n"
141146
string-from-code-points([list: 955, -1]) raises "Natural Number"
142147
string-from-code-points([list:]) is ""

0 commit comments

Comments
 (0)