forked from lmorg/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenise.go
168 lines (139 loc) · 2.92 KB
/
tokenise.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
package readline
import "strings"
func tokeniseLine(line []rune, linePos int) ([]string, int, int) {
if len(line) == 0 {
return nil, 0, 0
}
var index, pos int
var punc bool
split := make([]string, 1)
for i, r := range line {
switch {
case (r >= 33 && 47 >= r) ||
(r >= 58 && 64 >= r) ||
(r >= 91 && 94 >= r) ||
r == 96 ||
(r >= 123 && 126 >= r):
if i > 0 && line[i-1] != r {
split = append(split, "")
}
split[len(split)-1] += string(r)
punc = true
case r == ' ' || r == '\t':
split[len(split)-1] += string(r)
punc = true
default:
if punc {
split = append(split, "")
}
split[len(split)-1] += string(r)
punc = false
}
if i == linePos {
index = len(split) - 1
pos = len(split[index]) - 1
}
}
return split, index, pos
}
func tokeniseSplitSpaces(line []rune, linePos int) ([]string, int, int) {
if len(line) == 0 {
return nil, 0, 0
}
var index, pos int
split := make([]string, 1)
for i, r := range line {
switch {
case r == ' ' || r == '\t':
split[len(split)-1] += string(r)
default:
if i > 0 && (line[i-1] == ' ' || line[i-1] == '\t') {
split = append(split, "")
}
split[len(split)-1] += string(r)
}
if i == linePos {
index = len(split) - 1
pos = len(split[index]) - 1
}
}
return split, index, pos
}
func tokeniseBrackets(line []rune, linePos int) ([]string, int, int) {
var (
open, close rune
split []string
count int
pos = make(map[int]int)
match int
single, double bool
)
switch line[linePos] {
case '(', ')':
open = '('
close = ')'
case '{', '[':
open = line[linePos]
close = line[linePos] + 2
case '}', ']':
open = line[linePos] - 2
close = line[linePos]
default:
return nil, 0, 0
}
for i := range line {
switch line[i] {
case '\'':
if !single {
double = !double
}
case '"':
if !double {
single = !single
}
case open:
if !single && !double {
count++
pos[count] = i
if i == linePos {
match = count
split = []string{string(line[:i-1])}
}
} else if i == linePos {
return nil, 0, 0
}
case close:
if !single && !double {
if match == count {
split = append(split, string(line[pos[count]:i]))
return split, 1, 0
}
if i == linePos {
split = []string{
string(line[:pos[count]-1]),
string(line[pos[count]:i]),
}
return split, 1, len(split[1])
}
count--
} else if i == linePos {
return nil, 0, 0
}
}
}
return nil, 0, 0
}
func rTrimWhiteSpace(oldString string) (newString string) {
return strings.TrimRight(oldString, " ")
// TODO: support tab chars
/*defer fmt.Println(">" + oldString + "<" + newString + ">")
newString = oldString
for len(oldString) > 0 {
if newString[len(newString)-1] == ' ' || newString[len(newString)-1] == '\t' {
newString = newString[:len(newString)-1]
} else {
break
}
}
return*/
}