Browse Source

Allow empty strings

master
Dylan Baker 5 years ago
parent
commit
f77ad26a69
2 changed files with 27 additions and 1 deletions
  1. 10
    1
      src/parser.js
  2. 17
    0
      test/parser.js

+ 10
- 1
src/parser.js View File

89
 
89
 
90
   string() {
90
   string() {
91
     this.tokenStream.eat(tokenTypes.QUOTE)
91
     this.tokenStream.eat(tokenTypes.QUOTE)
92
+
93
+    let value = ''
94
+
95
+    if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
96
+      value = this.tokenStream.eat(tokenTypes.LITERAL).value
97
+    }
98
+
92
     let node = new AST.String({
99
     let node = new AST.String({
93
-      value: this.tokenStream.eat(tokenTypes.LITERAL).value
100
+      value: value
94
     })
101
     })
102
+
95
     this.tokenStream.eat(tokenTypes.QUOTE)
103
     this.tokenStream.eat(tokenTypes.QUOTE)
104
+
96
     return node
105
     return node
97
   }
106
   }
98
 
107
 

+ 17
- 0
test/parser.js View File

43
     })
43
     })
44
   ])
44
   ])
45
 })
45
 })
46
+
47
+test('allow empty strings', t => {
48
+  t.plan(1)
49
+  const lexer = new Lexer()
50
+  const tokenStream = lexer.scan('(p "")')
51
+  const parser = new Parser(tokenStream)
52
+  const tree = parser.parse()
53
+
54
+  t.deepEqual(tree, [
55
+    new AST.Application({
56
+      functionName: new AST.Identifier({ name: 'p' }),
57
+      args: [
58
+        new AST.String({ value: '' })
59
+      ]
60
+    })
61
+  ])
62
+})

Loading…
Cancel
Save