简体   繁体   中英

Xtext - No viable alternative at input

i'm trying to create a grammar that join togheter a script language with the possibility to create method.

Grammar

grammar org.example.domainmodel.Domainmodel with org.eclipse.xtext.xbase.Xbase

generate domainmodel "http://www.example.org/domainmodel/Domainmodel"

import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase


Model:
    imports = XImportSection
    methods += XMethodDeclaration*
    body = XBlockScriptLanguage;

XMethodDeclaration:
    "def" type=JvmTypeReference name=ValidID 
    '('(params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'
        body=XBlockExpression
;

XBlockScriptLanguage returns xbase::XExpression:
    {xbase::XBlockExpression}
        (expressions+=XExpressionOrVarDeclaration ';'?)*
;

At the moment i create the following JvmModelInferr, for defining the main method for scripting language.

JvmModelInferr

def dispatch void infer(Model model, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
        acceptor.accept(
            model.toClass("myclass")
        ).initializeLater [
                members += model.toMethod("main", model.newTypeRef(Void::TYPE)) [                       
                    parameters += model.toParameter("args", model.newTypeRef(typeof(String)).addArrayTypeDimension)
                    setStatic(true)
                    body = model.body

                    ]
            ]
    }  

When i tryed to use my grammar, i obtain the following error after that i wrote my method:

  • no viable alternative at input 'def'
  • The method mymethod() is undefined

The problem is related only with method declaration, without it myclass.java is created. Moreover i obtain the "Warning 200" for a not clear grammar, why?

There are two fixes that appear necessary:

  1. The imports section is not marked as optional. If it was intended to be optional, it should be declared as imports ?= XImportSection . Or, add necessary import statements to your JvmModelInferr example.

  2. The dispatch keyword isn't defined in your grammar. As defined, a method should consist of def , followed by a Java type (the return type), and then the method's name (then the body, etc.). You could add `(dispatch ?= 'dispatch') if you're targeting Xtend and intend to support its multiple dispatch feature (or your own version of it).

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM