public class CodeGenerator extends Visitor
module.AST.importType(java.lang.String, java.lang.String) and all imported modules have been registered with
AST.importModule(java.lang.String).locatable (as opposed to definitely being or not being
locatable), the grammar must have a locatable property with value Boolean.TRUE.generic
values, the grammar must have a generic
property with value Boolean.TRUE.action base
values, generic action value, or generic recursion values, the grammar must
have a recursive property with value
Boolean.TRUE.production must be a full production and must have been annotated with
the appropriate meta-data.options, repetitions, and nested ordered choices must
have been desugared into equivalent productions. However, an
ordered choice may appear as the last element of a sequence
(that is not part of a predicate), a repetition may appear
if the list of repeated expressions need not be memoized, an an
option may appear if its value is not bound or depends on a single
bound element.repetition must be a sequence (with the last element possibly being an ordered
choice; see previous assumption). If the repetition is bound,
Analyzer.bind(List) must be able to capture the semantic
value of the repeated element.option must be a sequence (with the last element possibly being an ordered
choice). If the option is bound, Analyzer.bind(List) must
be able to capture the semantic value of the optional element.string match must be a
nonterminal.FollowedBy or NotFollowedBy
predicate must be a sequence.character switch must
either be ordered choices or sequences. Furthermore, character
switches may only appear as the last element in a sequence and not
within predicates.| Modifier and Type | Field and Description |
|---|---|
protected Analyzer |
analyzer
The analyzer utility.
|
static java.lang.String |
ARG_INDEX
The name of the index argument.
|
protected AST |
ast
The type operations.
|
protected boolean |
attributeConstant
The flag for making variable bindings constant.
|
protected boolean |
attributeDump
The flag for including a method to dump the memoization table.
|
protected boolean |
attributeFlatten
The flag for flattening lists.
|
protected boolean |
attributeIgnoringCase
The flag for performing case-insensitive comparisons.
|
protected boolean |
attributeMain
The flag for creating a main method.
|
protected boolean |
attributeParseTree
The flag for generating a parse tree.
|
protected boolean |
attributePrinter
The flag for using a grammar-specified printer in the main method.
|
protected boolean |
attributeProfile
The flag for including code to produce a memoization profile.
|
protected boolean |
attributeRawTypes
The flag for using raw types.
|
protected boolean |
attributeStateful
The flag for using a global state object.
|
protected boolean |
attributeStringSet
The flag for having a string set attribute.
|
protected boolean |
attributeVerbose
The flag for generating debugging code.
|
protected boolean |
attributeWithLocation
The flag for generating code to annotate nodes with location
information.
|
static java.lang.String |
BASE_INDEX
The name for the base index variable.
|
protected java.lang.String |
baseIndex
The expression for the base index.
|
protected Element |
bindingElement
The element being bound.
|
protected java.lang.String |
bindingName
The name of the current binding.
|
protected Type |
bindingType
The type of the element being bound.
|
static java.lang.String |
CHAR
The name of the character variable.
|
protected int |
choiceLevel
The nesting level for nested choices.
|
static int |
CHUNK_SIZE
The size of chunks.
|
protected int |
chunkCount
The number of chunks.
|
protected boolean |
chunked
Flag for whether the memoization fields are organized in chunks.
|
protected java.util.Map<NonTerminal,java.lang.Integer> |
chunkMap
The map from nonterminals to chunk numbers.
|
protected java.lang.String |
className
The class name for the current grammar.
|
static java.lang.String |
COLUMN
The name of the memoization column variable.
|
protected boolean |
createsNodeValue
The flag for whether an alternative creates a node value.
|
protected java.util.Iterator<Element> |
elementIter
The iterator over the elements of a sequence.
|
protected boolean |
endsWithParseError
Flag for whether the current choice ends with a parse error.
|
protected java.lang.String |
factoryClassName
The class name for the generic node factory.
|
protected boolean |
firstElement
The flag for the first element in a top-level choice.
|
protected int |
indentLevel
The indentation level for choices.
|
static java.lang.String |
INDEX
The name of the index variable.
|
protected java.lang.String |
indexName
The name of the index variable.
|
protected java.lang.String |
mainMethodNonterminal
The nonterminal for the main method.
|
static java.lang.String |
NESTED_CHOICE
The prefix for the index variable for nested choices.
|
protected boolean |
notFollowedBy
Flag for whether the current predicate is a not-followed-by
predicate.
|
static java.lang.String |
OP_VALUE
The prefix for the variable referencing the semantic value of a
bound option.
|
static java.lang.String |
OPTION
The prefix for the index variable for options.
|
protected boolean |
optional
The flag for options.
|
protected java.lang.String |
optionalElement
The name of the variable referencing the element value for bound
options, or
null if no such option is currently
being processed. |
protected int |
optionLevel
The nesting level for options.
|
protected java.util.List<Type> |
optionTypes
The types of bound options, i.e.
|
static java.lang.String |
PARSE_CHAR
The name of the character parsing method.
|
static java.lang.String |
PARSE_ERROR
The name for the parse error variable.
|
static java.lang.String |
PRED_INDEX
The name of the predicate index variable.
|
static java.lang.String |
PRED_MATCHED
The name for the predicate matched variable.
|
static java.lang.String |
PRED_RESULT
The name for the predicate result variable.
|
protected boolean |
predicate
Flag for whether we are currently emitting a predicate.
|
protected java.util.Iterator<Element> |
predicateIter
The predicate iterator.
|
static java.lang.String |
PREFIX
The general prefix for internal parser fields and variables.
|
static java.lang.String |
PREFIX_COUNT_FIELD
The prefix for field names that count accesses to memoized results.
|
static java.lang.String |
PREFIX_FIELD
The prefix for field names that memoize the parsers results.
|
static java.lang.String |
PREFIX_METHOD
The prefix for parsing method names.
|
protected Printer |
printer
The printer utility.
|
static java.lang.String |
PRINTER
The name for the variable referencing the verbose mode printer.
|
protected java.lang.String |
printerClassName
The class name for the grammar-specified printer.
|
static java.lang.String |
REP_VALUE
The prefix for the variable referencing the semantic value of a
bound repetition.
|
protected boolean |
repeated
The flag for repetitions.
|
static java.lang.String |
REPEATED
The prefix for the flag indicating that a repetition has been
matched at least once.
|
protected java.lang.String |
repeatedElement
The name of the variable referencing the element value for bound
repetitions, or
null if no such repetition is
currently being processed. |
protected boolean |
repeatedOnce
The flag for at-least-once repetitions.
|
static java.lang.String |
REPETITION
The prefix for the index variable for repetitions.
|
protected int |
repetitionLevel
The nesting level for repetitions.
|
protected java.util.List<Type> |
repetitionTypes
The types of bound repetitions, i.e.
|
static java.lang.String |
RESULT
The name for the result variable.
|
protected java.lang.String |
resultName
The name of the result variable.
|
protected Runtime |
runtime
The runtime.
|
protected java.lang.String |
savedBaseIndex
The saved base index.
|
protected boolean |
savedFirstElement
The saved first element.
|
protected boolean |
savedOptional
The saved option flag for predicates.
|
protected boolean |
savedRepeated
The saved repetition flag for predicates.
|
protected boolean |
savedRepeatedOnce
The saved at-least-once flag for predicates.
|
protected boolean |
savedUseBaseIndex
The saved flag for using the base index.
|
protected boolean |
seenTest
Flag for whether a test has been emitted.
|
static java.lang.String |
STATE
The name for the variable referencing the global state object.
|
protected java.lang.String |
stateClassName
The class name for the global state object.
|
protected boolean |
useBaseIndex
The flag for using the base index.
|
static java.lang.String |
VALUE
The name for the value variable (i.e.,
yyValue). |
| Constructor and Description |
|---|
CodeGenerator(Runtime runtime,
Analyzer analyzer,
AST ast,
Printer printer)
Create a new code generator.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
action(Action a)
Actually emit code for the specified action.
|
protected void |
binding()
Actually emit the code for the last visited binding.
|
java.lang.String |
booleanT()
Get the primitive boolean type as a string.
|
java.lang.String |
charT()
Get the primitive character type as a string.
|
protected void |
charValueTest()
Emit the code testing whether the character has a value.
|
protected void |
clearBinding()
Clear binding information after usage.
|
protected void |
dump()
Emit code for dumping the memoization table.
|
protected void |
emitAction(GenericActionValue v)
Emit the action creating a new generic node.
|
protected void |
emitChildren(java.lang.String first,
java.util.List<Binding> children)
Emit an expression adding the children to a generic node.
|
protected void |
emitDifference()
Emit code for determining the textual different.
|
protected void |
emitFactoryName()
Emit the class name of the class creating generic nodes.
|
protected void |
emitFormatting(java.util.List<Binding> formatting)
Emit a statement adding formatting to a generic node.
|
protected void |
emitNumberOfChildren(int base,
java.util.List<Binding> children)
Emit an expression calculating a generic node's number of children.
|
java.lang.String |
emptyListExpr()
Get the empty list expression.
|
java.lang.String |
extern(Type type)
Extern the specified type.
|
java.lang.String |
fieldName(NonTerminal nt,
java.lang.String prefix)
Generate a field name for the specified nonterminal.
|
protected boolean |
hasBinding()
Determine whether the current element has a binding.
|
protected void |
index(java.lang.String oldIndex,
boolean isLastChar)
Emit the code for assigning the index variable.
|
java.lang.String |
indexT()
Get the primitive index type as a string.
|
java.lang.String |
intT()
Get the primtive integer type as a string.
|
protected void |
mainMethod(java.lang.String nt)
Emit code for a static main method.
|
java.lang.String |
methodName(NonTerminal nt)
Generate the method name for the specified nonterminal.
|
protected java.lang.String |
nestedChoice()
Return the name of the parser variable for the current nested
choice level.
|
protected void |
nextElement()
Emit the code for the next element.
|
protected boolean |
notFollowedBy()
Determine whether we are processing a not-followed-by predicate.
|
java.lang.String |
nullExpr()
Get the null expression.
|
protected int |
numberOfChildren(int base,
java.util.List<Binding> children)
Calculate the number of a generic node's children.
|
protected void |
parseError()
Emit the code for generating a parse error based on the
production's name.
|
protected void |
parseError(java.lang.String text)
Emit the code for generating a parse error based on a fixed text.
|
protected void |
profile()
Emit code for printing the memoization profile.
|
java.lang.String |
rawT(java.lang.String type)
Get the raw, non-generic type for the specified type.
|
protected void |
result(java.lang.String methodName,
boolean saveIndex,
boolean threadError)
Emit the code for assigning the result variable, threading the
parse error, and for testing the result.
|
protected void |
returnValue()
Emit the code for returning a semantic value.
|
protected void |
saveIndex(java.lang.String savedIndex,
java.lang.String spacer,
java.lang.String base)
Emit the code for saving the index variable.
|
java.lang.String |
stringExpr(java.lang.String text)
Get a string expression.
|
protected void |
stringValueTest(java.lang.String text,
boolean ignoreCase)
Emit the code for testing the result.
|
protected void |
tested()
Note that a test has been emitted.
|
protected void |
threadParseError(int align)
Emit code for threading parse error.
|
protected void |
valueTest()
Emit the code testing whether the result has a value.
|
protected java.lang.String |
var(Binding b)
Convert a binding into the corresponding variable name.
|
protected void |
verbose()
Emit code for verbose operation.
|
void |
visit(Action a)
Generate code for the specified action.
|
void |
visit(ActionBaseValue v)
Generate code for the specified action base value.
|
void |
visit(AnyChar a)
Generate code for the any character element.
|
void |
visit(Binding b)
Generate code for the specified binding.
|
void |
visit(BindingValue v)
Generate code for the specified binding value.
|
void |
visit(CharClass c)
Generate code for the specified character class.
|
void |
visit(CharLiteral l)
Generate code for the specified character literal.
|
void |
visit(CharSwitch s)
Generate code for the specified character switch.
|
void |
visit(EmptyListValue v)
Generate code for the specified empty list value.
|
void |
visit(FollowedBy p)
Generate code for the specified followed-by predicate.
|
void |
visit(FullProduction p)
Generate code for the specified production.
|
void |
visit(GenericActionValue v)
Generate code for the specified generic action value.
|
void |
visit(GenericNodeValue v)
Generate code for the specified generic node value.
|
void |
visit(GenericRecursionValue v)
Generate code for the specified generic recursion value.
|
void |
visit(Module m)
Generate code for the specified grammar.
|
void |
visit(NodeMarker m)
Generate code for the specified node marker.
|
void |
visit(NonTerminal nt)
Generate code for the specified nonterminal.
|
void |
visit(NotFollowedBy p)
Generate code for the specified not-followed-by predicate.
|
void |
visit(NullLiteral l)
Generate code for the specified null literal.
|
void |
visit(NullValue v)
Generate code for the specified null value.
|
void |
visit(Option o)
Generate code for the specified option.
|
void |
visit(OrderedChoice c)
Generate code for the specified ordered choice.
|
void |
visit(ParserAction pa)
Generate code for the specified parser action.
|
void |
visit(ParseTreeNode n)
Generate code for the specified parse tree node.
|
void |
visit(ProperListValue v)
Generate code for the specified proper list value.
|
void |
visit(Repetition r)
Generate code for the specified repetition.
|
void |
visit(SemanticPredicate p)
Generate code for the specified semantic predicate.
|
void |
visit(StringLiteral l)
Generate code for the specified literal.
|
void |
visit(StringMatch m)
Generate code for the specified string match.
|
void |
visit(StringValue v)
Generate code for the specified string value.
|
void |
visit(TokenValue v)
Generate code for the specified token value.
|
void |
visit(VoidedElement v)
Generate code for the specified voided element.
|
dispatch, equals, hashCode, iterate, map, mapInPlace, unableToVisit, visitpublic static final int CHUNK_SIZE
public static final java.lang.String PREFIX_METHOD
public static final java.lang.String PREFIX_FIELD
public static final java.lang.String PREFIX_COUNT_FIELD
public static final java.lang.String PREFIX
public static final java.lang.String PRINTER
public static final java.lang.String STATE
public static final java.lang.String PARSE_CHAR
public static final java.lang.String ARG_INDEX
public static final java.lang.String COLUMN
public static final java.lang.String CHAR
public static final java.lang.String INDEX
public static final java.lang.String RESULT
public static final java.lang.String PRED_INDEX
public static final java.lang.String PRED_RESULT
public static final java.lang.String PRED_MATCHED
public static final java.lang.String BASE_INDEX
public static final java.lang.String NESTED_CHOICE
public static final java.lang.String REPETITION
public static final java.lang.String OPTION
public static final java.lang.String REPEATED
public static final java.lang.String REP_VALUE
public static final java.lang.String OP_VALUE
public static final java.lang.String VALUE
yyValue).public static final java.lang.String PARSE_ERROR
protected final Runtime runtime
protected final Analyzer analyzer
protected final AST ast
protected final Printer printer
protected boolean attributeVerbose
protected boolean attributeWithLocation
protected boolean attributeConstant
protected boolean attributeFlatten
protected boolean attributeParseTree
protected boolean attributeRawTypes
protected boolean attributeIgnoringCase
protected boolean attributeStateful
protected boolean attributeStringSet
protected java.lang.String stateClassName
protected java.lang.String factoryClassName
protected boolean attributeMain
protected java.lang.String mainMethodNonterminal
protected boolean attributePrinter
protected java.lang.String printerClassName
protected boolean attributeProfile
protected boolean attributeDump
protected java.lang.String className
protected boolean chunked
protected java.util.Map<NonTerminal,java.lang.Integer> chunkMap
protected int chunkCount
protected boolean firstElement
protected boolean savedFirstElement
protected java.lang.String baseIndex
protected boolean useBaseIndex
protected java.lang.String savedBaseIndex
protected boolean savedUseBaseIndex
protected int indentLevel
protected int choiceLevel
protected boolean repeated
protected boolean savedRepeated
protected boolean repeatedOnce
protected boolean savedRepeatedOnce
protected int repetitionLevel
protected java.lang.String repeatedElement
null if no such repetition is
currently being processed.protected java.util.List<Type> repetitionTypes
MetaData.boundRepetitions.protected boolean optional
protected boolean savedOptional
protected int optionLevel
protected java.lang.String optionalElement
null if no such option is currently
being processed.protected java.util.List<Type> optionTypes
MetaData.options.protected boolean createsNodeValue
protected boolean seenTest
protected boolean endsWithParseError
protected java.util.Iterator<Element> elementIter
protected java.lang.String indexName
protected java.lang.String resultName
protected java.lang.String bindingName
protected Element bindingElement
protected Type bindingType
protected boolean predicate
protected boolean notFollowedBy
protected java.util.Iterator<Element> predicateIter
public java.lang.String booleanT()
public java.lang.String charT()
public java.lang.String intT()
public java.lang.String indexT()
public java.lang.String extern(Type type)
null, this method returns its external
representation. Otherwise, it returns null.type - The type.public java.lang.String rawT(java.lang.String type)
type - The type as a string.public java.lang.String nullExpr()
public java.lang.String stringExpr(java.lang.String text)
text - The string's text.public java.lang.String emptyListExpr()
public java.lang.String fieldName(NonTerminal nt, java.lang.String prefix)
nt - The nonterminal.prefix - The field name's prefix.public java.lang.String methodName(NonTerminal nt)
nt - The nonterminal.protected void verbose()
protected void profile()
protected void dump()
protected void mainMethod(java.lang.String nt)
nt - The name of the top-level nonterminal to parse.public void visit(Module m)
public void visit(FullProduction p)
protected void result(java.lang.String methodName,
boolean saveIndex,
boolean threadError)
methodName - The name of the parser method to use.saveIndex - The flag for whether to save the index in the
base index variable.threadError - The flag for whether to thread the parse
error.protected void threadParseError(int align)
align - The alignment for the assignment operator.protected void valueTest()
protected void charValueTest()
protected void stringValueTest(java.lang.String text,
boolean ignoreCase)
text - The expected text value.ignoreCase - The flag for whether to ignore the case.protected void index(java.lang.String oldIndex,
boolean isLastChar)
oldIndex - The old index.isLastChar - Flag for whether the just recognized character
is the terminal's last character.protected void saveIndex(java.lang.String savedIndex,
java.lang.String spacer,
java.lang.String base)
savedIndex - The saved index.spacer - Any extra space.base - The current base index.protected void tested()
protected void nextElement()
returnValue()protected void returnValue()
protected void parseError()
protected void parseError(java.lang.String text)
text - The expected text.protected java.lang.String nestedChoice()
public void visit(OrderedChoice c)
public void visit(Repetition r)
public void visit(Option o)
public void visit(FollowedBy p)
protected boolean notFollowedBy()
true if we are processing a not-followed-by
predicate.public void visit(NotFollowedBy p)
public void visit(SemanticPredicate p)
public void visit(VoidedElement v)
public void visit(Binding b)
protected boolean hasBinding()
true if the current element has a binding.protected void binding()
protected void clearBinding()
public void visit(StringMatch m)
public void visit(NonTerminal nt)
public void visit(AnyChar a)
public void visit(CharLiteral l)
public void visit(CharClass c)
public void visit(StringLiteral l)
public void visit(CharSwitch s)
public void visit(NodeMarker m)
protected void action(Action a)
public void visit(Action a)
public void visit(ParserAction pa)
public void visit(ParseTreeNode n)
public void visit(NullLiteral l)
public void visit(NullValue v)
protected void emitDifference()
public void visit(StringValue v)
public void visit(TokenValue v)
protected java.lang.String var(Binding b)
b - The binding.public void visit(BindingValue v)
public void visit(EmptyListValue v)
public void visit(ProperListValue v)
public void visit(ActionBaseValue v)
protected int numberOfChildren(int base,
java.util.List<Binding> children)
Integer.MAX_VALUE if any of the
children has a non-null list value, and
Integer.MIN_VALUE if any of the children has a list
value that may be null.base - The number of children not from the specified list.children - The list of bindings representing the children.protected void emitNumberOfChildren(int base,
java.util.List<Binding> children)
base - The number of children not from the specified list.children - The list of bindings representing the children.protected void emitChildren(java.lang.String first,
java.util.List<Binding> children)
first - The name of the optional first child.children - The list of children.protected void emitFactoryName()
protected void emitFormatting(java.util.List<Binding> formatting)
formatting - The list of bindings.protected void emitAction(GenericActionValue v)
v - The generic action value.public void visit(GenericNodeValue v)
public void visit(GenericActionValue v)
public void visit(GenericRecursionValue v)
Copyright © 2014. All Rights Reserved.