-
Notifications
You must be signed in to change notification settings - Fork 2
/
lex.go
111 lines (92 loc) · 2.13 KB
/
lex.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
/* Copyright (C) 2015 by Alexandru Cojocaru */
/* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package logparse
import (
"strings"
"unicode/utf8"
)
const eof rune = -1
type lex struct {
s string
p int
width int
err error
}
func newLex(s string) *lex {
return &lex{s, 0, 0, nil}
}
func (l *lex) next() (r rune) {
if l.p >= len(l.s) {
return eof
}
r, l.width = utf8.DecodeRuneInString(l.s[l.p:])
l.p += l.width
return
}
func (l *lex) LineNumber() int {
return 1 + strings.Count(l.s[:l.p], "\n")
}
func (l *lex) ColumnNumber() int {
return l.p - strings.LastIndex(l.s[:l.p], "\n")
}
func (l *lex) Err() error {
return l.err
}
func (l *lex) setErr(err error) {
if l.err == nil {
l.err = err
}
}
func (l *lex) match(m string) bool {
if !strings.HasPrefix(l.s[l.p:], m) {
return false
}
l.p += len(m)
return true
}
/*
func expect(l *lex, sep string) bool {
if l.err != nil {
return false
}
if !l.match(sep) {
r := l.next()
if r == eof {
// setErr(fmt.Errorf("expected %q but got EOF", sep))
l.setErr(io.EOF)
return false
} else {
l.setErr(fmt.Errorf("%d: expected %q but got %q", l.ColumnNumber()-1, sep, r))
return false
}
}
return true
}
*/
func (l *lex) span(m string) (string, bool) {
i := strings.Index(l.s[l.p:], m)
if i < 0 {
return "", false
}
s := l.s[l.p : l.p+i]
l.p += i + len(m)
return s, true
}
func (l *lex) spanAny(chars string) (string, bool) {
i := strings.IndexAny(l.s[l.p:], chars)
if i < 0 {
return "", false
}
s := l.s[l.p : l.p+i]
l.p += i + len(chars)
return s, true
}