All files parser.js

92.04% Statements 81/88
80% Branches 16/20
80% Functions 4/5
92.04% Lines 81/88

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 891x 1x 1x 1x 1x 1x       1x 1x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 16x 16x 208x 208x 208x 1x 14025x 14025x 5901x 5901x 8124x 8124x 14025x 13817x 13817x 13817x 5891x 5891x 31x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 31x 31x 5891x 13817x 13817x 13817x 13817x 14025x 1x 826x 826x     826x 826x 826x 826x 1x 569x 569x     569x 569x 569x 569x 1x 1x 1x 1x 1x 1x 1x  
import { CharStream, CommonTokenStream, ErrorListener } from 'antlr4';
import ALLexer from "../parser/ALLexer.js";
import ALParser from "../parser/ALParser.js";
 
class BreakProcessErrorListener extends ErrorListener {
    syntaxError(recognizer, offendingSymbol, line, column, msg, e) {
        const errorMessage = `Parsing error at line ${line}, column ${column}: ${msg}`;
        throw new Error(errorMessage);
    }
}
 
function parse(text, options) {
    const input = text;
    const chars = new CharStream(input);
    const lexer = new ALLexer(chars);
    const tokens = new CommonTokenStream(lexer);
    const parser = new ALParser(tokens);
 
    parser.addErrorListener(new BreakProcessErrorListener());
 
    const compilationUnit = parser.compilationUnit();
 
    const comments = [];
    attachNodeComments(compilationUnit, tokens, comments);
    if (comments.length > 0) {
        compilationUnit.comments = comments;
    }
 
    return compilationUnit;
}
 
function attachNodeComments(node, tokenStream, comments) {
    if (!node || !Array.isArray(node.children)) {
        return;
    }
 
    let index = 0;
    while (index < node.children.length) {
        const child = node.children[index];
 
        if (child && !child.hidden && child.symbol && typeof child.symbol.tokenIndex === "number") {
            const commentTokens = tokenStream.getHiddenTokensToLeft(child.symbol.tokenIndex, ALLexer.COMMENTS_CHANNEL);
            if (commentTokens && commentTokens.length > 0) {
                comments.push(...commentTokens.map(token => ({
                    symbol: {
                        text: token.text,
                        start: token.start,
                        stop: token.stop,
                        startLine: token.line,
                        startColumn: token.column,
                        stopLine: token.line,
                        stopColumn: token.column + token.text.length,
                    },
                    value: token.text,
                    printed: false
                })));
            }
        }
 
        attachNodeComments(child, tokenStream, comments);
        index += 1;
    }
}
 
function locStart(node) {
    if (!node) {
        return 0;
    }
 
    const token = node.start || node.symbol;
    return token && typeof token.start === "number" ? token.start : 0;
}
 
function locEnd(node) {
    if (!node) {
        return 0;
    }
 
    const token = node.stop || node.symbol;
    return token && typeof token.stop === "number" ? token.stop : 0;
}
 
export default {
    parse,
    astFormat: "al-ast",
    locStart,
    locEnd
};