forked from jasder/antlr
Add TrimToSizeListener and Parser.get/setTrimParseTrees
This commit is contained in:
parent
dd69a7532d
commit
fa9ec191bd
|
@ -38,6 +38,7 @@ import org.antlr.v4.runtime.misc.IntervalSet;
|
||||||
import org.antlr.v4.runtime.misc.Nullable;
|
import org.antlr.v4.runtime.misc.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** This is all the parsing support code essentially; most of it is error recovery stuff. */
|
/** This is all the parsing support code essentially; most of it is error recovery stuff. */
|
||||||
|
@ -61,6 +62,26 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class TrimToSizeListener implements ParseListener<Token> {
|
||||||
|
public static final TrimToSizeListener INSTANCE = new TrimToSizeListener();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitTerminal(ParserRuleContext<Token> parent, Token token) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enterNonLRRule(ParserRuleContext<Token> ctx) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exitEveryRule(ParserRuleContext<Token> ctx) {
|
||||||
|
if (ctx.children instanceof ArrayList) {
|
||||||
|
((ArrayList<?>)ctx.children).trimToSize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected ANTLRErrorStrategy _errHandler = new DefaultErrorStrategy();
|
protected ANTLRErrorStrategy _errHandler = new DefaultErrorStrategy();
|
||||||
|
|
||||||
protected TokenStream _input;
|
protected TokenStream _input;
|
||||||
|
@ -153,6 +174,39 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
|
||||||
return _buildParseTrees;
|
return _buildParseTrees;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trim the internal lists of the parse tree during parsing to conserve memory.
|
||||||
|
* This property is set to {@code false} by default for a newly constructed parser.
|
||||||
|
*
|
||||||
|
* @param trimParseTrees {@code true} to trim the capacity of the {@link ParserRuleContext#children}
|
||||||
|
* list to its size after a rule is parsed.
|
||||||
|
*/
|
||||||
|
public void setTrimParseTrees(boolean trimParseTrees) {
|
||||||
|
if (trimParseTrees) {
|
||||||
|
if (getTrimParseTrees()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
addParseListener(TrimToSizeListener.INSTANCE);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
removeParseListener(TrimToSizeListener.INSTANCE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return {@code true} if the {@link ParserRuleContext#children} list is trimmed
|
||||||
|
* using the default {@link Parser.TrimToSizeListener} during the parse process.
|
||||||
|
*/
|
||||||
|
public boolean getTrimParseTrees() {
|
||||||
|
if (_parseListeners == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _parseListeners.contains(TrimToSizeListener.INSTANCE);
|
||||||
|
}
|
||||||
|
|
||||||
// public void setTraceATNStates(boolean traceATNStates) {
|
// public void setTraceATNStates(boolean traceATNStates) {
|
||||||
// this.traceATNStates = traceATNStates;
|
// this.traceATNStates = traceATNStates;
|
||||||
// }
|
// }
|
||||||
|
|
Loading…
Reference in New Issue