forked from stephens2424/php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.go
62 lines (58 loc) · 1.23 KB
/
blocks.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
package php
import (
"github.com/stephens2424/php/ast"
"github.com/stephens2424/php/token"
)
func (p *Parser) parseBlock() *ast.Block {
p.expect(token.BlockBegin)
b := p.parseStatementsUntil(token.BlockEnd)
p.expectCurrent(token.BlockEnd)
return b
}
func (p *Parser) parseStatementsUntil(endTokens ...token.Token) *ast.Block {
block := &ast.Block{}
breakTypes := map[token.Token]bool{}
for _, Typ := range endTokens {
breakTypes[Typ] = true
}
for {
p.next()
if _, ok := breakTypes[p.current.Typ]; ok {
break
}
stmt := p.parseStmt()
if stmt != nil {
block.Statements = append(block.Statements, stmt)
}
if p.current.Typ == token.EOF {
break
}
}
return block
}
func (p *Parser) parseExpressionsUntil(separator token.Token, endTokens ...token.Token) []ast.Expression {
exprs := make([]ast.Expression, 0, 1)
breakTypes := map[token.Token]bool{}
for _, Typ := range endTokens {
breakTypes[Typ] = true
}
p.next()
first := true
for {
if _, ok := breakTypes[p.current.Typ]; ok {
break
} else if first {
first = false
} else {
p.expectCurrent(separator)
p.next()
}
expr := p.parseExpression()
if expr == nil {
return exprs
}
exprs = append(exprs, expr)
p.next()
}
return exprs
}