Workaround semantic predicates [message #1851634] |
Mon, 11 April 2022 14:04 |
Antonio Garmendia Messages: 93 Registered: May 2014 |
Member |
|
|
Hi all,
I am currently working on mapping the concepts of JSON Schema (https://json-schema.org/) to Ecore+OCL, and then, generating an Xtext grammar that has json as concrete syntax.
We have a problem with the specifications of PatternProperties [3]. According to the Pattern Properties specifications, when a key matches a given regular expression [4], its value has to conform to a given schema.
In the excerpt of the grammar shown below, when a key matches the regex "a|b", the value has to be parsed as (Some other schema 1), and when a key matches "c|d" the value has to be parsed as (Some other schema 2).
Properties returns Properties:
{Properties}
'{' ( property+=PatternProperties ( ',' property += PatternProperties)* )? '}';
PatternProperties returns PatternProperties:
=>PatternProperties1 | =>PatternProperties2;
// key has to match the regex "a|b"
PatternProperties1 returns PatternProperties1:
{PatternProperties1} key = EString ':' value= ( Some other schema 1) ;
// key has to match the regex "c|d"
PatternProperties2 returns PatternProperties2:
{PatternProperties2} key = EString ':' value= ( Some other schema 2) ;
This case could be solved with semantic predicates, where the condition would be the key matching with the given regular expression. But as far as we know, Xtext does not support the semantic predicate [1].
We have been trying different workarounds, like [2] without success.
Is there any work around in Xtext that solves our problem?
Thank you in advance.
Best regards
[1] https://www.eclipse.org/forums/index.php/t/202846/
[2] https://dslmeinte.wordpress.com/2010/12/08/getting-alternative-cross-references-to-work-with-existing-epackages/
[3]https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties
[4]https://json-schema.org/understanding-json-schema/reference/regular_expressions.html#regular-expressions
|
|
|
Re: Workaround semantic predicates [message #1851702 is a reply to message #1851634] |
Tue, 12 April 2022 19:21 |
Ed Willink Messages: 7669 Registered: July 2009 |
Senior Member |
|
|
Hi
Semantics predicates are one way to go, but not my favoured approach at all, since the basic premise is that you start from a bad grammar and force its ambiguities in a 'good' direction. This can work, but in my experience it can easily leading to silently excluding an alternative that the user would prefer. Attempting to resolve the problem in the parser is hard if not impossible because you have to make a decision while the source text is only half parsed. You can of course throw yourself on the mercies of backtracking and keep your fingers crossed.
Consequently I prefer a different approach. Preferably you change the language to eliminate the ambiguities, but in the real world external / legacy compatibility forces something else, for which the solution is what I call a superset grammar whereby each ambiguity is accommodated bv a unified grammar production that parses all alternatives. The parse therefore proceeds without trouble, however in a subsequent analysis phase, possibly in an overload of Xtext's afterModelLinked you can exploit the fully superset-parsed source to decide which of the alternatives is actually appropriate and elaborate/rewrite the CS tree accordingly. You will find that this approach is taken to resolve some nasty characteristics of the OCL grammar. I pioneered the approach in [1] chapter 5 and so successfully produced what was, possibly still is, the only yaccable grammar for C++. It resolves the arithmetic < / template < ambiguity without predicates or backtracking..
This approach is aided by using an LALR parser so that conflicts are noisily revealed. For this, I use [2] to convert my Xtext grammar to LPG to get shift-reduce / reduce-reduce conflict reports. I also use a manually defined Ecore metamodel since I do not find the auto-generated Ecore flexible enough.
Regards
Ed Willink
[1] http://www.computing.surrey.ac.uk/research/dsrg/fog/FogThesis.pdf
[2] GIT\org.eclipse.ocl\examples\org.eclipse.ocl.examples.xtext2lpg
|
|
|
|
Powered by
FUDForum. Page generated in 0.03797 seconds