* Scan next token. Context-sensitive: `cmd` mode treats [ as operator (test * command start), `arg` mode treats [ as word char (glob/subscript).
(L: Lexer, ctx: 'cmd' | 'arg' = 'arg')
| 302 | * command start), `arg` mode treats [ as word char (glob/subscript). |
| 303 | */ |
| 304 | function nextToken(L: Lexer, ctx: 'cmd' | 'arg' = 'arg'): Token { |
| 305 | skipBlanks(L) |
| 306 | const start = L.b |
| 307 | if (L.i >= L.len) return { type: 'EOF', value: '', start, end: start } |
| 308 | |
| 309 | const c = L.src[L.i]! |
| 310 | const c1 = peek(L, 1) |
| 311 | const c2 = peek(L, 2) |
| 312 | |
| 313 | if (c === '\n') { |
| 314 | advance(L) |
| 315 | return { type: 'NEWLINE', value: '\n', start, end: L.b } |
| 316 | } |
| 317 | |
| 318 | if (c === '#') { |
| 319 | const si = L.i |
| 320 | while (L.i < L.len && L.src[L.i] !== '\n') advance(L) |
| 321 | return { |
| 322 | type: 'COMMENT', |
| 323 | value: L.src.slice(si, L.i), |
| 324 | start, |
| 325 | end: L.b, |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Multi-char operators (longest match first) |
| 330 | if (c === '&' && c1 === '&') { |
| 331 | advance(L) |
| 332 | advance(L) |
| 333 | return { type: 'OP', value: '&&', start, end: L.b } |
| 334 | } |
| 335 | if (c === '|' && c1 === '|') { |
| 336 | advance(L) |
| 337 | advance(L) |
| 338 | return { type: 'OP', value: '||', start, end: L.b } |
| 339 | } |
| 340 | if (c === '|' && c1 === '&') { |
| 341 | advance(L) |
| 342 | advance(L) |
| 343 | return { type: 'OP', value: '|&', start, end: L.b } |
| 344 | } |
| 345 | if (c === ';' && c1 === ';' && c2 === '&') { |
| 346 | advance(L) |
| 347 | advance(L) |
| 348 | advance(L) |
| 349 | return { type: 'OP', value: ';;&', start, end: L.b } |
| 350 | } |
| 351 | if (c === ';' && c1 === ';') { |
| 352 | advance(L) |
| 353 | advance(L) |
| 354 | return { type: 'OP', value: ';;', start, end: L.b } |
| 355 | } |
| 356 | if (c === ';' && c1 === '&') { |
| 357 | advance(L) |
| 358 | advance(L) |
| 359 | return { type: 'OP', value: ';&', start, end: L.b } |
| 360 | } |
| 361 | if (c === '>' && c1 === '>') { |
no test coverage detected