[go: up one dir, main page]

Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to remove the '\' before each single quote in a string added by the formatter ?
How to remove the '\' before each single quote in a string added by the formatter ? [message #1859888] Tue, 04 July 2023 07:10 Go to next message
Ronan Babin is currently offline Ronan BabinFriend
Messages: 18
Registered: July 2023
Junior Member
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 #1859901 is a reply to message #1859888] Tue, 04 July 2023 15:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
can you check the value converter you use for the STRING terminal?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
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 11:57 Go to previous messageGo to next message
Ronan Babin is currently offline Ronan BabinFriend
Messages: 18
Registered: July 2023
Junior Member
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 11:58]

Report message to a moderator

Re: How to remove the '\' before each single quote in a string added by the formatter ? [message #1859932 is a reply to message #1859918] Thu, 06 July 2023 04:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
The value converter has a method that convert from text to model (to value) and vice versa (tostring)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com

[Updated on: Thu, 06 July 2023 04:15]

Report message to a 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 13:34 Go to previous message
Ronan Babin is currently offline Ronan BabinFriend
Messages: 18
Registered: July 2023
Junior Member
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.
Previous Topic:File extension conflict
Next Topic:Hover service on keywords (language server)
Goto Forum:
  


Current Time: Wed Dec 04 18:02:47 GMT 2024

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

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

Back to the top