Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Grammar problem (newbie)
Grammar problem (newbie) [message #757605] Sun, 20 November 2011 10:23 Go to next message
John Howard is currently offline John HowardFriend
Messages: 83
Registered: July 2009
Member
I have the following rule in a reasonably complex grammar that I am porting from ANTLR

BidPriority: PRIORITY priorityLevel=INT;

PRIORITY: 'p';

This matches p 1, but won't match p1.

Is there any way I can match p1?
Re: Grammar problem (newbie) [message #757607 is a reply to message #757605] Sun, 20 November 2011 10:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i guess you have to use a terminal rule for this (plus a valueconverter that gives you the int part of the whole stuff)

import "http://www.eclipse.org/emf/2002/Ecore" as ecore
	
BidPriority: priorityLevel=PRIORITY;

terminal PRIORITY returns ecore::EInt: 'p' ('0'..'9')+;


public class MyDslRuntimeModule extends org.xtext.example.mydsl2.AbstractMyDslRuntimeModule {
	
	@Override
	public Class<? extends IValueConverterService> bindIValueConverterService() {
		return MyDslValueConverterService.class;
	}

}


public class MyDslValueConverterService extends DefaultTerminalConverters {
	
	@ValueConverter(rule = "PRIORITY")
	public IValueConverter<Integer> PRIORITY() {
		return new IValueConverter<Integer>() {

			@Override
			public Integer toValue(String string, INode node)
					throws ValueConverterException {
				return Integer.valueOf(string.substring(1));
			}

			@Override
			public String toString(Integer value)
					throws ValueConverterException {
				return "p" + value.toString();
			}
			
		};
	}

}


if you want to use something like p1 at a place you would use and id
you have to introduce a datatype rule for this

Stuff: ID | PRIORITY;
...
stuff=Stuff

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Grammar problem (newbie) [message #757610 is a reply to message #757605] Sun, 20 November 2011 10:45 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

p1 is matched by the ID rule. It is hard to say what the best option would be for your use case. Possibly a data type rule with a value converter.
That way you would not have to introduce "unnecessary" keywords.

Bipriority: priorityLevel=Priority;
Priority returns ecore::EInt: ID;

And a value converter for Priority that throws a value conversion exception if the format is not p<Number> and extracts the number.

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: Grammar problem (newbie) [message #757814 is a reply to message #757610] Mon, 21 November 2011 19:22 Go to previous message
John Howard is currently offline John HowardFriend
Messages: 83
Registered: July 2009
Member
Christian/Alex,

Many thanks for the replies - As I have control of the grammar I have decided to introduce a # so I can define the priority e.g. p#1 which I can manage through a terminal. However, it is good to know that I can revert back to my original syntax, which I may well do once I have got the grammar fully ported.

Thanks,

John

Previous Topic:understanding ImportManager
Next Topic:How i load resources dynamically
Goto Forum:
  


Current Time: Fri Apr 26 13:53:44 GMT 2024

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

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

Back to the top