forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.go
63 lines (56 loc) · 1.1 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
// +build js
package parse
import (
"container/list"
"fmt"
)
type lexer struct {
name string
input string
leftDelim string
rightDelim string
state stateFn
pos Pos
start Pos
width Pos
lastPos Pos
items chan item
parenDepth int
itemsList *list.List
}
func (l *lexer) emit(t itemType) {
l.itemsList.PushBack(item{t, l.start, l.input[l.start:l.pos]})
l.start = l.pos
}
func (l *lexer) errorf(format string, args ...interface{}) stateFn {
l.itemsList.PushBack(item{itemError, l.start, fmt.Sprintf(format, args...)})
return nil
}
func (l *lexer) nextItem() item {
element := l.itemsList.Front()
for element == nil {
l.state = l.state(l)
element = l.itemsList.Front()
}
l.itemsList.Remove(element)
item := element.Value.(item)
l.lastPos = item.pos
return item
}
func lex(name, input, left, right string) *lexer {
if left == "" {
left = leftDelim
}
if right == "" {
right = rightDelim
}
l := &lexer{
name: name,
input: input,
leftDelim: left,
rightDelim: right,
itemsList: list.New(),
}
l.state = lexText
return l
}