<- Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/main/lex.go
blob: c08f402afa6e3fa059a0cff9ea07ab34b3623b9d (plain)
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
package main

import (
	"unicode/utf8"
)

const eof rune = -1
type RuneReader struct {
	input string
	pos, width int
}
func (l *RuneReader) next() rune {
	if l.pos >= len(l.input) {
		l.width = 0
		return eof
	}
	var r rune
	r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
	l.pos += l.width
	return r
}
func (l *RuneReader) accept(chars string) bool {
	r := l.next()
	for _, char := range chars {
		if char == r {
			return true
		}
	}
	l.rewind()
	return false
}
func (l *RuneReader) rewind() {
	l.pos -= l.width
}