Skip to content

Commit

Permalink
Support both single and double quotes for strings (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peripheral1994 authored Apr 16, 2019
1 parent 78c311b commit ebc37a2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/lexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ test('Lexer passes Strings fixtures', () => {
fix('"d"', [t.String('"d"'), t.EOS()], false),
fix('"and"', [t.String('"and"'), t.EOS()], false),
fix('"or and"', [t.String('"or and"'), t.EOS()], false),
fix('"a" "b"', [t.String('"a"'), t.String('"b"'), t.EOS()], false)
fix('"a" "b"', [t.String('"a"'), t.String('"b"'), t.EOS()], false),
fix('\'a\'', [t.String('\'a\''), t.EOS()], false),
fix('\'and\'', [t.String('\'and\''), t.EOS()], false),
fix('\'or and\'', [t.String('\'or and\''), t.EOS()], false),
fix('\'a\' \'b\'', [t.String('\'a\''), t.String('\'b\''), t.EOS()], false)
])
})

Expand Down
13 changes: 7 additions & 6 deletions src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export class Lexer {
continue
}

if (char === '"') {
tokens.push(this.lexString())
if (char === '"' || char === '\'') {
tokens.push(this.lexString(char))
continue
}

Expand Down Expand Up @@ -118,9 +118,10 @@ export class Lexer {
}
}

private lexString(): Token {
private lexString(openQuote: string): Token {
let str = ''
while (this.peek() !== '"') {
// Looking for closing string of same type of quote (single or double)
while (this.peek() !== openQuote) {
const { char, isEOS } = this.next()
str += char

Expand All @@ -133,8 +134,8 @@ export class Lexer {
}
}

this.accept('"') // Eat the last quote
return t.String(`"${str}"`)
this.accept(openQuote) // Eat the last quote
return t.String(`${openQuote}${str}${openQuote}`)
}

private lexNumber(previous: string): Token {
Expand Down

0 comments on commit ebc37a2

Please sign in to comment.