~egtann/up

ref: e0094c2d396c40d9b217e16605545b83ed0985b4 up/lexer.go -rw-r--r-- 4.0 KiB
e0094c2d — Evan Tann update license to openbsd isc 7 months ago
                                                                                
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
package up

import (
	"fmt"
	"strings"
	"unicode"
	"unicode/utf8"
)

const eof = -1

type tokenType int

const (
	tokenError   tokenType = iota // Error occurred. Value is text of err
	tokenEOF                      // Designate the end of the file
	tokenSpace                    // Run of spaces separating arguments
	tokenTab                      // Tab '\t'
	tokenNewline                  // Line break
	tokenText                     // Plaintext
	tokenComment                  // Pound '#'

	// Keywords follow
	tokenKeyword   // Used only to delimit keywords
	tokenInventory // "inventory"
)

type token struct {
	typ tokenType
	pos int
	val string
}

type stateFn func(*lexer) stateFn

// run lexes the input by executing state functions until the state is nil.
func (l *lexer) run() {
	for l.state = lexText; l.state != nil; {
		l.state = l.state(l)
	}
	close(l.tokens) // No more tokens will be delivered
}

// lexer holds the state of the scanner.
type lexer struct {
	input   string     // The string being scanned
	state   stateFn    // The next lexing function to enter
	start   int        // Start position of this token
	pos     int        // Current position in the input
	width   int        // Width of the last rune read
	lastPos int        // Position of last token returned by nextToken
	tokens  chan token // Channel of scanned tokens
}

func lex(input string) *lexer {
	l := &lexer{
		input:  input,
		state:  lexText,
		tokens: make(chan token),
	}
	go l.run()
	return l
}

// drain the output so the lexing goroutine will exit. Called by the parser,
// not in the lexing goroutine.
func (l *lexer) drain() {
	for range l.tokens {
	}
}

// emit passes an token back to the client.
func (l *lexer) emit(t tokenType) {
	tkn := token{typ: t, val: l.input[l.start:l.pos]}
	l.tokens <- tkn
	l.start = l.pos
}

func (l *lexer) next() rune {
	if l.pos >= len(l.input) {
		l.width = 0
		return eof
	}
	r, w := utf8.DecodeRuneInString(l.input[l.pos:])
	l.width = w
	l.pos += l.width
	return r
}

// nextToken reports the next token from the input.
func (l *lexer) nextToken() token {
	token := <-l.tokens
	l.lastPos = token.pos
	return token
}

// ignore skips over the pending input before this point.
func (l *lexer) ignore() {
	l.start = l.pos
}

// backup steps back one rune. It can be called only once per call of next.
func (l *lexer) backup() {
	l.pos -= l.width
}

// peek returns but does not consume the next rune in the input.
func (l *lexer) peek() rune {
	r := l.next()
	l.backup()
	return r
}

// accept consumes the next rune if it's from the valid set.
func (l *lexer) accept(valid string) bool {
	if strings.IndexRune(valid, l.next()) >= 0 {
		return true
	}
	l.backup()
	return false
}

// acceptRun consumes a run of runes from the valid set.
func (l *lexer) acceptRun(valid string) {
	for strings.IndexRune(valid, l.next()) >= 0 {
	}
	l.backup()
}

// errorf returns an error token and terminates the scan by passing back a nil
// pointer as the next state, terminating l.run.
func (l *lexer) errorf(format string, args ...interface{}) stateFn {
	l.tokens <- token{typ: tokenError, val: fmt.Sprintf(format, args...)}
	return nil
}

func lexText(l *lexer) stateFn {
Outer:
	for {
		text := l.input[l.start:l.pos]
		r := l.next()
		switch {
		case r == eof:
			break Outer
		case r == '#':
			l.emit(tokenComment)
		case text == "inventory":
			l.backup()
			l.emit(tokenInventory)
		case isEndOfLine(r):
			l.backup()
			if len(text) > 0 {
				l.emit(tokenText)
			}
			l.next()
			l.emit(tokenNewline)
		case r == ' ':
			l.backup()
			if len(text) > 0 {
				l.emit(tokenText)
			}
			return lexSpace
		case r == '\t':
			l.emit(tokenTab)
		}
	}
	// Correctly reached EOF
	if l.pos > l.start {
		l.emit(tokenText)
	}
	l.emit(tokenEOF)
	return nil
}

func lexSpace(l *lexer) stateFn {
	for l.peek() == ' ' {
		l.next()
	}
	l.emit(tokenSpace)
	return lexText
}

func isAlphaNumeric(r rune) bool {
	return r == '_' || r == '.' || unicode.IsLetter(r) ||
		unicode.IsDigit(r)
}

func isEndOfLine(r rune) bool {
	return r == '\r' || r == '\n'
}