-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
175 lines (155 loc) · 3.57 KB
/
lexer.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
package narg
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"unicode/utf8"
"github.com/nochso/narg/token"
)
// Lexer reads narg input and parses it into tokens.
type Lexer struct {
// Token that was read after the latest successful call to Scan()
Token token.T
Err error
buf *bufio.Reader
line int
col int
}
var eof = rune(0)
// NewLexer returns a new Lexer for parsing tokens.
func NewLexer(r io.Reader) *Lexer {
return &Lexer{
buf: bufio.NewReader(r),
line: 1,
col: 1,
}
}
// Scan attempts to read the next Token into Lexer.Token.
// Returns true when a new Token is ready.
func (l *Lexer) Scan() bool {
l.Token = l.scan()
l.line += strings.Count(l.Token.Str, "\n")
lastIndex := strings.LastIndexByte(l.Token.Str, '\n')
if lastIndex == -1 {
// there's no linefeed, read full token str
lastIndex = 0
} else {
// begin new line
l.col = 0
}
l.col += utf8.RuneCountInString(l.Token.Str[lastIndex:])
return l.Token.Type != token.EOF && l.Err == nil
}
func (l *Lexer) scan() token.T {
r := l.read()
switch r {
case eof:
return l.newToken(string(r), token.EOF)
case '{':
return l.newToken(string(r), token.BraceOpen)
case '}':
return l.newToken(string(r), token.BraceClose)
case '\n':
return l.newToken(string(r), token.Linefeed)
case '#':
return l.scanWhile(r, token.Comment, continuesComment)
case '"':
return l.scanQuotedValue()
}
if isWhitespace(r) {
return l.scanWhile(r, token.Whitespace, isWhitespace)
}
t := l.scanWhile(r, token.UnquotedValue, isUnquotedValue)
return l.notFollowedBy(t, "value is missing separator from next value", isQuote)
}
func (l *Lexer) scanWhile(start rune, tokenType token.Type, fn func(rune) bool) token.T {
buf := &bytes.Buffer{}
buf.WriteRune(start)
r := l.read()
for r != eof {
if !fn(r) {
l.unread()
break
}
buf.WriteRune(r)
r = l.read()
}
return l.newToken(buf.String(), tokenType)
}
func (l *Lexer) scanQuotedValue() token.T {
buf := bytes.NewBufferString(`"`)
r := l.read()
escaped := false
for r != eof {
buf.WriteRune(r)
if !escaped && r == '"' {
break
}
if !escaped && r == '\\' {
escaped = true
r = l.read()
continue
}
r = l.read()
escaped = false
}
if r == eof {
t := l.newToken(buf.String(), token.Invalid)
return l.setErr(t, "quoted value is missing closing quote")
}
t := l.newToken(buf.String(), token.QuotedValue)
return l.notFollowedBy(t, "value is missing separator from next value", invalidAfterQuotedValue)
}
func (l *Lexer) setErr(t token.T, err string) token.T {
l.Err = fmt.Errorf("line %d, column %d: %s: %#v", t.Line, t.Col, err, t.Str)
t.Type = token.Invalid
return t
}
func (l *Lexer) notFollowedBy(t token.T, err string, invalidFn func(rune) bool) token.T {
r := l.read()
if r == eof {
return t
}
if !invalidFn(r) {
l.unread()
return t
}
t.Str += string(r)
return l.setErr(t, err)
}
func (l *Lexer) newToken(str string, tokenType token.Type) token.T {
return token.T{
Str: str,
Type: tokenType,
Line: l.line,
Col: l.col,
}
}
func (l *Lexer) read() rune {
r, _, err := l.buf.ReadRune()
if err != nil {
return eof
}
return r
}
func (l *Lexer) unread() error {
return l.buf.UnreadRune()
}
func isWhitespace(r rune) bool {
return r == ' ' || r == '\r' || r == '\t'
}
func isQuote(r rune) bool {
return r == '"'
}
func continuesComment(r rune) bool {
return r != '\n'
}
func invalidAfterQuotedValue(r rune) bool {
return isQuote(r) || isUnquotedValue(r)
}
// stop on anything meaningful
func isUnquotedValue(r rune) bool {
return !isWhitespace(r) && r != '{' && r != '}' && r != '\n' && r != '#' && r != '"'
}