Skip to content

Commit

Permalink
Support backslash-escaped strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tjvr committed Oct 17, 2017
1 parent 701d658 commit ca89791
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion editor/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

const moo = require('moo')

const backslashes = s => s.replace(/\\["\\]/g, x => x[1])


let lexer = moo.compile([
{name: 'NL', match: '\n', lineBreaks: true },
{name: 'WS', match: /[ \t]+/},
Expand Down Expand Up @@ -119,7 +122,7 @@ var negateNumber = factory(function (a, _, n) {
})

var string = factory(function decodeString(...d) {
return d[0].value
return backslashes(d[0].value)
}, function encodeString(d) {
if (typeof d === 'string') {
return d
Expand Down
51 changes: 51 additions & 0 deletions test/grammar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,54 @@ function parseBlock(source) {
}


describe('tokenize', () => {

const lexer = grammar.lexer
const source = `
when flag clicked
say "Hello \\"world\\"!"
forever {
move 10 steps
}`

// TODO move backslash-escapes into tokenizer?

test('file', () => {
lexer.reset(source)
expect(Array.from(lexer).map(t => `${t.type} ${t.value}`)).toEqual([
"NL \n",
"WS ",
"symbol when",
"WS ",
"symbol flag",
"WS ",
"symbol clicked",
"NL \n",
"WS ",
"symbol say",
"WS ",
'string Hello \\"world\\"!',
"NL \n",
"WS ",
"symbol forever",
"WS ",
"{ {",
"NL \n",
"WS ",
"symbol move",
"WS ",
"number 10",
"WS ",
"symbol steps",
"NL \n",
"WS ",
"} }",
])
})

})


describe('parse', () => {

test('line', () => {
Expand Down Expand Up @@ -72,6 +120,9 @@ describe('parse', () => {
expect(parseBlock('forever {\n }')).toEqual(['doForever', null])
})

test('backslash-escapes', () => {
expect(parseBlock('say "Hello \\"world\\"!"')).toEqual(['say:', 'Hello "world"!'])
})

})

Expand Down
3 changes: 3 additions & 0 deletions test/mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,8 @@ describe('highlight', () => {
MT('closed string',
'[s-looks say] [string "foo "] ')

MT('backslash-escaped strings',
'[s-looks say] [string "potato \\"waffles\\"."] ')

})

0 comments on commit ca89791

Please sign in to comment.