[TCS] Discarding tokens [message #2323] |
Thu, 21 February 2008 15:33 |
T Kruse Messages: 73 Registered: July 2009 |
Member |
|
|
Hi,
I noticed the way you dicard tokens in ParserWrapper.parse():
try {
TT_NL = ((Integer)parser_.getField("NL").get(null)).intValue();
tokens.setTokenTypeChannel(TT_NL, 99);
} catch(Exception e) {
}
I don't really like this kind of dependency, and i do not like setting a
member like
Field f = parser_.getField("ei");
f.set(parser, runtime);
directly.
I would think this is mostly due to ANTLR not being able to let generated
Parsers and Lexers implement other interfaces, else one could write
"nicer" methods to do the job.
How about creating an abstract class adding the following snippet to the
generated Parser:
/**
* to access generated fields in a generated parser.
*/
public abstract class AbstractGeneratedParserAccess {
private Parser parser;
public AbstractGeneratedParserAccess(Parser parser) {
this.parser = parser;
}
protected Parser getParser() {
return parser;
}
public abstract void setInjector(TCSInjector injector);
}
********* Within the generated Parser:**********************
/**
* to be used instead of constructor for injection
*/
public static AbstractGeneratedParserAccess
getParserAccess(CommonTokenStream commonTokenStream) {
BibtextParser parser = new BibtextParser(commonTokenStream);
AbstractGeneratedParserAccess access = new
AbstractGeneratedParserAccess(parser) {
public void setInjector(TCSInjector injector) {
((BibtextParser)getParser()).ei = injector;
}
public void discardTokens() {
CommonTokenStream stream =
(CommonTokenStream)(getParser().getTokenStream());
CommonTokenStream cts = (CommonTokenStream) stream;
cts.discardTokenType(WS);
cts.discardTokenType(NL);
cts.discardTokenType(COMMENT);
}
};
return access;
}
The abstract class exists before generating, so all calling code can use
it easily, no need for reflective java calls except for getting the static
getParserAccess.
Just an idea.
|
|
|
Powered by
FUDForum. Page generated in 0.03160 seconds