|
|
|
|
|
Re: Problem with ID Terminal Rule [message #1782085 is a reply to message #1782083] |
Fri, 16 February 2018 20:26 |
Greg Lusk Messages: 36 Registered: February 2017 Location: USA |
Member |
|
|
Interestingly enough, the following test fails (as expected):
@Test def testParseIllegalVarName(){
parseFunctionBlock('''
FUNCTION_BLOCK 3V0
VAR_INPUT
e:BOOL;
E:BOOL;
X:NUMERIC:=1.23E-23;
END_VAR
VAR_OUTPUT
B:BOOL;
D:BOOL;
END_VAR
VAR
C(1):BOOL;
END_VAR
END_FUNCTION_BLOCK
''').assertNoErrors
}
But building a model with the Factory and trying to serialize an illegal variable name succeeds. I would expect that, if parsing faile, the semantic sequencer would throw an exception when backtracking during serialization too.
[Updated on: Fri, 16 February 2018 20:27] Report message to a moderator
|
|
|
Re: Problem with ID Terminal Rule [message #1782086 is a reply to message #1782083] |
Fri, 16 February 2018 20:30 |
|
so your question is about serialization and not parsing?
you need to implement a value converter service for that.
package org.xtext.example.mydsl;
import org.eclipse.xtext.common.services.DefaultTerminalConverters;
import org.eclipse.xtext.conversion.IValueConverter;
import org.eclipse.xtext.conversion.ValueConverter;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.conversion.impl.KeywordAlternativeConverter;
import org.eclipse.xtext.nodemodel.INode;
import com.google.inject.Inject;
public class MyDslValueConverterService extends DefaultTerminalConverters {
@Inject
private VarNameVC varNameVC;
@ValueConverter(rule = "Variable_Name")
public IValueConverter<String> Variable_Name() {
return varNameVC;
}
public static class VarNameVC implements IValueConverter<String> {
@Override
public String toValue(String string, INode node) throws ValueConverterException {
return string;
}
@Override
public String toString(String value) throws ValueConverterException {
if (value.contains("(")) {
throw new ValueConverterException("mimimi", null, null);
}
return value;
}
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Re: Problem with ID Terminal Rule [message #1782088 is a reply to message #1782087] |
Fri, 16 February 2018 21:05 |
|
yes and now. its not really easy.
you would have to build a combination of AbstractLexerBasedConverter
and QualifiedNameValueConverter and KeywordAlternativeConverter
calling the parser manually i huge overhead and might be extremely inperformant
public static class MyDslParser2 extends MyDslParser {
public IParseResult doParseVarName(CharSequence sequence) {
try {
return parse("Variable_Name", new ANTLRReaderStream(new StringReader(sequence.toString())));
} catch (IOException e) {
throw new WrappedException(e);
}
}
}
public static class VarNameVC implements IValueConverter<String> {
@Inject
MyDslParser2 p;
@Override
public String toValue(String string, INode node) throws ValueConverterException {
return string;
}
@Override
public String toString(String value) throws ValueConverterException {
IParseResult result = p.doParseVarName(value);
if (result.hasSyntaxErrors()) {
System.err.println(result.getSyntaxErrors());
throw new ValueConverterException("mimimi", null, null);
}
return value;
}
=> what about using a positive regex instead of opting out?
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04570 seconds