Browse Source

Allow using special characters in literals

master
Dylan Baker 5 years ago
parent
commit
d60e736fec
2 changed files with 26 additions and 5 deletions
  1. 5
    5
      src/lexer.js
  2. 21
    0
      test/lexer.js

+ 5
- 5
src/lexer.js View File

@@ -6,10 +6,10 @@ module.exports = class Lexer {
6 6
     let pos = 0
7 7
     let line = 1
8 8
     let tokenStream = new TokenStream()
9
-    let allowWhiteSpaceInLiterals = false
9
+    let allowSpecialCharactersInLiterals = false
10 10
 
11 11
     while (pos < source.length) {
12
-      if (source[pos].match(/\(/)) {
12
+      if (source[pos].match(/\(/) && !allowSpecialCharactersInLiterals) {
13 13
         tokenStream.tokens.push({
14 14
           type: tokenTypes.OPAREN,
15 15
           line: line,
@@ -22,7 +22,7 @@ module.exports = class Lexer {
22 22
         })
23 23
         pos++
24 24
       } else if (source[pos].match(/['"]/)) {
25
-        allowWhiteSpaceInLiterals = !allowWhiteSpaceInLiterals
25
+        allowSpecialCharactersInLiterals = !allowSpecialCharactersInLiterals
26 26
         tokenStream.tokens.push({
27 27
           type: tokenTypes.QUOTE,
28 28
           line: line,
@@ -44,8 +44,8 @@ module.exports = class Lexer {
44 44
       } else {
45 45
         let endPattern = /[^()"':\s]+/
46 46
 
47
-        if (allowWhiteSpaceInLiterals) {
48
-          endPattern = /[^()"':]+/
47
+        if (allowSpecialCharactersInLiterals) {
48
+          endPattern = /[^"']+/
49 49
         }
50 50
 
51 51
         let value = endPattern.exec(source.slice(pos))[0]

+ 21
- 0
test/lexer.js View File

@@ -72,3 +72,24 @@ test('multiple identifiers in a row are kept separate', t => {
72 72
     ]
73 73
   )
74 74
 })
75
+
76
+test('allow special characters inside quotes', t => {
77
+  t.plan(2)
78
+  const lexer = new Lexer()
79
+  let tokens = lexer.scan(`
80
+    (p "(test)")
81
+  `).tokens
82
+  t.deepEqual(
83
+    tokens.map(token => token.type),
84
+    [
85
+      tt.OPAREN,
86
+      tt.LITERAL,
87
+      tt.QUOTE,
88
+      tt.LITERAL,
89
+      tt.QUOTE,
90
+      tt.CPAREN,
91
+      tt.EOF,
92
+    ]
93
+  )
94
+  t.equal(tokens[3].value, '(test)')
95
+})

Loading…
Cancel
Save