-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
75 lines (57 loc) · 1.87 KB
/
token.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
package sausage
import (
"strings"
)
//Tokenize - Reads source code and tokenizes it, return can be slice with tokens or an error
func Tokenize(source string) interface{} {
var tokens []*Token
sourceOriginal := removeImediateWhitespace(source)
if len(source) == 0 {
return tokens
}
for len(source) > 0 {
var currentToken string
if currentToken == "" {
source = sourceOriginal
for tokenOrder := 0; tokenOrder < len(tokenTypes); tokenOrder++ {
tokenIndex := tokenPatterns[tokenTypes[tokenOrder]].FindStringIndex(source)
if len(tokenIndex) > 0 {
if tokenIndex[0] == 0 {
currentToken = getCurrentToken(source, tokenIndex, tokenTypes[tokenOrder])
tokens = append(tokens, &Token{
Type: tokenTypes[tokenOrder],
Value: currentToken,
Range: getAbsoluteRange(sourceOriginal, source, tokenIndex),
})
source = removeCurrentTokenFromSource(source, tokenIndex)
tokenOrder = 0
currentToken = ""
}
}
}
}
}
return tokens
}
func removeImediateWhitespace(source string) string {
return strings.Trim(source, " \n\r")
}
func getAbsoluteRange(originalString string, sourceString string, tokenIndex []int) []int {
rangeDiff := len(originalString) - len(sourceString)
var absoluteRange []int
absoluteRange = append(absoluteRange, tokenIndex[0]+rangeDiff)
absoluteRange = append(absoluteRange, tokenIndex[1]+rangeDiff)
return absoluteRange
}
func getCurrentToken(source string, tokenIndex []int, tokenType string) string {
currentToken := source[tokenIndex[0]:tokenIndex[1]]
if tokenType == "BlockComment" {
currentToken = strings.Trim(currentToken, "/*")
} else if tokenType == "LineComment" {
currentToken = strings.Trim(currentToken, "//")
}
return currentToken
}
func removeCurrentTokenFromSource(source string, tokenIndex []int) string {
return strings.TrimSpace(source[tokenIndex[1]:len(source)])
}