| 39 | } |
| 40 | |
| 41 | class Parser { |
| 42 | private str: string; |
| 43 | |
| 44 | constructor(str: string) { |
| 45 | this.str = str; |
| 46 | } |
| 47 | |
| 48 | private skip(match: RegExpExecArray): void { |
| 49 | this.str = this.str.slice(match[0].length); |
| 50 | } |
| 51 | |
| 52 | /** Comma tokens: "," */ |
| 53 | private comma(): CommaToken | undefined { |
| 54 | const m = /^, */.exec(this.str); |
| 55 | if (!m) return; |
| 56 | this.skip(m); |
| 57 | return { type: 'comma', string: ',' }; |
| 58 | } |
| 59 | |
| 60 | /** Identifier tokens: word or dash sequences */ |
| 61 | private ident(): IdentToken | undefined { |
| 62 | const m = /^([\w-]+) */.exec(this.str); |
| 63 | if (!m) return; |
| 64 | this.skip(m); |
| 65 | return { type: 'ident', string: m[1] }; |
| 66 | } |
| 67 | |
| 68 | /** Integer tokens, possibly with unit */ |
| 69 | private int(): NumberToken | undefined { |
| 70 | const m = /^(([-+]?\d+)(\S+)?) */.exec(this.str); |
| 71 | if (!m) return; |
| 72 | this.skip(m); |
| 73 | const n = parseInt(m[2], 10); |
| 74 | const u = m[3] || ''; |
| 75 | return { type: 'number', string: m[1], unit: u, value: n }; |
| 76 | } |
| 77 | |
| 78 | /** Float tokens, possibly with unit */ |
| 79 | private float(): NumberToken | undefined { |
| 80 | const m = /^(((?:[-+]?\d+)?\.\d+)(\S+)?) */.exec(this.str); |
| 81 | if (!m) return; |
| 82 | this.skip(m); |
| 83 | const n = parseFloat(m[2]); |
| 84 | const u = m[3] || ''; |
| 85 | return { type: 'number', string: m[1], unit: u, value: n }; |
| 86 | } |
| 87 | |
| 88 | /** Number tokens, either float or int */ |
| 89 | private number(): NumberToken | undefined { |
| 90 | return this.float() || this.int(); |
| 91 | } |
| 92 | |
| 93 | /** Double-quoted strings */ |
| 94 | private double(): StringToken | undefined { |
| 95 | const m = /^"([^"]*)" */.exec(this.str); |
| 96 | if (!m) return; |
| 97 | this.skip(m); |
| 98 | return { type: 'string', quote: '"', string: `"${m[1]}"`, value: m[1] }; |
nothing calls this directly
no outgoing calls
no test coverage detected