-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TypeScript]: emitToken(token Token) is not pushing new tokens to the TokensArray in CommonTokenStream #4024
Comments
This bug applies to which grammar in this repo, grammars-v4? We don't have a grammar for yaml. I recommend that you try the Antlr4ng tool and runtime. There is a TypeScript target for Antlr 4.13.1, but it is unlikely any fixes will be addressed in that code. You can then raise an Github Issue over there if you see a problem with the runtime. You will need to use the Antlr4ng-cli tool to generate the updated code for your parser. |
I have my own grammar I referred Python sample in this repo for Indent handling logic // Override emit method to customize token emission if necessary where I am emitting Intent and Dedent custom tokens, they are not getting emitted |
I don't know if it helps.
|
Hello RobEin Thank you for sharing detailed logic. Now I don't see DEFAULT_CHANNEL/HIDDEN_CHANNEL in the Token.d.ts. Could you please help me out here @RobEin . Token.d.ts import {CharStream} from "./CharStream"; export declare class Token {
} |
Already added to Token.d.ts. You can rebuild it with:
Although it may not be necessary to build if only the two constants are inserted ( |
I have provided constants for DEFAULT_CHANNEL/HIDDEN_CHANNEL. It worked, Thank you Robein. |
I created a parser in TypeScript for YAML like language but my language is more simpler one. I want to handle the indents and dedents when newline token occurs
Issue 1: Observed that everytime I try to get next token using super.nextToken function it is directly calling emitToken() fucntion and pushing a token in tokens array. Due to this token is getting pushed before the check needs to be performed (Example: Added condition to skip the white spaces using Skip() but token is already pushed before reaching this line)
Issue 2: When I create instance of Lexer I can see all the tokens those got pushed (Even unnecessary tokens: Issue 1 ) but after trying convert them into TokenStream using 'CommonTokenStream', I can't see the tokens I pushed in the tokens array but I noticed in side tokenSource there are tokens but unable to access them
After passing Lexer to CommonTokenStream
Lexer Class in ANTLR4
`export declare class Lexer extends Recognizer {
}
`
Logic implemented to Push Indents and Dedents token array
`import { CharStream, Token, CommonToken, Lexer } from "antlr4";
import MyParser from "./MyParser";
export default class MyLexerBase extends Lexer {
/**
* Our document does not explicity provide begin and end nesting tokens, the indentation is used to determine the nesting level.
* So solve this, we need to keep track of the indentation level and emit the INDENT and DEDENT tokens when the indentation level changes.
* Multiple DEDENT tokens may be emitted if the indentation level decreases. The same needs to be sent to the parser without any input symbols.
* e.g.
* Example 1:
* if a == 1:
* print("a")
* print("b")
* if a > 2:
* print("c")
* print("b")
*
* Output: if a == 1: \n INDENT print("a") \n print("b") \n if a > 2: \n INDENT print("c") \n DEDENT DEDENT print("b") \n EOF
*
* Example 2:
* table Sales
* column id
* datatype: Int64
* primaryKey
* summarizeBy: None
*
* Output: table Sales NEWLINE INDENT column id NEWLINE INDENT datatype : Int64 NEWLINE primaryKey NEWLINE summarizeBy : None NEWLINE DEDENT DEDENT EOF
*
*/
tokens: any[];
indents: any[];
opened: number;
}
`
The text was updated successfully, but these errors were encountered: