Explorar el Código

Implement missing closing “/) errors

master
Dylan Baker hace 6 años
padre
commit
80c7326df2
Se han modificado 3 ficheros con 43 adiciones y 7 borrados
  1. 2
    2
      src/index.js
  2. 14
    5
      src/parser.js
  3. 27
    0
      test/parserTest.js

+ 2
- 2
src/index.js Ver fichero

@@ -11,8 +11,8 @@ module.exports = function oslo(source, context) {
11 11
   const parser = new Parser()
12 12
   const tree = parser.parse(tokens)
13 13
 
14
-  if (tree.error) {
15
-    return tree.error
14
+  if (tree.constructor == OsloError) {
15
+    return tree
16 16
   }
17 17
 
18 18
   const evaluator = new Evaluator()

+ 14
- 5
src/parser.js Ver fichero

@@ -8,13 +8,18 @@ module.exports = class Parser {
8 8
     let tree = []
9 9
     while (this.tokenStream.peek().type !== tokenTypes.EOF) {
10 10
       let expr = this.expr()
11
-      tree.push(expr)
11
+
12 12
       if (this.tokenStream.error) {
13
-        return {
14
-          error: this.tokenStream.error,
15
-        }
13
+        break
16 14
       }
15
+
16
+      tree.push(expr)
17 17
     }
18
+
19
+    if (this.tokenStream.error) {
20
+      return this.tokenStream.error
21
+    }
22
+
18 23
     return tree
19 24
   }
20 25
 
@@ -67,9 +72,13 @@ module.exports = class Parser {
67 72
 
68 73
       node.args = []
69 74
 
70
-      while (this.tokenStream.peek().type !== tokenTypes.CPAREN) {
75
+      while (this.tokenStream.peek().type !== tokenTypes.CPAREN && this.tokenStream.peek().type != tokenTypes.EOF) {
71 76
         node.args.push(this.expr())
72 77
       }
78
+
79
+      if (this.tokenStream.error) {
80
+        return this.tokenStream.error
81
+      }
73 82
     }
74 83
 
75 84
     this.tokenStream.eat(tokenTypes.CPAREN)

+ 27
- 0
test/parserTest.js Ver fichero

@@ -2,6 +2,7 @@ const test = require('tape')
2 2
 const helpers = require('./helpers')
3 3
 
4 4
 const AST = require('../src/ast')
5
+const OsloError = require('../src/osloError')
5 6
 const tt = require('../src/tokenTypes')
6 7
 
7 8
 test('parses token stream into a tree', t => {
@@ -88,3 +89,29 @@ test('parse conditionals', t => {
88 89
     }),
89 90
   ])
90 91
 })
92
+
93
+test('missing close paren returns error', t => {
94
+  t.plan(1)
95
+  const tree = helpers.parse('(p ""')
96
+
97
+  t.deepEqual(
98
+    tree,
99
+    new OsloError({
100
+      line: 1,
101
+      message: 'Encountered an unexpected EOF while looking for a ).',
102
+    })
103
+  )
104
+})
105
+
106
+test('missing close quote returns an error', t => {
107
+  t.plan(1)
108
+  const tree = helpers.parse('(p "hello world)')
109
+
110
+  t.deepEqual(
111
+    tree,
112
+    new OsloError({
113
+      line: 1,
114
+      message: 'Encountered an unexpected EOF while looking for a ".',
115
+    })
116
+  )
117
+})

Loading…
Cancelar
Guardar