0

I am trying to extend ANTLR4 with a new PHP target and have a problem with the StringTemplate file (.stg):

In the StringTemplate file the Parser is defined by the template Parser_. Parser_ contains the following rule :

    <funcs; separator="\n">

This rule emits class definitions and functions. The rule is located inside the parser class, but PHP does not support nested classes.

Is there a possibility to divide between class definitions and functions in the StringTemplate file? I want to set the class definitions of the funcs template before the parser class and the functions of the funcs template inside the parser class.

The whole parser-Template :

Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass) ::= <<    

// funcs beinhaltet die Klasse und dann die Funktion

class <parser.name> extends <if(superClass)><superClass><else>\antlr4\php7_runtime\Parser<endif>    {

    public $grammarFileName = "<parser.grammarFileName>";
    public $atn = null;
    public $decisionsToDFA = array( );
    public $sharedContextCache = null;


    public $literalNames = [ <parser.literalNames:{t | <t>}; null="'\<INVALID>'", separator=", ", wrap, anchor> ];

    public $symbolicNames = [ <parser.symbolicNames:{t | <t>}; null="'\<INVALID>'", separator=", ", wrap, anchor> ];

    <parser.rules:{r | const RULE_<r.name> = <r.index>;}; separator="\n", wrap, anchor>

    public $ruleNames =  [ <parser.ruleNames:{r | "<r>"}; separator=", ", wrap, anchor> ];

    public $EOF = <PathRuntime()><TokenLabelType()>::EOF;

    <if(parser.tokens)>
    <parser.tokens:{k | const <k>=<parser.tokens.(k)>;}; separator="\n", wrap, anchor>
    <endif>    

    <atn>    

    <parser:(ctor)()>

    <namedActions.members>

// funcs -- Start
    <funcs; separator="\n">
// funcs -- Ende        

} // class <parser.name>    



<if(sempredFuncs)>
    function sempred( $localctx, int $ruleIndex, int $predIndex){
        if ($this->_predicates == null) {
            $this->_predicates = py_dict();
        }
<parser.sempredFuncs.values:{ f |
        $this->predicates[<f.ruleIndex>] = $this-><f.name>_sempred}; separator="\n">
        $pred = $this->_predicates->get($ruleIndex, null);
        if ( $pred == null) {
            throw Exception("No predicate with index:" . (string) $ruleIndex );
        } else {
            return pred( $localctx, $predIndex)
        }


    <sempredFuncs.values; separator="\n">
    }
<endif>

>>

Thank you for your time!

1 Answers1

0

The solution was to walk the parser.funcs.ruleCtx and to exclude from the RuleFunction template :-)

Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass ) ::= <<

<funcs :{ func | <func.ruleCtx> }; separator="\n"> // here the Comtext classes

class <parser.name> extends <if(superClass)><superClass><else>\antlr4\php7_runtime\Parser<endif>    {

    public $grammarFileName = "<parser.grammarFileName>";
    public $atn = null;
    public $decisionsToDFA = array( );
    public $sharedContextCache = null;


    public $literalNames = [ <parser.literalNames:{t | <t>}; null="'\<INVALID>'", separator=", ", wrap, anchor> ];

    public $symbolicNames = [ <parser.symbolicNames:{t | <t>}; null="'\<INVALID>'", separator=", ", wrap, anchor> ];

    <parser.rules:{r | const RULE_<r.name> = <r.index>;}; separator="\n", wrap, anchor>

    public $ruleNames =  [ <parser.ruleNames:{r | "<r>"}; separator=", ", wrap, anchor> ];

    public $EOF = <PathRuntime()><TokenLabelType()>::EOF;

    <if(parser.tokens)>
    <parser.tokens:{k | const <k>=<parser.tokens.(k)>;}; separator="\n", wrap, anchor>
    <endif>    


<atn>    


    <parser:(ctor)()>

   <funcs; separator="\n"> // here RuleFunction is emitted


} // class <parser.name>    


<namedActions.members>

<if(sempredFuncs)>
    function sempred( $localctx, int $ruleIndex, int $predIndex){
        if ($this->_predicates == null) {
            $this->_predicates = py_dict();
        }
<parser.sempredFuncs.values:{ f |
        $this->predicates[<f.ruleIndex>] = $this-><f.name>_sempred}; separator="\n">
        $pred = $this->_predicates->get($ruleIndex, null);
        if ( $pred == null) {
            throw Exception("No predicate with index:" . (string) $ruleIndex );
        } else {
            return pred( $localctx, $predIndex)
        }


    <sempredFuncs.values; separator="\n">
    }
<endif>
>>