[Xtext] Defining Strings with included crosslinks [message #547489] |
Sat, 17 July 2010 15:28 |
awidegreen Messages: 14 Registered: July 2010 |
Junior Member |
|
|
Hej,
I'm trying to implement a language feature that provides linking in Strings, like a Javadoc {@link} - see the 'desc' entry in the following example:
service service1;
service service2;
def myDef {
desc = [some text link(service1) some text]
}
Therefore I've already defined a grammar like this:
Model:
(elements+=(Service|Def))+;
Def hidden(WS, ML_COMMENT, SL_COMMENT):
'def' name=ID '{'
'desc' '=' '[' (values+=AbstractDesContent)* ']'
'}';
AbstractDesContent:
Str | Link;
Str hidden():
val=(DESC_STRING | OTHER_CHAR);
Service hidden(WS, ML_COMMENT, SL_COMMENT):
'service' name=ID ';';
Link:
'link' '(' serviceRef=[Service|ID] ')';
terminal ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal DESC_STRING: ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal OTHER_CHAR : '/' | ':' | ';' | '<' | '=' | '>' | '|' | '\\' | '*' |
'#' | '&' | '\'' | '(' | ')' | '+' | ',' | '-';
terminal ML_COMMENT : '/*' -> '*/';
terminal SL_COMMENT : '//' !('\n'|'\r')* ('\r'? '\n')?;
terminal WS : (' '|'\t'|'\r'|'\n')+;
This obviously doesn't work, because the lexer can not distinguish between ID and DESC_STRING, respectively ID hides DESC_STRING, am I right? If I add a '^' as the first character for the ID terminal - which i don't want - it expectably works fine.
So, is there a way to get this working? Do I need an appropriate Value Converter?
Thanks,
a widegreen
[Updated on: Sun, 18 July 2010 17:06] Report message to a moderator
|
|
|
|
|
Defining Extensive Strings with included crosslinks [message #558215 is a reply to message #549331] |
Fri, 10 September 2010 14:29 |
awidegreen Messages: 14 Registered: July 2010 |
Junior Member |
|
|
Hej,
I have to push this topic again, because the solution isn't actually working.
Again I want to do something like this:
element {
aString = "a normal string";
description = [Some Text @Link{<LinkToFooOrBar>}. More text with a { and } and =. This does not work];
}
I've got it working to define a grammar so that i can add 'Links' in this 'ExtensiveString'. The problem is, when i type other characters like '{', '}', '=', etc. which I defined as data rules, the parser doesn't not work again.
To be more precise, here another snippet of the grammar.
grammar org.test.ExtStr with org.eclipse.xtext.common.Terminals
generate extStr "http://www.test.org/ExtStr"
Model hidden(WS, ML_COMMENT, SL_COMMENT):
'model' OpenToken
(elements+=Element)+
ClosingToken;
/*
* Element
*/
Element:
'element' name=QualifiedName OpenToken
'aString' AssignToken strVal=STRING SeperatorToken
desc=Description
ClosingToken;
Description:
'description' AssignToken extString=ExtensiveString SeperatorToken;
/*
* Extensive String
*/
ExtensiveString:
'[' (elements+=AbstractExtensiveStringElement)+ ']';
AbstractExtensiveStringElement:
StringElement | LinkElement;
StringElement:
value=ID | value=EXTSTR_OTHER_CHAR;
LinkElement:
{LinkElement}
'@link' OpenToken (linkTo=[LinkableElement|QualifiedName])? ClosingToken;
LinkableElement:
Foo | Bar | Element;
Foo:
name=QualifiedName;
Bar:
name=QualifiedName;
QualifiedName:
ID ('.' ID)*;
QualifiedNameWithWildCard:
QualifiedName '.*'?;
SeperatorToken : ';';
OpenToken : '{';
ClosingToken : '}';
AssignToken : '=';
/*
* Terminals
*/
terminal ID: ('^')?('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal EXTSTR_OTHER_CHAR: ('/' | ':' | ';' | '<' | '=' | '>' | '|' | '\\' | '*' | '#' | '&' | '\'' | '(' | ')' | '+' | ',' | '-' | '.' | '\n')*;
terminal STRING: '"' ( '\\' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'\\') | !('\\'|'"') )* '"' | "'" ( '\\' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'\\') | !('\\'|"'") )* "'"
;
terminal ML_COMMENT: '/*' -> '*/';
terminal SL_COMMENT: '//' !('\n'|'\r')* ('\r'? '\n')?;
terminal WS: (' '|'\t'|'\r'|'\n')+;
In the above example, the given parser-error - while running the eclipse-plugin - says that it expects a ']' and not an '{'.
Also, when I include keywords from the grammar - in this case e.g. 'element' - the parser recognizes such keywords. This is obviously not the behavior I intended to have.
So is there an appropriate solution for this grammar?
Thanks,
a
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03345 seconds