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:- 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
Nested Classes Modifier and Type Class Description protected class
GenericCompiler.Subformat
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
Fields Modifier and Type Field Description Format
callFunctionForRaisingException
format for calling the method which throwsTakeAlternativeException
, cfNAME_METHOD_THROW_TAKE_ALTERNATIVE
.protected boolean
checkedmode
are we currently visiting left part of a "?" operator ?protected java.util.Map<java.lang.String,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.protected java.util.Map<java.lang.String,Format>
exprCache
protected Format
fresult
accumulator for format generating functional code.protected boolean
isdynamic
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 int
MODIF_CONST
modifier used for all fields generated to hold constant format expressions (= "protected static final").protected java.lang.String
NAME_CONST_FORMAT_EMPTY
name of the generated field which holds an empty format, normally "format_empty
".java.lang.String
NAME_METHOD_THROW_TAKE_ALTERNATIVE
name of the method generated for convenience for throwingTakeAlternativeException
.protected java.lang.String
NAME_VAR_DEFAULT_INDENT
name of the generated field which holds the default idententation, normally "default_indent
".protected java.lang.String
NAME_VAR_FORMAT_RESULT
name of the generated field which holds the resulting format.protected java.lang.String
NAME_VAR_MODE
name of the generated field which holds the current mode, normally "mode
".protected MessageReceiver<SimpleMessage<XMLDocumentIdentifier>>
rec
Needed ONLY because ".getReceiver()" is of type "? super M", which does not wokr.static Format
REF_FORMAT_Format
The format which refers toFormat
protected GeneratedClass
targetclass
class into which to generate all const field definitions.static EnvironmentClass
WRAPPEDCLASS_CompoundConstructor
static EnvironmentClass
WRAPPEDCLASS_Format
static EnvironmentClass
WRAPPEDCLASS_Function
Refers toeu.bandm.tools.ops
, and is for convenience of derived compilers only.static EnvironmentClass
WRAPPEDCLASS_ListoidFormatter
For convenience of derived compilers only.static EnvironmentClass
WRAPPEDCLASS_Lists
Attention, refers NOT to java.util, but toeu.bandm.tools.ops
, and is for convenience of derived compilers only.static EnvironmentClass
WRAPPEDCLASS_MapEntry
For convenience of derived compilers only.static EnvironmentClass
WRAPPEDCLASS_MapoidFormatter
For convenience of derived compilers only.static EnvironmentClass
WRAPPEDCLASS_Object
static EnvironmentClass
WRAPPEDCLASS_TakeAlternativeException
static EnvironmentClass
WRAPPEDCLASS_TryerCatcher
-
Fields inherited from class eu.bandm.tools.tdom.runtime.BaseVisitor
validating
-
-
Constructor Summary
Constructors Constructor Description GenericCompiler(MessageReceiver<SimpleMessage<XMLDocumentIdentifier>> rec)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
checkconst()
protected void
compileCallToFormatText(Format model)
compiles a call toFormat.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
create_resultfield(GeneratedClass gc)
creates a field "Format result=Format.empty;
" for formatting wisitors which need such a temp storage (ie.protected void
error(Location<XMLDocumentIdentifier> loc, java.lang.String text)
protected Format
expr(java.lang.String code)
protected abstract Format
getCurrentModel()
Currently only needed to compileescapeToJava()
in the variant operating directly on the model element.protected MessageReceiver<SimpleMessage<XMLDocumentIdentifier>>
getMsgReceiver()
void
initTransformerClass(GeneratedClass gc)
Creates standard fields for the generated format generating class.protected void
process_compound(eu.bandm.tools.formatfrontends.absy.Element[] subs, java.lang.String constr)
protected Format
refer(CompoundConstructor c)
protected void
setMsgReceiver(MessageReceiver<SimpleMessage<XMLDocumentIdentifier>> rec)
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, java.lang.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 Detail
-
WRAPPEDCLASS_Object
public static final EnvironmentClass WRAPPEDCLASS_Object
-
WRAPPEDCLASS_Lists
public static final EnvironmentClass WRAPPEDCLASS_Lists
Attention, refers NOT to java.util, but toeu.bandm.tools.ops
, and is for convenience of derived compilers only.
-
WRAPPEDCLASS_Function
public static final EnvironmentClass WRAPPEDCLASS_Function
Refers toeu.bandm.tools.ops
, and is for convenience of derived compilers only.
-
WRAPPEDCLASS_Format
public static final EnvironmentClass WRAPPEDCLASS_Format
-
WRAPPEDCLASS_TakeAlternativeException
public static final EnvironmentClass WRAPPEDCLASS_TakeAlternativeException
-
WRAPPEDCLASS_MapoidFormatter
public static final EnvironmentClass WRAPPEDCLASS_MapoidFormatter
For convenience of derived compilers only.
-
WRAPPEDCLASS_MapEntry
public static final EnvironmentClass WRAPPEDCLASS_MapEntry
For convenience of derived compilers only.
-
WRAPPEDCLASS_ListoidFormatter
public static final EnvironmentClass WRAPPEDCLASS_ListoidFormatter
For convenience of derived compilers only.
-
WRAPPEDCLASS_TryerCatcher
public static final EnvironmentClass WRAPPEDCLASS_TryerCatcher
-
WRAPPEDCLASS_CompoundConstructor
public static final EnvironmentClass WRAPPEDCLASS_CompoundConstructor
-
NAME_METHOD_THROW_TAKE_ALTERNATIVE
public final java.lang.String NAME_METHOD_THROW_TAKE_ALTERNATIVE
name of the method generated for convenience for throwingTakeAlternativeException
.- See Also:
- Constant Field Values
-
callFunctionForRaisingException
public final Format callFunctionForRaisingException
format for calling the method which throwsTakeAlternativeException
, cfNAME_METHOD_THROW_TAKE_ALTERNATIVE
.
-
NAME_VAR_DEFAULT_INDENT
protected java.lang.String NAME_VAR_DEFAULT_INDENT
name of the generated field which holds the default idententation, normally "default_indent
".
-
NAME_VAR_MODE
protected java.lang.String NAME_VAR_MODE
name of the generated field which holds the current mode, normally "mode
".
-
NAME_CONST_FORMAT_EMPTY
protected java.lang.String NAME_CONST_FORMAT_EMPTY
name of the generated field which holds an empty format, normally "format_empty
".
-
MODIF_CONST
protected static final int MODIF_CONST
modifier used for all fields generated to hold constant format expressions (= "protected static final").- See Also:
- Constant Field Values
-
NAME_VAR_FORMAT_RESULT
protected java.lang.String 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
protected java.util.Map<java.lang.String,Format> exprCache
-
rec
protected MessageReceiver<SimpleMessage<XMLDocumentIdentifier>> rec
Needed ONLY because ".getReceiver()" is of type "? super M", which does not wokr.
-
targetclass
protected GeneratedClass targetclass
class into which to generate all const field definitions.
-
isdynamic
protected boolean isdynamic
indicates 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 checkedmode
are we currently visiting left part of a "?" operator ?
-
fresult
protected Format fresult
accumulator for format generating functional code. (of course coded in form of a format !-).
-
constcache
protected final java.util.Map<java.lang.String,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 Detail
-
GenericCompiler
public GenericCompiler(MessageReceiver<SimpleMessage<XMLDocumentIdentifier>> rec)
-
-
Method Detail
-
setMsgReceiver
protected void setMsgReceiver(MessageReceiver<SimpleMessage<XMLDocumentIdentifier>> rec)
-
getMsgReceiver
protected MessageReceiver<SimpleMessage<XMLDocumentIdentifier>> getMsgReceiver()
-
getCurrentModel
protected abstract Format getCurrentModel()
Currently only needed to compileescapeToJava()
in the variant operating directly on the model element. Introduced 20170428.
-
refer
protected Format refer(CompoundConstructor c)
-
expr
protected Format expr(java.lang.String code)
-
warning
protected void warning(Location<XMLDocumentIdentifier> loc, java.lang.String text)
-
error
protected void error(Location<XMLDocumentIdentifier> loc, java.lang.String text)
-
initTransformerClass
public void initTransformerClass(GeneratedClass gc)
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
public void create_resultfield(GeneratedClass gc)
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
protected void process_compound(eu.bandm.tools.formatfrontends.absy.Element[] subs, java.lang.String constr)
-
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
protected void compileCallToFormatText(Format model)
compiles a call toFormat.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...? ... ? ... ? ...
.
- 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
-
-