Skip to content

Commit

Permalink
feat: support escapes in strings
Browse files Browse the repository at this point in the history
fixes #304
  • Loading branch information
kantord committed Mar 11, 2019
1 parent 8bca19a commit 858d960
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/interpreter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ exports[`interpreter correct target code !(3 * 2 -1 > -1.34 * 3) and true 1`] =

exports[`interpreter correct target code "1" == 1 1`] = `"(function(_) { return (function(input) { return \\"1\\"===1})})"`;

exports[`interpreter correct target code "Hello\\" World\\'\\nx" 1`] = `"(function(_) { return (function(input) { return \\"Hello\\\\\\" World\\\\'\\\\nx\\"})})"`;
exports[`interpreter correct target code $ {
Person: user {
name {
Expand Down Expand Up @@ -987,6 +989,16 @@ Object {
}
`;
exports[`interpreter correct target tree "Hello\\" World\\'\\nx" 1`] = `
Object {
"status": true,
"value": Object {
"name": "primitive",
"value": "\\"Hello\\\\\\" World\\\\'\\\\nx\\"",
},
}
`;
exports[`interpreter correct target tree $ {
Person: user {
name {
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ const tests = [
sourceCode: `null: true`,
output: [null, true]
},
{
sourceCode: `"Hello\\" World\\'\\nx"`,
output: `Hello" World'
x`
},
{
sourceCode: `{
"foo": [-42, $],
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import P from 'parsimmon'
import type { NodeType } from '../types'

const keywords = ['null', 'true', 'false']
const DoubleQuoteStringRegexp = /("(((?=\\)\\(["\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\\\0-\x1F\x7F]+)*")/
const SingleQuoteStringRegexp = /('(((?=\\)\\(['\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^'\\\0-\x1F\x7F]+)*')/
const DoubleQuoteStringRegexp = /("(((?=\\)\\(["'\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\\\0-\x1F\x7F]+)*")/
const SingleQuoteStringRegexp = /('(((?=\\)\\(['"\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^'\\\0-\x1F\x7F]+)*')/
export const StringParserRegExp = new RegExp(`(${DoubleQuoteStringRegexp.source}|${SingleQuoteStringRegexp.source})`)
const NumberParserRegExp = /(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/

Expand Down

0 comments on commit 858d960

Please sign in to comment.