|
@@ -2,56 +2,50 @@ const Node = require('./node')
|
2
|
2
|
const tokenTypes = require('./tokenTypes')
|
3
|
3
|
|
4
|
4
|
module.exports = class Parser {
|
5
|
|
- constructor(tokens) {
|
6
|
|
- this.tokens = tokens
|
|
5
|
+ constructor(tokenStream) {
|
|
6
|
+ this.tokenStream = tokenStream
|
7
|
7
|
}
|
8
|
8
|
|
9
|
9
|
parse() {
|
10
|
10
|
let tree = []
|
11
|
|
- while (this.tokens.peek().type !== tokenTypes.EOF) {
|
|
11
|
+ while (this.tokenStream.peek().type !== tokenTypes.EOF) {
|
12
|
12
|
tree.push(this.expr())
|
13
|
13
|
}
|
14
|
14
|
return tree
|
15
|
15
|
}
|
16
|
16
|
|
17
|
17
|
expr() {
|
18
|
|
- this.tokens.eat(tokenTypes.OPAREN)
|
19
|
|
-
|
20
|
|
- let tokenTypesToEndTheLoop = [
|
21
|
|
- tokenTypes.OPAREN,
|
22
|
|
- tokenTypes.CPAREN,
|
23
|
|
- tokenTypes.EOF,
|
24
|
|
- ]
|
|
18
|
+ this.tokenStream.eat(tokenTypes.OPAREN)
|
25
|
19
|
|
26
|
20
|
let functionCallNode = new Node()
|
27
|
|
- functionCallNode.functionName = this.tokens.eat(tokenTypes.LITERAL).value
|
|
21
|
+ functionCallNode.functionName = this.tokenStream.eat(tokenTypes.LITERAL).value
|
28
|
22
|
functionCallNode.args = []
|
29
|
23
|
functionCallNode.subtree = []
|
30
|
24
|
|
31
|
|
- while (this.tokens.peek().type != tokenTypes.CPAREN) {
|
32
|
|
- if (this.tokens.peek().type === tokenTypes.ATTRIBUTE) {
|
|
25
|
+ while (this.tokenStream.peek().type != tokenTypes.CPAREN && this.tokenStream.peek().type !== tokenTypes.EOF) {
|
|
26
|
+ if (this.tokenStream.peek().type === tokenTypes.ATTRIBUTE) {
|
33
|
27
|
functionCallNode.args.push(this.attribute())
|
34
|
|
- } else if (this.tokens.peek().type === tokenTypes.OPAREN) {
|
35
|
|
- functionCallNode.subtree = this.expr()
|
|
28
|
+ } else if (this.tokenStream.peek().type === tokenTypes.OPAREN) {
|
|
29
|
+ functionCallNode.subtree.push(this.expr())
|
36
|
30
|
}
|
37
|
31
|
}
|
38
|
32
|
|
39
|
|
- this.tokens.eat(tokenTypes.CPAREN)
|
|
33
|
+ this.tokenStream.eat(tokenTypes.CPAREN)
|
40
|
34
|
|
41
|
35
|
return functionCallNode
|
42
|
36
|
}
|
43
|
37
|
|
44
|
38
|
attribute() {
|
45
|
39
|
let attributeNode = new Node()
|
46
|
|
- attributeNode.attributeName = this.tokens.eat(tokenTypes.ATTRIBUTE).value
|
|
40
|
+ attributeNode.attributeName = this.tokenStream.eat(tokenTypes.ATTRIBUTE).value
|
47
|
41
|
attributeNode.attributeValue = this.string()
|
48
|
42
|
return attributeNode
|
49
|
43
|
}
|
50
|
44
|
|
51
|
45
|
string() {
|
52
|
|
- this.tokens.eat(tokenTypes.QUOTE)
|
53
|
|
- let stringValue = this.tokens.eat(tokenTypes.LITERAL).value
|
54
|
|
- this.tokens.eat(tokenTypes.QUOTE)
|
|
46
|
+ this.tokenStream.eat(tokenTypes.QUOTE)
|
|
47
|
+ let stringValue = this.tokenStream.eat(tokenTypes.LITERAL).value
|
|
48
|
+ this.tokenStream.eat(tokenTypes.QUOTE)
|
55
|
49
|
return stringValue
|
56
|
50
|
}
|
57
|
51
|
}
|