Skip to content

Commit

Permalink
fixup! fix: properly parse octal escape sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz committed Sep 9, 2019
1 parent 7656aa3 commit ee8da84
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
30 changes: 17 additions & 13 deletions tests/parse/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ R(["pyret-base/js/pyret-tokenizer", "pyret-base/js/pyret-parser", "fs"], functio
expect(parse("```asd``\\````")).not.toBe(false);
expect(parse("```asd```asd```")).toBe(false);
});

it('should lex octal escape sequences', function() {
const escapeSequences = ['\\0', '\\77', '\\101'];

This comment has been minimized.

Copy link
@blerner

blerner Sep 9, 2019

Member

Not quite. I think you've lost a layer of string delimiters here, which is why your expectedValues is an array, rather than a single token value. I think you mean something closer to:

    it('should lex octal escape sequences', function() {
      const escapeSequences = ['"\\0"', '"\\77"', '"\\101"'];
      const expectedValues = ['"\0"', '"\77"', '"\101"'];
      for (let i = 0; i < escapeSequences.length; ++i) {
        lex(escapeSequences[i]);
        expect(toks.length).toEqual(2);
        expect(toks[0].value).toEqual(expectedValues[i]);
        expect(toks[1].name).toEqual("EOF");

        const parseStr = `str = ${escapeSequences[i]}`;
        expect(parse(parseStr)).not.toBe(false);
      }

      // invalid escape sequence
      expect(parse('str = \'\\8\'')).toBe(false);
    });

This comment has been minimized.

Copy link
@ayazhafiz

ayazhafiz Sep 9, 2019

Author Contributor

Fixed.

const expectedSequences = ['0', '77', '101'];
for (let i = 0; i < escapeSequences.length; ++i) {
const expectedValues = ['\\', expectedSequences[i], undefined];

const lexedValues = lex(escapeSequences[i]).map(token => token.value);
expect(lexedValues).toEqual(expectedValues);

const parseStr = `str = "${escapeSequences[i]}"`;
expect(parse(parseStr)).not.toBe(false);
}

// invalid escape sequence
expect(parse('str = \'\\8\'')).toBe(false);
});
});
describe("parsing", function() {
it("should parse lets and letrecs", function() {
Expand Down Expand Up @@ -762,19 +779,6 @@ R(["pyret-base/js/pyret-tokenizer", "pyret-base/js/pyret-parser", "fs"], functio
expect(parse("spy \"five\": x end")).not.toBe(false);
expect(parse("spy \"five\": x: 5 end")).not.toBe(false);
});

it("should parse octal escape squences", function() {
expect(parse("a = '\\0'").toString()).toContain(stringAst('\\u0000'));
expect(parse("a = '\\101'").toString()).toContain(stringAst('A'));
expect(parse("a = '\\101bc'").toString()).toContain(stringAst('Abc'));
expect(parse("a = '\\77'").toString()).toContain(stringAst('?'));

expect(parse("a = '\\88'")).toBe(false);

function stringAst(str) {
return `'STRING "'${str}'"`;
}
});
});

jazz.execute();
Expand Down
5 changes: 5 additions & 0 deletions tests/pyret/tests/test-strings.arr
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ check:
string-to-code-points("abcd") is [list: 97, 98, 99, 100]
string-to-code-points("") is [list:]

# octal escape sequences
string-to-code-point("\0") is 0
string-to-code-point("\77") is 63
string-to-code-point("\101") is 65

string-from-code-points([list: 955, 97, 10]) is "λa\n"
string-from-code-points([list: 955, -1]) raises "Natural Number"
string-from-code-points([list:]) is ""
Expand Down

0 comments on commit ee8da84

Please sign in to comment.