org.cesta.parsers.java
Class AbstractTreeParser

java.lang.Object
  extended by org.antlr.runtime.BaseRecognizer
      extended by org.antlr.runtime.tree.TreeParser
          extended by org.cesta.parsers.java.AbstractTreeParser
Direct Known Subclasses:
CheckTransactionsParser, IdentifyBlocksParser, IfSwitchReplacementParser, ShadowVariablesParser, ValidateStateTransitionsParser

public abstract class AbstractTreeParser
extends org.antlr.runtime.tree.TreeParser

Abstract tree parser with some usefull utility functions. Can be used as a parent for all tree parsers, that provide rewrite and original source code should be commented out. Additionally, it support automatic passing of parameters to templates created via getTemplateInstance().


Nested Class Summary
protected static class AbstractTreeParser.runtimeRewriteParams_scope
          Scope used to track execution of rewrites.
 
Field Summary
protected  java.util.Map<java.lang.String,java.lang.Object> parameters
          Map of parameters, which were added to transformation
protected  int rewriteID
          Counts initialized rewrites
protected  java.util.Stack runtimeRewriteParams_stack
          Stack of runtime parameters (each item represents one rewrite)
protected  org.antlr.runtime.TokenRewriteStream tokens
          Current token stream, on which is transformation working
 
Fields inherited from class org.antlr.runtime.tree.TreeParser
DOWN, input, UP
 
Fields inherited from class org.antlr.runtime.BaseRecognizer
DEFAULT_TOKEN_CHANNEL, HIDDEN, INITIAL_FOLLOW_STACK_SIZE, MEMO_RULE_FAILED, MEMO_RULE_UNKNOWN, NEXT_TOKEN_RULE_NAME, state
 
Constructor Summary
AbstractTreeParser(org.antlr.runtime.tree.TreeNodeStream input)
          Public constructor
AbstractTreeParser(org.antlr.runtime.tree.TreeNodeStream input, org.antlr.runtime.RecognizerSharedState state)
          Public constructor
 
Method Summary
 void cleanupRewrite()
          Should be called after every rewrite.
 void clearParams()
          Clears all parameters
 AbstractTreeParser.runtimeRewriteParams_scope getCurrentRewriteParams()
          Current runtime params from top of the stack
 int getCurrentStartIndex()
          Returns current token start index for tree, on which we are currently working.
 int getCurrentStopIndex()
          Returns current token stop index for tree, on which we are currently working.
 java.lang.String getDefaultTemplateGroup()
          Returns default template group, which will be used to load templates.
 java.util.logging.Logger getLogger()
          Returns default logger, which should be used in parsers.
 int getMaxRewriteID()
          Returns highest rewrite ID used so far
protected  java.lang.String getOriginalCode(org.antlr.runtime.tree.CommonTree tree)
          Will return original code of the tree (before any rewrite)
 java.lang.Object getParam(java.lang.String name)
          Returns previously set parameter with given name
 java.lang.Object getParam(java.lang.String name, java.lang.Object defaultValue)
          Returns previously set parameter with given name
 java.lang.Boolean getParamB(java.lang.String name)
          Helper method for getting Boolean parameters
 java.lang.Boolean getParamB(java.lang.String name, java.lang.Boolean defaultValue)
          Helper method for getting Boolean parameters
 java.util.Map<java.lang.String,java.lang.Object> getParams()
          Returns all set parameters for this transformation
 java.lang.String getParamS(java.lang.String name)
          Helper method for getting String parameters
 java.lang.String getParamS(java.lang.String name, java.lang.String defaultValue)
          Helper method for getting String parameters
 int getRewritesCount()
          Returns overall number of performed rewrites so far.
protected  int getStartIndex(org.antlr.runtime.tree.Tree tree)
          Returns current start index (index of first token) of provided tree.
protected  int getStopIndex(org.antlr.runtime.tree.Tree tree)
          Returns current stop index (index of last token) of provided tree.
abstract  org.antlr.stringtemplate.StringTemplateGroup getTemplateLib()
          Current template library.
 org.antlr.runtime.TokenRewriteStream getTokenRewriteStream()
           
 void initRewrite()
          Initializes rewrite (automatically sets runtime params like ID and current tree).
 void performRewrite(java.util.Map ruleParams)
          Performs unified rewrite, calls rule from initTransformation and passes default parameters (id of rewrite, originalCode) + requested parameters from provided map.
protected  java.lang.String removeIndent(org.antlr.runtime.Token token)
          Will remove indent string before token
 void setDefaultTemplateLib()
          Sets default template library - loads default template group and defines map "params", which will hold all current parameters.
 void setParam(java.lang.String name, java.lang.Object value)
          Sets one single parameter
 void setParams(java.util.Map<java.lang.String,java.lang.Object> params)
          Sets list of parameters.
abstract  void setTemplateLib(org.antlr.stringtemplate.StringTemplateGroup templateLib)
          Sets current template library.
 
Methods inherited from class org.antlr.runtime.tree.TreeParser
getCurrentInputSymbol, getErrorHeader, getErrorMessage, getMissingSymbol, getSourceName, getTreeNodeStream, matchAny, recoverFromMismatchedToken, reset, setTreeNodeStream, traceIn, traceOut
 
Methods inherited from class org.antlr.runtime.BaseRecognizer
alreadyParsedRule, beginResync, combineFollows, computeContextSensitiveRuleFOLLOW, computeErrorRecoverySet, consumeUntil, consumeUntil, displayRecognitionError, emitErrorMessage, endResync, failed, getBacktrackingLevel, getGrammarFileName, getNumberOfSyntaxErrors, getRuleInvocationStack, getRuleInvocationStack, getRuleMemoization, getRuleMemoizationCacheSize, getTokenErrorDisplay, getTokenNames, match, memoize, mismatchIsMissingToken, mismatchIsUnwantedToken, pushFollow, recover, recoverFromMismatchedSet, reportError, setBacktrackingLevel, toStrings, traceIn, traceOut
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

parameters

protected java.util.Map<java.lang.String,java.lang.Object> parameters
Map of parameters, which were added to transformation


rewriteID

protected int rewriteID
Counts initialized rewrites


tokens

protected org.antlr.runtime.TokenRewriteStream tokens
Current token stream, on which is transformation working


runtimeRewriteParams_stack

protected java.util.Stack runtimeRewriteParams_stack
Stack of runtime parameters (each item represents one rewrite)

Constructor Detail

AbstractTreeParser

public AbstractTreeParser(org.antlr.runtime.tree.TreeNodeStream input)
Public constructor

Parameters:
input -

AbstractTreeParser

public AbstractTreeParser(org.antlr.runtime.tree.TreeNodeStream input,
                          org.antlr.runtime.RecognizerSharedState state)
Public constructor

Parameters:
input -
state -
Method Detail

getParams

public java.util.Map<java.lang.String,java.lang.Object> getParams()
Returns all set parameters for this transformation

Returns:
collection of parameters

getParam

public java.lang.Object getParam(java.lang.String name)
Returns previously set parameter with given name

Parameters:
name - name of parameter
Returns:
parameter or null

getParam

public java.lang.Object getParam(java.lang.String name,
                                 java.lang.Object defaultValue)
Returns previously set parameter with given name

Parameters:
name - name of parameter
defaultValue - default value
Returns:
previously set parameter or parameter default value

getParamS

public java.lang.String getParamS(java.lang.String name)
Helper method for getting String parameters


getParamS

public java.lang.String getParamS(java.lang.String name,
                                  java.lang.String defaultValue)
Helper method for getting String parameters


getParamB

public java.lang.Boolean getParamB(java.lang.String name)
Helper method for getting Boolean parameters


getParamB

public java.lang.Boolean getParamB(java.lang.String name,
                                   java.lang.Boolean defaultValue)
Helper method for getting Boolean parameters


setParams

public void setParams(java.util.Map<java.lang.String,java.lang.Object> params)
Sets list of parameters. Previous list is not overriden. You can call clearParams() before.

Parameters:
params - list of parameters

setParam

public void setParam(java.lang.String name,
                     java.lang.Object value)
Sets one single parameter

Parameters:
name - name of parameter
value - value of parameter, any Object

clearParams

public void clearParams()
Clears all parameters


getMaxRewriteID

public int getMaxRewriteID()
Returns highest rewrite ID used so far

Returns:
number of rewrites

getRewritesCount

public int getRewritesCount()
Returns overall number of performed rewrites so far.

Returns:
number of rewrites

getLogger

public java.util.logging.Logger getLogger()
Returns default logger, which should be used in parsers.

Returns:
logger

getTokenRewriteStream

public org.antlr.runtime.TokenRewriteStream getTokenRewriteStream()
                                                           throws antlr.RecognitionException
Throws:
antlr.RecognitionException

getCurrentRewriteParams

public AbstractTreeParser.runtimeRewriteParams_scope getCurrentRewriteParams()
Current runtime params from top of the stack


initRewrite

public void initRewrite()
Initializes rewrite (automatically sets runtime params like ID and current tree). Should be called before every rewrite, for which we want to automatically write original source code in comments.


cleanupRewrite

public void cleanupRewrite()
Should be called after every rewrite. It pops runtime parameters from stack.


getCurrentStartIndex

public int getCurrentStartIndex()
Returns current token start index for tree, on which we are currently working.


getCurrentStopIndex

public int getCurrentStopIndex()
Returns current token stop index for tree, on which we are currently working.


performRewrite

public void performRewrite(java.util.Map ruleParams)
Performs unified rewrite, calls rule from initTransformation and passes default parameters (id of rewrite, originalCode) + requested parameters from provided map.

Special parameter "enabled" set to false will disable the rewrite.

Parameters:
ruleParams - map of parameters that should be passed directly to template

getOriginalCode

protected java.lang.String getOriginalCode(org.antlr.runtime.tree.CommonTree tree)
Will return original code of the tree (before any rewrite)


getStartIndex

protected int getStartIndex(org.antlr.runtime.tree.Tree tree)
Returns current start index (index of first token) of provided tree.

Parameters:
tree - tree
Returns:
index of first token

getStopIndex

protected int getStopIndex(org.antlr.runtime.tree.Tree tree)
Returns current stop index (index of last token) of provided tree.

Parameters:
tree - tree
Returns:
index of last token

removeIndent

protected java.lang.String removeIndent(org.antlr.runtime.Token token)
Will remove indent string before token


getTemplateLib

public abstract org.antlr.stringtemplate.StringTemplateGroup getTemplateLib()
Current template library.

Returns:
current template library

setTemplateLib

public abstract void setTemplateLib(org.antlr.stringtemplate.StringTemplateGroup templateLib)
Sets current template library. Should be called from transformation, otherwise default StringTemplateGroup will be used.

Parameters:
templateLib - Template library to be used

getDefaultTemplateGroup

public java.lang.String getDefaultTemplateGroup()
Returns default template group, which will be used to load templates. The default value is simple class name and can be overriden by parameter templateGroup.

Returns:
default template group

setDefaultTemplateLib

public void setDefaultTemplateLib()
                           throws TransformationException
Sets default template library - loads default template group and defines map "params", which will hold all current parameters.

Throws:
TransformationException - in case default template group couldn't be loaded