Goal :
To replace modify text, which is a rule I defined in .g4 file, that will enter and exit in my listener class in input String
My code :
def textModify(input: String) = {
val loadLexer = new DSLSQLLexer(new ANTLRInputStream(input))
val tokens = new CommonTokenStream(loadLexer)
val rewriter = new TokenStreamRewriter(tokens)
val parser = new DSLSQLParser(tokens)
parser.statement()
ParseTreeWalker.DEFAULT.walk(listener, stat)
}
But how can I call the rewriter
in my enter function, which can only get the context?
Or can I just rewrite it in textModify()
? But rewriter.replace()
requires index
of token which I can't know when I call it.
I thought it might be easier to show the real situation:
I defined a rule like this:
proJob
: realJob
;
realJob
: job
;
job
: IDENTIFIER
| quotedIdentifier
;
quotedIdentifier
: BACKQUOTED_IDENTIFIER
;
IDENTIFIER
: (LETTER | DIGIT | '_')+
;
BACKQUOTED_IDENTIFIER
: '`' ( ~'`' | '``' )* '`'
;
I want to change the text of token when it hits rule proJob
,for example:
The input : I am a teacher.
, will enter rule proJob function and I want to modify teacher
to teacher_blahblahblah
, how can I manage that