XText中的自定义终端

时间:2014-11-10 18:07:00

标签: xtext

我最近开始学习XText并尝试做一件简单的事情 - 拥有自定义终端定义而不是导入org.eclipse.xtext.common.Terminals

首先,我在x.mvmn.blah.xtext包中通过向导创建了BlahLang.xtest。

然后我检查了教程并做了两件事: a)在我的xtext文件所在的同一个包中创建了自定义终端文件。 即x.mvmn.blah.xtext中的Test.xtext。

该文件的内容与Terminals.xtext中的内容相同,但更正了包声明:

grammar x.mvmn.blah.xtext.Test hidden(WS, ML_COMMENT, SL_COMMENT)

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

terminal ID:
    '^'? ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9')*;

terminal INT returns ecore::EInt:
    ('0'..'9')+;

terminal STRING:
    '"' ('\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\' | '"'))* '"' |
    "'" ('\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\' | "'"))* "'";

terminal ML_COMMENT:
    '/*'->'*/';

terminal SL_COMMENT:
    '//' !('\n' | '\r')* ('\r'? '\n')?;

terminal WS:
    (' ' | '\t' | '\r' | '\n')+;

terminal ANY_OTHER:
    .;

b)在我的BlahLang.xtext文件中,我通过'with'声明导入了该文件,而不是由向导放置在那里的org.eclipse.xtext.common.Terminals

grammar x.mvmn.blah.xtext.BlahLang with x.mvmn.blah.xtext.Test

generate blahLang "http://www.mvmn.x/blah/xtext/BlahLang"

Model:
    greetings+=Greeting*;

Greeting:
    'Hello' name=ID '!';

根据我在网上找到的所有文档和提示,一切都应该没问题。但没有任何作用 - XText生成的文件引用了不存在的类:

x.mvmn.blah.xtext.services.TestGrammarAccess
x.mvmn.blah.xtext.TestStandaloneSetup
x.mvmn.blah.xtext.validation.TestValidator

我尝试了不同的解决方法(在Test.xtext等中添加“generate”),但没有任何效果。我几乎陷入困境,一点也不知道出了什么问题,我该如何解决问题。

请帮忙。

2 个答案:

答案 0 :(得分:2)

听起来你没有为你的终端语法生成适当的基础设施。您可能希望采用与Xtext的c ommon.Terminals相同的生成器配置。

component = Generator auto-inject {
    pathRtProject = runtimeProject
    projectNameRt = runtimeProjectName
    language = {
        uri = "platform:/resource/x.mvmn.blah/src/x/mvmn/blah/xtext/Test.xtext"
        fragment = grammarAccess.GrammarAccessFragment auto-inject {}
        fragment = parseTreeConstructor.ParseTreeConstructorFragment {}
    }
}

答案 1 :(得分:0)

感谢Sebastian的建议,我终于设法让XText生成的代码不会通过将此生成器组件添加到我的GenerateBlahLang.mwe2来显示编译错误(主要是从现有生成器复制而来,其中一些部分已注释掉,因为错误“您可能无法为没有生产规则的语法生成ANTLR解析器”在生成期间显示“:

component = Generator auto-inject {
        pathRtProject = runtimeProject
        pathUiProject = "${runtimeProject}.ui"
        pathTestProject = "${runtimeProject}.tests"
        projectNameRt = projectName
        projectNameUi = "${projectName}.ui"
        encoding = encoding
        language = {
            uri = "classpath:/x/mvmn/blah/xtext/Test.xtext"// Java API to access grammar elements (required by several other fragments)
            fragment = grammarAccess.GrammarAccessFragment auto-inject {}

            // generates Java API for the generated EPackages
            fragment = ecore.EMFGeneratorFragment auto-inject {}

            // the old serialization component
            // fragment = parseTreeConstructor.ParseTreeConstructorFragment auto-inject {}    

            // serializer 2.0
            fragment = serializer.SerializerFragment auto-inject {
                generateStub = false
            }

            // a custom ResourceFactory for use with EMF
            fragment = resourceFactory.ResourceFactoryFragment auto-inject {}

            // The antlr parser generator fragment.
            //fragment = parser.antlr.XtextAntlrGeneratorFragment auto-inject {
            //  options = {
            //      backtrack = true
            //  }
            //}

            // Xtend-based API for validation
            fragment = validation.ValidatorFragment auto-inject {
            //    composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"
            //    composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
            }

            // old scoping and exporting API
            // fragment = scoping.ImportURIScopingFragment auto-inject {}
            // fragment = exporting.SimpleNamesFragment auto-inject {}

            // scoping and exporting API
            fragment = scoping.ImportNamespacesScopingFragment auto-inject {}
            fragment = exporting.QualifiedNamesFragment auto-inject {}
            fragment = builder.BuilderIntegrationFragment auto-inject {}

            // generator API
            fragment = generator.GeneratorFragment auto-inject {}

            // formatter API
            fragment = formatting.FormatterFragment auto-inject {}

            // labeling API
            fragment = labeling.LabelProviderFragment auto-inject {}

            // outline API
            fragment = outline.OutlineTreeProviderFragment auto-inject {}
            fragment = outline.QuickOutlineFragment auto-inject {}

            // quickfix API
            fragment = quickfix.QuickfixProviderFragment auto-inject {}

            // content assist API
            fragment = contentAssist.ContentAssistFragment auto-inject {}

            // generates a more lightweight Antlr parser and lexer tailored for content assist
            //fragment = parser.antlr.XtextAntlrUiGeneratorFragment auto-inject {}

            // generates junit test support classes into Generator#pathTestProject
            fragment = junit.Junit4Fragment auto-inject {}

            // rename refactoring
            fragment = refactoring.RefactorElementNameFragment auto-inject {}

            // provides the necessary bindings for java types integration
            fragment = types.TypesGeneratorFragment auto-inject {}

            // generates the required bindings only if the grammar inherits from Xbase
            fragment = xbase.XbaseGeneratorFragment auto-inject {}

            // generates the required bindings only if the grammar inherits from Xtype
            fragment = xbase.XtypeGeneratorFragment auto-inject {}

            // provides a preference page for template proposals
            //fragment = templates.CodetemplatesGeneratorFragment auto-inject {}

            // provides a compare view
            fragment = compare.CompareFragment auto-inject {}
        }
    }