Closed AleiynikovPavel closed 6 months ago
Good analysis, thanks! Your patch adds another overload signature, but looking at antlr4ts we can make this even simpler, by changing the existing context type signature to type: new (...args: unknown[])
. This type only has one task (and hence the parameters are not relevant): it is used to check if a child is of this type.
And given that both overloads of getChild
have an index as first parameter, this entire signature can be reduced to:
public override getChild<T extends ParseTree>(i: number, type?: new (...args: unknown[]) => T): T | null {
...
}
What do you think?
I think type: new (...args: unknown[])
is a very good proposal. Especially if we assume that in the future new classes may appear that implement the ParseTree
interface. Already added a new commit.
Problem
antlr4-runtime
and TSantlr4ts
libraries. In these libraries, thegetChild
method of classParserRuleContext
take a type that implements both TerminalNode and ParserRuleContext.antlr-4ng
allows you to pass a type that implements onlyParserRuleContext
.Сomparison of libraries
TS
antlr-4ng
class
ParserRuleContext
have mthodgetChild
with declarationTS
antlr4ts
class
ParserRuleContext
have mthodgetChild
with declarationJava
antlr4-runtime
class
ParserRuleContext
have mthodgetChild
with declarationConclusion
getChild
in Java takeParseTree
interface, I think that your library should provide this tooantlr4ts
library method take the node type argument, casting it to the constructor type with argument - varargs of any (...args: any[]
)I assume that the
antlr-4ng
library does not use theany
type. Because of this, thegetChild
method takes theParserRuleContext
class constructor as its second argumentComparison of node constructors in
antlr-4ng
Let's look at the constructors of all classes that implement the
ParseTree
interfaceParserRuleContext
Class
ParserRuleContext
extendRuleContext
which implementParseTree
TerminalNode
Class
TerminalNode
implementsParseTree
ErrorNode
Class
ErrorNode
extendsTerminalNode
and inherits its constructorConclusion
ParseTree
interface, thegetChild
method does not accept onlyTerminalNode
andErrorNode
classesTerminalNode
andErrorNode
have the same constructor:constructor(symbol: Token);
Proposed changes
new (symbol: Token) => T
to thegetChild
method ofParserRuleContext
class.