0

所以我有以下 XText 语法:

grammar org.xtext.example.hyrule.HyRule with org.eclipse.xtext.xbase.Xbase

generate hyRule "http://www.xtext.org/example/hyrule/HyRule"


Start:
    rules+=Rule+;

Rule:
    constantDecs+= XVariableDeclaration*
    elementDecs+=elementDec+
    'PAYLOAD' payload=PAYLOAD 'CONSTRAINT' expression= XExpression
;

elementDec:
    variable=FullJvmFormalParameter '=' xpath=XPATH
;




PAYLOAD:
    "Stacons" |  "any" | "sse";



//we override the definition in Xtype.xtext, so that we can have // in Xpaths
terminal SL_COMMENT:
    '#' !('\n' | '\r')* ('\r'? '\n')?;

terminal XPATH: 
    (('//'|'/')('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'_'|':'|'0'..'9')*)+

    ;

我正在使用 JvmModelInferrer 来推断每个规则的方法。推断器代码的相关部分是:

def dispatch void infer(Start start, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {

        for (rule: start.rules) {
            acceptor.accept(rule.toClass("Rule" + counter)).initializeLater[
                ...
                // method used to check the rule this class represents 
                members += rule.toMethod("checkRule",rule.newTypeRef('boolean'))[
                    for (e : rule.elementDecs) {
                        parameters += e.variable.toParameter(e.variable.name, e.variable.parameterType)
                    }
                    setStatic(true)
                    body = expression
                ]

我不知道该怎么做是让常量decal中的变量声明的值对表达式可见,更具体地说,如何为包含这些值的表达式生成代码。目前,变量声明在生成代码的范围内,但它们的值被声明为它们的名称。例如输入:

val j = Integer::parseInt('199')
int x = //val/v
PAYLOAD Stacons CONSTRAINT x>j

结果生成的方法:

public static boolean checkRule(final int x) {
    int _j = j;
    boolean _greaterThan = (x > _j);
    return _greaterThan;
  }

而我希望它生成方法:

public static boolean checkRule(final int x) {
        int _j = 199;
        boolean _greaterThan = (x > _j);
        return _greaterThan;
      }

我的范围提供程序如下所示:

@Inject
    IJvmModelAssociations associations;

    @Override
    protected IScope createLocalVarScopeForJvmOperation(JvmOperation context, IScope parentScope) {
        parentScope = super.createLocalVarScopeForJvmOperation(context, parentScope);

        // retrieve the AST element associated to the method
        // created by our model inferrer
        EObject sourceElement = associations.getPrimarySourceElement(context);
        if (sourceElement instanceof Rule) {
            Rule rule = (Rule) sourceElement;
            return Scopes.scopeFor(rule.getConstantDecs(), parentScope);
        }

        return parentScope;

我曾尝试摆弄范围和推断器,但无济于事。我正在尝试做的事情可能吗?

4

1 回答 1

2

基本上有两种选择:

  1. 您可以为模型中的每个 constantDecl 推断 JvmType 中的常量。
  2. 您必须自定义范围、代码生成和验证等各种事情,以便将常量值内联到您的表达式中。

由于我觉得(2)太糟糕了,我建议去(1)。

acceptor.accept(rule.toClass("Rule" + counter)).initializeLater[
  ..
  for(varDecl : constantDecs) {
    switch(varDecl) {
     XVariableDeclaration: members += varDecl.toField(varDecl.name, varDecl.type) [
       initializer = varDecl.right
       setStatic(true)
       setFinal(true)
     ]
    }

  }
  // method used to check the rule this class represents 
  members += rule.toMethod("checkRule",rule.newTypeRef('boolean'))[
    for (e : rule.elementDecs) {
      parameters += e.variable.toParameter(e.variable.name, e.variable.parameterType) 
    }
    setStatic(true)
    body = expression
  ]
]
于 2012-09-10T20:37:53.413 回答