如何构建我的XText终端?字/ SL_STRING / ML_STRING

时间:2015-06-18 07:50:29

标签: java eclipse eclipse-plugin dsl xtext

在我的XText DSL中,我希望能够使用三种不同的文本终端。它们都用于在UML图中绘制的箭头顶部添加注释:

终端 WORD

Actor -> Actor: WORD

终端 SL_STRINGS

Actor -> Actor: A sequence of words on a single line

终端 ML_STRINGS

    Actor -> Actor: A series of words on 
                      multiple 
                      lines

我最初的方法是使用org.eclipse.xtext.common.Terminals中的 ID 终端作为我的 WORD 终端,然后只使用 SL_STRINGS (WORD)*,而 ML_STRINGS (NEWLINE? WORD)*,但是这会在规则之间产生很多问题。

我如何以良好的方式构建这个?

有关该项目的更多信息。 (因为这是第一次使用XText,请耐心等待):

我正在尝试实现一个与PlantUML http://plantuml.sourceforge.net/sequence.html的Eclipse插件一起使用的DSL,主要用于语法检查和着色。目前我的语法是这样的:

Model:
    (diagrams+=Diagram)*;

Diagram:
    '@startuml' NEWLINE (instructions+=(Instruction))* '@enduml' NEWLINE*
;

指令可以是很多东西:

Instruction: 
((name1=ID SEQUENCE name2=ID (':' ID)?) 
| Definition (Color)?
| AutoNumber
| Title
| Legend
| Newpage
| AltElse
| GroupingMessages
| Note
| Divider
| Reference
| Delay
| Space
| Hidefootbox
| Lifeline
| ParticipantCreation
| Box)? NEWLINE
;

需要不同类型文本终端的规则示例:

Group:
'group' TEXT
;

Reference:
    'ref over' ID (',' ID)* ((':' SL_TEXT)|((ML_TEXT) NEWLINE 'end ref'))
;

对于Group,文本只能在一行上,而对于Reference,如果规则调用之后没有“:”,则文本可以在两行上。

目前我的终端看起来像这样:

terminal NEWLINE    : ('\r'? '\n');

// Multiline comment begins with /', and ends with '/
terminal ML_COMMENT : '/\'' -> '\'/';

// Singleline comment begins with ', and continues until end of line.
terminal SL_COMMENT : '\'' !('\n'|'\r')* ('\r'? '\n')?;

// INT is a sequence of numbers 0-9.
terminal INT returns ecore::EInt: ('0'..'9')+;

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

terminal ANY_OTHER: .;

我想在此基础上添加三个新终端来处理文本。

1 个答案:

答案 0 :(得分:0)

您应该实现数据类型规则以实现所需的行为。 塞巴斯蒂安写了一篇关于这个主题的优秀博客文章,可以在这里找到:http://zarnekow.blogspot.de/2012/11/xtext-corner-6-data-types-terminals-why.html

以下是语法的最小示例:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
    greetings+=Greeting*;

Greeting:
    'Example' ':' comment=Comment;

Comment:
    (ID ('\r'? '\n')?)+ 
;

这将允许你写这样的东西:

Example: A series of words

Example: A series of words on
    multiple lines

然后,您可能希望实现自己的value converter,以便将转换微调到字符串。

如果有帮助,请告诉我!