|
| 1 | +import { |
| 2 | + CompilerErrorCodes, |
| 3 | + CompilerError, |
| 4 | + createCompilerError |
| 5 | +} from './errors' |
1 | 6 | import { SourceLocation, Position } from './location'
|
2 | 7 | import { createTokenizer, Tokenizer, TokenTypes } from './tokenizer'
|
| 8 | +import { isUnDef } from '../utils' |
3 | 9 |
|
4 | 10 | export const enum NodeTypes {
|
5 | 11 | Resource, // 0
|
@@ -72,11 +78,40 @@ export interface LinkedModitierNode extends Node {
|
72 | 78 | value: Identifier
|
73 | 79 | }
|
74 | 80 |
|
| 81 | +export type ParserOptions = { |
| 82 | + onError?: (error: CompilerError) => void |
| 83 | +} |
| 84 | + |
75 | 85 | export type Parser = Readonly<{
|
76 | 86 | parse: (source: string) => ResourceNode
|
77 | 87 | }>
|
78 | 88 |
|
79 |
| -export function createParser(): Parser { |
| 89 | +export function createParser(/* options: ParserOptions = {} */): Parser { |
| 90 | + // TODO: |
| 91 | + /* |
| 92 | + const { onError } = options |
| 93 | +
|
| 94 | + const emitError = ( |
| 95 | + code: CompilerErrorCodes, |
| 96 | + loc: Position, |
| 97 | + offset?: number |
| 98 | + ): void => { |
| 99 | + if (offset) { |
| 100 | + loc.offset += offset |
| 101 | + loc.column += offset |
| 102 | + } |
| 103 | + if (onError) { |
| 104 | + onError( |
| 105 | + createCompilerError(code, { |
| 106 | + start: loc, |
| 107 | + end: loc, |
| 108 | + source: '' |
| 109 | + }) |
| 110 | + ) |
| 111 | + } |
| 112 | + } |
| 113 | + */ |
| 114 | + |
80 | 115 | const startNode = (type: NodeTypes, offset: number, loc: Position): Node => {
|
81 | 116 | return {
|
82 | 117 | type,
|
@@ -113,11 +148,11 @@ export function createParser(): Parser {
|
113 | 148 | return node
|
114 | 149 | }
|
115 | 150 |
|
116 |
| - const parseList = (tokenizer: Tokenizer, index: number): ListNode => { |
| 151 | + const parseList = (tokenizer: Tokenizer, index: string): ListNode => { |
117 | 152 | const context = tokenizer.context()
|
118 | 153 | const { lastOffset: offset, lastStartLoc: loc } = context // get brace left loc
|
119 | 154 | const node = startNode(NodeTypes.List, offset, loc) as ListNode
|
120 |
| - node.index = index |
| 155 | + node.index = parseInt(index, 10) |
121 | 156 | tokenizer.nextToken() // skip brach right
|
122 | 157 | endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition())
|
123 | 158 | return node
|
@@ -202,21 +237,21 @@ export function createParser(): Parser {
|
202 | 237 |
|
203 | 238 | switch (token.type) {
|
204 | 239 | case TokenTypes.LinkedKey:
|
205 |
| - if (!token.value || typeof token.value !== 'string') { |
| 240 | + if (isUnDef(token.value)) { |
206 | 241 | // TODO: should be thrown syntax error
|
207 | 242 | throw new Error()
|
208 | 243 | }
|
209 | 244 | linkedNode.key = parseLinkedKey(tokenizer, token.value)
|
210 | 245 | break
|
211 | 246 | case TokenTypes.Named:
|
212 |
| - if (!token.value || typeof token.value === 'number') { |
| 247 | + if (isUnDef(token.value)) { |
213 | 248 | // TODO: should be thrown syntax error
|
214 | 249 | throw new Error()
|
215 | 250 | }
|
216 | 251 | linkedNode.key = parseNamed(tokenizer, token.value)
|
217 | 252 | break
|
218 | 253 | case TokenTypes.List:
|
219 |
| - if (token.value === undefined || typeof token.value === 'string') { |
| 254 | + if (isUnDef(token.value)) { |
220 | 255 | // TODO: should be thrown syntax error
|
221 | 256 | throw new Error()
|
222 | 257 | }
|
@@ -257,21 +292,21 @@ export function createParser(): Parser {
|
257 | 292 | const token = tokenizer.nextToken()
|
258 | 293 | switch (token.type) {
|
259 | 294 | case TokenTypes.Text:
|
260 |
| - if (!token.value || typeof token.value === 'number') { |
| 295 | + if (isUnDef(token.value)) { |
261 | 296 | // TODO: should be thrown syntax error
|
262 | 297 | throw new Error()
|
263 | 298 | }
|
264 | 299 | node.items.push(parseText(tokenizer, token.value))
|
265 | 300 | break
|
266 | 301 | case TokenTypes.List:
|
267 |
| - if (token.value === undefined || typeof token.value === 'string') { |
| 302 | + if (isUnDef(token.value)) { |
268 | 303 | // TODO: should be thrown syntax error
|
269 | 304 | throw new Error()
|
270 | 305 | }
|
271 | 306 | node.items.push(parseList(tokenizer, token.value))
|
272 | 307 | break
|
273 | 308 | case TokenTypes.Named:
|
274 |
| - if (!token.value || typeof token.value === 'number') { |
| 309 | + if (isUnDef(token.value)) { |
275 | 310 | // TODO: should be thrown syntax error
|
276 | 311 | throw new Error()
|
277 | 312 | }
|
|
0 commit comments