File: lex.go

package info (click to toggle)
golang-sourcehut-sircmpwn-go-bare 0.0~git20210406.ab86bc2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 268 kB
  • sloc: makefile: 5
file content (329 lines) | stat: -rw-r--r-- 5,424 bytes parent folder | download
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package schema

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"io"
	"unicode"
)

// A scanner for reading lexographic tokens from a BARE schema language
// document.
type Scanner struct {
	// TODO: track lineno/colno information and attach it to the tokens
	// returned, for better error reporting
	br       *bufio.Reader
	pushback []Token
}

// Creates a new BARE schema language scanner for the given reader.
func NewScanner(reader io.Reader) *Scanner {
	return &Scanner{bufio.NewReader(reader), nil}
}

// Returns the next token from the reader. If the token has a string associated
// with it (e.g. UserTypeName, Name, and Integer), the second return value is
// set to that string.
func (sc *Scanner) Next() (Token, error) {
	if len(sc.pushback) != 0 {
		tok := sc.pushback[0]
		sc.pushback = sc.pushback[1:]
		return tok, nil
	}

	var (
		err error
		r   rune
	)

	for {
		r, _, err = sc.br.ReadRune()
		if err != nil {
			break
		}

		if unicode.IsSpace(r) {
			continue
		}
		if unicode.IsLetter(r) {
			sc.br.UnreadRune()
			return sc.scanWord()
		}
		if unicode.IsDigit(r) {
			sc.br.UnreadRune()
			return sc.scanInteger()
		}

		switch r {
		case '#':
			sc.br.ReadString('\n')
			continue
		case '<':
			return Token{TLANGLE, ""}, nil
		case '>':
			return Token{TRANGLE, ""}, nil
		case '{':
			return Token{TLBRACE, ""}, nil
		case '}':
			return Token{TRBRACE, ""}, nil
		case '[':
			return Token{TLBRACKET, ""}, nil
		case ']':
			return Token{TRBRACKET, ""}, nil
		case '(':
			return Token{TLPAREN, ""}, nil
		case ')':
			return Token{TRPAREN, ""}, nil
		case '|':
			return Token{TPIPE, ""}, nil
		case '=':
			return Token{TEQUAL, ""}, nil
		case ':':
			return Token{TCOLON, ""}, nil
		}

		return Token{}, &ErrUnknownToken{r}
	}

	return Token{}, err
}

// Pushes a token back to the scanner, causing it to be returned on the next
// call to Next.
func (sc *Scanner) PushBack(tok Token) {
	sc.pushback = append(sc.pushback, tok)
}

// Returned when the lexer encounters an unexpected character
type ErrUnknownToken struct {
	token rune
}

func (e *ErrUnknownToken) Error() string {
	return fmt.Sprintf("Unknown token '%c'", e.token)
}

func (sc *Scanner) scanWord() (Token, error) {
	var buf bytes.Buffer

	for {
		r, _, err := sc.br.ReadRune()
		if err != nil  {
			if err == io.EOF {
				break
			}
			return Token{}, err
		}

		if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
			buf.WriteRune(r)
		} else {
			sc.br.UnreadRune()
			break
		}
	}

	tok := buf.String()
	switch tok {
	case "type":
		return Token{TTYPE, ""}, nil
	case "enum":
		return Token{TENUM, ""}, nil
	case "uint":
		return Token{TUINT, ""}, nil
	case "u8":
		return Token{TU8, ""}, nil
	case "u16":
		return Token{TU16, ""}, nil
	case "u32":
		return Token{TU32, ""}, nil
	case "u64":
		return Token{TU64, ""}, nil
	case "int":
		return Token{TINT, ""}, nil
	case "i8":
		return Token{TI8, ""}, nil
	case "i16":
		return Token{TI16, ""}, nil
	case "i32":
		return Token{TI32, ""}, nil
	case "i64":
		return Token{TI64, ""}, nil
	case "f32":
		return Token{TF32, ""}, nil
	case "f64":
		return Token{TF64, ""}, nil
	case "bool":
		return Token{TBOOL, ""}, nil
	case "string":
		return Token{TSTRING, ""}, nil
	case "data":
		return Token{TDATA, ""}, nil
	case "void":
		return Token{TVOID, ""}, nil
	case "optional":
		return Token{TOPTIONAL, ""}, nil
	case "map":
		return Token{TMAP, ""}, nil
	}

	return Token{TNAME, tok}, nil
}

func (sc *Scanner) scanInteger() (Token, error) {
	var buf bytes.Buffer

	for {
		r, _, err := sc.br.ReadRune()
		if err != nil  {
			if err == io.EOF {
				break
			}
			return Token{}, err
		}

		if unicode.IsDigit(r) {
			buf.WriteRune(r)
		} else {
			sc.br.UnreadRune()
			break
		}
	}

	return Token{TINTEGER, buf.String()}, nil
}

// A single lexographic token from a schema language token stream
type Token struct {
	Token TokenKind
	Value string
}

type TokenKind int

const (
	TTYPE TokenKind = iota
	TENUM

	// NAME is used for name, user-type-name, and enum-value-name.
	// Distinguishing between these requires context.
	TNAME
	TINTEGER

	TUINT
	TU8
	TU16
	TU32
	TU64
	TINT
	TI8
	TI16
	TI32
	TI64
	TF32
	TF64
	TBOOL
	TSTRING
	TDATA
	TVOID
	TMAP
	TOPTIONAL

	// <
	TLANGLE
	// >
	TRANGLE
	// {
	TLBRACE
	// }
	TRBRACE
	// [
	TLBRACKET
	// ]
	TRBRACKET
	// (
	TLPAREN
	// )
	TRPAREN
	// |
	TPIPE
	// =
	TEQUAL
	// :
	TCOLON
)

func (t Token) String() string {
	switch t.Token {
	case TTYPE:
		return "type"
	case TENUM:
		return "enum"
	case TNAME:
		return "name"
	case TINTEGER:
		return "integer"
	case TUINT:
		return "uint"
	case TU8:
		return "u8"
	case TU16:
		return "u16"
	case TU32:
		return "u32"
	case TU64:
		return "u64"
	case TINT:
		return "int"
	case TI8:
		return "i8"
	case TI16:
		return "i16"
	case TI32:
		return "i32"
	case TI64:
		return "i64"
	case TF32:
		return "f32"
	case TF64:
		return "f64"
	case TBOOL:
		return "bool"
	case TSTRING:
		return "string"
	case TDATA:
		return "data"
	case TVOID:
		return "void"
	case TMAP:
		return "map"
	case TOPTIONAL:
		return "optional"
	case TLANGLE:
		return "<"
	case TRANGLE:
		return ">"
	case TLBRACE:
		return "{"
	case TRBRACE:
		return "}"
	case TLBRACKET:
		return "["
	case TRBRACKET:
		return "]"
	case TLPAREN:
		return "("
	case TRPAREN:
		return ")"
	case TPIPE:
		return "|"
	case TEQUAL:
		return "="
	case TCOLON:
		return ":"
	default:
		panic(errors.New("Invalid token value"))
	}
}