antlr4ts | Optimized TypeScript target for ANTLR | Hacking library
kandi X-RAY | antlr4ts Summary
kandi X-RAY | antlr4ts Summary
Optimized TypeScript target for ANTLR 4
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of antlr4ts
antlr4ts Key Features
antlr4ts Examples and Code Snippets
Community Discussions
Trending Discussions on antlr4ts
QUESTION
I have the following antlr4 grammar:
...ANSWER
Answered 2021-Feb-28 at 13:16I cannot reproduce that with 0.5.0-alpha.4
.
Given the grammar:
QUESTION
Consider the following lexer rule:
...ANSWER
Answered 2020-Oct-19 at 20:41That can only be done by using target specific code. For example, in Java that would look like this:
QUESTION
My ANTLR lexer has below rules for a string.
...ANSWER
Answered 2020-Jun-27 at 09:40Your grammar works pretty well for me (also using the TS runtime in my extension):
Maybe it's something in the input handling, how you load the input? The correct approach is to use the (relatively) new CharStreams:
QUESTION
I'm trying to write some tooling (validation/possibly autocomplete) for a SQL-esk query language. However, parser is tokenizing invalid/incomplete inputs in a way that is making it more difficult to work with.
I've reduce my scenario to its simplest reproducible form. Here is my minimized grammar:
...ANSWER
Answered 2018-Nov-04 at 18:39It was my understanding since FROM is a identifier, it's not a valid field in the select_clause (maybe I'm just misunderstanding)?
All the parser sees is a discrete stream of typed tokens coming from the lexer. The parser has no intrinsic way to tell if a token is intended to be an identifier, or for that matter, have any particular semantic nature.
In designing a fault-tolerant grammar, plan the parser to be fairly permissive to syntax errors and expect to use several tree-walkers to progressively identify and where possible resolve the syntax and semantic ambiguities.
Two ANTLR features particularly useful to this end include:
1) implement a lexer TokenFactory and custom token, typically extending CommonToken. The custom token provides a convenient space for flags and logic for identifying the correct syntactic/semantic use and expected context for a particular token instance.
2) implement a parser error strategy, extending or expanding on the DefaultErrorStrategy. The error strategy will allow modest modifications to the parser operation on the token stream when an attempted match results in a recognition error. If the error cannot be fully resolved and appropriately fixed upon examining the surrounding (custom) tokens, at least those same custom tokens can be suitably annotated to ease problem resolution during the subsequent tree-walks.
QUESTION
I'm currently working on a project and i have a task to validate an identifier using an ANTLR4 grammar. This part of the project if the frontend using Angular 6, and the grammar will also be compiled to a backend microservice.
The validation consist in validating a string that start with a letter|digit character, and then it can have letter|digit|underscore and finishes with a letter|digit character.
I'm currently having problems with the grammar implementation (since I have no experience in Lex) and handling the errors. Here is my grammar, and implementation for the error.
...ANSWER
Answered 2018-Sep-18 at 13:46I think using ANTLR is a bit of overkill for your task. ANTLR, or any other parsing tool, is good for constructing the structure of a string, but here, you just want to know if a string is an identifier or not. If you really need ANTLR, please elaborate why and then I can help you with the error handling.
For this task, I'd suggest, you just use a regular expression like the following for testing an identifier:
QUESTION
I run npm install
from my sample project's root folder to build it using scripts in package.json
.
The build requires a few transpilation steps currently in the prepublish
script, but npm version 4 displays a warning that a breaking change is coming, leading me to believe the new prepare
build event script is more future proof.
ANSWER
Answered 2017-May-22 at 19:22NPM docs state that engines
field casts an error only when your package is installed as a dependency:
Unless the user has set the
engine-strict
config flag, [engines] field is advisory only will produce warnings when your package is installed as a dependency.
As developer/mantainer of the project you should not see any alert coming from your engines
field.
Since you need to compile your files when you run npm install
only, you might simply use postinstall
hook.
Please also note that prepublish
is there to stay, it will just change its behaviour on npm@5
by firing only before publish
hook.
QUESTION
I am trying to use a 3rd party typescript library(antlr - https://github.com/tunnelvisionlabs/antlr4ts) in my angular 2 project created using angular-cli. It's failing with this error class constructor MyLexer cannot be invoked without 'new. If you look at below code snippet , it's failing while making call to super();.Also Lexer.js is sitting in node_modules of antlr library.
I am not using babel in my project but I found this other stackoverflow post which has similar error as mine Babel error: Class constructor Foo cannot be invoked without 'new'. It says that due to the way ES6 classes work, you cannot extend a native class with a transpiled class. Is it somehow related to my issue as well? Please guide.
Code snippet
...ANSWER
Answered 2017-May-03 at 07:28That's quite surprising. I'm using the same approach like you in my node module antlr4-graps, which has ES6 set as transpilation target and everything is working very well. Take a closer look there. Maybe you can spot other signifcant differences between your code and mine. I assume you have the latest alpha of antlr4ts installed.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install antlr4ts
Install antlr4ts as a runtime dependency using your preferred package manager.
Install antlr4ts-cli as a development dependency using your preferred package manager.
Add a grammar to your project, e.g. path/to/MyGrammar.g4
Add a script to package.json for compiling your grammar to TypeScript "scripts": { // ... "antlr4ts": "antlr4ts -visitor path/to/MyGrammar.g4" }
Use your grammar in TypeScript import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts'; // Create the lexer and parser let inputStream = new ANTLRInputStream("text"); let lexer = new MyGrammarLexer(inputStream); let tokenStream = new CommonTokenStream(lexer); let parser = new MyGrammarParser(tokenStream); // Parse the input, where `compilationUnit` is whatever entry point you defined let tree = parser.compilationUnit(); The two main ways to inspect the tree are by using a listener or a visitor, you can read about the differences between the two here. Listener Approach // ... import { MyGrammarParserListener } from './MyGrammarParserListener' import { FunctionDeclarationContext } from './MyGrammarParser' import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker' class EnterFunctionListener implements MyGrammarParserListener { // Assuming a parser rule with name: `functionDeclaration` enterFunctionDeclaration(context: FunctionDeclarationContext) { console.log(`Function start line number ${context._start.line}`) // ... } // other enterX functions... } // Create the listener const listener: MyGrammarParserListener = new EnterFunctionListener(); // Use the entry point for listeners ParseTreeWalker.DEFAULT.walk(listener, tree) Visitor Approach Note you must pass the -visitor flag to antlr4ts to get the generated visitor file. // ... import { MyGrammarParserVisitor } from './MyGrammarParserVisitor' import { AbstractParseTreeVisitor } from 'antlr4ts/tree/AbstractParseTreeVisitor' // Extend the AbstractParseTreeVisitor to get default visitor behaviour class CountFunctionsVisitor extends AbstractParseTreeVisitor<number> implements MyGrammarParserVisitor<number> { defaultResult() { return 0 } aggregateResult(aggregate: number, nextResult: number) { return aggregate + nextResult } visitFunctionDeclaration(context: FunctionDeclarationContext): number { return 1 + super.visitChildren(context) } } // Create the visitor const countFunctionsVisitor = new CountFunctionsVisitor() // Use the visitor entry point countFunctionsVisitor.visit(tree)
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page