Mismatched character [message #1838634] |
Tue, 02 March 2021 17:49 |
M D Messages: 33 Registered: January 2021 |
Member |
|
|
I'm trying to develop a DSL that uses JSON. But I've run into a problem with the following grammar:
grammar org.xtext.example.mydsl.MyDsl hidden(WS, ML_COMMENT, SL_COMMENT)
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
'[' objects += Object (',' objects += Object)* ']'
;
Object:
'{' '"name"' ':' '"' name = STRING '"' ',' '"type"' ':' '"object"' ',' '"isOptional"' ':' value = BOOLEAN ',' '"dataType"' ':' '"' dataType = PredefinedType '"' '}'
;
PredefinedType:
type = ('String' | 'int' | 'float' | 'boolean')
;
terminal STRING: "'" ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|"'") )* "'";
terminal BOOLEAN: 'true' | 'false';
terminal ML_COMMENT : '/*' -> '*/';
terminal SL_COMMENT : '//' !('\n'|'\r')* ('\r'? '\n')?;
terminal WS : (' '|'\t'|'\r'|'\n')+;
terminal ANY_OTHER: .;
The DSL:
[
{
"name": "'Test'",
"type": "object",
"isOptional": false,
"dataType" : "int"
}
]
It gives error "mismatched charachter 'n' expections 's'" at int. It want me to write isOptional instead of int. I've tried changed '"isOptional"' to '"optional"' in the grammar, which fixes the issue. But I'm curious about why I get the error, and why changing the keyword fixes it. Any ideas?
[Updated on: Tue, 02 March 2021 17:51] Report message to a moderator
|
|
|
Re: Mismatched character [message #1838640 is a reply to message #1838634] |
Tue, 02 March 2021 18:37 |
|
the lexer is greedy and thus cannot handle
'"isOptional"' vs '"' 'int' '"'
you can either enable backtracking
parserGenerator = {
options = {
backtrackLexer = true
}
}
or use terminals consistently
e.g.
Object:
'{' '"name"' ':' '"' name = STRING '"' ','
'"type"' ':' '"object"' ','
'"isOptional"' ':' value = BOOLEAN ','
'"dataType"' ':' dataType = PREDEFINED_TYPE '}'
;
terminal PREDEFINED_TYPE:
'"' ('String' | 'int' | 'float' | 'boolean')'"'
;
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Re: Mismatched character [message #1838670 is a reply to message #1838649] |
Wed, 03 March 2021 11:03 |
M D Messages: 33 Registered: January 2021 |
Member |
|
|
Thanks for the explanation!
I've encountered another problem. I try to make it so the user can define their own types. dataType can then either be a predefined type or an already defined type.
If I do the following, I get a "NullPointerException: null" at the beginning of the grammar. You know why?
Model:
'[' objects += Object (',' objects += Object)* ',' '{' '"definedType"' ':' definedType = DefinedType '}' ']'
;
Object:
'{' '"name"' ':' '"' name = STRING '"' ',' '"type"' ':' '"object"' ',' '"isOptional"' ':' value = BOOLEAN ',' '"dataType"' ':' dataType = ([DefinedType|STRING] | PREDEFINED_TYPE) '}'
;
DefinedType:
'"' name = STRING '"'
;
terminal PREDEFINED_TYPE:
'"' ('String' | 'int' | 'float' | 'boolean') '"';
terminal STRING: "'" ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|"'") )* "'";
[Updated on: Wed, 03 March 2021 17:11] Report message to a moderator
|
|
|
|
Re: Mismatched character [message #1838801 is a reply to message #1838800] |
Fri, 05 March 2021 08:59 |
|
dataType = ([DefinedType|STRING] | PREDEFINED_TYPE is an antipattern.
without a stacktrace hard to jude
maybe
dataType =AbstractDataType
AbstractDataType: PrimiteDataType | DataTypeRef;
PrimiteDataType: type= PREDEFINED_TYPE;
DataTypeRef: ref=[DefinedType|STRING];
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Powered by
FUDForum. Page generated in 0.03956 seconds