-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
55 lines (43 loc) · 887 Bytes
/
scanner.go
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
package main
import "slices"
var (
TerminalChars = []rune{'(', ' ', ')'}
)
type Scanner struct {
symbols_steam []rune
head_index int
}
func NewScanner(stream []rune, offset int) *Scanner {
return &Scanner{
symbols_steam: stream,
head_index: offset,
}
}
func (s *Scanner) HasNext() bool {
return s.head_index < len(s.symbols_steam)-1
}
func (s *Scanner) Peek() tokenType {
n := s.symbols_steam[s.head_index+1]
if (n < 'a' || n > 'z') && !slices.Contains(TerminalChars, n) {
return UNKNOWN_TOKEN
}
switch n {
case '(':
return LPAREN_TOKEN
case ' ':
return SPACE_TOKEN
case ')':
return RPAREN_TOKEN
default:
return CHAR_TOKEN
}
}
func (s *Scanner) Next() Token {
tt := s.Peek()
s.head_index++
lexeme := s.symbols_steam[s.head_index]
return Token{
kind: tt,
lexeme: lexeme,
}
}