Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Case insensitive and warnings
Case insensitive and warnings [message #810197] Wed, 29 February 2012 20:36 Go to next message
Andrew Gacek is currently offline Andrew GacekFriend
Messages: 32
Registered: October 2011
Member
I have a language which is case insensitive, but I would like to issues warnings when the wrong case is used. Is there a way to do this? When I go to write the validation rule, it looks like I cannot access the original text. The only thing I can see is the corrected variable name.

Thanks,
Andrew
Re: Case insensitive and warnings [message #810235 is a reply to message #810197] Wed, 29 February 2012 21:54 Go to previous messageGo to next message
Joey Mink is currently offline Joey MinkFriend
Messages: 87
Registered: July 2009
Location: Centreville, VA, USA
Member

Andrew,

Your post is a bit confusing:

Quote:
I have a language which is case insensitive, but I would like to issues warnings when the wrong case is used.


What is the wrong case if the language is case-insensitive? Do you have an example of your DSL syntax that you can use to help describe what you want?
Re: Case insensitive and warnings [message #810249 is a reply to message #810235] Wed, 29 February 2012 22:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

you can always use the node model to get the physical text behind the semantic model
following utilities might be helpful

org.eclipse.xtext.nodemodel.util.NodeModelUtils.getNode(EObject)
org.eclipse.xtext.nodemodel.util.NodeModelUtils.findNodesForFeature(EObject, EStructuralFeature)

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Case insensitive and warnings [message #810290 is a reply to message #810249] Wed, 29 February 2012 23:41 Go to previous messageGo to next message
Andrew Gacek is currently offline Andrew GacekFriend
Messages: 32
Registered: October 2011
Member
Joey, the language is case insensitive, but there is a preferred case (based on the case with which variables were declared). I was looking for a way to allow any case, but issue a warning if the preferred case is not used.

Christian, thank you. That is exactly what I needed. Here's is what I'm doing now:

final public static String DEPRECATED_VARIABLE_CASE = "deprecated_variable_case";

@Check
public void checkVariableCase(IdExpr idExpr) {
	String correctCase = idExpr.getId().getName();
	ICompositeNode node = NodeModelUtils.getNode(idExpr);
	String actualCase = node.getText().trim();
	if (!correctCase.equals(actualCase)) {
		warning("Deprecated variable case",
				CasPackage.Literals.ID_EXPR__ID,
				DEPRECATED_VARIABLE_CASE,
				correctCase);
	}
}


and I provide the following quickfix:

@Fix(CasJavaValidator.DEPRECATED_VARIABLE_CASE)
public void fixVariableCase(final Issue issue, IssueResolutionAcceptor acceptor) {
	final String correct = issue.getData()[0];
	acceptor.accept(issue,
		"Change to " + correct,
		null,
		null,
		new IModification() {
			public void apply(IModificationContext context) throws BadLocationException {
				IXtextDocument document = context.getXtextDocument();
				document.replace(issue.getOffset(), issue.getLength(), correct);
			}
		}
	);
}


Everything seems to be working great. Please let me know if I'm doing something wrong however.

Thanks again,
Andrew
Re: Case insensitive and warnings [message #810931 is a reply to message #810290] Thu, 01 March 2012 17:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

how does your grammar look like?

i guess you have something like

IdExpr: id=[Variable]


=> the cleaner solution would be to use

NodeModelUtils.findNodesForFeature(idExpr, CasPackage.Literals.ID_EXPR__ID).get(0).getText().trim()


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Case insensitive and warnings [message #810949 is a reply to message #810931] Thu, 01 March 2012 18:21 Go to previous message
Andrew Gacek is currently offline Andrew GacekFriend
Messages: 32
Registered: October 2011
Member
I have a rule like

Atom returns Expr:
  {IdExpr} id=[Variable]
| ...
;


So yes, using getNode() works fine I suppose since I have a single item in my node. But I'll switch to using the findNodesForFeature so that it's more robust in the future.

[Updated on: Thu, 01 March 2012 18:25]

Report message to a moderator

Previous Topic:xbase: LongExtensions
Next Topic:Hyperlinks - more than one possible hyperlink destinations?
Goto Forum:
  


Current Time: Thu Apr 25 13:19:06 GMT 2024

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

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

Back to the top