1 | import {from, trim, charat, strlen, substr, append, assign} from './Utility.js'
|
---|
2 |
|
---|
3 | export var line = 1
|
---|
4 | export var column = 1
|
---|
5 | export var length = 0
|
---|
6 | export var position = 0
|
---|
7 | export var character = 0
|
---|
8 | export var characters = ''
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * @param {string} value
|
---|
12 | * @param {object | null} root
|
---|
13 | * @param {object | null} parent
|
---|
14 | * @param {string} type
|
---|
15 | * @param {string[] | string} props
|
---|
16 | * @param {object[] | string} children
|
---|
17 | * @param {object[]} siblings
|
---|
18 | * @param {number} length
|
---|
19 | */
|
---|
20 | export function node (value, root, parent, type, props, children, length, siblings) {
|
---|
21 | return {value: value, root: root, parent: parent, type: type, props: props, children: children, line: line, column: column, length: length, return: '', siblings: siblings}
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @param {object} root
|
---|
26 | * @param {object} props
|
---|
27 | * @return {object}
|
---|
28 | */
|
---|
29 | export function copy (root, props) {
|
---|
30 | return assign(node('', null, null, '', null, null, 0, root.siblings), root, {length: -root.length}, props)
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @param {object} root
|
---|
35 | */
|
---|
36 | export function lift (root) {
|
---|
37 | while (root.root)
|
---|
38 | root = copy(root.root, {children: [root]})
|
---|
39 |
|
---|
40 | append(root, root.siblings)
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * @return {number}
|
---|
45 | */
|
---|
46 | export function char () {
|
---|
47 | return character
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @return {number}
|
---|
52 | */
|
---|
53 | export function prev () {
|
---|
54 | character = position > 0 ? charat(characters, --position) : 0
|
---|
55 |
|
---|
56 | if (column--, character === 10)
|
---|
57 | column = 1, line--
|
---|
58 |
|
---|
59 | return character
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * @return {number}
|
---|
64 | */
|
---|
65 | export function next () {
|
---|
66 | character = position < length ? charat(characters, position++) : 0
|
---|
67 |
|
---|
68 | if (column++, character === 10)
|
---|
69 | column = 1, line++
|
---|
70 |
|
---|
71 | return character
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * @return {number}
|
---|
76 | */
|
---|
77 | export function peek () {
|
---|
78 | return charat(characters, position)
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * @return {number}
|
---|
83 | */
|
---|
84 | export function caret () {
|
---|
85 | return position
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * @param {number} begin
|
---|
90 | * @param {number} end
|
---|
91 | * @return {string}
|
---|
92 | */
|
---|
93 | export function slice (begin, end) {
|
---|
94 | return substr(characters, begin, end)
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * @param {number} type
|
---|
99 | * @return {number}
|
---|
100 | */
|
---|
101 | export function token (type) {
|
---|
102 | switch (type) {
|
---|
103 | // \0 \t \n \r \s whitespace token
|
---|
104 | case 0: case 9: case 10: case 13: case 32:
|
---|
105 | return 5
|
---|
106 | // ! + , / > @ ~ isolate token
|
---|
107 | case 33: case 43: case 44: case 47: case 62: case 64: case 126:
|
---|
108 | // ; { } breakpoint token
|
---|
109 | case 59: case 123: case 125:
|
---|
110 | return 4
|
---|
111 | // : accompanied token
|
---|
112 | case 58:
|
---|
113 | return 3
|
---|
114 | // " ' ( [ opening delimit token
|
---|
115 | case 34: case 39: case 40: case 91:
|
---|
116 | return 2
|
---|
117 | // ) ] closing delimit token
|
---|
118 | case 41: case 93:
|
---|
119 | return 1
|
---|
120 | }
|
---|
121 |
|
---|
122 | return 0
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * @param {string} value
|
---|
127 | * @return {any[]}
|
---|
128 | */
|
---|
129 | export function alloc (value) {
|
---|
130 | return line = column = 1, length = strlen(characters = value), position = 0, []
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * @param {any} value
|
---|
135 | * @return {any}
|
---|
136 | */
|
---|
137 | export function dealloc (value) {
|
---|
138 | return characters = '', value
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * @param {number} type
|
---|
143 | * @return {string}
|
---|
144 | */
|
---|
145 | export function delimit (type) {
|
---|
146 | return trim(slice(position - 1, delimiter(type === 91 ? type + 2 : type === 40 ? type + 1 : type)))
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * @param {string} value
|
---|
151 | * @return {string[]}
|
---|
152 | */
|
---|
153 | export function tokenize (value) {
|
---|
154 | return dealloc(tokenizer(alloc(value)))
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * @param {number} type
|
---|
159 | * @return {string}
|
---|
160 | */
|
---|
161 | export function whitespace (type) {
|
---|
162 | while (character = peek())
|
---|
163 | if (character < 33)
|
---|
164 | next()
|
---|
165 | else
|
---|
166 | break
|
---|
167 |
|
---|
168 | return token(type) > 2 || token(character) > 3 ? '' : ' '
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * @param {string[]} children
|
---|
173 | * @return {string[]}
|
---|
174 | */
|
---|
175 | export function tokenizer (children) {
|
---|
176 | while (next())
|
---|
177 | switch (token(character)) {
|
---|
178 | case 0: append(identifier(position - 1), children)
|
---|
179 | break
|
---|
180 | case 2: append(delimit(character), children)
|
---|
181 | break
|
---|
182 | default: append(from(character), children)
|
---|
183 | }
|
---|
184 |
|
---|
185 | return children
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * @param {number} index
|
---|
190 | * @param {number} count
|
---|
191 | * @return {string}
|
---|
192 | */
|
---|
193 | export function escaping (index, count) {
|
---|
194 | while (--count && next())
|
---|
195 | // not 0-9 A-F a-f
|
---|
196 | if (character < 48 || character > 102 || (character > 57 && character < 65) || (character > 70 && character < 97))
|
---|
197 | break
|
---|
198 |
|
---|
199 | return slice(index, caret() + (count < 6 && peek() == 32 && next() == 32))
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * @param {number} type
|
---|
204 | * @return {number}
|
---|
205 | */
|
---|
206 | export function delimiter (type) {
|
---|
207 | while (next())
|
---|
208 | switch (character) {
|
---|
209 | // ] ) " '
|
---|
210 | case type:
|
---|
211 | return position
|
---|
212 | // " '
|
---|
213 | case 34: case 39:
|
---|
214 | if (type !== 34 && type !== 39)
|
---|
215 | delimiter(character)
|
---|
216 | break
|
---|
217 | // (
|
---|
218 | case 40:
|
---|
219 | if (type === 41)
|
---|
220 | delimiter(type)
|
---|
221 | break
|
---|
222 | // \
|
---|
223 | case 92:
|
---|
224 | next()
|
---|
225 | break
|
---|
226 | }
|
---|
227 |
|
---|
228 | return position
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * @param {number} type
|
---|
233 | * @param {number} index
|
---|
234 | * @return {number}
|
---|
235 | */
|
---|
236 | export function commenter (type, index) {
|
---|
237 | while (next())
|
---|
238 | // //
|
---|
239 | if (type + character === 47 + 10)
|
---|
240 | break
|
---|
241 | // /*
|
---|
242 | else if (type + character === 42 + 42 && peek() === 47)
|
---|
243 | break
|
---|
244 |
|
---|
245 | return '/*' + slice(index, position - 1) + '*' + from(type === 47 ? type : next())
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * @param {number} index
|
---|
250 | * @return {string}
|
---|
251 | */
|
---|
252 | export function identifier (index) {
|
---|
253 | while (!token(peek()))
|
---|
254 | next()
|
---|
255 |
|
---|
256 | return slice(index, position)
|
---|
257 | }
|
---|