Browse Source

Don't wrap eaches in an empty function call node

Because I was assuming that every expression
started with a literal, calls to `each` were
getting wrapped by a functionCall node with no
functionName, which was subsequently ignored by
the compiler. This removes all of that uselessness
master
Dylan Baker 5 years ago
parent
commit
d82d4ed612
2 changed files with 18 additions and 19 deletions
  1. 3
    8
      src/compiler.js
  2. 15
    11
      src/parser.js

+ 3
- 8
src/compiler.js View File

16
         )
16
         )
17
         const compiler = new Compiler(node.subtree, this.context)
17
         const compiler = new Compiler(node.subtree, this.context)
18
         const content = compiler.compile()
18
         const content = compiler.compile()
19
-
20
-        if (node.functionName) {
21
-          this.result += `<${node.functionName}${
22
-            attributes.length ? ' ' : ''
23
-          }${attributes.join(' ')}>${content}</${node.functionName}>`
24
-        } else {
25
-          this.result += content
26
-        }
19
+        this.result += `<${node.functionName}${
20
+          attributes.length ? ' ' : ''
21
+        }${attributes.join(' ')}>${content}</${node.functionName}>`
27
       } else if (node.type === 'string') {
22
       } else if (node.type === 'string') {
28
         this.result += node.content
23
         this.result += node.content
29
       } else if (node.type === 'identifier') {
24
       } else if (node.type === 'identifier') {

+ 15
- 11
src/parser.js View File

17
   expr() {
17
   expr() {
18
     this.tokenStream.eat(tokenTypes.OPAREN)
18
     this.tokenStream.eat(tokenTypes.OPAREN)
19
 
19
 
20
-    let elementNode = new Node()
21
-    elementNode.type = 'functionCall'
22
-    elementNode.functionName = this.tokenStream.eat(tokenTypes.LITERAL).value
23
-    elementNode.args = []
24
-    elementNode.subtree = []
20
+    let node = new Node()
21
+
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 = []
25
 
29
 
26
     while (
30
     while (
27
       this.tokenStream.peek().type != tokenTypes.CPAREN &&
31
       this.tokenStream.peek().type != tokenTypes.CPAREN &&
28
       this.tokenStream.peek().type !== tokenTypes.EOF
32
       this.tokenStream.peek().type !== tokenTypes.EOF
29
     ) {
33
     ) {
30
       if (this.tokenStream.peek().type === tokenTypes.ATTRIBUTE) {
34
       if (this.tokenStream.peek().type === tokenTypes.ATTRIBUTE) {
31
-        elementNode.args.push(this.attribute())
35
+        node.args.push(this.attribute())
32
       } else if (this.tokenStream.peek().type === tokenTypes.OPAREN) {
36
       } else if (this.tokenStream.peek().type === tokenTypes.OPAREN) {
33
-        elementNode.subtree.push(this.expr())
37
+        node.subtree.push(this.expr())
34
       } else if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
38
       } else if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
35
-        elementNode.subtree.push(this.identifier())
39
+        node.subtree.push(this.identifier())
36
       } else if (this.tokenStream.peek().type === tokenTypes.QUOTE) {
40
       } else if (this.tokenStream.peek().type === tokenTypes.QUOTE) {
37
-        elementNode.subtree.push(this.quotedString())
41
+        node.subtree.push(this.quotedString())
38
       } else if (this.tokenStream.peek().type === tokenTypes.KEYWORD) {
42
       } else if (this.tokenStream.peek().type === tokenTypes.KEYWORD) {
39
-        elementNode.subtree.push(this.keyword())
43
+        node = this.keyword()
40
       }
44
       }
41
     }
45
     }
42
 
46
 
43
     this.tokenStream.eat(tokenTypes.CPAREN)
47
     this.tokenStream.eat(tokenTypes.CPAREN)
44
 
48
 
45
-    return elementNode
49
+    return node
46
   }
50
   }
47
 
51
 
48
   attribute() {
52
   attribute() {

Loading…
Cancel
Save