Package eu.bandm.tools.formatfrontends
Class GenericCompiler
java.lang.Object
eu.bandm.tools.tdom.runtime.BaseVisitor
eu.bandm.tools.formatfrontends.absy.Visitor
eu.bandm.tools.formatfrontends.GenericCompiler
- All Implemented Interfaces:
PCDataVisitor
- Direct Known Subclasses:
Tdom2format,UMod2format.Compiler
public abstract class GenericCompiler
extends eu.bandm.tools.formatfrontends.absy.Visitor
Base class for different compilers which generate format-generating java
sources,
realizing the code generation for the common constructs
of the
format description language.
A specialization of this class generates code for generating formats by acting as a visitor of the format language tdom model, and creating code which (in most cases) is also a visitor, of some target model.
Any derived visitor has to act as follows:
A specialization of this class generates code for generating formats by acting as a visitor of the format language tdom model, and creating code which (in most cases) is also a visitor, of some target model.
Any derived visitor has to act as follows:
- It has to construct some target class (with the appropriate base class, specific fields, interface methods, etc)
-
It has to call
initTransformerClass(GeneratedClass)on this class. -
It has to define the
visit()methods for all the specific language constructs. -
It has to call
compileOnePattern(Element_pattern)for each expression it wants to translate. This method intializes internal registers, callsvisit(), and finally relocates all constant expressions to initializer blocks.
The format result (which contains code to construct the required format) is left infresult, The caller has to store this code in the generated class according to its need. -
During the execution of the specific code, the following registers
are relevant:
-
fresult(read/write) holding the code = translation result. ("f-result" = "functional result"), -
isdynamic(read/write) must be set by custom code to true in most cases, ie. when the constructed format will not be constant, but will depend on the data. -
checkedmode(read) if we are at the left of some "?" operator,
-
-
Whenever a sub-structure of aggreate type has to be compiled, a sub-class
of
GenericCompiler.Subformatcan be defined. This class reduces all relevant parameters from the font-end constructs for aggregates (fold operators, code, etc.) into interal values and stores them, for easy recursion.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classA sub-parser which extracts the list format description for a sub-object which is an aggregate, and stores them locally, for ease of recursion. -
Field Summary
FieldsModifier and TypeFieldDescriptionfinal Formatformat for calling the method which throwsTakeAlternativeException, cfNAME_METHOD_THROW_TAKE_ALTERNATIVE.protected booleanare we currently visiting left part of a "?" operator ?Maps "fresult" contents (=unformatted java code, used as a key) to the name of the constant fields which will hold the code's result.(package private) final FormatClosureprotected Formataccumulator for format generating functional code.protected booleanindicates whether the code currently contained infresultdoes generate formats "dynamically" (which implies that its result can NOT be stored in a constant field).protected static final intmodifier used for all fields generated to hold constant format expressions (= "protected static final").protected Stringname of the generated field which holds an empty format, normally "format_empty".final StringName of the method generated for convenience for throwing aTakeAlternativeException.protected Stringname of the generated field which holds the default idententation, normally "default_indent".protected Stringname of the generated field which holds the resulting format.protected Stringname of the generated field which holds the current mode, normally "mode".protected MessageReceiver<SimpleMessage<XMLDocumentIdentifier>> Needed ONLY because ".getReceiver()" is of type "? super M", which does not wokr.static final FormatThe format which refers toFormatstatic final FormatThe format which refers toFormatsprotected GeneratedClassclass into which to generate all const field definitions.static final EnvironmentClassstatic final EnvironmentClassstatic final EnvironmentClassRefers toeu.bandm.tools.ops, and is for convenience of derived compilers only.static final EnvironmentClassFor convenience of derived compilers only.static final EnvironmentClassAttention, refers NOT to java.util, but toeu.bandm.tools.ops, and is for convenience of derived compilers only.static final EnvironmentClassFor convenience of derived compilers only.static final EnvironmentClassFor convenience of derived compilers only.static final EnvironmentClassstatic final EnvironmentClassstatic final EnvironmentClassFields inherited from class eu.bandm.tools.tdom.runtime.BaseVisitor
validating -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidprotected voidcompileCallToFormatText(Format model) compiles a call toFormats.text(String), if model != null.protected voidcompileModeSwitch(eu.bandm.tools.formatfrontends.absy.Element_cases el) Compiles a switch statement with$modeas its selector.voidcompileOnePattern(eu.bandm.tools.formatfrontends.absy.Element_pattern el) called from derived code for translating one(1) rule.voidcreates a field "Format result=Format.empty;" for formatting wisitors which need such a temp storage (ie.protected voiderror(Location<XMLDocumentIdentifier> loc, String text) protected Formatprotected abstract FormatCurrently only needed to compileescapeToJava()in the variant operating directly on the model element.protected MessageReceiver<SimpleMessage<XMLDocumentIdentifier>> voidCreates standard fields for the generated format generating class.protected voidprocess_compound(eu.bandm.tools.formatfrontends.absy.Element[] subs, String constr) protected Formatprotected voidvoidvisit(eu.bandm.tools.formatfrontends.absy.Element_alternative element) Compiles the cascade of alternatives/catch clauses from the front end representation...? ... ? ... ? ....voidvisit(eu.bandm.tools.formatfrontends.absy.Element_append element) voidvisit(eu.bandm.tools.formatfrontends.absy.Element_beneath element) voidvisit(eu.bandm.tools.formatfrontends.absy.Element_beside element) voidvisit(eu.bandm.tools.formatfrontends.absy.Element_block element) voidvisit(eu.bandm.tools.formatfrontends.absy.Element_escapeToJava element) Creates code for a java escape.voidvisit(eu.bandm.tools.formatfrontends.absy.Element_indent element) creates code for indentation.voidvisit(eu.bandm.tools.formatfrontends.absy.Element_line element) voidvisit(eu.bandm.tools.formatfrontends.absy.Element_literal el) Creates code for one literal, which might also be empty.voidvisit(eu.bandm.tools.formatfrontends.absy.Element_nullException el) Creates code for the$throwelement.voidvisit(eu.bandm.tools.formatfrontends.absy.Element_prior el) <!ELEMENT prior (number, (number | appendOp)?, pattern, pattern) >It directly maps its arguments to a call ofFormat.prior(int, int, eu.bandm.tools.format.Format, eu.bandm.tools.format.Format).voidvisit(eu.bandm.tools.formatfrontends.absy.Element_tabular element) voidvisit(eu.bandm.tools.formatfrontends.absy.Element_whitespace el) Creates code for whitespace.protected voidwarning(Location<XMLDocumentIdentifier> loc, String text) Methods inherited from class eu.bandm.tools.formatfrontends.absy.Visitor
visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visitMethods inherited from class eu.bandm.tools.tdom.runtime.BaseVisitor
visit
-
Field Details
-
formatClosure
-
WRAPPEDCLASS_Object
-
WRAPPEDCLASS_Lists
Attention, refers NOT to java.util, but toeu.bandm.tools.ops, and is for convenience of derived compilers only. -
WRAPPEDCLASS_Function
Refers toeu.bandm.tools.ops, and is for convenience of derived compilers only. -
WRAPPEDCLASS_Format
-
REF_FORMAT_Format
The format which refers toFormat -
REF_FORMAT_Formats
The format which refers toFormats -
WRAPPEDCLASS_TakeAlternativeException
-
WRAPPEDCLASS_MapoidFormatter
For convenience of derived compilers only. -
WRAPPEDCLASS_MapEntry
For convenience of derived compilers only. -
WRAPPEDCLASS_ListoidFormatter
For convenience of derived compilers only. -
WRAPPEDCLASS_TryerCatcher
-
WRAPPEDCLASS_CompoundConstructor
-
NAME_METHOD_THROW_TAKE_ALTERNATIVE
Name of the method generated for convenience for throwing aTakeAlternativeException.- See Also:
-
callFunctionForRaisingException
format for calling the method which throwsTakeAlternativeException, cfNAME_METHOD_THROW_TAKE_ALTERNATIVE. -
NAME_VAR_DEFAULT_INDENT
name of the generated field which holds the default idententation, normally "default_indent". -
NAME_VAR_MODE
name of the generated field which holds the current mode, normally "mode". -
NAME_CONST_FORMAT_EMPTY
name of the generated field which holds an empty format, normally "format_empty". -
MODIF_CONST
protected static final int MODIF_CONSTmodifier used for all fields generated to hold constant format expressions (= "protected static final").- See Also:
-
NAME_VAR_FORMAT_RESULT
name of the generated field which holds the resulting format. such a field is NOT necesarry and not created iff the generated code (including facade code !) can be fully functional. -
exprCache
-
rec
Needed ONLY because ".getReceiver()" is of type "? super M", which does not wokr. -
targetclass
class into which to generate all const field definitions. -
isdynamic
protected boolean isdynamicindicates whether the code currently contained infresultdoes generate formats "dynamically" (which implies that its result can NOT be stored in a constant field). -
checkedmode
protected boolean checkedmodeare we currently visiting left part of a "?" operator ? -
fresult
accumulator for format generating functional code. (of course coded in form of a format !-). -
constcache
Maps "fresult" contents (=unformatted java code, used as a key) to the name of the constant fields which will hold the code's result.
-
-
Constructor Details
-
GenericCompiler
-
-
Method Details
-
setMsgReceiver
-
getMsgReceiver
-
getCurrentModel
Currently only needed to compileescapeToJava()in the variant operating directly on the model element. -
refer
-
expr
-
warning
-
error
-
initTransformerClass
Creates standard fields for the generated format generating class. (Like "default indent", "result" as intermediate storage, a cache of the empty format, a method to throw the "take alternative" exception, etc.) Must be called. This is ensured by the fieldtargetclass, which otherwise stays =null. -
create_resultfield
creates a field "Format result=Format.empty;" for formatting wisitors which need such a temp storage (ie. are not purely functinal code).
Currently used by UMod2format and Tdom2format. -
compileOnePattern
public void compileOnePattern(eu.bandm.tools.formatfrontends.absy.Element_pattern el) called from derived code for translating one(1) rule. (1) initialize registers, (2) callvisit(el)and (3) leave result infresult. after return, the contents of fresult (=format representing format-generating code) must be stored to somewhere by the calling code! -
checkconst
protected void checkconst() -
process_compound
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_beneath element) - Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_beside element) - Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_line element) - Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_block element) - Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_append element) - Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_tabular element) - Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
compileModeSwitch
protected void compileModeSwitch(eu.bandm.tools.formatfrontends.absy.Element_cases el) Compiles a switch statement with$modeas its selector. Defaults to the empty format. -
compileCallToFormatText
compiles a call toFormats.text(String), if model != null. otherwise an exception or an empty format, if not left of a "?". -
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_alternative element) Compiles the cascade of alternatives/catch clauses from the front end representation...? ... ? ... ? ....
<!ELEMENT alternative (beneath, catchcase?) >
<!ELEMENT catchcase (alternative?) >- Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_nullException el) Creates code for the$throwelement. This causes explicit failure, i.e. throwing of an instance ofTakeAlternativeException. This is sensible when eg. done out of one (or some) of the cases of a$switchstatement. It may only be used to the left side of a?operator, which results in the corresponding right side to be executed.- Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_prior el) <!ELEMENT prior (number, (number | appendOp)?, pattern, pattern) >It directly maps its arguments to a call ofFormat.prior(int, int, eu.bandm.tools.format.Format, eu.bandm.tools.format.Format).- Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_literal el) Creates code for one literal, which might also be empty. Consequently, is compiles toFormat.literal(String)or toFormat.empty- Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_whitespace el) Creates code for whitespace. The width of the input is preserved in the generated format. Creates a call toFormat.space(int)- Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_escapeToJava element) Creates code for a java escape.
The syntax in the front end language is "$java 'my.package.Class.method'(formatsAsArgument..)". or "$java 'my.package.Class.method'". The first argument must point to some static function from Format to Format. If there is a second argument, it is a "block" construct of formats which are arguments to the function. Otherwise the current model element is the only argument.
Attention: "$java" is now one level higher than "atom"; can be used as an instance of "atom" simply by additional parentheses "($java x.y(..))"- Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_indent element) creates code for indentation.
Supports two variants: with inline constant, or without, reading a runtime variable for default indentation. The name of this variable is defined byNAME_VAR_DEFAULT_INDENT- Overrides:
visitin classeu.bandm.tools.formatfrontends.absy.Visitor
-