aboutsummaryrefslogtreecommitdiffstats
path: root/exprparser.go
blob: 0d62dbcc3f18614620b53d59775e4e554a114303 (plain) (blame)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main

import (
	"bufio"
	"bytes"
	"io"
)

type Token int

const eof = rune(0)

const (
	ILLEGAL Token = iota
	EOF
	WS

	IDENT

	ASTERISK
	COMMA
	SEMICOLON
	GT
	LT
	GET
	LET
	EQ
	PLUS
	MINUS
	DIVISION
	R_PAREN
	L_PAREN
	PERIOD
)

type Scanner struct {
	r *bufio.Reader
}

func newScanner(r io.Reader) *Scanner {
	return &Scanner{r: bufio.NewReader(r)}
}

func (s *Scanner) read() rune {
	ch, _, err := s.r.ReadRune()
	if err != nil {
		return eof
	}

	return ch
}

func isWhiteSpace(ch rune) bool {
	if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
		return true
	}
	return false
}

func isLetter(ch rune) bool {
	if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
		return true
	}
	return false
}

func isDigit(ch rune) bool {
	if ch >= '0' && ch <= '9' {
		return true
	}
	return false
}

func isNumber(chs []rune) bool {
	var dotCounter int
	for i := range chs {
		if chs[i] == '.' {
			dotCounter++
			if dotCounter > 1 {
				return false
			}
			continue
		}

		if !isDigit(chs[i]) {
			return false
		}
	}

	return true
}

func (s *Scanner) unread() { _ = s.r.UnreadRune() }

func (s *Scanner) scanWhitespace() (tok Token, lit string) {
	var buf bytes.Buffer
	buf.WriteRune(s.read())

	for {
		if ch := s.read(); ch == eof {
			break
		} else if !isWhiteSpace(ch) {
			s.unread()
			break
		} else {
			buf.WriteRune(ch)
		}
	}

	return WS, buf.String()
}

func (s *Scanner) scanIdent() (tok Token, lit string) {
	var buf bytes.Buffer
	buf.WriteRune(s.read())

	for {
		if ch := s.read(); ch == eof {
			break
		} else if !isLetter(ch) && !isDigit(ch) && ch != '_' {
			s.unread()
			break
		} else {
			_, _ = buf.WriteRune(ch)
		}
	}

	return IDENT, buf.String()
}

func (s *Scanner) scan() (tok Token, lit string) {
	ch := s.read()

	if isWhiteSpace(ch) {
		s.unread()
		return s.scanWhitespace()
	} else if isLetter(ch) {
		s.unread()
		return s.scanIdent()
	}

	switch ch {
	case eof:
		return EOF, string(ch)
	case '*':
		return ASTERISK, string(ch)
	case '+':
		return PLUS, string(ch)
	case '-':
		return MINUS, string(ch)
	case '/':
		return ASTERISK, string(ch)
	case '(':
		return R_PAREN, string(ch)
	case ')':
		return L_PAREN, string(ch)
	case ',':
		return ASTERISK, string(ch)
	case ';':
		return ASTERISK, string(ch)
	case '<':
		return LT, string(ch)
	case '>':
		return GT, string(ch)
	case '.':
		return PERIOD, string(ch)
	}

	return ILLEGAL, string(ch)
}

type Parser struct {
	s   *Scanner
	buf struct {
		tok Token
		lit string
		n   int
	}
}

func newParser(r io.Reader) *Parser {
	return &Parser{s: newScanner(r)}
}

func (p *Parser) scan() (tok Token, lit string) {
	if p.buf.n != 0 {
		p.buf.n = 0
		return p.buf.tok, p.buf.lit
	}

	tok, lit = p.s.scan()
	p.buf.tok, p.buf.lit = tok, lit

	return
}

func (p *Parser) unscan() { p.buf.n = 1 }

type ASTNode struct {
	isIdentifier bool
	isLiteral    bool
	isIllegal    bool
	isEOF        bool
	isWS         bool
	children     []*ASTNode
	value        interface{}
}