forked from stephens2424/php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
178 lines (158 loc) · 3.71 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
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
package php
import (
"fmt"
"github.com/stephens2424/php/ast"
"github.com/stephens2424/php/lexer"
"github.com/stephens2424/php/token"
)
// Parser handles scanning through and parsing a PHP source string into an AST. It is configurable
// to have various types of debugging features.
type Parser struct {
Debug bool // Debug causes the parser to print all errors to stdout and relay any panic upon internal panic recovery.
PrintTokens bool // PrintTokens causes the parser to print all tokens received from the lexer to stdout.
MaxErrors int // Indicates the number of errors to allow before triggering a panic. The default is 10.
FileSet *ast.FileSet
lexer token.Stream
previous []token.Item
idx int
current token.Item
errors []error
parenLevel int
errorMap map[int]bool
errorCount int
namespace *ast.Namespace
scope *ast.Scope
instantiation bool
}
// NewParser readies a parser
func NewParser() *Parser {
p := &Parser{
idx: -1,
MaxErrors: 10,
errorMap: make(map[int]bool),
FileSet: ast.NewFileSet(),
}
return p
}
// Parse consumes the input string to produce an AST that represents it.
func (p *Parser) Parse(filepath, input string) (file *ast.File, errors []error) {
file = &ast.File{}
p.scope = p.FileSet.Scope
p.namespace = p.FileSet.GlobalNamespace
p.lexer = lexer.NewLexer(input)
p.FileSet.Files[filepath] = file
defer func() {
if r := recover(); r != nil {
errors = append([]error{fmt.Errorf("%s", r)}, p.errors...)
if p.Debug {
for _, err := range p.errors {
fmt.Println(err)
}
panic(r)
}
}
}()
// expecting either token.HTML or token.PHPBegin
file.Nodes = make([]ast.Node, 0, 1)
TokenLoop:
for {
p.next()
switch p.current.Typ {
case token.EOF:
break TokenLoop
default:
n := p.parseNode()
if n != nil {
file.Nodes = append(file.Nodes, n)
}
}
}
errors = p.errors
return
}
func (p *Parser) parseNode() ast.Node {
switch p.current.Typ {
case token.HTML:
return ast.Echo(ast.Literal{Type: ast.String, Value: p.current.Val})
case token.PHPBegin:
return nil
case token.PHPEnd:
return nil
}
return p.parseStmt()
}
func (p *Parser) next() {
p.idx += 1
if len(p.previous) <= p.idx {
p.current = p.lexer.Next()
if p.PrintTokens {
fmt.Println(p.current)
}
p.previous = append(p.previous, p.current)
} else {
p.current = p.previous[p.idx]
}
}
func (p *Parser) backup() {
p.idx -= 1
p.current = p.previous[p.idx]
}
func (p *Parser) peek() (i token.Item) {
p.next()
i = p.current
p.backup()
return
}
func (p *Parser) expectCurrent(i ...token.Token) {
for _, Typ := range i {
if p.current.Typ == Typ {
return
}
}
p.expected(i...)
}
func (p *Parser) expectAndNext(i ...token.Token) {
defer p.next()
for _, Typ := range i {
if p.current.Typ == Typ {
return
}
}
p.expected(i...)
}
func (p *Parser) expect(i ...token.Token) {
p.next()
p.expectCurrent(i...)
}
func (p *Parser) expected(i ...token.Token) {
p.errorf("Found %s, expected %s", p.current, i)
}
func (p *Parser) accept(i ...token.Token) bool {
nextTyp := p.peek().Typ
for _, Typ := range i {
if nextTyp == Typ {
p.next()
return true
}
}
return false
}
func (p *Parser) errorf(str string, args ...interface{}) {
if p.errorCount > p.MaxErrors {
panic("too many errors")
}
if _, ok := p.errorMap[p.current.Begin.Line]; ok {
return
}
errString := fmt.Sprintf(str, args...)
p.errorCount += 1
p.errors = append(p.errors, fmt.Errorf("%s: %s", p.errorPrefix(), errString))
p.errorMap[p.current.Begin.Line] = true
}
func (p *Parser) errorPrefix() string {
return fmt.Sprintf("%d", p.current.Begin.Line)
}
func (p *Parser) parseNextExpression() ast.Expression {
p.next()
return p.parseExpression()
}