aboutsummaryrefslogtreecommitdiff
path: root/driver/parser.go
blob: 10c716221e8cb0f8a5eb80deead2b2b0f374961b (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package driver

import (
	"fmt"
	"io"
	"strings"

	mldriver "github.com/nihei9/maleeni/driver"
	mlspec "github.com/nihei9/maleeni/spec"
	"github.com/nihei9/vartan/spec"
)

type Node struct {
	KindName string
	Text     string
	Children []*Node
}

func PrintTree(w io.Writer, node *Node) {
	printTree(w, node, "", "")
}

func printTree(w io.Writer, node *Node, ruledLine string, childRuledLinePrefix string) {
	if node == nil {
		return
	}

	if node.Text != "" {
		fmt.Fprintf(w, "%v%v %#v\n", ruledLine, node.KindName, node.Text)
	} else {
		fmt.Fprintf(w, "%v%v\n", ruledLine, node.KindName)
	}

	num := len(node.Children)
	for i, child := range node.Children {
		var line string
		if num > 1 && i < num-1 {
			line = "├─ "
		} else {
			line = "└─ "
		}

		var prefix string
		if i >= num-1 {
			prefix = "    "
		} else {
			prefix = "│  "
		}

		printTree(w, child, childRuledLinePrefix+line, childRuledLinePrefix+prefix)
	}
}

type semanticFrame struct {
	cst *Node
	ast *Node
}

type Parser struct {
	gram       *spec.CompiledGrammar
	lex        *mldriver.Lexer
	stateStack []int
	semStack   []*semanticFrame
	cst        *Node
	ast        *Node
}

func NewParser(gram *spec.CompiledGrammar, src io.Reader) (*Parser, error) {
	lex, err := mldriver.NewLexer(gram.LexicalSpecification.Maleeni.Spec, src)
	if err != nil {
		return nil, err
	}

	return &Parser{
		gram:       gram,
		lex:        lex,
		stateStack: []int{},
		semStack:   []*semanticFrame{},
	}, nil
}

func (p *Parser) Parse() error {
	termCount := p.gram.ParsingTable.TerminalCount
	p.push(p.gram.ParsingTable.InitialState)
	tok, err := p.nextToken()
	if err != nil {
		return err
	}
	for {
		var tsym int
		if tok.EOF {
			tsym = p.gram.ParsingTable.EOFSymbol
		} else {
			tsym = p.gram.LexicalSpecification.Maleeni.KindToTerminal[tok.KindID]
		}
		act := p.gram.ParsingTable.Action[p.top()*termCount+tsym]
		switch {
		case act < 0: // Shift
			tokText := tok.Text()
			tok, err = p.shift(act * -1)
			if err != nil {
				return err
			}

			// semantic action
			p.semStack = append(p.semStack, &semanticFrame{
				cst: &Node{
					KindName: p.gram.ParsingTable.Terminals[tsym],
					Text:     tokText,
				},
				ast: &Node{
					KindName: p.gram.ParsingTable.Terminals[tsym],
					Text:     tokText,
				},
			})
		case act > 0: // Reduce
			accepted := p.reduce(act)
			if accepted {
				top := p.semStack[len(p.semStack)-1]
				p.cst = top.cst
				p.ast = top.ast
				return nil
			}

			// semantic action

			prodNum := act
			lhs := p.gram.ParsingTable.LHSSymbols[prodNum]

			// When an alternative is empty, `n` will be 0, and `handle` will be empty slice.
			n := p.gram.ParsingTable.AlternativeSymbolCounts[prodNum]
			handle := p.semStack[len(p.semStack)-n:]

			var cst *Node
			{
				children := make([]*Node, len(handle))
				for i, f := range handle {
					children[i] = f.cst
				}
				cst = &Node{
					KindName: p.gram.ParsingTable.NonTerminals[lhs],
					Children: children,
				}
			}

			var ast *Node
			{
				act := p.gram.ASTAction.Entries[prodNum]
				children := []*Node{}
				if act != nil {
					for _, e := range act {
						if e > 0 {
							offset := e - 1
							children = append(children, handle[offset].ast)
						} else {
							offset := e*-1 - 1
							for _, c := range handle[offset].ast.Children {
								children = append(children, c)
							}
						}
					}
				} else {
					// If an alternative has no AST action, a driver generates
					// a node with the same structure as a CST.
					for _, f := range handle {
						children = append(children, f.ast)
					}
				}
				ast = &Node{
					KindName: p.gram.ParsingTable.NonTerminals[lhs],
					Children: children,
				}
			}

			p.semStack = p.semStack[:len(p.semStack)-n]
			p.semStack = append(p.semStack, &semanticFrame{
				cst: cst,
				ast: ast,
			})
		default:
			var tokText string
			if tok.EOF {
				tokText = "<EOF>"
			} else {
				tokText = fmt.Sprintf("%v (%v)", tok.KindName.String(), tok.Text())
			}

			eKinds, eof := p.expectedKinds(p.top())

			var b strings.Builder
			fmt.Fprintf(&b, "%v", eKinds[0])
			for _, k := range eKinds[1:] {
				fmt.Fprintf(&b, ", %v", k)
			}
			if eof {
				if len(eKinds) > 0 {
					fmt.Fprintf(&b, ", <EOF>")
				} else {
					fmt.Fprintf(&b, "<EOF>")
				}
			}

			return fmt.Errorf("unexpected token: %v, expected: %v", tokText, b.String())
		}
	}
}

func (p *Parser) nextToken() (*mldriver.Token, error) {
	skip := p.gram.LexicalSpecification.Maleeni.Skip
	for {
		tok, err := p.lex.Next()
		if err != nil {
			return nil, err
		}
		if tok.Invalid {
			return nil, fmt.Errorf("invalid token: '%v'", tok.Text())
		}

		if skip[tok.KindID] > 0 {
			continue
		}

		return tok, nil
	}
}

func (p *Parser) shift(nextState int) (*mldriver.Token, error) {
	p.push(nextState)
	return p.nextToken()
}

func (p *Parser) reduce(prodNum int) bool {
	tab := p.gram.ParsingTable
	lhs := tab.LHSSymbols[prodNum]
	if lhs == tab.LHSSymbols[tab.StartProduction] {
		return true
	}
	n := tab.AlternativeSymbolCounts[prodNum]
	p.pop(n)
	nextState := tab.GoTo[p.top()*tab.NonTerminalCount+lhs]
	p.push(nextState)
	return false
}

func (p *Parser) top() int {
	return p.stateStack[len(p.stateStack)-1]
}

func (p *Parser) push(state int) {
	p.stateStack = append(p.stateStack, state)
}

func (p *Parser) pop(n int) {
	p.stateStack = p.stateStack[:len(p.stateStack)-n]
}

func (p *Parser) CST() *Node {
	return p.cst
}

func (p *Parser) AST() *Node {
	return p.ast
}

func (p *Parser) expectedKinds(state int) ([]mlspec.LexKindName, bool) {
	kinds := []mlspec.LexKindName{}
	eof := false
	terms := p.gram.ParsingTable.ExpectedTerminals[state]
	for _, tsym := range terms {
		if tsym == 1 {
			eof = true
			continue
		}

		kindID := p.gram.LexicalSpecification.Maleeni.TerminalToKind[tsym]
		kindName := p.gram.LexicalSpecification.Maleeni.Spec.KindNames[kindID]
		kinds = append(kinds, kindName)
	}

	return kinds, eof
}