How to use the antlr4ts/BailErrorStrategy.BailErrorStrategy function in antlr4ts

To help you get started, we’ve selected a few antlr4ts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / botbuilder-js / libraries / botbuilder-ai / src / lg / templateEngine.ts View on Github external
public EvaluateInline(inlinsStr: string, scope: any): string {
        const fakeTemplateId: string = '__temp__';
        const wrappedStr: string = `# ${fakeTemplateId} \r\n - ${inlinsStr}`;
        try {
            const input: ANTLRInputStream = new ANTLRInputStream(wrappedStr);
            const lexer: LGFileLexer = new LGFileLexer(input);
            const tokens: CommonTokenStream = new CommonTokenStream(lexer);
            const parser: LGFileParser = new LGFileParser(tokens);
            parser.removeErrorListeners();
            parser.addErrorListener(TemplateErrorListener.INSTANCE);
            parser.buildParseTree = true;
            parser.errorHandler = new BailErrorStrategy();

            const context: TemplateDefinitionContext = parser.templateDefinition();
            const evaluationContext: EvaluationContext = new EvaluationContext(this.evaluationContext.TemplateContexts,
                                                                               this.evaluationContext.TemplateParameters);
            evaluationContext.TemplateContexts[fakeTemplateId] = context;
            const evalutor: Evaluator = new Evaluator(evaluationContext);

            return evalutor.EvaluateTemplate(fakeTemplateId, scope);
        } catch (error) {
            throw error;
        }
    }
}
github microsoft / botbuilder-js / libraries / botbuilder-ai / src / lg / templateEngine.ts View on Github external
public static FromText(lgFileContent: string): TemplateEngine {
        if (lgFileContent === undefined || lgFileContent === '') {
            return new TemplateEngine();
        }

        try {
            const input: ANTLRInputStream = new ANTLRInputStream(lgFileContent);
            const lexer: LGFileLexer = new LGFileLexer(input);
            const tokens: CommonTokenStream = new CommonTokenStream(lexer);
            const parser: LGFileParser = new LGFileParser(tokens);
            parser.removeErrorListeners();
            parser.addErrorListener(TemplateErrorListener.INSTANCE);
            parser.buildParseTree = true;
            parser.errorHandler = new BailErrorStrategy();

            const context: FileContext = parser.file();

            return new TemplateEngine(context);
        } catch (e) {
            throw e;
        }
    }