-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
175 lines (158 loc) · 3.42 KB
/
lexer.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
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
/*
This file contains simple lexical extractors for tfparser
*/
package tfparser
import (
"fmt"
"strings"
)
func (p *parser) skipComment() error {
if p.i >= len(p.data) {
return nil
}
if p.data[p.i] == '#' {
p.skipTillEOL()
return nil
}
if p.data[p.i] == '/' {
if p.i+1 > len(p.data) {
p.err = fmt.Errorf("Unexpected end of file after '/'")
return p.err
}
// Single line comment starting with // ?
if p.data[p.i+1] == '/' {
p.skipTillEOL()
return nil
}
return p.skipMulitlineComment()
}
return nil
}
func (p *parser) skipTillEOL() {
for ; p.i < len(p.data) && p.data[p.i] != '\n'; p.i++ {
}
p.i++
p.skipNewLine()
}
func (p *parser) skipMulitlineComment() error {
if p.i > len(p.data) {
return nil
}
if p.i+1 >= len(p.data) || (p.data[p.i] != '/' && p.data[p.i+1] != '*') {
return nil
}
nestedCount := 0
for ; p.i+1 < len(p.data); p.i++ {
if p.data[p.i] == '/' && p.data[p.i+1] == '*' {
nestedCount++
}
if p.data[p.i] == '*' && p.data[p.i+1] == '/' {
p.i += 2
nestedCount--
if nestedCount == 0 {
p.skipNewLine()
return nil
}
}
}
p.err = fmt.Errorf("Unable to find closing multiline comment")
return p.err
}
var newlines = "\n\r"
func (p *parser) skipNewLine() {
if p.i >= len(p.data) {
return
}
for ; p.i < len(p.data) && strings.Contains(newlines, string(p.data[p.i])); p.i++ {
}
}
// Func skips block till the corresponding closing bracket
func (p *parser) skipBlock() error {
if p.i >= len(p.data) || p.data[p.i] != '{' {
return nil
}
bracesBalance := 0
for ; p.i < len(p.data); p.i++ {
if p.data[p.i] == '{' {
bracesBalance++
}
if p.data[p.i] == '}' {
bracesBalance--
}
if bracesBalance == 0 {
p.i++
p.skipNewLine()
return nil
}
p.skipComment()
}
if bracesBalance != 0 {
p.err = fmt.Errorf("Unable to find closing brace for block")
return p.err
}
return nil
}
func (p *parser) peek() string {
p.popWhitespaces()
peeked, _ := p.peekWithLength()
return peeked
}
func (p *parser) pop() string {
p.popWhitespaces()
peeked, len := p.peekWithLength()
p.i += len
p.popWhitespaces()
return peeked
}
func (p *parser) popToken(tok string) error {
p.popWhitespaces()
t := p.pop()
if t != tok {
p.err = fmt.Errorf("Unexpected token %#v, expected %#q", t, tok)
return p.err
}
return nil
}
// gets next 'word' from data and returns it along with its length
func (p *parser) peekWithLength() (string, int) {
if p.i >= len(p.data) {
return "", 0
}
if p.data[p.i] == '"' {
return p.peekQuotedStringWithLength()
}
if strings.Contains(symbols, string(p.data[p.i])) {
return string(p.data[p.i]), 1
}
return p.peekIdentifierWithLength()
}
func (p *parser) peekQuotedStringWithLength() (string, int) {
if p.i >= len(p.data) || p.data[p.i] != '"' {
return "", 0
}
for i := p.i + 1; i < len(p.data); i++ {
if p.data[i] == '"' && p.data[i-1] != '\\' {
return p.data[p.i+1 : i], i - p.i + 1 //len(p.data[p.i+1:i]) + 2
}
}
return "", 0
}
var symbols = "{}=\""
func (p *parser) peekIdentifierWithLength() (string, int) {
for i := p.i; i <= len(p.data); i++ {
if strings.Contains(symbols+whitespaces, string(p.data[i])) {
return p.data[p.i:i], i - p.i //len(p.data[p.i:i])
}
}
return "", 0
}
var whitespaces = "\t\r\n "
func (p *parser) popWhitespaces() {
i := p.i
p.skipComment()
for ; p.i < len(p.data) && strings.Contains(whitespaces, string(p.data[p.i])); p.i++ {
}
if i != p.i {
p.popWhitespaces()
}
}