How to remove the '\' before each single quote in a string added by the formatter ? [message #1859888] |
Tue, 04 July 2023 03:10  |
Eclipse User |
|
|
|
Hello,
I'm taking over a project from a colleague who recently retired and I'm slowly getting into the xtext world.
Our project is processing specific language provided by the supplier tool.
A grammar file has been built to support the language and I am using serialization/deserialization to xmi.
My problem is coming from the deserialization from .xmi to the proprietary language.
Example of specific language:
__Row {
__Pins = __Expression { __String = "SWData_pl"; __Type = PIN; }
__InstrumentMode = __Expression { __String = "'GX1:PWRMODE:HIVOLTAGE'"; __Type = STRING; }
__InstrumentMode = __Expression { __String = "'GX1:SLEWRATE:MAX'"; __Type = STRING; }
__InstrumentMode = __Expression { __String = "'MP1:EXT_XOR:DISABLED'"; __Type = STRING; }
}
Below is the rule I'm using
PinConfigRow:
'__Row' '{'
'__Pins' '=' '__Expression' '{' '__String' '=' pins=STRING ';' '__Type' '=' pins_type=PinTyp1 ';' '}'
('__InstrumentMode' '=' '__Expression' '{' '__String' '=' instrument_mode+=STRING '__Type' '=' 'STRING' ';' '}')+
'}';
terminal STRING :
'"' ( ESCAPED_CHAR | !('\\'|'"') )* '"' |
"'" ( ESCAPED_CHAR | !('\\'|"'") )* "'"
;
In the experiment, I serialize to xmi the input file and restore it back to validate the formatter I built is properly written.
What I saw is that for the "keyword" __InstrumentMode, the formatter is adding extra '\' in front of each single quote character found in the string.
__Row {
__Pins = __Expression { __String = "SWData_pl"; __Type = PIN; }
__InstrumentMode = __Expression { __String = "\'GX1:PWRMODE:HIVOLTAGE\'"; __Type = STRING; }
__InstrumentMode = __Expression { __String = "\'GX1:SLEWRATE:MAX\'"; __Type = STRING; }
__InstrumentMode = __Expression { __String = "\'MP1:EXT_XOR:DISABLED\'"; __Type = STRING; }
}
The string format is indeed different between the keyword __Pins and the keyword __InstrumentMode. The second one is surrounded by single quotes in addition to the standard double quotes for strings. Is there a way to change the formatter to not include those single quotes ? Or maybe I could optimize the grammar to take care of different string format ?
What do you think ?
If you think I did not provided enough information let me know.
Regards,
|
|
|
|
Re: How to remove the '\' before each single quote in a string added by the formatter ? [message #1859918 is a reply to message #1859901] |
Wed, 05 July 2023 07:57   |
Eclipse User |
|
|
|
I'm not sure what the value converter is used for...
Searching in the project, I'm seeing that a specific value converter has been added for the STRING terminal.
public class UnaStringValueConverter extends STRINGValueConverter
{
....
}
The method convertFromString() has been override with a small change. In the switch... case processing the escaped character, there is one new one added. The rest of the code is identical.
...
case '\'':
out[outLen++] = '\'';
break;
case '\\':
out[outLen++] = '\\';
break;
case '/': // This is the new one added !!!!!
out[outLen++] = '/';
break;
default:
I've just ran the debugger on this class to see the input and the output.
The input parameter 'literal' value is: "\'GX1:SLEWRATE:MAX\'".
At the end of the function, on the return line, the parameter 'out' of the String constructor is: [', G, X, 1, :, S, L, E, W, R, A, T, E, :, M, A, X, ', , , ].
But I don't really know how I should interpret this and what the function is supposed to do.
[Updated on: Wed, 05 July 2023 07:58] by Moderator
|
|
|
|
Re: How to remove the '\' before each single quote in a string added by the formatter ? [message #1859944 is a reply to message #1859932] |
Thu, 06 July 2023 09:34  |
Eclipse User |
|
|
|
Hi Christian,
I played around the 2 functions you mentioned and figured out a workaround by overriding the method toEscapedString().
@Override
protected String toEscapedString(String value)
{
// For specific strings from uno file with double and single quotes ("'<your string>'")
if ( value.startsWith( "'" ) )
{
String new_string = Strings.convertToJavaString(value, false);
new_string = new_string.replace("\\", "");
return '"' + new_string + '"';
}
return '"' + Strings.convertToJavaString(value, false) + '"';
}
This may not be the cleaner solution but it is working in my case.
Thanks for the support.
|
|
|
Powered by
FUDForum. Page generated in 0.08105 seconds