我正在编写一个Xtext语法,可以访问在函数之前声明的文档。
我们当前的语法定义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"
}
现在,在我的第三个插件项目中,我希望以独立方式访问此解析器。所以我创建了以下Parser文件(基于此示例: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();
}
}
然而,当我尝试运行它时,我收到Guice Injection错误,说
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时,它完全正常。
答案 0 :(得分:2)
您必须确保您的子语言还会调用您的超级语言的独立设置。通常这是在DocumentationGrammarStandaloneSetupGenerated
类中生成的,但请确保这是由Xtext正确设置的。最后,应该有像
if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("xtextbin"))
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"xtextbin", new BinaryGrammarResourceFactoryImpl());
在您设置的createInjectorAndDoEMFRegistration
方法的密码链中。