|
| 1 | +import { TagParserError } from "./errors"; |
| 2 | + |
| 3 | +import { |
| 4 | + isAt, |
| 5 | + isClosingParanthesis, |
| 6 | + isComma, |
| 7 | + isDigit, |
| 8 | + isEqual, |
| 9 | + isOpeningParanthesis, |
| 10 | + isQuote, |
| 11 | + isWordChar, |
| 12 | + Tokenizer, |
| 13 | +} from "./tokenizer"; |
| 14 | + |
| 15 | +function createUnexpectedEndOfString() { |
| 16 | + return new TagParserError("Unexpected end-of-string"); |
| 17 | +} |
| 18 | + |
| 19 | +function createUnexpectedToken( |
| 20 | + token: TYield<TokenGenerator>, |
| 21 | + expectation: string |
| 22 | +) { |
| 23 | + return new Error( |
| 24 | + `Unexpected token at ${token.position}: ${token.value} (${expectation})` |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function expectToken(token: Token) { |
| 29 | + if (token.done) { |
| 30 | + throw createUnexpectedEndOfString(); |
| 31 | + } |
| 32 | + |
| 33 | + return token; |
| 34 | +} |
| 35 | + |
| 36 | +function parsePrimitiveToken(token: Token) { |
| 37 | + if (token.done) { |
| 38 | + throw createUnexpectedEndOfString(); |
| 39 | + } |
| 40 | + |
| 41 | + const value = token.value.value; |
| 42 | + |
| 43 | + const char = value[0]; |
| 44 | + |
| 45 | + if (value === "false") { |
| 46 | + return false; |
| 47 | + } else if (value === "true") { |
| 48 | + return true; |
| 49 | + } |
| 50 | + if (isDigit(char)) { |
| 51 | + return parseInt(value); |
| 52 | + } else if (isQuote(char)) { |
| 53 | + return value.slice(1, -1); |
| 54 | + } else { |
| 55 | + throw createUnexpectedToken( |
| 56 | + token.value, |
| 57 | + "expected a string, a boolean or a number" |
| 58 | + ); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +type TYield<T> = T extends Generator<infer R, any, any> ? R : never; |
| 63 | + |
| 64 | +type TReturn<T> = T extends Generator<any, infer R, any> ? R : never; |
| 65 | + |
| 66 | +interface IteratorYieldResult<TYield> { |
| 67 | + done?: false; |
| 68 | + value: TYield; |
| 69 | +} |
| 70 | + |
| 71 | +interface IteratorReturnResult<TReturn> { |
| 72 | + done: true; |
| 73 | + value: TReturn; |
| 74 | +} |
| 75 | + |
| 76 | +type IteratorResult<T> = |
| 77 | + | IteratorYieldResult<TYield<T>> |
| 78 | + | IteratorReturnResult<TReturn<T>>; |
| 79 | + |
| 80 | +type TokenGenerator = ReturnType<typeof Tokenizer.prototype["tokens"]>; |
| 81 | + |
| 82 | +type Token = IteratorResult<TokenGenerator>; |
| 83 | + |
| 84 | +class BufferedGenerator<T, TReturn, TNext> { |
| 85 | + private tokens: (IteratorYieldResult<T> | IteratorReturnResult<TReturn>)[] = |
| 86 | + []; |
| 87 | + |
| 88 | + private position = -1; |
| 89 | + |
| 90 | + constructor(generator: Generator<T, TReturn, TNext>) { |
| 91 | + do { |
| 92 | + this.tokens.push(generator.next()); |
| 93 | + } while (!this.tokens[this.tokens.length - 1].done); |
| 94 | + } |
| 95 | + |
| 96 | + next() { |
| 97 | + if (this.position < this.tokens.length - 1) { |
| 98 | + this.position++; |
| 99 | + } |
| 100 | + |
| 101 | + return this.tokens[this.position]; |
| 102 | + } |
| 103 | + |
| 104 | + peak(n: number = 1) { |
| 105 | + return this.tokens[this.position + n]; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +type Primitive = string | boolean | number; |
| 110 | + |
| 111 | +export default class Parser { |
| 112 | + public constructor(private content: string) {} |
| 113 | + |
| 114 | + parse(): Record<string, Primitive | Primitive[] | Record<string, Primitive>> { |
| 115 | + const tokens = new BufferedGenerator(new Tokenizer(this.content).tokens()); |
| 116 | + |
| 117 | + let next: Token = expectToken(tokens.next()); |
| 118 | + |
| 119 | + if (!isAt(next.value.value)) { |
| 120 | + throw createUnexpectedToken(next.value, "expected tag to begin with '@'"); |
| 121 | + } |
| 122 | + |
| 123 | + next = expectToken(tokens.next()); |
| 124 | + |
| 125 | + if (!isWordChar(next.value.value[0])) { |
| 126 | + throw createUnexpectedToken( |
| 127 | + next.value, |
| 128 | + "expected tag to start with a property name" |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + const propertyName = next.value.value; |
| 133 | + |
| 134 | + next = expectToken(tokens.next()); |
| 135 | + |
| 136 | + if (!isOpeningParanthesis(next.value.value)) { |
| 137 | + throw createUnexpectedToken(next.value, "expected opening paranthesis"); |
| 138 | + } |
| 139 | + |
| 140 | + const isObjectMode = isEqual(expectToken(tokens.peak(2)).value.value); |
| 141 | + const entries: [string, Primitive][] = []; |
| 142 | + const values: Primitive[] = []; |
| 143 | + |
| 144 | + if (isObjectMode) { |
| 145 | + while (true) { |
| 146 | + const key = expectToken(tokens.next()).value.value; |
| 147 | + |
| 148 | + next = expectToken(tokens.next()); |
| 149 | + |
| 150 | + if (!isEqual(next.value.value)) { |
| 151 | + throw createUnexpectedToken(next.value, "expected equal sign"); |
| 152 | + } |
| 153 | + |
| 154 | + const value = parsePrimitiveToken(tokens.next()); |
| 155 | + |
| 156 | + entries.push([key, value]); |
| 157 | + |
| 158 | + if (!isComma(expectToken(tokens.peak()).value.value)) { |
| 159 | + break; |
| 160 | + } else { |
| 161 | + tokens.next(); |
| 162 | + } |
| 163 | + } |
| 164 | + } else { |
| 165 | + while (true) { |
| 166 | + const value = parsePrimitiveToken(tokens.next()); |
| 167 | + |
| 168 | + values.push(value); |
| 169 | + |
| 170 | + if (!isComma(expectToken(tokens.peak()).value.value)) { |
| 171 | + break; |
| 172 | + } else { |
| 173 | + tokens.next(); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + next = expectToken(tokens.next()); |
| 179 | + |
| 180 | + if (next.done) { |
| 181 | + throw createUnexpectedEndOfString(); |
| 182 | + } else if (!isClosingParanthesis(next.value.value)) { |
| 183 | + throw createUnexpectedToken(next.value, "expected closing paranthesis"); |
| 184 | + } |
| 185 | + |
| 186 | + if (isObjectMode) { |
| 187 | + return { |
| 188 | + [propertyName]: Object.fromEntries(entries), |
| 189 | + }; |
| 190 | + } else { |
| 191 | + return { |
| 192 | + [propertyName]: values.length === 1 ? values[0] : values, |
| 193 | + }; |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments