| 13 | import { OPEN, CLOSE } from '../../utils'; |
| 14 | |
| 15 | function createToken( |
| 16 | state: StateBlock | StateInline, |
| 17 | content: string, |
| 18 | contentStart?: number |
| 19 | ): Token { |
| 20 | try { |
| 21 | const { type, meta, nesting = 0 } = parse(content, { Variable, Function }); |
| 22 | const token = state.push(type, '', nesting); |
| 23 | token.info = content; |
| 24 | token.meta = meta; |
| 25 | |
| 26 | if (!state.delimiters) { |
| 27 | state.delimiters = []; |
| 28 | } |
| 29 | |
| 30 | return token; |
| 31 | } catch (error) { |
| 32 | if (!(error instanceof SyntaxError)) throw error; |
| 33 | |
| 34 | const { |
| 35 | message, |
| 36 | location: { start, end }, |
| 37 | } = error as SyntaxError; |
| 38 | const location = contentStart |
| 39 | ? { |
| 40 | start: { offset: start.offset + contentStart }, |
| 41 | end: { offset: end.offset + contentStart }, |
| 42 | } |
| 43 | : null; |
| 44 | |
| 45 | const token = state.push('error', '', 0); |
| 46 | token.meta = { error: { message, location } }; |
| 47 | return token; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | function block( |
| 52 | state: StateBlock, |