Illegal escape character in json grammar for character that is in the grammar [message #1072331] |
Mon, 22 July 2013 10:37  |
Eclipse User |
|
|
|
Consider this json grammar where the STRING terminal allows for the backslash "\" to escape a number of character including the solidus "/".
grammar com.test.example.Json hidden(WS)
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate json "http://www.test.com/example/Json"
RootObject returns JsonObject:
(JsonObject | EmptyObject)?;
JsonObject:
{JsonObject} '{' (members+=Member) (',' members+=Member)* '}';
EmptyObject returns JsonObject:
{JsonObject} '{' '}';
Member:
key=STRING ':' name=Value;
Value:
JsonObject | EmptyObject | StringValue | Array | EmptyArray | Boolean | Null | NumberValue;
NumberValue:
name=NUMBER;
StringValue:
name=STRING;
Array:
{Array} '[' ( => values+=Value) (',' values+=Value)* ']';
EmptyArray returns Array:
{Array} '[' ']';
Boolean:
{Boolean} (name='true' | 'false');
Null:
{Null} name='null';
terminal NUMBER:
'-'? INT ('.' INT)? (('E' | 'e') '-'? INT)?;
terminal INT returns ecore::EInt:
('0'..'9')+;
terminal STRING returns ecore::EString:
'"' ( '\\' ('"'|'/'|'\\'|'b'|'f'|'n'|'r'|'t') | !('\\'|'"') )* '"';
terminal WS:
(' '|'\t'|'\r'|'\n')+;
The editor for this grammar puts an error mark in eclipse when a string contains the "\/" sub-string, but not for other escapable characters like \n, \t, \\ or \".
Ex.
This Works
{
"test": "\t\"\n\b\f\\\r"
}
This does not work
{
"test": "\t\"\n\b\f\\\r\/"
}
I covered the basics like made sure that I generate the latest version of my grammar and cleaned my build and stuff.
Why, could this be a bug in xtext?
|
|
|
|
|
|
|
|
|
Re: Illegal escape character in json grammar for character that is in the grammar [message #1075371 is a reply to message #1073136] |
Mon, 29 July 2013 06:07   |
Eclipse User |
|
|
|
Hello,
sorry for reviving this thread again - I have now tried to work with the ValueConverter - Strings containing '\_' can be parsed now - however, Strings only containing a \, i.e. '\', still do not work and won't even lead to Illegal Escape Character Issues - the parsing will just break completely and there will be lots of syntactic errors as the String can't be read correctly.
However, shouldn't defining "'\\'" in my String terminal explicity allow such a String to exist?
Model:
assign+=STRING;
terminal STRING :
("\'\\\'") | ("'" ( '\\' ('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') | !('\\'|"'") )* "'")
;
Entering '\' leads to an error message - the error message states "1". Which is quite confusing.
--
Further edit:
Defining STRING as "\\", i.e. only the escape character, and then trying it out, leads to "String Index out of Range: -1" - seems to be a problem in the ValueConverter anyway, then?
---
More Editing:
Defining Strings as "\' \\ \'" also works as expected, with ' \ ' being parsed correctly as a String. Without the second space, it stops working - the \ seems to be seen as an escape for ' in any case, then.
Attachment: 1.png
(Size: 31.52KB, Downloaded 226 times)
[Updated on: Mon, 29 July 2013 06:41] by Moderator
|
|
|
Re: Illegal escape character in json grammar for character that is in the grammar [message #1075400 is a reply to message #1075371] |
Mon, 29 July 2013 07:06  |
Eclipse User |
|
|
|
How i solved the issue:
First I explicitly disallowed the \' escaping.
Doing that led to the "1" error message, which still strikes me as a bug.
Following that I edited my ValueConverter (convertFromJavaString) so that it doesn't enter its further conversion on reading \ if the length of the read String is 1, i.e. adding the second clause in the following code if (aChar == '\\' && len>1)
Possibly not clean, but it seems to get the job done.
However, as mentioned, I still believe the "1" error message and the String Index out of Range message are bugs.
[Updated on: Mon, 29 July 2013 07:07] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 1.09629 seconds