public class TokenStreamRewriter extends Object
You can insert stuff, replace, and delete chunks. Note that the operations
are done lazily--only if you convert the buffer to a String
with
TokenStream.getText()
. This is very efficient because you are not
moving data around all the time. As the buffer of tokens is converted to
strings, the getText()
method(s) scan the input token stream and
check to see if there is an operation at the current index. If so, the
operation is done and then normal String
rendering continues on the
buffer. This is like having multiple Turing machine instruction streams
(programs) operating on a single input tape. :)
This rewriter makes no modifications to the token stream. It does not ask the
stream to fill itself up nor does it advance the input cursor. The token
stream IntStream.index()
will return the same value before and
after any getText()
call.
The rewriter only works on tokens that you have in the buffer and ignores the
current input cursor. If you are buffering tokens on-demand, calling
getText()
halfway through the input will only do rewrites for those
tokens in the first half of the file.
Since the operations are done lazily at getText()
-time, operations do
not screw up the token index values. That is, an insert operation at token
index i
does not change the index values for tokens
i
+1..n-1.
Because operations never actually alter the buffer, you may always get the original token stream back without undoing anything. Since the instructions are queued up, you can easily simulate transactions and roll back any changes if there is an error just by removing instructions. For example,
CharStream input = new ANTLRFileStream("input"); TLexer lex = new TLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); T parser = new T(tokens); TokenStreamRewriter rewriter = new TokenStreamRewriter(tokens); parser.startRule();
Then in the rules, you can execute (assuming rewriter is visible):
Token t,u; ... rewriter.insertAfter(t, "text to put after t");} rewriter.insertAfter(u, "text after u");} System.out.println(rewriter.getText());
You can also have multiple "instruction streams" and get multiple rewrites from a single pass over the input. Just name the instruction streams and use that name again when printing the buffer. This could be useful for generating a C file and also its header file--all from the same buffer:
rewriter.insertAfter("pass1", t, "text to put after t");} rewriter.insertAfter("pass2", u, "text after u");} System.out.println(rewriter.getText("pass1")); System.out.println(rewriter.getText("pass2"));
If you don't use named rewrite streams, a "default" stream is used as the first example shows.
Modifier and Type | Class and Description |
---|---|
class |
TokenStreamRewriter.RewriteOperation |
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_PROGRAM_NAME |
protected Map<String,Integer> |
lastRewriteTokenIndexes
Map String (program name) → Integer index
|
static int |
MIN_TOKEN_INDEX |
static int |
PROGRAM_INIT_SIZE |
protected Map<String,List<TokenStreamRewriter.RewriteOperation>> |
programs
You may have multiple, named streams of rewrite operations.
|
protected TokenStream |
tokens
Our source stream
|
Constructor and Description |
---|
TokenStreamRewriter(TokenStream tokens) |
Modifier and Type | Method and Description |
---|---|
protected String |
catOpText(Object a,
Object b) |
void |
delete(int index) |
void |
delete(int from,
int to) |
void |
delete(String programName,
int from,
int to) |
void |
delete(String programName,
Token from,
Token to) |
void |
delete(Token indexT) |
void |
delete(Token from,
Token to) |
void |
deleteProgram() |
void |
deleteProgram(String programName)
Reset the program so that no instructions exist
|
protected <T extends TokenStreamRewriter.RewriteOperation> |
getKindOfOps(List<? extends TokenStreamRewriter.RewriteOperation> rewrites,
Class<T> kind,
int before)
Get all operations before an index of a particular kind
|
int |
getLastRewriteTokenIndex() |
protected int |
getLastRewriteTokenIndex(String programName) |
protected List<TokenStreamRewriter.RewriteOperation> |
getProgram(String name) |
String |
getText()
Return the text from the original tokens altered per the
instructions given to this rewriter.
|
String |
getText(Interval interval)
Return the text associated with the tokens in the interval from the
original token stream but with the alterations given to this rewriter.
|
String |
getText(String programName)
Return the text from the original tokens altered per the
instructions given to this rewriter in programName.
|
String |
getText(String programName,
Interval interval) |
TokenStream |
getTokenStream() |
void |
insertAfter(int index,
Object text) |
void |
insertAfter(String programName,
int index,
Object text) |
void |
insertAfter(String programName,
Token t,
Object text) |
void |
insertAfter(Token t,
Object text) |
void |
insertBefore(int index,
Object text) |
void |
insertBefore(String programName,
int index,
Object text) |
void |
insertBefore(String programName,
Token t,
Object text) |
void |
insertBefore(Token t,
Object text) |
protected Map<Integer,TokenStreamRewriter.RewriteOperation> |
reduceToSingleOperationPerIndex(List<TokenStreamRewriter.RewriteOperation> rewrites)
We need to combine operations and report invalid operations (like
overlapping replaces that are not completed nested).
|
void |
replace(int from,
int to,
Object text) |
void |
replace(int index,
Object text) |
void |
replace(String programName,
int from,
int to,
Object text) |
void |
replace(String programName,
Token from,
Token to,
Object text) |
void |
replace(Token indexT,
Object text) |
void |
replace(Token from,
Token to,
Object text) |
void |
rollback(int instructionIndex) |
void |
rollback(String programName,
int instructionIndex)
Rollback the instruction stream for a program so that
the indicated instruction (via instructionIndex) is no
longer in the stream.
|
protected void |
setLastRewriteTokenIndex(String programName,
int i) |
public static final String DEFAULT_PROGRAM_NAME
public static final int PROGRAM_INIT_SIZE
public static final int MIN_TOKEN_INDEX
protected final TokenStream tokens
protected final Map<String,List<TokenStreamRewriter.RewriteOperation>> programs
public TokenStreamRewriter(TokenStream tokens)
public final TokenStream getTokenStream()
public void rollback(int instructionIndex)
public void rollback(String programName, int instructionIndex)
public void deleteProgram()
public void deleteProgram(String programName)
public void insertAfter(int index, Object text)
public void insertBefore(int index, Object text)
public void replace(int index, Object text)
public void replace(int from, int to, Object text)
public void delete(int index)
public void delete(int from, int to)
public void delete(Token indexT)
public void delete(String programName, int from, int to)
public int getLastRewriteTokenIndex()
protected int getLastRewriteTokenIndex(String programName)
protected void setLastRewriteTokenIndex(String programName, int i)
protected List<TokenStreamRewriter.RewriteOperation> getProgram(String name)
public String getText()
public String getText(String programName)
public String getText(Interval interval)
protected Map<Integer,TokenStreamRewriter.RewriteOperation> reduceToSingleOperationPerIndex(List<TokenStreamRewriter.RewriteOperation> rewrites)
protected <T extends TokenStreamRewriter.RewriteOperation> List<? extends T> getKindOfOps(List<? extends TokenStreamRewriter.RewriteOperation> rewrites, Class<T> kind, int before)
Copyright © 1992-2015 ANTLR. All Rights Reserved.