Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Grammar problem (newbie)
Grammar problem (newbie) [message #757605] Sun, 20 November 2011 05:23 Go to next message
Eclipse UserFriend
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 05:38 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Grammar problem (newbie) [message #757610 is a reply to message #757605] Sun, 20 November 2011 05:45 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Grammar problem (newbie) [message #757814 is a reply to message #757610] Mon, 21 November 2011 14:22 Go to previous message
Eclipse UserFriend
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: Wed Jul 23 15:12:40 EDT 2025

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

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

Back to the top