forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertion_parser.go
221 lines (196 loc) · 4.27 KB
/
assertion_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
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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"regexp"
)
const (
NONE = iota
OR
AND
LPAREN
RPAREN
URL
EOF
ERROR
)
type Token struct {
Typ int
value []byte
}
func (t Token) getString() string {
return string(t.value)
}
func (t Token) unexpectedError() error {
switch t.Typ {
case EOF:
return NewAssertionParseError("Unexpected EOF")
default:
return NewAssertionParseError("Unexpected token: %s", t.getString())
}
}
func byteArrayEq(a1, a2 []byte) bool {
if len(a1) != len(a2) {
return false
}
for i, c := range a1 {
if c != a2[i] {
return false
}
}
return true
}
func (t Token) Eq(t2 Token) bool {
return (t.Typ == t2.Typ) && byteArrayEq(t.value, t2.value)
}
func NewToken(typ int) *Token {
return &Token{Typ: typ}
}
type Lexer struct {
buffer []byte
last *Token
putback bool
re *regexp.Regexp
wss *regexp.Regexp
}
func NewLexer(s string) *Lexer {
// We're allowing '||' or ',' for disjunction
// We're allowing '&&' or '+' for conjunction
re := regexp.MustCompile(`^(\|\|)|(\,)|(\&\&)|(\+)|(\()|(\))|([^ \n\t&|(),+]+)`)
wss := regexp.MustCompile(`^([\n\t ]+)`)
l := &Lexer{buffer: []byte(s), re: re, wss: wss}
l.stripBuffer()
return l
}
func (lx *Lexer) stripBuffer() {
if len(lx.buffer) > 0 {
if match := lx.wss.FindSubmatchIndex(lx.buffer); match != nil {
lx.buffer = lx.buffer[match[3]:]
}
}
}
func (lx *Lexer) advanceBuffer(i int) {
lx.buffer = lx.buffer[i:]
lx.stripBuffer()
}
func (lx *Lexer) Putback() {
lx.putback = true
}
func (lx *Lexer) Get() *Token {
var ret *Token
if lx.putback {
ret = lx.last
lx.putback = false
} else if len(lx.buffer) == 0 {
ret = NewToken(EOF)
} else if match := lx.re.FindSubmatchIndex(lx.buffer); match != nil {
seq := []int{NONE, OR, OR, AND, AND, LPAREN, RPAREN, URL}
for i := 1; i <= len(seq); i++ {
if match[i*2] >= 0 {
ret = &Token{seq[i], lx.buffer[match[2*i]:match[2*i+1]]}
lx.advanceBuffer(match[2*i+1])
break
}
}
} else {
lx.buffer = nil
ret = NewToken(ERROR)
}
lx.last = ret
return ret
}
type Parser struct {
lexer *Lexer
err error
andOnly bool
}
func NewParser(lexer *Lexer) *Parser {
ret := &Parser{lexer, nil, false}
return ret
}
func NewAssertionAnd(left, right AssertionExpression) AssertionAnd {
factors := []AssertionExpression{left, right}
return AssertionAnd{factors}
}
func NewAssertionOr(left, right AssertionExpression) AssertionOr {
terms := []AssertionExpression{left, right}
return AssertionOr{terms}
}
func (p *Parser) Parse() AssertionExpression {
ret := p.parseExpr()
if ret != nil {
tok := p.lexer.Get()
if tok.Typ != EOF {
p.err = NewAssertionParseError("Found junk at end of input: %s",
tok.value)
ret = nil
}
}
return ret
}
func (p *Parser) parseTerm() (ret AssertionExpression) {
factor := p.parseFactor()
tok := p.lexer.Get()
if tok.Typ == AND {
term := p.parseTerm()
ret = NewAssertionAnd(factor, term)
} else {
ret = factor
p.lexer.Putback()
}
return ret
}
func (p *Parser) parseFactor() (ret AssertionExpression) {
tok := p.lexer.Get()
switch tok.Typ {
case URL:
url, err := ParseAssertionURL(tok.getString(), false)
if err != nil {
p.err = err
} else {
ret = url
}
case LPAREN:
if ex := p.parseExpr(); ex == nil {
ret = nil
p.err = NewAssertionParseError("Illegal parenthetical expression")
} else {
tok = p.lexer.Get()
if tok.Typ == RPAREN {
ret = ex
} else {
ret = nil
p.err = NewAssertionParseError("Unbalanced parentheses")
}
}
default:
p.err = tok.unexpectedError()
}
return ret
}
func (p *Parser) parseExpr() (ret AssertionExpression) {
term := p.parseTerm()
tok := p.lexer.Get()
if tok.Typ != OR {
ret = term
p.lexer.Putback()
} else if p.andOnly {
p.err = NewAssertionParseError("Unexpected 'OR' operator")
} else {
ex := p.parseExpr()
ret = NewAssertionOr(term, ex)
}
return ret
}
func AssertionParse(s string) (AssertionExpression, error) {
lexer := NewLexer(s)
parser := Parser{lexer, nil, false}
ret := parser.Parse()
return ret, parser.err
}
func AssertionParseAndOnly(s string) (AssertionExpression, error) {
lexer := NewLexer(s)
parser := Parser{lexer, nil, true}
ret := parser.Parse()
return ret, parser.err
}