ANTLR4 - 没有嵌套类的目标

时间:2017-11-12 21:38:45

标签: antlr4 stringtemplate-4

我正在尝试使用新的PHP目标扩展ANTLR4并且遇到StringTemplate文件(.stg)的问题:

在StringTemplate文件中,Parser由模板Parser_定义。 Parser_包含以下规则:

submitHandler(inputValue, inputIdentifier) {
    console.log('inside the submitHandler');

    var newArray = [];
    newArray.push({
        value: inputValue,
        identificer: inputIdentifier.item
    });
    this.setState({ newArray: newArray })

    let dataStorage = [newArray, ...this.state.dictionaryForAddedItems]

    this.setState({ dataStorage: dataStorage }, () => {

        console.log('her is was inside the newArray' + JSON.stringify(newArray));
        console.log('her is was inside the state.dictionaryForAddedItems' + JSON.stringify(this.state.dataStorage));
    });
}

此规则发出类定义和函数。规则位于解析器类中,但PHP不支持嵌套类。

是否有可能在StringTemplate文件中划分类定义和函数?我想在解析器类之前设置funcs模板的类定义,在解析器类中设置funcs模板的函数。

整个解析器 - 模板:

    <funcs; separator="\n">

感谢您的时间!

1 个答案:

答案 0 :(得分:0)

解决方案是遍历parser.funcs.ruleCtx并从RuleFunction模板中排除: - )

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>
>>