forked from qrtz/livedev
/
parser.go
270 lines (220 loc) · 5.2 KB
/
parser.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
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
package gosource
import (
"go/scanner"
"go/token"
"io/ioutil"
"os"
"strings"
)
// Parse parses a single Go source file into a slice of Line
func Parse(filename string) ([]*Line, error) {
r, err := os.Open(filename)
if err != nil {
return nil, err
}
defer r.Close()
src, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
var s scanner.Scanner
fileSet := token.NewFileSet()
s.Init(fileSet.AddFile(filename, fileSet.Base(), len(src)), src, nil, scanner.ScanComments)
var lines []*Line
for {
pos, tok, lit := s.Scan()
if tok == token.EOF {
break
}
position := fileSet.Position(pos)
indent := space
if len(lines) != position.Line {
indent = tab
// Print empty lines
for i := len(lines) + 1; i < position.Line; i++ {
lines = append(lines, &Line{Num: i})
}
lines = append(lines, &Line{Num: position.Line, offset: 1})
}
current := lines[len(lines)-1]
// Print white spaces
if i := position.Column - current.offset; i > 0 {
current.offset = position.Column
current.Add(Token{t: tokenSpace, Line: current.Num, Text: strings.Repeat(indent, i)})
}
text := lit
kind := "text"
name := kind
if n, ok := tokens[tok]; ok {
name = n
}
if _, ok := literals[lit]; ok {
kind = "keyword"
}
switch {
case tok.IsKeyword():
kind = "keyword"
case tok.IsLiteral():
switch tok {
case token.INT, token.FLOAT, token.IMAG:
kind = "number"
}
case tok.IsOperator():
// Use the operator as literal. Excluding auto-inserted semi-colons
kind = "operator"
switch tok {
default:
lit = tok.String()
case token.LPAREN:
lit = tok.String()
if p := len(current.Tokens) - 1; p >= 0 && current.Tokens[p].t == token.IDENT {
current.Tokens[p].Name = "funcall"
}
case token.SEMICOLON:
if lit != tok.String() {
// Remove auto-inserted semi-colons
lit = ""
}
}
text = lit
}
current.offset = position.Column + len(lit)
ln := len(text) - 1
p := 0
for i, ch := range text {
if ch == '\n' {
t := Token{t: tok, Line: current.Num, Text: text[p:i], Kind: kind, Name: name}
current.Add(t)
current = &Line{Num: current.Num + 1, offset: current.offset}
lines = append(lines, current)
p = i + 1 // Skip the newline character
} else if i == ln {
t := Token{t: tok, Line: current.Num, Text: text[p:], Kind: kind, Name: name}
current.Add(t)
}
}
}
return lines, nil
}
const (
space = " "
tab = " " // 4 space tab
tokenSpace = token.Token(token.COMMENT + 1)
)
var literals = map[string]struct{}{
"bool": {},
"close": {},
"error": {},
"iota": {},
"int": {},
"int8": {},
"int16": {},
"int32": {},
"int64": {},
"uint": {},
"uint8": {},
"uint16": {},
"uint32": {},
"uint64": {},
"rune": {},
"string": {},
"true": {},
"flase": {},
}
var tokens = map[token.Token]string{
token.COMMENT: "comment",
token.IDENT: "ident",
token.INT: "int",
token.FLOAT: "float",
token.IMAG: "imag",
token.CHAR: "char",
token.STRING: "string",
token.ADD: "add",
token.SUB: "sub",
token.MUL: "mul",
token.QUO: "quo",
token.REM: "rem",
token.AND: "and",
token.OR: "or",
token.XOR: "xor",
token.SHL: "shl",
token.SHR: "shr",
token.AND_NOT: "and_not",
token.ADD_ASSIGN: "add_assign",
token.SUB_ASSIGN: "sub_assign",
token.MUL_ASSIGN: "mul_assign",
token.QUO_ASSIGN: "quo_assign",
token.REM_ASSIGN: "rem_assign",
token.AND_ASSIGN: "and_assign",
token.OR_ASSIGN: "or_assign",
token.XOR_ASSIGN: "xor_assign",
token.SHL_ASSIGN: "shl_assign",
token.SHR_ASSIGN: "shr_assign",
token.AND_NOT_ASSIGN: "and_not_assign",
token.LAND: "land",
token.LOR: "lor",
token.ARROW: "arrow",
token.INC: "inc",
token.DEC: "dec",
token.EQL: "eql",
token.LSS: "lss",
token.GTR: "gtr",
token.ASSIGN: "assign",
token.NOT: "not",
token.NEQ: "neq",
token.LEQ: "leq",
token.GEQ: "geq",
token.DEFINE: "define",
token.ELLIPSIS: "ellipsis",
token.LPAREN: "paren",
token.LBRACK: "brack",
token.LBRACE: "brace",
token.COMMA: "comma",
token.PERIOD: "period",
token.RPAREN: "paren",
token.RBRACK: "brack",
token.RBRACE: "brace",
token.SEMICOLON: "semicolon",
token.COLON: "colon",
token.BREAK: "break",
token.CASE: "case",
token.CHAN: "chan",
token.CONST: "const",
token.CONTINUE: "continue",
token.DEFAULT: "default",
token.DEFER: "defer",
token.ELSE: "else",
token.FALLTHROUGH: "fallthrough",
token.FOR: "for",
token.FUNC: "func",
token.GO: "go",
token.GOTO: "goto",
token.IF: "if",
token.IMPORT: "import",
token.INTERFACE: "interface",
token.MAP: "map",
token.PACKAGE: "package",
token.RANGE: "range",
token.RETURN: "return",
token.SELECT: "select",
token.STRUCT: "struct",
token.SWITCH: "switch",
token.TYPE: "type",
token.VAR: "var",
}
// Line represents a line
type Line struct {
Num int
Tokens []Token
offset int
}
// Add appends a token to the line.
func (l *Line) Add(t Token) {
l.Tokens = append(l.Tokens, t)
}
// Token represents a token in a line
type Token struct {
Name, Text, Kind string
Line int
t token.Token
}