Procházet zdrojové kódy

Rework main parser logic

The way that we were doing it before was going to
make it very difficult to implement any sort of
error handling or extension to the language.
master
Dylan Baker před 5 roky
rodič
revize
ebae8a3536
1 změnil soubory, kde provedl 28 přidání a 19 odebrání
  1. 28
    19
      src/parser.js

+ 28
- 19
src/parser.js Zobrazit soubor

@@ -15,30 +15,16 @@ module.exports = class Parser {
15 15
   }
16 16
 
17 17
   expr() {
18
-    this.tokenStream.eat(tokenTypes.OPAREN)
19
-
20
-    let node = new Node()
18
+    let node
21 19
 
22
-    if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
23
-      node.type = 'functionCall'
24
-      node.functionName = this.tokenStream.eat(tokenTypes.LITERAL).value
25
-    }
26
-
27
-    node.args = []
28
-    node.subtree = []
20
+    this.tokenStream.eat(tokenTypes.OPAREN)
29 21
 
30 22
     while (
31
-      this.tokenStream.peek().type != tokenTypes.CPAREN &&
23
+      this.tokenStream.peek().type !== tokenTypes.CPAREN &&
32 24
       this.tokenStream.peek().type !== tokenTypes.EOF
33 25
     ) {
34
-      if (this.tokenStream.peek().type === tokenTypes.ATTRIBUTE) {
35
-        node.args.push(this.attribute())
36
-      } else if (this.tokenStream.peek().type === tokenTypes.OPAREN) {
37
-        node.subtree.push(this.expr())
38
-      } else if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
39
-        node.subtree.push(this.identifier())
40
-      } else if (this.tokenStream.peek().type === tokenTypes.QUOTE) {
41
-        node.subtree.push(this.quotedString())
26
+      if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
27
+        node = this.element()
42 28
       } else if (this.tokenStream.peek().type === tokenTypes.KEYWORD) {
43 29
         node = this.keyword()
44 30
       }
@@ -49,6 +35,29 @@ module.exports = class Parser {
49 35
     return node
50 36
   }
51 37
 
38
+  element() {
39
+    let elementNode = new Node({
40
+      type: 'functionCall',
41
+      args: [],
42
+      subtree: [],
43
+      functionName: this.tokenStream.eat(tokenTypes.LITERAL).value
44
+    })
45
+
46
+    while (![tokenTypes.CPAREN, tokenTypes.EOF].includes(this.tokenStream.peek().type)) {
47
+      if (this.tokenStream.peek().type === tokenTypes.ATTRIBUTE) {
48
+        elementNode.args.push(this.attribute())
49
+      } else if (this.tokenStream.peek().type === tokenTypes.QUOTE) {
50
+        elementNode.subtree.push(this.quotedString())
51
+      } else if (this.tokenStream.peek().type === tokenTypes.OPAREN) {
52
+        elementNode.subtree.push(this.expr())
53
+      } else if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
54
+        elementNode.subtree.push(this.identifier())
55
+      }
56
+    }
57
+
58
+    return elementNode
59
+  }
60
+
52 61
   attribute() {
53 62
     let attributeNode = new Node()
54 63
     attributeNode.attributeName = this.tokenStream.eat(

Načítá se…
Zrušit
Uložit