Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Rule needs a delimiter?
Rule needs a delimiter? [message #1069300] Mon, 15 July 2013 16:15 Go to next message
Tobias Freudenreich is currently offline Tobias FreudenreichFriend
Messages: 19
Registered: July 2013
Junior Member
Hi,

I have a (at least for me strange) problem with my grammar/the JUnit parser.

Here are the relevant rules from the grammar:
Policy:
	'concepts'
		(concepts+=Concept)+
	'conditions'
		(conditions+=Condition)+
	'actions'
		(actions+=Action)+

Concept:
	name=ID
	alias=Alias
	selection=Selection
;

Condition:
	leftSide=[Alias] operation=ConditionOperationDebug rightSide=[Alias]
;

Alias:
	name=ID
;


When I feed the parser with the following input:
concepts
	person A (status='unknown')
	person B (status='known')
	room R (security='restricted')
conditions
	A within B
	A insideof R
actions
	raiseAlarm IN R.building


it identifies 3 conditions ("A within B", "A insideof null", "null R null"). When I add another condition, it identifies 4 conditions, first 2 are correct, the last one is split in a similar fashion is in the shown case.
What's strange to me is that the generated editor works totally fine. It is even able to do correct highlighting (so if I click R in the actions part, it highlights R in the concepts part). Which makes me assume the thing is working in principle.

If I change the grammar to enforce a delimiter (i.e., ';') after each condition, the parser works as expected, which makes me assume there is some issue with my grammar which prevents the parser from correctly delimiting the last condition.

Does anyone have any helpful thoughts on this? Thanks.
Re: Rule needs a delimiter? [message #1069348 is a reply to message #1069300] Mon, 15 July 2013 18:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

this confuses me a lot. imho the actual problem is that Xtext uses QualifiedNames out of the box.
thus the Aliases have the qualifed name <concept.name>.<alias.name>
if you want to reference to the you have following options
(1) Fix the naming problem by binding a custom subclass of DefaultDeclarativeNameProvider or binding SimpleNameProvider
(2) change syntax and grammar to ref=[Alias|FQN] with FQN: ID ("." ID)*; (then of course you have to change the model file too.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Rule needs a delimiter? [message #1069713 is a reply to message #1069348] Tue, 16 July 2013 12:50 Go to previous messageGo to next message
Tobias Freudenreich is currently offline Tobias FreudenreichFriend
Messages: 19
Registered: July 2013
Junior Member
The problem is not with qualified names (which I use in the actions part, but they are not modeled as an FQN, and the problem does not arise in the actions part anyway).
I have double checked the issue. Using
@Inject private ParseHelper<Policy> parser
...
parser.parse(...)

in a unit test yields 3 condition elements

while using
URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
ResourceSet rs = resourceSetProvider.get(project);
Resource r = rs.getResource(uri, true);
EObject eobject = r.getContents().get(0);

as part of the code generation process (taken thankfully from your Blog Christian) yields 2 condition elements on the same input.

And while typing above answer, I realized the following:
In the unit test, I read the file with a simple BufferedReader(FileReader). The plugin obviously uses Eclipse's infrastructure. The BufferdReader seems to strip away file endings which means the lexer will get a "Ractions" element. Adding a simple space after R solves the issue.

So thanks again and sorry for a rather stupid mistake in the end.
Re: Rule needs a delimiter? [message #1069716 is a reply to message #1069713] Tue, 16 July 2013 12:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

can you please post

(1) a complete grammar
(2) a test
(3) possibile customizations you did


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Rule needs a delimiter? [message #1069731 is a reply to message #1069348] Tue, 16 July 2013 13:08 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
It may also help to share your Selection and ConditionOperationDebug rules in case they have some optional part that may allow alternate matches.
Re: Rule needs a delimiter? [message #1069738 is a reply to message #1069716] Tue, 16 July 2013 13:19 Go to previous messageGo to next message
Tobias Freudenreich is currently offline Tobias FreudenreichFriend
Messages: 19
Registered: July 2013
Junior Member
Sure, anything that helps tracking the issue:
(1)
Policy:
	'concepts'
		(concepts+=Concept)+
	'conditions'
		(conditions+=Condition)+
	'actions'
		(actions+=Action)+
	;
	
Concept:
	name=ID
	alias=Alias
	selection=Selection
;

Selection:
	"(" attribute=Attribute "=" state=AttributeValue ")"
;

Alias:
	name=ID
;

Attribute:
	ID
;

AttributeValue:
	STRING
;

Condition:
	leftSide=[Alias] operation=ConditionOperationDebug rightSide=[Alias]
;

ConditionOperationDebug:
	function=ID
;

ConditionOperation:
	'IS' 'NOT'? function=ID (value=ID 'OF')?
;

Action:
	function=ID ('IN')? arguments+=AttributeQualifier*
;

AttributeQualifier:
	alias=[Alias] "." attribute=Attribute
;


(2)
@InjectWith(PolicyInjectorProvider.class)
@RunWith(XtextRunner.class)
public class ParserTest {
	@Inject private ParseHelper<Policy> parser;
	
	private Policy policy;
	
	@Before
	public void setup() throws Exception {
		policy = parseInput(readPolicy());
	}

	private CharSequence readPolicy() throws Exception {
		BufferedReader in = new BufferedReader(new FileReader("res/security.policy"));
		StringBuilder input = new StringBuilder();
		String line;
		while ((line = in.readLine()) != null) {
			input.append(line);
		}
		in.close();
		return input;
	}
	
	private Policy parseInput(CharSequence input) throws Exception {
		return parser.parse(input);
	}

	@Test
	public void testCondition() throws Exception {
		// setup
		
		// verify
		Concept concept = policy.getConcepts().get(0);
		Condition condition = policy.getConditions().get(0);
		
		assertEquals(concept.getAlias().getName(), condition.getLeftSide().getName());
		assertSame(concept.getAlias(), condition.getLeftSide());
		assertEquals(2, policy.getConditions().size());
	}
}


(3)
Customizations include making Aliases defined in the concepts part available to the conditions and AttributeQualifier

It is really just a mistake on my side though. readLine() strips "\n" from each line. If I change
input.append(line);
to
input.append(line + "\n");
everything behaves as expected.
Re: Rule needs a delimiter? [message #1069741 is a reply to message #1069738] Tue, 16 July 2013 13:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Ok,

just thought you were reading the model from a rich string and not from a file.
i you do read it from a file you have to care about doing this right yourself ;D


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Rule needs a delimiter? [message #1069745 is a reply to message #1069741] Tue, 16 July 2013 13:32 Go to previous message
Tobias Freudenreich is currently offline Tobias FreudenreichFriend
Messages: 19
Registered: July 2013
Junior Member
Yeah... I thought I might have used XText in a wrong way (since it is rather complex), but in the end, it was a stupid bug. Sorry for causing the fuzz.
Previous Topic:How to add Xtext DSL into the Spring project.
Next Topic:I have a working editor, now what?
Goto Forum:
  


Current Time: Tue Apr 16 04:32:43 GMT 2024

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

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

Back to the top