Class 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:
    1. It has to construct some target class (with the appropriate base class, specific fields, interface methods, etc)
    2. It has to call initTransformerClass(GeneratedClass) on this class.
    3. It has to define the visit() methods for all the specific language constructs.
    4. It has to call compileOnePattern(Element_pattern) for each expression it wants to translate. This method intializes internal registers, calls visit(), and finally relocates all constant expressions to initializer blocks.
      The format result (which contains code to construct the required format) is left in fresult, The caller has to store this code in the generated class according to its need.
    5. 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,
    6. 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.
    • Field Detail

      • WRAPPEDCLASS_Lists

        public static final EnvironmentClass WRAPPEDCLASS_Lists
        Attention, refers NOT to java.util, but to eu.bandm.tools.ops, and is for convenience of derived compilers only.
      • REF_FORMAT_Format

        public static final Format REF_FORMAT_Format
        The format which refers to 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 throwing TakeAlternativeException.
        See Also:
        Constant Field Values
      • 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
      • targetclass

        protected GeneratedClass targetclass
        class into which to generate all const field definitions.
      • isdynamic

        protected boolean isdynamic
        indicates whether the code currently contained in fresult 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.
    • Method Detail

      • getCurrentModel

        protected abstract Format getCurrentModel()
        Currently only needed to compile escapeToJava() in the variant operating directly on the model element. Introduced 20170428.
      • expr

        protected Format expr​(java.lang.String code)
      • 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 field targetclass, 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) call visit(el) and (3) leave result in fresult. after return, the contents of fresult (=format representing format-generating code) must be stored to somewhere by the calling code!
      • checkconst

        protected void checkconst()
        Whenever possible, store the code from fresult into an initializer of a new constant, and replace fresult by a reference to this (or to an already existing constant).
      • 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 class eu.bandm.tools.formatfrontends.absy.Visitor
      • visit

        public void visit​(eu.bandm.tools.formatfrontends.absy.Element_beside element)
        Overrides:
        visit in class eu.bandm.tools.formatfrontends.absy.Visitor
      • visit

        public void visit​(eu.bandm.tools.formatfrontends.absy.Element_line element)
        Overrides:
        visit in class eu.bandm.tools.formatfrontends.absy.Visitor
      • visit

        public void visit​(eu.bandm.tools.formatfrontends.absy.Element_block element)
        Overrides:
        visit in class eu.bandm.tools.formatfrontends.absy.Visitor
      • visit

        public void visit​(eu.bandm.tools.formatfrontends.absy.Element_append element)
        Overrides:
        visit in class eu.bandm.tools.formatfrontends.absy.Visitor
      • visit

        public void visit​(eu.bandm.tools.formatfrontends.absy.Element_tabular element)
        Overrides:
        visit in class eu.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 to Format.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 class eu.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 of TakeAlternativeException. 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 class eu.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 to Format.literal(String) or to Format.empty
        Overrides:
        visit in class eu.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 to Format.space(int)
        Overrides:
        visit in class eu.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 class eu.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 by NAME_VAR_DEFAULT_INDENT
        Overrides:
        visit in class eu.bandm.tools.formatfrontends.absy.Visitor