/
driver.go
82 lines (70 loc) · 1.77 KB
/
driver.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
// Copyright 2016 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package vm
import (
"flag"
"fmt"
"io"
"strconv"
"github.com/golang/glog"
)
func Parse(name string, input io.Reader) (astNode, error) {
p := newParser(name, input)
r := mtailParse(p)
if r != 0 || p == nil || p.errors != nil {
return nil, p.errors
}
return p.root, nil
}
const EOF = 0
type parser struct {
name string
root astNode
errors ErrorList
l *lexer
t token // Most recently lexed token.
pos position // Optionally contains the position of the start of a production
}
func newParser(name string, input io.Reader) *parser {
return &parser{name: name, l: newLexer(name, input)}
}
func (p *parser) ErrorP(s string, pos *position) {
p.errors.Add(pos, s)
}
func (p *parser) Error(s string) {
p.errors.Add(&p.t.pos, s)
}
func (p *parser) Lex(lval *mtailSymType) int {
p.t = p.l.nextToken()
switch p.t.kind {
case INVALID:
p.Error(p.t.text)
return EOF
case INTLITERAL:
var err error
lval.intVal, err = strconv.ParseInt(p.t.text, 10, 64)
if err != nil {
p.Error(fmt.Sprintf("bad number '%s': %s", p.t.text, err))
return INVALID
}
case FLOATLITERAL:
var err error
lval.floatVal, err = strconv.ParseFloat(p.t.text, 64)
if err != nil {
p.Error(fmt.Sprintf("bad number '%s': %s", p.t.text, err))
return INVALID
}
case LT, GT, LE, GE, NE, EQ, SHL, SHR, BITAND, BITOR, AND, OR, XOR, NOT, INC, DIV, MUL, MINUS, PLUS, ASSIGN, ADD_ASSIGN, POW, MOD, CONCAT, MATCH, NOT_MATCH:
lval.op = int(p.t.kind)
default:
lval.text = p.t.text
}
return int(p.t.kind)
}
func (p *parser) inRegex() {
glog.V(2).Info("Entering regex")
p.l.inRegex = true
}
func init() {
flag.IntVar(&mtailDebug, "mtailDebug", 0, "Set parser debug level.")
}