Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XText Syntax
XText Syntax [message #1741153] Tue, 23 August 2016 11:35 Go to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Hello community,

I'm new with xtext. So i tried to write my first own DSL.
It's very similar to a if condition, just without variables.
e.g selector operator argument
selector and argument is typed by the user.
The operator is "hard coded"
description==hallo


MyDsl:
Module:
	(statements+=Statement)*;

Statement:
	   left=Selector op=(Expression) right=Argument;

Selector:
	name=ID;
	
Argument:
	name=ID;

Equal:
	'==';

NotEqual:
	'!=';
	
Expression:
    Equal|NotEqual;


If i start the RCP, i just can type the first selector, but i get no autocompletion for the operators.
So what's wrong? For me looks fine Smile

[Updated on: Tue, 23 August 2016 11:38]

Report message to a moderator

Re: XText Syntax [message #1741154 is a reply to message #1741153] Tue, 23 August 2016 11:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
the operators are datatype rules.
thus you get not proposals out of the box. => you would need to adapt the content assist

i propose to change the operator to a keyword or a enum

op=('=='|'!=')


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741159 is a reply to message #1741154] Tue, 23 August 2016 12:01 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
too late

[Updated on: Tue, 23 August 2016 12:08]

Report message to a moderator

Re: XText Syntax [message #1741169 is a reply to message #1741153] Tue, 23 August 2016 12:55 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

you cannot beat Christian Very Happy
Re: XText Syntax [message #1741247 is a reply to message #1741153] Wed, 24 August 2016 07:02 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Ok next question Smile Rolling Eyes Rolling Eyes

Tried a lot but nothing really works...

Same like in a if statement, i would like to add some operators.

So first you need a condition then a operator is optional, but when a operator is available, then you need 1 more condition.

Do I need 1 more rule for that?

Module:
	(conditions+=Condition)* op+=('&&'|'and'|'or'|'||')?;

Condition:
	   left=Selector op=('=='|'!=' | '>' | '<' | '>=' | '<='|'=le=' | '=ge=' | '=lt=' | '=gt=' | '=in=' | '=out=') right=Argument ;

Selector:
	name=ID;
	
Argument:
	name=ID;


Thanks a lot Christian Smile
Re: XText Syntax [message #1741248 is a reply to message #1741247] Wed, 24 August 2016 07:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
well at some point you will

- need datatype rules anyway and having to adapt the proposal provider
- consider doing a real expression grammar https://typefox.io/parsing-expressions-with-xtext


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741280 is a reply to message #1741248] Wed, 24 August 2016 10:28 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
ok i will try that.

I thought there is a easy way, to this without a big expression grammer
Re: XText Syntax [message #1741281 is a reply to message #1741280] Wed, 24 August 2016 10:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
well yes that is easy

Module:
(conditions+=Condition op+=('&&'|'and'|'or'|'||') conditions+=Condition)*;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741282 is a reply to message #1741281] Wed, 24 August 2016 10:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
sry wrong pasting

Module:
conditions+=Condition (op+=('&&'|'and'|'or'|'||') conditions+=Condition)*;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741450 is a reply to message #1741282] Thu, 25 August 2016 09:42 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Thanks a lot Cool . The first couple times it works fine with your suggestion, but now i get follow error:

java.lang.NoSuchFieldError: EOF_TOKEN
	at org.eclipse.xtext.parser.antlr.Lexer.nextToken(Lexer.java:59) ~[org.eclipse.xtext-2.10.0.jar:na]
	at org.antlr.runtime.BufferedTokenStream.fetch(BufferedTokenStream.java:143) ~[antlr4-4.5.jar:4.5]
	at org.antlr.runtime.BufferedTokenStream.sync(BufferedTokenStream.java:137) ~[antlr4-4.5.jar:4.5]
	at org.antlr.runtime.BufferedTokenStream.fill(BufferedTokenStream.java:283) ~[antlr4-4.5.jar:4.5]
	at org.antlr.runtime.BufferedTokenStream.toString(BufferedTokenStream.java:251) ~[antlr4-4.5.jar:4.5]
	at org.eclipse.xtext.parser.antlr.AbstractInternalAntlrParser.parse(AbstractInternalAntlrParser.java:541) ~[org.eclipse.xtext-2.10.0.jar:na]
	at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:102) ~[org.eclipse.xtext-2.10.0.jar:na]
	at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.parse(AbstractAntlrParser.java:84) ~[org.eclipse.xtext-2.10.0.jar:na]
	at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:62) ~[org.eclipse.xtext-2.10.0.jar:na]
	at org.eclipse.xtext.parser.AbstractParser.parse(AbstractParser.java:33) ~[org.eclipse.xtext-2.10.0.jar:na]
	at org.eclipse.xtext.resource.XtextResource.doLoad(XtextResource.java:170) ~[org.eclipse.xtext-2.10.0.jar:na]
	at org.eclipse.xtext.linking.lazy.LazyLinkingResource.doLoad(LazyLinkingResource.java:100) ~[org.eclipse.xtext-2.10.0.jar:na]
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518) ~[org.eclipse.emf.ecore-2.11.1-v20150805-0538.jar:na]
	at org.xtext.example.mydsl.web.XTextParsingService.loadResource(XTextParsingService.java:70) ~[classes/:na]
	at org.xtext.example.mydsl.web.XTextParsingService.createProposals(XTextParsingService.java:54) ~[classes/:na]
	at org.xtext.example.mydsl.web.SuggestService.getSuggestion(SuggestService.java:76) ~[classes/:na]
	at com.bosch.example.ui.component.TextFieldSuggestionBox.getSuggestions(TextFieldSuggestionBox.java:44) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74]


But i don't know what i have changed. It's a little bit weired. Any ideas?

Grammer is the same:
Module:
	conditions+=Condition (op+=('&&'|'and'|'or'|'||') conditions+=Condition)*;

Condition:
	   left=Selector op=('=='|'!=' | '>' | '<' | '>=' | '<='|'=le=' | '=ge=' | '=lt=' | '=gt=' | '=in=' | '=out=') right=Argument ;

Selector:
	name=ID;
	
Argument:
	name=ID;

[Updated on: Thu, 25 August 2016 09:43]

Report message to a moderator

Re: XText Syntax [message #1741453 is a reply to message #1741450] Thu, 25 August 2016 09:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
how does your test model look like?

but for me this looks like a messed up classpath problem
which antlr jar in on your classpath? maybe something that provides a broken antlr.Token class?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741459 is a reply to message #1741453] Thu, 25 August 2016 10:25 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
You are great. About a 3rd party dependency the antrl4 jar is on the classpath.
Re: XText Syntax [message #1741473 is a reply to message #1741459] Thu, 25 August 2016 13:04 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Currently im reading the terminal rules chapter (https://eclipse.org/Xtext/documentation/301_grammarlanguage.html#terminal-rules)

I tried to create my own terminal rule, but it cannot match.

terminal VALUE:
	( '*' | 'a'..'z'|'A'..'Z' |'0'..'9')*;


Error:
error(208): ../org.xtext.example.mydsl/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyDsl.g:545:1: The following token definitions can never be matched because prior tokens match the same input: RULE_INT
error(208): ../org.xtext.example.mydsl.ide/src-gen/org/xtext/example/mydsl/ide/contentassist/antlr/internal/InternalMyDsl.g:658:1: The following token definitions can never be matched because prior tokens match the same input: RULE_INT
Re: XText Syntax [message #1741475 is a reply to message #1741473] Thu, 25 August 2016 13:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
well lexers are context free. xtext by default ships with a bunch of terminal rules like INT and ID. both do similar things than your value.

so what about using a datatype rule instead:

VALUE : ('*'|ID|INT)+;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741481 is a reply to message #1741475] Thu, 25 August 2016 13:47 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
But if he use ID, then he can use a '^' character.

[Updated on: Thu, 25 August 2016 13:56]

Report message to a moderator

Re: XText Syntax [message #1741483 is a reply to message #1741481] Thu, 25 August 2016 13:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
the * is not requited


your rules lexe

1*1
1
a

and others

you can add additionals validations using a valueconverter


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741485 is a reply to message #1741483] Thu, 25 August 2016 13:57 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Sure but then e.g the value =*dhd^11 is valid, or?
Re: XText Syntax [message #1741486 is a reply to message #1741485] Thu, 25 August 2016 14:00 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Tried it:
Argument:
	value= VALUE | string=STRING | int=INT;
	
VALUE : ('*'|ID|INT)+;


warning(200): ../org.xtext.example.mydsl/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyDsl.g:498:2: Decision can match input such as "RULE_INT" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
error(201): ../org.xtext.example.mydsl/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyDsl.g:498:2: The following alternatives can never be matched: 3
Re: XText Syntax [message #1741488 is a reply to message #1741485] Thu, 25 August 2016 14:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
- you can implement the value converter to restrict that
- you can override ID to forbid ^


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741510 is a reply to message #1741488] Thu, 25 August 2016 14:42 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
What is your preferred solution?

I would like to try a own converter but this didn't work:

Argument:
	value= VALUE | string=STRING | int=INT;
	
VALUE : ('*'|ID|INT)+;

[Updated on: Thu, 25 August 2016 14:43]

Report message to a moderator

Re: XText Syntax [message #1741512 is a reply to message #1741510] Thu, 25 August 2016 14:46 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
@Christian could you explain why the terminal rule did not work in this case? I don't understand the problem.

I would expected something like that should work;
terminal VALUE:
	('*' | 'a'..'z'|'A'..'Z')+ | INT;
Re: XText Syntax [message #1741515 is a reply to message #1741512] Thu, 25 August 2016 14:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
the problem is that lexers are context free.
means the whole file is lexed regardless the place in the file you are,

means: once lexed as VALUE, always lexed as value

means

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

terminal VALUE:
('*' | 'a'..'z'|'A'..'Z')+ | INT;

will never parse


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741517 is a reply to message #1741515] Thu, 25 August 2016 14:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Argument:
value= VALUE | string=STRING;

VALUE : ('*'|ID|INT)+;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741523 is a reply to message #1741517] Thu, 25 August 2016 15:05 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Thanks for your explanation again.

Then maybe this could work:
Argument:
	value= ArgumentValue | string=STRING;
	
ArgumentValue : (VALUE|INT)*;
	
terminal VALUE:
	('*' | 'a'..'z'|'A'..'Z')*;


Re: XText Syntax [message #1741524 is a reply to message #1741523] Thu, 25 August 2016 15:26 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
This is exactly what I was looking for.
Argument:
	value+= VALUE | string+=STRING;
	
	
terminal VALUE:
	('a'..'z'|'A'..'Z' | '*' | INT)*;


But now i have to adapt the proposal provider, correct?
Re: XText Syntax [message #1741537 is a reply to message #1741524] Thu, 25 August 2016 17:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
well that should be easy

import javax.inject.Inject
import org.eclipse.xtext.RuleCall
import org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext
import org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor
import org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalProvider
import org.xtext.example.mydsl2.services.MyDslGrammarAccess

class MyDslIdeContentProposalProvider extends IdeContentProposalProvider {
	
	@Inject extension MyDslGrammarAccess
	
	override dispatch createProposals(RuleCall rc, ContentAssistContext context,
			IIdeContentProposalAcceptor acceptor) {
		if (VALUERule === rc.rule) {
			//TODO check context.prefix
			val e1 = proposalCreator.createProposal("1",context)
			acceptor.accept(e1, proposalPriorities.getDefaultPriority(e1))
			val e2 = proposalCreator.createProposal("*",context)
			acceptor.accept(e2, proposalPriorities.getDefaultPriority(e2))
			val e3 = proposalCreator.createProposal("ID",context)
			acceptor.accept(e1, proposalPriorities.getDefaultPriority(e3))
			
		} else {		
			super._createProposals(rc, context, acceptor)	
		}
	}
	
}
class MyDslWebModule extends AbstractMyDslWebModule {
	
	def void configureIPreferenceValuesProvider(Binder binder) {
		binder.bind(IPreferenceValuesProvider).annotatedWith(FormatterPreferences).to(FormatterPreferenceValuesProvider)
	}
	
	def Class<? extends IdeContentProposalProvider> bindIdeContentProposalProvider() {
		MyDslIdeContentProposalProvider
	}
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText Syntax [message #1741735 is a reply to message #1741537] Mon, 29 August 2016 07:20 Go to previous message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Yeah cool thanks Cool.
Previous Topic:How to iterate over the grammar
Next Topic:xtext GenModelSupport error
Goto Forum:
  


Current Time: Fri Mar 29 01:43:19 GMT 2024

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

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

Back to the top