<- Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/main/lex.go
diff options
context:
space:
mode:
Diffstat (limited to 'main/lex.go')
-rw-r--r--main/lex.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/main/lex.go b/main/lex.go
new file mode 100644
index 0000000..c08f402
--- /dev/null
+++ b/main/lex.go
@@ -0,0 +1,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
+}