2014-10-08 23 views
0

我在写一个Xtext语法,它可以访问在函数之前声明的文档。Xtext语法mixin导致Guice注入错误

我们目前的语法定义hidden(ML_COMMENT, SL_COMMENT,...)有:

ML_COMMENT: '/*' -> '*/' 
SL_COMMENT: '//' -> EOL 

我现在已经创建了一个第二的Xtext项目,用下面的语法:

grammar my.DocumentationGrammar with my.OriginalGrammar hidden(WS, FUNCTION_BODY, EOL, SL_COMMENT) 

import "http://www.originalGrammar.my" 

generate documentationGrammar "http://www.documentationGrammar.my" 

/* Parser rules */ 
TranslationUnit: 
    eds+=DoxExternalDefinition* 
; 

DoxExternalDefinition: 
    def = Definition 
    | lib = CtrlLibUsage 
    | comment=ML_COMMENT 
; 

FunctionDefinition: 
    aml=AccessModifiersList ts=TypeSpecifier? f=Function '(' pl=ParameterTypeList? ')' /* cs=CompoundStatement */ // the compound statement is ignored 
; 

//terminal DOXYGEN_COMMENT: ML_COMMENT; 
terminal FUNCTION_BODY: '{' -> '}'; 

我已经创建的插件的依赖,并将此到

bean = StandaloneSetup { 
    scanClassPath = true 
    platformUri = "${runtimeProject}/.." 
    // The following two lines can be removed, if Xbase is not used. 
    registerGeneratedEPackage = "org.eclipse.xtext.xbase.XbasePackage" 
    registerGenModelFile = "platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel" 

    // we need to register the super genmodel 
    registerGeneratedEPackage = "my.OriginalGrammar.OriginalGrammarPackage" 
    registerGenModelFile = "platform:/resource/my.OriginalGrammar/model/generated/OriginalGrammar.genmodel" 
} 

现在,在我的第三个插件p roject,我想以独立的方式访问这个解析器。所以,我创建了下面的解析器文件(基于这个例子:http://davehofmann.de/blog/?p=101):

import java.io.IOException; 
import java.io.Reader; 
import java.nio.file.Path; 

import org.eclipse.emf.common.util.URI; 
import org.eclipse.emf.ecore.EObject; 
import org.eclipse.emf.ecore.resource.Resource; 
import org.eclipse.xtext.parser.IParseResult; 
import org.eclipse.xtext.parser.IParser; 
import org.eclipse.xtext.parser.ParseException; 
import org.eclipse.xtext.resource.XtextResource; 
import org.eclipse.xtext.resource.XtextResourceSet; 

import my.DocumentationGrammar.DocumentationGrammarStandaloneSetup; 

import com.google.inject.Inject; 
import com.google.inject.Injector; 

public class DoxygenParser { 

    @Inject 
    private IParser parser; 
    private Injector injector; 

    public DoxygenParser() { 
     setupParser(); 
    } 

    private void setupParser() {  
     injector = new DocumentationGrammarStandaloneSetup().createInjectorAndDoEMFRegistration(); 
     injector.injectMembers(this); 

    } 

    /** 
    * Parses data provided by an input reader using Xtext and returns the root node of the resulting object tree. 
    * @param reader Input reader 
    * @return root object node 
    * @throws IOException when errors occur during the parsing process 
    */ 
    public EObject parse(Reader reader) throws IOException 
    { 
     IParseResult result = parser.parse(reader); 
     if(result.hasSyntaxErrors()) 
     { 
      throw new ParseException("Provided input contains syntax errors."); 
     } 
     return result.getRootASTElement(); 
    } 
} 

然而,当我尝试运行它,我收到吉斯注入错误说

com.google.inject.ProvisionException: Guice provision errors: 
1) Error injecting constructor, org.eclipse.emf.common.util.WrappedException: java.lang.RuntimeException: Cannot create a resource for 'classpath:/my/documentationGrammar/DocumentationGrammar.xtextbin'; a registered resource factory is needed 

我知道,解析器“应该”是正确的,因为当我使用OriginalGrammarStandaloneSetup时它工作得很好。

回答

2

您必须确保您的子语言也能调用您的超级语言的独立设置。通常这是在DocumentationGrammarStandaloneSetupGenerated类中生成的,但请确保这是由Xtext正确设置的。最后,应该有这样的东西

if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("xtextbin")) 
    Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
       "xtextbin", new BinaryGrammarResourceFactoryImpl()); 

在你的设置的方法的callchain。