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.Subformat
can 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
Modifier and TypeClassDescriptionprotected class
A 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
Modifier and TypeFieldDescriptionfinal Format
format for calling the method which throwsTakeAlternativeException
, cfNAME_METHOD_THROW_TAKE_ALTERNATIVE
.protected boolean
are 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.protected Format
accumulator for format generating functional code.protected boolean
indicates whether the code currently contained infresult
does generate formats "dynamically" (which implies that its result can NOT be stored in a constant field).protected static final int
modifier used for all fields generated to hold constant format expressions (= "protected static final").protected String
name of the generated field which holds an empty format, normally "format_empty
".final String
name of the method generated for convenience for throwingTakeAlternativeException
.protected String
name of the generated field which holds the default idententation, normally "default_indent
".protected String
name of the generated field which holds the resulting format.protected String
name 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 Format
The format which refers toFormat
static final Format
The format which refers toFormats
protected GeneratedClass
class into which to generate all const field definitions.static final EnvironmentClass
static final EnvironmentClass
static final EnvironmentClass
Refers toeu.bandm.tools.ops
, and is for convenience of derived compilers only.static final EnvironmentClass
For convenience of derived compilers only.static final EnvironmentClass
Attention, refers NOT to java.util, but toeu.bandm.tools.ops
, and is for convenience of derived compilers only.static final EnvironmentClass
For convenience of derived compilers only.static final EnvironmentClass
For convenience of derived compilers only.static final EnvironmentClass
static final EnvironmentClass
static final EnvironmentClass
Fields inherited from class eu.bandm.tools.tdom.runtime.BaseVisitor
validating
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
protected void
compileCallToFormatText
(Format model) compiles a call toFormats.text(String)
, if model != null.protected void
compileModeSwitch
(eu.bandm.tools.formatfrontends.absy.Element_cases el) Compiles a switch statement with$mode
as its selector.void
compileOnePattern
(eu.bandm.tools.formatfrontends.absy.Element_pattern el) called from derived code for translating one(1) rule.void
creates a field "Format result=Format.empty;
" for formatting wisitors which need such a temp storage (ie.protected void
error
(Location<XMLDocumentIdentifier> loc, String text) protected Format
protected abstract Format
Currently only needed to compileescapeToJava()
in the variant operating directly on the model element.protected MessageReceiver<SimpleMessage<XMLDocumentIdentifier>>
void
Creates standard fields for the generated format generating class.protected void
process_compound
(eu.bandm.tools.formatfrontends.absy.Element[] subs, String constr) protected Format
protected void
void
visit
(eu.bandm.tools.formatfrontends.absy.Element_alternative element) Compiles the cascade of alternatives/catch clauses from the front end representation...? ... ? ... ? ...
.void
visit
(eu.bandm.tools.formatfrontends.absy.Element_append element) void
visit
(eu.bandm.tools.formatfrontends.absy.Element_beneath element) void
visit
(eu.bandm.tools.formatfrontends.absy.Element_beside element) void
visit
(eu.bandm.tools.formatfrontends.absy.Element_block element) void
visit
(eu.bandm.tools.formatfrontends.absy.Element_escapeToJava element) Creates code for a java escape.void
visit
(eu.bandm.tools.formatfrontends.absy.Element_indent element) creates code for indentation.void
visit
(eu.bandm.tools.formatfrontends.absy.Element_line element) void
visit
(eu.bandm.tools.formatfrontends.absy.Element_literal el) Creates code for one literal, which might also be empty.void
visit
(eu.bandm.tools.formatfrontends.absy.Element_nullException el) Creates code for the$throw
element.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)
.void
visit
(eu.bandm.tools.formatfrontends.absy.Element_tabular element) void
visit
(eu.bandm.tools.formatfrontends.absy.Element_whitespace el) Creates code for whitespace.protected void
warning
(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, visit
Methods inherited from class eu.bandm.tools.tdom.runtime.BaseVisitor
visit
-
Field Details
-
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 throwingTakeAlternativeException
.- 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 infresult
does 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. Introduced 20170428. -
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:
visit
in classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_beside element) - Overrides:
visit
in classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_line element) - Overrides:
visit
in classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_block element) - Overrides:
visit
in classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_append element) - Overrides:
visit
in classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_tabular element) - Overrides:
visit
in classeu.bandm.tools.formatfrontends.absy.Visitor
-
compileModeSwitch
protected void compileModeSwitch(eu.bandm.tools.formatfrontends.absy.Element_cases el) Compiles a switch statement with$mode
as 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:
visit
in classeu.bandm.tools.formatfrontends.absy.Visitor
-
visit
public void visit(eu.bandm.tools.formatfrontends.absy.Element_nullException el) Creates code for the$throw
element. 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$switch
statement. It may only be used to the left side of a?
operator, which results in the corresponding right side to be executed.- Overrides:
visit
in 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:
visit
in 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:
visit
in 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:
visit
in 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:
visit
in 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:
visit
in classeu.bandm.tools.formatfrontends.absy.Visitor
-