All files / src/backend InterpreterDataReader.ts

0% Statements 0/68
0% Branches 0/19
0% Functions 0/1
0% Lines 0/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                                                                                                                                                                                                                                                             
/*!
 * Copyright 2016 The ANTLR Project. All rights reserved.
 * Licensed under the BSD-3-Clause license. See LICENSE file in the project root for license information.
 */
 
import * as fs from "fs";
 
import { ATN } from "antlr4ts/atn";
import { Vocabulary, VocabularyImpl } from "antlr4ts";
import { CompatibleATNDeserializer } from "./CompatibleATNDeserializer";
 
export interface IInterpreterData {
    atn: ATN;
    vocabulary: Vocabulary;
    ruleNames: string[];
    channels: string[]; // Only valid for lexer grammars.
    modes: string[]; // ditto
}
 
export class InterpreterDataReader {
 
    public static parseFile(fileName: string): IInterpreterData {
        const ruleNames: string[] = [];
        const channels: string[] = [];
        const modes: string[] = [];
 
        const literalNames = [];
        const symbolicNames = [];
        const source = fs.readFileSync(fileName, "utf8");
        const lines = source.split("\n");
        let index = 0;
        let line = lines[index++];
        Iif (line !== "token literal names:") {
            throw new Error("Unexpected data entry");
        }
 
        do {
            line = lines[index++];
            Iif (line.length === 0) {
                break;
            }
            literalNames.push(line === "null" ? "" : line);
        } while (true);
 
        line = lines[index++];
        Iif (line !== "token symbolic names:") {
            throw new Error("Unexpected data entry");
        }
 
        do {
            line = lines[index++];
            Iif (line.length === 0) {
                break;
            }
            symbolicNames.push(line === "null" ? "" : line);
        } while (true);
 
        line = lines[index++];
        Iif (line !== "rule names:") {
            throw new Error("Unexpected data entry");
        }
 
        do {
            line = lines[index++];
            Iif (line.length === 0) {
                break;
            }
            ruleNames.push(line);
        } while (true);
 
        line = lines[index++];
        Iif (line === "channel names:") { // Additional lexer data.
            do {
                line = lines[index++];
                Iif (line.length === 0) {
                    break;
                }
                channels.push(line);
            } while (true);
 
            line = lines[index++];
            Iif (line !== "mode names:") {
                throw new Error("Unexpected data entry");
            }
 
            do {
                line = lines[index++];
                Iif (line.length === 0) {
                    break;
                }
                modes.push(line);
            } while (true);
        }
 
        line = lines[index++];
        Iif (line !== "atn:") {
            throw new Error("Unexpected data entry");
        }
 
        line = lines[index++];
        const elements = line.split(",");
        let value;
        const serializedATN = new Uint16Array(elements.length);
        for (let i = 0; i < elements.length; ++i) {
            const element = elements[i];
            if (element.startsWith("[")) {
                value = Number(element.substring(1).trim());
            } else if (element.endsWith("]")) {
                value = Number(element.substring(0, element.length - 1).trim());
            } else {
                value = Number(element.trim());
            }
            serializedATN[i] = value;
        }
 
        const deserializer = new CompatibleATNDeserializer();
 
        return {
            atn: deserializer.deserialize(serializedATN),
            vocabulary: new VocabularyImpl(literalNames, symbolicNames, []),
            ruleNames,
            channels,
            modes,
        };
    }
}