Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » SyntaxErrorMessageProvider custom implementation(SyntaxErrorMessageProvider custom implementation)
SyntaxErrorMessageProvider custom implementation [message #1770757] Wed, 16 August 2017 20:15 Go to next message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi,
I want to customize the syntax error messages provided by the framework. So I tried extending SyntaxErrorMessageProvider class in order to give an error for user defined terms as follows.

public class MyDslErrorMessageProvider extends SyntaxErrorMessageProvider {

	@Override
	public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) {
		//SyntaxErrorMessage syntaxError1=new SyntaxErrorMessage("Name should start with a letter", IssueCodes.AMBIGUOUS_FEATURE_CALL);
		if(context.getRecognitionException() instanceof RecognitionException){
			return new SyntaxErrorMessage("Name should start with a letter", IssueCodes.AMBIGUOUS_FEATURE_CALL);
			
		}
		return super.getSyntaxErrorMessage(context);
	}

	@Override
	public SyntaxErrorMessage getSyntaxErrorMessage(IValueConverterErrorContext context) {
		// TODO Auto-generated method stub
		return null;
	}

}


But when I run this message is shown at all errorneous places in the editor. How can I show the message only in needed places?
Thank you.

[Updated on: Wed, 16 August 2017 20:17]

Report message to a moderator

Re: SyntaxErrorMessageProvider custom implementation [message #1770773 is a reply to message #1770757] Thu, 17 August 2017 03:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Examine the context and if else ?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: SyntaxErrorMessageProvider custom implementation [message #1770859 is a reply to message #1770773] Fri, 18 August 2017 02:13 Go to previous messageGo to next message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi
I had travered
val unexpectedText = context?.recognitionException?.token?.text
val currNode = context?.currentNode

for (node : currNode.asTreeIterable) {
			println(unexpectedText)
			println(node.getGrammarElement())
			
			println(node.getGrammarElement() == MyDslGrammarAccess.getDefinitionStreamAccess().srcSource1ParserRuleCall_4_0)
			println(GrammarUtil::getAllKeywords(MyDslGrammarAccess.getGrammar()).contains(node.text))
			
			if(node.getGrammarElement() == MyDslGrammarAccess.getDefinitionStreamAccess().srcSource1ParserRuleCall_4_0 && GrammarUtil::getAllKeywords(MyDslGrammarAccess.getGrammar()).contains(node.text)){
				println(context.defaultMessage)
				return new SyntaxErrorMessage('''"«unexpectedText»" is a reserved keyword which is not allowed as Identifier.''',USED_RESERVED_KEYWORD)
			}
		}



like this inside getSyntaxErrorMessage method. The problem is the error only shows at the start of the line , not at relevant places in the editor. So a simplified version of this as follows cannot be used.

val unexpectedText = context?.recognitionException?.token?.text

if (GrammarUtil::getAllKeywords(MyDslGrammarAccess.getGrammar()).contains(unexpectedText)) {

            return new SyntaxErrorMessage('''"«unexpectedText»" is a reserved keyword which is not allowed as Identifier.''',USED_RESERVED_KEYWORD)  
        }


That's why I thought of traversing, but the editor pass the first token with the error context not the relevant token which has the error. Can we customize the places where errors are showing by considering the offsets ?
Re: SyntaxErrorMessageProvider custom implementation [message #1770867 is a reply to message #1770859] Fri, 18 August 2017 06:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i do not understand nor can reproduce.

Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' name=ID '!';


import com.google.inject.Inject
import org.antlr.runtime.RecognitionException
import org.eclipse.xtext.GrammarUtil
import org.eclipse.xtext.nodemodel.SyntaxErrorMessage
import org.eclipse.xtext.parser.antlr.SyntaxErrorMessageProvider
import org.xtext.example.mydsl2.services.MyDslGrammarAccess

class MyDslErrorMessageProvider extends SyntaxErrorMessageProvider {

	@Inject MyDslGrammarAccess ga

	override SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) {
		if (context.getRecognitionException() instanceof RecognitionException) {
			val unexpectedText = context?.recognitionException?.token?.text

			if (GrammarUtil::getAllKeywords(ga.grammar).contains(unexpectedText)) {

				return new SyntaxErrorMessage('''"«unexpectedText»" is a reserved keyword which is not allowed as Identifier.''',
					"bääääääh")
			}
		}
		return super.getSyntaxErrorMessage(context)
	}

	override SyntaxErrorMessage getSyntaxErrorMessage(IValueConverterErrorContext context) {
		// TODO Auto-generated method stub
		return null
	}
}



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: SyntaxErrorMessageProvider custom implementation [message #1770911 is a reply to message #1770867] Fri, 18 August 2017 16:32 Go to previous messageGo to next message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi,
What you said is correct for this simple grammar and I already checked it. But in my grammar for all the errors the in the line, the first token is marked red as shown in the figure attached. But "int" of second line should be marked in red.
Thank you.
  • Attachment: a.png
    (Size: 2.84KB, Downloaded 117 times)

[Updated on: Fri, 18 August 2017 16:36]

Report message to a moderator

Re: SyntaxErrorMessageProvider custom implementation [message #1770916 is a reply to message #1770911] Fri, 18 August 2017 18:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Sry I have. I idea withou something reproducible

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: SyntaxErrorMessageProvider custom implementation [message #1771107 is a reply to message #1770916] Tue, 22 August 2017 05:02 Go to previous messageGo to next message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi,
In my grammar the rule that initially runs is like,
Model:
         a*((=>b|=>c|=>d)=>(';'(=>b|=>c|=>d))*=>(';' e ';'?))
;


When the rule is in this format, all the errors that are relevant to a particular model are shown at the beginning of the line as I mentioned earlier. But when there are no alternatives and the rule is like,
Model:
         a*((b)=>(';'(b))*=>(';' e ';'?))
;

Errors are shown in the expected way. Is this thing happening due to the predicates that are used or due to alternatives?

Thank you.

[Updated on: Tue, 22 August 2017 05:13]

Report message to a moderator

Re: SyntaxErrorMessageProvider custom implementation [message #1771185 is a reply to message #1771107] Tue, 22 August 2017 17:19 Go to previous messageGo to next message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi,
The actual grammar starts as follows.
Model:
	(elements += ExecutionPlan)*
;

ExecutionPlan:
	(appAnn+=AppAnnotation)*( 
	(=>defStream+=DefinitionStream|=>defTable+=DefinitionTable| =>def_window+=DefinitionWindow| =>defTrigger+=DefinitionTrigger|=>defFunction+=DefinitionFunction)=>(';' 
	(=>defStream+=DefinitionStream|=>defTable+=DefinitionTable| =>def_window+=DefinitionWindow| =>defTrigger+=DefinitionTrigger|=>defFunction+=DefinitionFunction))*
	=>(';'(exElement+=ExecutionElement))* ';'?) 
;


When it is like this the error doesn't appear in the relevant place. I have tried several ways and finally I found when the grammar has no alternatives the error is shown at the relevant place.
Model:
	(elements += ExecutionPlan)*
;

ExecutionPlan:
	(appAnn+=AppAnnotation)*( 
	(=>defStream+=DefinitionStream)=>(';' 
	(=>defStream+=DefinitionStream|=>defTable+=DefinitionTable| =>def_window+=DefinitionWindow| =>defTrigger+=DefinitionTrigger|=>defFunction+=DefinitionFunction))*
	=>(';'(exElement+=ExecutionElement))* ';'?) 
;


Is this error reporting variation happens due to the grammar initial rule?

A minimal reproducible grammar would be as follows.
Model:
	(elements += ExecutionPlan)*
;

ExecutionPlan:
	(=>defStream+=DefinitionStream|=>defTable+=DefinitionTable| =>def_window+=DefinitionWindow| =>defTrigger+=DefinitionTrigger|=>defFunction+=DefinitionFunction)=>(';' 
	(=>defStream+=DefinitionStream|=>defTable+=DefinitionTable| =>def_window+=DefinitionWindow| =>defTrigger+=DefinitionTrigger|=>defFunction+=DefinitionFunction))*
	=>(';'(exElement+=ExecutionElement))* ';'?
;
ExecutionElement:
	(que=Query)
;
DefinitionStream:
	{DefinitionStream}(ann += Annotation)* DEFINE STREAM src=Source1 
;
DefinitionTable:
	{DefinitionTable}(ann1 += Annotation)* DEFINE TABLE src=Source1 
;
DefinitionWindow:
	{DefinitionWindow}(ann2 += Annotation)* DEFINE WINDOW src=Source1  func_body=FunctionBody 
;
DefinitionTrigger:
	{DefinitionTrigger}(DEFINE TRIGGER) tn=TriggerName AT (every=EVERY sv=StringValue) 
;

DefinitionFunction:
	{DefinitionFunction}(DEFINE FUNCTION) fn=FunctionName '[' ln=LanguageName ']' RETURN func_body=FunctionBody 
;
Annotation:
	'@' na = ID ('(' (annElement += AnnotationElement | ann += Annotation) 
		( ',' (annElement += AnnotationElement | ann += Annotation))* ')'	)
;

AnnotationElement:
	(propName = PropertyName '=')? propVal = PropertyValue
;

PropertyValue:
	sv=StringValue
;

PropertyName:
	{PropertyName} na+=ID (ps+=PropertySeparator na += ID )*
;

PropertySeparator:
	{PropertySeparator}DOT |{PropertySeparator} MINUS |{PropertySeparator} COL
;

FunctionName:
	id=ID
;

LanguageName:
	id=ID
;

FunctionBody:
	value=SCRIPT
;

terminal SCRIPT:
	'{' SCRIPT_ATOM* '}'
;

terminal SCRIPT_ATOM:
  ! ('{' | '}')
  | '"' (!'"')* '"'
  | '//' !('\r'|'\n')*
  | SCRIPT
;
StringValue:
	sl=STRING_LITERAL
;
STRING_LITERAL:
	STRING
;
TriggerName:
	id=ID
;
Source:
	strId=[Source1|IdNew] 
;

Source1:
	inner='#'? name=IdNew
;
IdNew: ID;
Query:
	{Query}(ann3 += Annotation)* FROM source=Source 
;
Keyword:
	{Keyword} STREAM 
	|{Keyword} DEFINE 
	|{Keyword} FROM 
	|{Keyword} TABLE
	|{Keyword} WINDOW
	|{Keyword} RETURN
	|{Keyword} every=EVERY
;
EVERY: (every='every' );
fragment STREAM: (str='stream');
fragment DEFINE: (define='define' );
fragment TABLE: (table='table' );
fragment WINDOW: (window='window' );
fragment TRIGGER: (trigger='trigger' );
fragment AT: (at='at' );
fragment FUNCTION: (function='function');
fragment RETURN: (return='return');
fragment FROM: (from='from');

COL : ':';
DOT : '.';
MINUS : '-';


If DefinitionStream,DefinitionTable,DefinitionWindow etc like definition rules can be combined and form one rule this problem solves as I checked. But no way of combining all the definitions. Can you please give any clue if you can?
Thank you very much.
Re: SyntaxErrorMessageProvider custom implementation [message #1771195 is a reply to message #1771185] Tue, 22 August 2017 19:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
which syntax error message provider impl and test model do you use? i dont want to guess such things

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: SyntaxErrorMessageProvider custom implementation [message #1771210 is a reply to message #1771195] Wed, 23 August 2017 01:47 Go to previous messageGo to next message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi,

I use SyntaxErrorMessageProvider class as the super class. And below is a sample I used.

class MyDslErrorMessageProvider extends SyntaxErrorMessageProvider {
	
	public static val String USED_RESERVED_KEYWORD = "USED_RESERVED_KEYWORD"
	 
	override getSyntaxErrorMessage(IParserErrorContext context) {
		val unexpectedText = context?.recognitionException?.token?.text
		
	    if (GrammarUtil::getAllKeywords(siddhiGrammarAccess.getGrammar()).contains(unexpectedText)) {

            println(context.defaultMessage)

            return new SyntaxErrorMessage('''"«unexpectedText»" is a reserved keyword which is not allowed as Identifier.''',USED_RESERVED_KEYWORD)  

           }

        super.getSyntaxErrorMessage(context)
		
	}
	
	override getSyntaxErrorMessage(IValueConverterErrorContext context) {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}
	
}


What did u mean by the test model? A set of sample test cases would be:
define stream str1;
from str1;

define table table1;
from table1; 


A wrong input would be:
define table stream


Here the error should be reported at stream, but for now it is reported at define.

Thank you very much.
Re: SyntaxErrorMessageProvider custom implementation [message #1771258 is a reply to message #1771210] Wed, 23 August 2017 12:56 Go to previous messageGo to next message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi,
Isn't the grammar rule affect this error reporting? Or else can I customize the place where the error shows by traversing through the model?

Thank you.
Re: SyntaxErrorMessageProvider custom implementation [message #1771264 is a reply to message #1771258] Wed, 23 August 2017 13:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i dont get any syntax errors with that grammar.
please provide me with a complete example


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: SyntaxErrorMessageProvider custom implementation [message #1771286 is a reply to message #1771264] Wed, 23 August 2017 17:03 Go to previous messageGo to next message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi,

Some correct inputs would be:
define stream str1;
from str1;

define table table1;
from table1;


Some inputs that give syntax errors would be:
//1st input
define stream table
//2nd input
define table 
//3rd input
define table window
//4th input
define stream str1;
from


Here all the errors are reported at the beginning of the line.

Thank you very much.

[Updated on: Wed, 23 August 2017 17:04]

Report message to a moderator

Re: SyntaxErrorMessageProvider custom implementation [message #1771288 is a reply to message #1771286] Wed, 23 August 2017 17:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
well the problem is the grammar and the error recovery
the original error i get is

"no viable alternative at input 'define'" at the first token.
that is caused by the first token to be defined as bad by antlr.
i have no idea if the antlr behaviour can be changed.
=> sorry i cant help you with that



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: SyntaxErrorMessageProvider custom implementation [message #1771295 is a reply to message #1771288] Wed, 23 August 2017 18:05 Go to previous messageGo to next message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi,
Yes, that's true. Grammar has some problems. But when I put the below grammar by combining definitions of stream and table error reporting works fine. But trigger,window and function definitions cannot be merged like table and stream. If they can be merged the issue solves, but as for my knowledge I don't see a way to combine them without changing grammar.
Model:
	(elements += ExecutionPlan)*
;

ExecutionPlan:
	(=>defStream+=DefinitionStream)=>(';' 
	(=>defStream+=DefinitionStream|=>defTable+=DefinitionTable| =>def_window+=DefinitionWindow| =>defTrigger+=DefinitionTrigger|=>defFunction+=DefinitionFunction))*
	=>(';'(exElement+=ExecutionElement))* ';'?
;
ExecutionElement:
	(que=Query)
;
DefinitionStream:
	{DefinitionStream}(ann += Annotation)* DEFINE (STREAM|TABLE ) src=Source1 
;
DefinitionWindow:
	{DefinitionWindow}(ann2 += Annotation)* DEFINE WINDOW src=Source1  func_body=FunctionBody 
;
DefinitionTrigger:
	{DefinitionTrigger}(DEFINE TRIGGER) tn=TriggerName AT (every=EVERY sv=StringValue) 
;

DefinitionFunction:
	{DefinitionFunction}(DEFINE FUNCTION) fn=FunctionName '[' ln=LanguageName ']' RETURN func_body=FunctionBody 
;


Thank you for the quick reply.
Re: SyntaxErrorMessageProvider custom implementation [message #1771299 is a reply to message #1771295] Wed, 23 August 2017 18:58 Go to previous message
Udeshika Sewwandi is currently offline Udeshika SewwandiFriend
Messages: 118
Registered: March 2017
Senior Member
Hi,
Sorry the thing I mentioned above works not for this minimal grammar, but for the complete grammar. However my question is, is there a way to merge these stream,table,trigger,window and function definitions? I don't see any way to combine all of them together to form one rule. If it can be done the issue solves :).

[Updated on: Wed, 23 August 2017 18:59]

Report message to a moderator

Previous Topic:How to infer a method with generics?
Next Topic:Confusing error message when switching on type with rewrite rule
Goto Forum:
  


Current Time: Thu Apr 18 16:31:40 GMT 2024

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

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

Back to the top