目前我正在尝试为PlantUML的类图创建一个DSL。我是Xtext的新手,我无法理解几件事。在列出我的问题之前,我向你展示了我当前语法的一些部分:
ClassUml:
{ClassUml}
'@startuml' umlElements+=(ClassElement)* '@enduml';
ClassElement:
Class
| Association;
Class:
{Class}
'class' name=ClassName
(color=ColorTag)?
('{' (classContents+=ClassContent)* '}')?;
ClassContent:
Attribute | Method;
ClassName:
(ID | STRING);
Attribute:
{Attribute}
(visibility=Visibility)? name=ID (":" type=ID)?;
Method:
{Method}
(visibility=Visibility)? name=METHID
(":" type=ID)?;
Association:
{Association}
(classFrom=[Class]
associationType=Bidirectional
classTo=[Class])
|
(classTo=[Class]
associationType=UnidirectionalLeft
classFrom=[Class])
|
(classFrom=[Class]
associationType=UnidirectionalRight
classTo=[Class])
(':' text+=(ID)*)?;
Bidirectional:
{Bidrectional}
('-' ("[" color=ColorTag "]")? '-'?)
| ('.' ("[" color=ColorTag "]")? '.'?);
UnidirectionalLeft:
{UnidirectionalLeft}
('<-' ("[" color=ColorTag "]")? '-'?)
| ('<.' ("[" color=ColorTag "]")? '.'?);
UnidirectionalRight:
{UnidirectionalRight}
((('-[' color=ColorTag "]")|'-')? '->')
| ((('.[' color=ColorTag "]")|'.')? '.>');
ColorTag:
(COLOR | HEXCODE);
enum Visibility:
PROTECTED='#'
| PRIVATE='-'
| DEFAULT='~'
| PUBLIC='+';
terminal COLOR:
"#"
('red') | ('orange');
terminal HEXCODE:
"#"
('A' .. 'F'|'0' .. '9')('A' .. 'F'|'0' .. '9')('A' .. 'F'|'0' .. '9')
('A' .. 'F'|'0' .. '9')('A' .. 'F'|'0' .. '9')('A' .. 'F'|'0' .. '9');
terminal STRING:
'"' ('\\' . | !('\\' | '"'))* '"';
terminal ID:
('a'..'z' | 'A'..'Z' | '_' | '0'..'9' | '\"\"' | '//' | '\\')
('a'..'z' | 'A'..'Z' | '_' | '0'..'9' | '\"\"' | '//' | '\\' | ':')*;
我遗漏了其他关联类型( - *, - o, - |&gt;),因为我已经以相同的方式定义了它们。
问题
1.如果没有与方法/属性名称分离,则可见性枚举'#'无效。但所有其他情况(+, - ,〜)都很好,两者之间有空白区域
在大多数情况下,协会似乎不起作用。我列举了几个例子:
' Working '
Alice -* Bob : Hello
Alice - Bob
Alice .o Bob
Alice <|-[#002211]- Bob
Alice *-[#red]- Bob
Alice -[#000000]-> Bob
Alice .[#red].> Bob
' Not Working '
Alice *-- Bob
Alice --* Bob
Alice .. Bob
Alice -[#ff0022]- Bob
Alice <-- Bob
Alice ..> Bob
Alice -- Bob
另外我猜测方法名称的附加终端是一个奇怪的解决方案,应该以不同的方式处理。
答案 0 :(得分:1)
1)颜色应该是解析器规则而不是终端规则。 同时删除十六进制规则,只需使用更改后的ID规则。
Color:
"#" ('red' | 'orange' | ID);
2)确保你统一差异,例如
之间存在冲突Bidirectional:
...
('-' ("[" ...;
和
UnidirectionalRight:
((('-[' ...;
序列&#39; - [&#39;将始终匹配后一版本。您应该创建一个规则AssociationType,并使其适用于所有情况。像这样:
Association:
{Association}
(classFrom=[Class | ClassName]
associationType=AssociationType
classTo=[Class | ClassName])
(':' text+=(ID)*)?;
AssociationType:
{AssociationType}
left?='<'? ('-'|'.') ("[" color=Color "]")? ('-'|'.') right?='>'?;
3)您也可以通过对crossrefs使用以下语法在交叉引用中允许STRING:classFrom = [Class | ClassName]