-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
144 lines (129 loc) · 2.66 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package glambda
import (
"fmt"
)
type parser struct {
lexer *lexer
input string
peekCount int
tokens []token
}
func newParser(input string) *parser {
return &parser{
lexer: lex(input),
input: input,
tokens: make([]token, 1, 1),
}
}
func parse(input string) []node {
p := newParser(input)
return p.parse()
}
func (p *parser) peek() token {
if p.peekCount > 0 {
return p.tokens[p.peekCount-1]
}
p.peekCount = 1
p.tokens[0] = p.lexer.nextItem()
return p.tokens[0]
}
func (p *parser) expect(expectedType tokenType) bool {
peeked := p.peek()
return peeked.tokenType == expectedType
}
func (p *parser) next() token {
if p.peekCount > 0 {
p.peekCount--
} else {
p.tokens[0] = p.lexer.nextItem()
}
return p.tokens[p.peekCount]
}
func (p *parser) consume(expectedType tokenType) token {
token := p.next()
if token.tokenType == expectedType {
return token
}
panic(fmt.Sprintf(
"expected '%s' token, got '%s' at:\n%s",
expectedType, token, p.input[p.lexer.position-1:],
))
}
func (p *parser) parse() []node {
var nodes []node
for {
token := p.peek()
for token.tokenType == tokenComment || token.tokenType == tokenNewLine {
p.consume(token.tokenType)
token = p.peek()
}
if p.expect(tokenEOF) {
break
}
nodes = append(nodes, p.parseDefinition())
}
return nodes
}
func (p *parser) parseDefinition() definitionNode {
name := p.consume(tokenIdentifier).value
p.consume(tokenEquals)
abstraction := p.parseAbstraction()
return definitionNode{
name,
abstraction,
}
}
func (p *parser) parseLambdaTerm() term {
return p.parseApplication()
}
func (p *parser) parseAbstraction() abstractionNode {
p.consume(tokenLambda)
var variables []variableNode
for p.expect(tokenIdentifier) {
variables = append(variables, p.parseVariable())
}
p.consume(tokenDot)
var abstraction term = p.parseLambdaTerm()
for i := len(variables) - 1; i >= 0; i-- {
variable := variables[i]
abstraction = abstractionNode{
variable,
abstraction,
}
}
return abstraction.(abstractionNode)
}
func (p *parser) parseApplication() term {
left := p.parseAtom()
for {
if p.expect(tokenNewLine) {
return left
}
right := p.parseAtom()
if right == nil {
return left
}
left = applicationNode{left, right}
}
}
func (p *parser) parseAtom() term {
if p.expect(tokenLeftParen) {
p.consume(tokenLeftParen)
term := p.parseLambdaTerm()
p.consume(tokenRightParen)
return term
}
if p.expect(tokenLambda) {
return p.parseAbstraction()
}
if p.expect(tokenIdentifier) {
return p.parseVariable()
}
return nil
}
func (p *parser) parseVariable() variableNode {
name := p.consume(tokenIdentifier).value
return variableNode{
name,
}
}