Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xtext] Defining Strings with included crosslinks
[Xtext] Defining Strings with included crosslinks [message #547489] Sat, 17 July 2010 15:28 Go to next message
awidegreen  is currently offline awidegreen Friend
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

Re: [Xtext] Defining Strings with included crosslinks [message #547582 is a reply to message #547489] Mon, 19 July 2010 04:58 Go to previous messageGo to next message
Alex is currently offline AlexFriend
Messages: 114
Registered: June 2010
Senior Member
Try

Str hidden():
	val=(ID | DESC_STRING | OTHER_CHAR);


I tried to do something alike (see here)
Maybe that helps...
Re: [Xtext] Defining Strings with included crosslinks [message #549331 is a reply to message #547582] Mon, 26 July 2010 13:26 Go to previous messageGo to next message
awidegreen  is currently offline awidegreen Friend
Messages: 14
Registered: July 2010
Junior Member
Hej Alex,

that little addition seems to be the solution, thanks.
Defining Extensive Strings with included crosslinks [message #558215 is a reply to message #549331] Fri, 10 September 2010 14:29 Go to previous messageGo to next message
awidegreen  is currently offline awidegreen Friend
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
Re: Defining Extensive Strings with included crosslinks [message #558286 is a reply to message #558215] Fri, 10 September 2010 19:50 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

> The problem is, when i type other characters like '{', '}', '=',...

obviously, as you do not allow {} in EXTSTR_OTHER_CHAR. At first sight the equals should work though.

Alex
Re: Defining Extensive Strings with included crosslinks [message #558304 is a reply to message #558286] Fri, 10 September 2010 22:51 Go to previous messageGo to next message
awidegreen  is currently offline awidegreen Friend
Messages: 14
Registered: July 2010
Junior Member
Hej Alex,

well your answer makes sense. Wink But I also got this problem with '.' and that is defined in EXTSTR_OTHER_CHAR. And i also don't want Eclipse to highlight the keywords like 'element' in an ExtensiveString.

Further ideas?

Thanks,
a
Re: Defining Extensive Strings with included crosslinks [message #558328 is a reply to message #558304] Sat, 11 September 2010 09:17 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
OK,

it is not sufficient to put the characters into EXTSTR_OTHER_CHAR. As
"{" etc. are keywords, they will be tokenised as keywords and never as
EXTSTR_OTHER_CHAR. What you need to do is put all of them into a
datatype rule and add them as further alternative to the String element.

AllowedKeywords: '{'|'.'|...;

StringElement:value=(ID|EXTSTR_OTHER_CHAR|AllowedKeywords);

As to the highlighting. Keywords are keywords wherever they appear. The
lexical highlighting does not care at all about the context. A keyword
will be highlighted. What you need to do is implement semantic
highlighting for the ExtensiveString element. See the documentation and
search the web for "xtext semantic highlighting". Several Xtext projects
also customise the hightlighting.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Defining Extensive Strings with included crosslinks [message #558434 is a reply to message #558328] Sun, 12 September 2010 19:41 Go to previous message
awidegreen  is currently offline awidegreen Friend
Messages: 14
Registered: July 2010
Junior Member
Hej Alex,

thanks for this very helpful input, it works so far. I do more testing in the next days.

Thanks,
a
Previous Topic:The Story of cq-markup-xtext (pt. 1)
Next Topic:Build Model from Project
Goto Forum:
  


Current Time: Fri Apr 19 14:45:59 GMT 2024

Powered by FUDForum. Page generated in 0.02506 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top