aboutsummaryrefslogtreecommitdiff
path: root/lexer.go
blob: 76ffc8b35532aec6e440f27e70c3e3dc15e0f9d4 (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
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
package main

import (
	"log"
	"strconv"
	"strings"
	"unicode"
	"unicode/utf8"
)

const eof = 0

type item struct {
	typ int
	val string
}

type yyLex struct {
	input string
	start int
	pos   int
	width int
	items chan item
}

func (y *yyLex) Error(s string) {
	log.Println(s)
}

func (y *yyLex) Lex(lval *yySymType) int {
	item := <-y.items
	switch item.typ {
	case NUMBER:
		n, err := strconv.ParseFloat(item.val, 64)
		if err != nil {
			log.Println(err)
		}
		lval.dval = Number(n)
	case VREG, DREG:
		lval.rval = rune(item.val[0])
	case STRING:
		lval.sval = item.val[1:len(item.val)-1]
	}
	return int(item.typ)
}

func lex(input string) *yyLex {
	l := &yyLex{
		input: input,
		items: make(chan item),
	}
	go l.run()
	return l
}

func (y *yyLex) run() {
	defer close(y.items)
	for {
		switch c := y.next(); {
		case unicode.IsDigit(c):
			y.lexNumber()
			y.emit(NUMBER)
		case unicode.IsUpper(c):
			y.emit(VREG)
		case unicode.IsLower(c):
			y.emit(DREG)
		case unicode.IsSpace(c):
			y.ignore()
		case c == '\'':
			if y.lexQuoted() {
				y.emit(STRING)
			} else {
				y.emit(int(c))
			}
		case c == eof:
			return
		default:
			y.emit(int(c))
		}
	}
}

func (y *yyLex) lexNumber() {
	y.acceptDigits()
	if y.acceptRune('.') {
		y.acceptDigits()
	}
	if y.acceptRune('e', 'E') {
		y.acceptRune('-')
		y.acceptDigits()
	}
}

func (y *yyLex) lexQuoted() bool {
	if n := strings.IndexRune(y.input[y.pos:], '\''); n >= 0 {
		y.pos += n
		y.next()
		return true
	}
	return false
}

func (y *yyLex) emit(t int) {
	y.items <- item{
		typ: t,
		val: y.input[y.start:y.pos],
	}
	y.start = y.pos
}

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

func (y *yyLex) ignore() {
	y.start = y.pos
}

func (y *yyLex) backup() {
	y.pos -= y.width
}

func (y *yyLex) peek() rune {
	defer y.backup()
	return y.next()
}

func (y *yyLex) acceptDigits() {
	defer y.backup()
	for unicode.IsDigit(y.next()) {
	}
}

func (y *yyLex) acceptRune(valid ...rune) bool {
	for _, r := range valid {
		if y.next() == r {
			return true
		}
		y.backup()
	}
	return false
}

func (y *yyLex) accept(valid string) bool {
	if strings.IndexRune(valid, y.next()) >= 0 {
		return true
	}
	y.backup()
	return false
}

func (y *yyLex) acceptRun(valid string) {
	for strings.IndexRune(valid, y.next()) >= 0 {
	}
	y.backup()
}