jaitools.jiffle.parser
Class AbstractSourceGenerator

java.lang.Object
  extended by org.antlr.runtime.BaseRecognizer
      extended by org.antlr.runtime.tree.TreeParser
          extended by jaitools.jiffle.parser.ErrorHandlingTreeParser
              extended by jaitools.jiffle.parser.AbstractSourceGenerator
All Implemented Interfaces:
SourceGenerator
Direct Known Subclasses:
RuntimeSourceGenerator

public abstract class AbstractSourceGenerator
extends ErrorHandlingTreeParser
implements SourceGenerator

Base class for tree parsers that generate Jiffle runtime source.

The runtime source generator is created from the ANTLR grammar (RuntimeSourceGenerator.g. This class provides a small number of common methods and fields.

Since:
1.1
Version:
$Id: AbstractSourceGenerator.java 1505 2011-03-05 11:37:43Z michael.bedward $
Author:
Michael Bedward

Field Summary
protected  String baseClassName
          The name of the base class for the runtime class.
protected  String className
          The runtime class name.
protected  List<String> imports
          The imports to be included with the runtime class.
protected  Jiffle.RuntimeModel model
          The runtime model to generate source for.
protected  String pkgName
          The package name to use for the runtime class.
protected  int varIndex
          A counter used in naming variables inserted into the runtime source.
 
Fields inherited from class jaitools.jiffle.parser.ErrorHandlingTreeParser
errorReporter
 
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
  AbstractSourceGenerator(org.antlr.runtime.tree.TreeNodeStream input)
          Constructor called by ANTLR.
protected AbstractSourceGenerator(org.antlr.runtime.tree.TreeNodeStream input, org.antlr.runtime.RecognizerSharedState state)
          Constructor called by ANTLR.
 
Method Summary
protected  void addImport(String... importNames)
          Adds the given imports to those that will be included in the runtime source.
protected abstract  org.antlr.runtime.RuleReturnScope generate()
          Starts generating source code based on the abstract syntax tree produced by the Jiffle compiler.
protected  String getOptionExpr(String name, String value)
          Gets the runtime source for a script option name:value pair.
protected  String getRuntimeExpr(String name, List<String> argTypes)
          Looks up the runtime source for a Jiffle function.
protected  String getRuntimeExpr(String name, String... argTypes)
          Looks up the runtime source for a Jiffle function.
 String getSource()
          Returns the source for the runtime class.
 void setBaseClassName(String baseClassName)
          Sets the runtime base class name.
 void setRuntimeModel(Jiffle.RuntimeModel model)
          Sets the runtime model.
abstract  void setTemplateLib(org.antlr.stringtemplate.StringTemplateGroup templateLib)
          Used internally to set the string templates for source generation.
 
Methods inherited from class jaitools.jiffle.parser.ErrorHandlingTreeParser
emitErrorMessage, getErrorReporter, setErrorReporter
 
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, 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

model

protected Jiffle.RuntimeModel model
The runtime model to generate source for.


pkgName

protected String pkgName
The package name to use for the runtime class.


imports

protected List<String> imports
The imports to be included with the runtime class.


className

protected String className
The runtime class name.


baseClassName

protected String baseClassName
The name of the base class for the runtime class.


varIndex

protected int varIndex
A counter used in naming variables inserted into the runtime source.

Constructor Detail

AbstractSourceGenerator

public AbstractSourceGenerator(org.antlr.runtime.tree.TreeNodeStream input)
Constructor called by ANTLR.

Parameters:
input - AST node stream

AbstractSourceGenerator

protected AbstractSourceGenerator(org.antlr.runtime.tree.TreeNodeStream input,
                                  org.antlr.runtime.RecognizerSharedState state)
Constructor called by ANTLR.

Parameters:
input - AST node stream
state - parser state (not used by Jiffle directly)
Method Detail

setRuntimeModel

public void setRuntimeModel(Jiffle.RuntimeModel model)
Sets the runtime model.

Specified by:
setRuntimeModel in interface SourceGenerator
Parameters:
model - runtime model

setBaseClassName

public void setBaseClassName(String baseClassName)
Sets the runtime base class name.

Specified by:
setBaseClassName in interface SourceGenerator
Parameters:
baseClassName - base class name

getSource

public String getSource()
                 throws JiffleException
Returns the source for the runtime class. The runtime model and base class name must be set before calling this method.

Specified by:
getSource in interface SourceGenerator
Returns:
source of the runtime class as a single String.
Throws:
JiffleException - on errors creating source

generate

protected abstract org.antlr.runtime.RuleReturnScope generate()
                                                       throws org.antlr.runtime.RecognitionException
Starts generating source code based on the abstract syntax tree produced by the Jiffle compiler.

Returns:
an ANTLR rule return object from which the results can be retrieved
Throws:
org.antlr.runtime.RecognitionException - on errors processing the AST

setTemplateLib

public abstract void setTemplateLib(org.antlr.stringtemplate.StringTemplateGroup templateLib)
Used internally to set the string templates for source generation. Declared public to accord with the underlying ANTLR tree parser.

Parameters:
templateLib - source generation templates

getRuntimeExpr

protected String getRuntimeExpr(String name,
                                List<String> argTypes)
Looks up the runtime source for a Jiffle function.

Parameters:
name - function name
argTypes - argument type names; null or empty for no-arg functions
Returns:
runtime source

getRuntimeExpr

protected String getRuntimeExpr(String name,
                                String... argTypes)
Looks up the runtime source for a Jiffle function.

Parameters:
name - function name
argTypes - argument type names; null or empty for no-arg functions
Returns:
runtime source

getOptionExpr

protected String getOptionExpr(String name,
                               String value)
Gets the runtime source for a script option name:value pair.

Parameters:
name - option name
value - option value
Returns:
the runtime source

addImport

protected void addImport(String... importNames)
Adds the given imports to those that will be included in the runtime source.

Parameters:
importNames - fully qualified class names


Copyright © 2009-2011. All Rights Reserved.