forked from laytan/odin-ini-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.odin
161 lines (138 loc) · 3.75 KB
/
lexer.odin
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
package ini
import "core:bytes"
import "core:unicode"
import "core:unicode/utf8"
TokenType :: enum {
Illegal,
EOF, // The last token parsed, caller should not call again.
Comment,
Section,
Assign,
Key,
Value,
}
Pos :: struct {
line: int,
col: int,
offset: int,
}
Token :: struct {
type: TokenType,
value: []byte,
pos: Pos,
}
// Lexer tokenizes the given data into .ini semantic tokens.
// The given data is not writen to or modified.
Lexer :: struct {
data: []byte,
ch: rune, // The current character being checked.
cursor: int, // The current offset/index (rune based) in data.
bytes_cursor: int, // The current offset/index (byte based) in data.
line: int, // The current line number.
bol: int, // The offset/index that is the beginning of the current line.
}
lexer_init :: proc(using l: ^Lexer, d: []byte) {
l.data = d
lexer_read(l)
l.cursor = 0
}
make_lexer :: proc(data: []byte) -> Lexer {
l: Lexer
lexer_init(&l, data)
return l
}
lexer_next :: proc(using l: ^Lexer) -> Token {
lexer_skip_whitespace(l)
t: Token
t.pos = lexer_pos(l)
switch {
case ch == 0:
t.type = .EOF
case (ch == ';' || ch == '#') && bol == cursor:
t.type = .Comment
s, _ := lexer_read_until(l, '\n')
t.value = data[s:bytes_cursor - 1]
// [section], if a matching ] is not found, this sets the whole line to illegal.
case ch == '[':
s, ok := lexer_read_until(l, ']')
if ok {
t.type = .Section
lexer_read(l)
} else {
t.type = .Illegal
}
t.value = data[s:bytes_cursor - 1]
case ch == ':' || ch == '=':
t.type = .Assign
t.value = data[cursor:cursor + 1]
lexer_read(l)
case ch == '"' || ch == '\'':
t.type = .Value
if s, is_terminated := lexer_read_until(l, ch); is_terminated {
t.value = data[s:bytes_cursor]
} else {
t.value = data[s:bytes_cursor - 1]
}
lexer_read(l)
case:
if s, is_key := lexer_read_until(l, ':', '='); is_key {
t.type = .Key
// // Trim the whitespace between the key and the '='.
t.value = bytes.trim_right_space(data[s:bytes_cursor - 1])
} else {
t.type = .Value
t.value = data[s:bytes_cursor - 1]
}
}
return t
}
lexer_read :: proc(using l: ^Lexer) {
read_ch, size := utf8.decode_rune(l.data[bytes_cursor:])
// EOF.
if size == 0 {
ch = 0
return
}
bytes_cursor += size
cursor += 1
ch = read_ch
lexer_check_newline(l)
}
lexer_check_newline :: proc(using l: ^Lexer) {
if ch != '\n' {
return
}
line += 1
bol = cursor + 1
}
lexer_skip_whitespace :: proc(using l: ^Lexer) {
for unicode.is_space(ch) {
lexer_read(l)
}
}
lexer_pos :: proc(using l: ^Lexer) -> Pos {
return Pos{line = line, col = cursor - bol, offset = cursor}
}
// Reads until a non-escaped rune in check, a newline or the eof.
// This returns the cursor where it started reading and the type of match.
// any match in check returns true.
// newline and eof return false.
lexer_read_until :: proc(using l: ^Lexer, check: ..rune) -> (int, bool) {
start_byte := bytes_cursor - 1
escaped := ch == '\\'
for ch != 0 {
lexer_read(l)
if !escaped {
if ch == '\r' || ch == '\n' {
return start_byte, false
}
for c in check {
if c == ch {
return start_byte, true
}
}
}
escaped = escaped ? false : ch == '\\'
}
return start_byte, false
}