Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Elegant way of determining which terminal was matched?
Elegant way of determining which terminal was matched? [message #1496280] Tue, 02 December 2014 23:12 Go to next message
Andrew Hoyt is currently offline Andrew HoytFriend
Messages: 6
Registered: December 2014
Junior Member
Hi there,

New to xText and just had a quick question. What's the best way for the grammar to determine which terminal was matched. Take the following example rule:

SimpleConstant:
	value=(INTEGER | SIGNED_INT | FLOAT | BOOLEAN | NULL | CSTRING)
;


Each of the alternatives there are custom terminals. The type of the value attribute is always going to be string, but is there any way to indicate which terminal was matched? The only thing I can think of is to either re-parse value to determine what type it is (which I'd like to avoid if possible) or change the rule to something like:

SimpleConstant:
	uInt = INTEGER | int = SIGNED_INT | float = FLOAT | bool = BOOLEAN | nul = NULL | cstring = CSTRING
;


And then check which of the attributes actually contains a value, but that seems like a lot of overhead. Is there something simple I'm missing?

Thanks for the help!
- Andrew
Re: Elegant way of determining which terminal was matched? [message #1496610 is a reply to message #1496280] Wed, 03 December 2014 05:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi you can use nodemodelutil to examine the node of the object / feature. The node model contains the information which grammar element (rule) is used.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Elegant way of determining which terminal was matched? [message #1497283 is a reply to message #1496610] Wed, 03 December 2014 18:04 Go to previous messageGo to next message
Andrew Hoyt is currently offline Andrew HoytFriend
Messages: 6
Registered: December 2014
Junior Member
Hey Christian,

Thanks for the response! So, using nodeModelUtil, I was able to get pretty close, but not perfect. The problem I see is that the only nodeModelUtils method that actually captures the information about which terminal was matched is the compactDump. This is due to the fact that terminals aren't considered gramatical or semantic elements in the model. By using compactDump on the leafNode that contains the text I'm interested in, I'm able to generate strings such as these:

value=SIGNED_INT => '-683431515'
value=INTEGER => '6'


Which is on the right track, and I can certainly use regex on these strings to capture the terminal that was matched, but that still seems like it should be unnecessary. It definitely appears as though the information about the matched terminal exists in a data structure somewhere, but using any of the other nodeModelUtils on "value" only ever returns the text or null. Any idea how to get access to terminal type?

Thanks again!
Re: Elegant way of determining which terminal was matched? [message #1497367 is a reply to message #1497283] Wed, 03 December 2014 19:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i cannot reproduce your problem or i dont understand it

Model:
	constants+=SimpleConstant*;
	
SimpleConstant:
	value=(ID | STRING)
;


/*
 * generated by Xtext
 */
package org.xtext.example.mydsl2.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import org.xtext.example.mydsl2.myDsl.SimpleConstant
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
import org.xtext.example.mydsl2.myDsl.MyDslPackage
import org.eclipse.xtext.RuleCall

/**
 * Generates code from your model files on save.
 * 
 * see http://www.eclipse.org/Xtext/documentation.html#TutorialCodeGeneration
 */
class MyDslGenerator implements IGenerator {
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		fsa.generateFile('greetings.txt', 
			'''
			«FOR sc : resource.allContents.filter(typeof(SimpleConstant)).toIterable»
				«FOR node : NodeModelUtils.findNodesForFeature(sc, MyDslPackage.Literals.SIMPLE_CONSTANT__VALUE)»
					«IF node.grammarElement instanceof RuleCall»
						«(node.grammarElement as RuleCall).rule.name»
					«ENDIF»
				«ENDFOR»
			«ENDFOR»
			''')
	}
}



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Elegant way of determining which terminal was matched? [message #1497579 is a reply to message #1497367] Wed, 03 December 2014 22:49 Go to previous message
Andrew Hoyt is currently offline Andrew HoytFriend
Messages: 6
Registered: December 2014
Junior Member
Didn't know about RuleCall. That's perfect! Thanks!
Previous Topic:Problem with isJavaEnabled()
Next Topic:"Ignoring" a portion of the AST
Goto Forum:
  


Current Time: Fri Apr 26 05:13:58 GMT 2024

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

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

Back to the top