org.jaitools.jiffle.parser
Class AbstractSourceGenerator

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

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:
0.1
Version:
$Id: AbstractSourceGenerator.java 1653 2011-06-16 10:23:02Z michael.bedward $
Author:
Michael Bedward

Field Summary
protected  java.lang.String baseClassName
          The name of the base class for the runtime class.
protected  java.lang.String className
          The runtime class name.
protected  java.util.List<java.lang.String> imports
          The imports to be included with the runtime class.
protected  Jiffle.RuntimeModel model
          The runtime model to generate source for.
protected  java.lang.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 org.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(java.lang.String... importNames)
          Adds the given imports to those that will be included in the runtime source.
protected abstract  org.antlr.runtime.RuleReturnScope generate(java.lang.String script)
          Starts generating source code based on the abstract syntax tree produced by the Jiffle compiler.
protected  java.lang.String getOptionExpr(java.lang.String name, java.lang.String value)
          Gets the runtime source for a script option name:value pair.
protected  java.lang.String getRuntimeExpr(java.lang.String name, java.util.List<java.lang.String> argTypes)
          Looks up the runtime source for a Jiffle function.
protected  java.lang.String getRuntimeExpr(java.lang.String name, java.lang.String... argTypes)
          Looks up the runtime source for a Jiffle function.
 java.lang.String getSource(java.lang.String script)
          Returns the source for the runtime class.
protected  java.util.List<java.lang.String> prepareScriptForComments(java.lang.String script)
          Prepares the Jiffle source for inclusion in the run-time class javadocs.
 void setBaseClassName(java.lang.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 org.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 java.lang.String pkgName
The package name to use for the runtime class.


imports

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


className

protected java.lang.String className
The runtime class name.


baseClassName

protected java.lang.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(java.lang.String baseClassName)
Sets the runtime base class name.

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

getSource

public java.lang.String getSource(java.lang.String script)
                           throws JiffleException
Returns the source for the runtime class. The runtime model and base class name must be set before calling this method. If the script is passed to this method it will be included in the class javadocs.

Specified by:
getSource in interface SourceGenerator
Parameters:
script - the Jiffle script which is being compiled; may be null or empty
Returns:
source of the runtime class as a single String.
Throws:
JiffleException - on errors creating source

generate

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

Parameters:
script - the Jiffle script to include in the class javadocs; may be null or empty
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 java.lang.String getRuntimeExpr(java.lang.String name,
                                          java.util.List<java.lang.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 java.lang.String getRuntimeExpr(java.lang.String name,
                                          java.lang.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 java.lang.String getOptionExpr(java.lang.String name,
                                         java.lang.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(java.lang.String... importNames)
Adds the given imports to those that will be included in the runtime source.

Parameters:
importNames - fully qualified class names

prepareScriptForComments

protected java.util.List<java.lang.String> prepareScriptForComments(java.lang.String script)
Prepares the Jiffle source for inclusion in the run-time class javadocs. Comments are stripped from the script and it is split into lines.

Parameters:
script - the Jiffle source
Returns:
prepared script as a list of lines


Copyright © 2011. All Rights Reserved.