|
1 |
| -import { convertString, CstNode, DefaultValueConverter, GrammarAST, ValueType } from 'langium'; |
| 1 | +import { convertBigint, CstNode, DefaultValueConverter, GrammarAST, ValueType } from 'langium'; |
2 | 2 |
|
3 | 3 | export class SafeDsValueConverter extends DefaultValueConverter {
|
4 | 4 | protected override runConverter(rule: GrammarAST.AbstractRule, input: string, cstNode: CstNode): ValueType {
|
5 | 5 | switch (rule.name.toUpperCase()) {
|
6 | 6 | case 'ID':
|
7 | 7 | return input.replaceAll('`', '');
|
| 8 | + case 'INT': |
| 9 | + return convertBigint(input); |
| 10 | + case 'STRING': |
| 11 | + return convertString(input, 1, 1); |
8 | 12 | case 'TEMPLATE_STRING_START':
|
9 |
| - return convertString(input.substring(0, input.length - 1)); |
| 13 | + return convertString(input, 1, 2); |
10 | 14 | case 'TEMPLATE_STRING_INNER':
|
11 |
| - return convertString(input.substring(1, input.length - 1)); |
| 15 | + return convertString(input, 2, 2); |
12 | 16 | case 'TEMPLATE_STRING_END':
|
13 |
| - return convertString(input.substring(1)); |
| 17 | + return convertString(input, 2, 1); |
14 | 18 | default:
|
15 | 19 | return super.runConverter(rule, input, cstNode);
|
16 | 20 | }
|
17 | 21 | }
|
18 | 22 | }
|
| 23 | + |
| 24 | +const convertString = (input: string, openingDelimiterLength: number, closingDelimiterLength: number): string => { |
| 25 | + let result = ''; |
| 26 | + const endIndex = input.length - 1 - closingDelimiterLength; |
| 27 | + |
| 28 | + for (let i = openingDelimiterLength; i <= endIndex; i++) { |
| 29 | + const current = input.charAt(i); |
| 30 | + if (current === '\\' && i < endIndex) { |
| 31 | + const [stringToAdd, newIndex] = handleEscapeSequence(input, i + 1, endIndex); |
| 32 | + result += stringToAdd; |
| 33 | + i = newIndex - 1; // -1 because the loop will increment it |
| 34 | + } else { |
| 35 | + result += current; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return result; |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Handle an escape sequence. |
| 44 | + * |
| 45 | + * @param input The entire input string. |
| 46 | + * @param index The index of the escape sequence (after the slash). |
| 47 | + * @param endIndex The index of the last character of the input string, excluding delimiters. |
| 48 | + * @returns An array containing the string to add to the result and the new index. |
| 49 | + */ |
| 50 | +const handleEscapeSequence = (input: string, index: number, endIndex: number): [string, number] => { |
| 51 | + const current = input.charAt(index); |
| 52 | + switch (current) { |
| 53 | + case 'b': |
| 54 | + return ['\b', index + 1]; |
| 55 | + case 'f': |
| 56 | + return ['\f', index + 1]; |
| 57 | + case 'n': |
| 58 | + return ['\n', index + 1]; |
| 59 | + case 'r': |
| 60 | + return ['\r', index + 1]; |
| 61 | + case 't': |
| 62 | + return ['\t', index + 1]; |
| 63 | + case 'v': |
| 64 | + return ['\v', index + 1]; |
| 65 | + case '0': |
| 66 | + return ['\0', index + 1]; |
| 67 | + } |
| 68 | + |
| 69 | + if (current === 'u' && index + 4 <= endIndex) { |
| 70 | + const code = input.substring(index + 1, index + 5); |
| 71 | + if (code.match(/[0-9a-fA-F]{4}/gu)) { |
| 72 | + return [String.fromCharCode(parseInt(code, 16)), index + 5]; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return [current, index + 1]; |
| 77 | +}; |
0 commit comments