Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Extend xbase with a more expressive range
Extend xbase with a more expressive range [message #1831563] Sun, 23 August 2020 18:34 Go to next message
Eclipse UserFriend
Hi,

I am trying to extend Xbase to provide a more expressive range type. Basically I want to be able to write code like below:

var x = 9
var y = 15.0
(x..15)  // evaluate to Range<Integer>.open(x,15)
[x..15) // evaluate to Range<Integer>.closedOpen(x, 15)
[x..y]  // error, x and y have different types


Questions are:
1. Which type should I override in Xbase grammar? I was trying with XOtherOperatorExpression, but not sure if this is the right choice.
2. How to write the grammar? My version below gets error when tried to generate artifact from xtext.

@Override
XOtherOperatorExpression returns XExpression:
	{Range}
	(open='('|'[' left=XAdditiveExpression  '..' right=XAdditiveExpression close= ')'|']') | super
;


Thanks in advance for any insight or hints!
Re: Extend xbase with a more expressive range [message #1831567 is a reply to message #1831563] Mon, 24 August 2020 00:30 Go to previous messageGo to next message
Eclipse UserFriend
Hi

You don't say what your problem is but it looks to preciselty what I have with OCL. From:

https://git.eclipse.org/r/plugins/gitiles/ocl/org.eclipse.ocl/+/refs/heads/master/plugins/org.eclipse.ocl.xtext.base/src/org/eclipse/ocl/xtext/base/services/RetokenizingTokenSource.java

/**
 * RetokenizingTokenSource accomodates backtracking limitations in the ANTRL lexer when used from Xtext.
 * 
 * The problem is the three overlapping syntaxes
 * INT.INT leading to a Floating Point lteral
 * INT..INT leading to a Collection range
 * INT.ID leading to a numeric navigation
 * 
 * ANTLR proceeds to INT. but won't backup when the character after the . is bad.
 * 
 * The code here allows the basic lexer to be ignorant of floating point syntax so that it correctly parses
 * INT.INT as INT DOT INT
 * INT..INT as INT DOTDOT INT
 * INT.INTe+INT as INT DOT INT ID PLUS INT
 * so the code here recognises the floating point literal and reconsttructs. As an additional benefit 'e' and 'E' are not keywords.
 */


Regards

Ed Willink
Re: Extend xbase with a more expressive range [message #1831568 is a reply to message #1831567] Mon, 24 August 2020 01:24 Go to previous messageGo to next message
Eclipse UserFriend
Ed,

Thanks for the hint. What you said makes sense but I am afraid that I have not hit that yet. Here is the error:
error(211): ../...omitted.../parser/antlr/internal/InternalScg.g:238:2: [fatal] rule ruleXOtherOperatorExpression has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
warning(200): ../...omitted.../parser/antlr/internal/InternalScg.g:238:2: Decision can match input such as "'(' ']'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
Semantic predicates were present but were hidden by actions.
warning(200): ../...omitted.../parser/antlr/internal/InternalScg.g:2449:6: Decision can match input such as "')'" using multiple alternatives: 2, 3
As a result, alternative(s) 3 were disabled for that input
Semantic predicates were present but were hidden by actions.
error(201): ../...omitted.../antlr/internal/InternalScg.g:2449:6: The following alternatives can never be matched: 3
warning(200): ../...omitted.../parser/antlr/internal/InternalScg.g:2994:3: Decision can match input such as "{RULE_STRING..RULE_DECIMAL, ']'..')', '['..'(', '{', '<', '+'..'-', '!', '#', 'if', 'switch', 'for'..'do', 'super'..'try', 'synchronized'}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
Semantic predicates were present but were hidden by actions.
error(201): ../...omitted.../parser/antlr/internal/InternalScg.g:2994:3: The following alternatives can never be matched: 2

warning(200): ../...omitted.../parser/antlr/internal/InternalScg.g:3246:3: Decision can match input such as "{RULE_ID..RULE_DECIMAL, ']'..')', '['..'(', '{', '<', '+'..'-', '!', '#', 'if', 'switch', 'for'..'try', 'synchronized'}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
Semantic predicates were present but were hidden by actions.
error(211): ../...omitted.../parser/antlr/internal/InternalScg.g:4067:3: [fatal] rule ruleXBasicForLoopExpression has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
warning(200): ../...omitted.../parser/antlr/internal/InternalScg.g:4767:4: Decision can match input such as "')'" using multiple alternatives: 2, 3
As a result, alternative(s) 3 were disabled for that input
Semantic predicates were present but were hidden by actions.
error(201): ../...omitted.../antlr/internal/InternalScg.g:4767:4: The following alternatives can never be matched: 3

warning(200): ../...omitted.../parser/antlr/internal/InternalScg.g:5100:4: Decision can match input such as "')'" using multiple alternatives: 2, 3
As a result, alternative(s) 3 were disabled for that input
Semantic predicates were present but were hidden by actions.
error(201): ../...omitted.../parser/antlr/internal/InternalScg.g:5100:4: The following alternatives can never be matched: 3


So I am afraid it was my using of parenthesis/brackets causing problem.
If I have to write my own lexer, could you point me to some good tutorials? Thanks. The OCL work is pretty cool but perhaps is too big for me to mimic.
Re: Extend xbase with a more expressive range [message #1831569 is a reply to message #1831568] Mon, 24 August 2020 01:29 Go to previous messageGo to next message
Eclipse UserFriend
as xbase already has

..
..<
>..

why not simply take these?
+ add overloads to the methods like

operator_upTo
operator_doubleDotLessThan
operator_greaterThanDoubleDot

[Updated on: Mon, 24 August 2020 01:39] by Moderator

Re: Extend xbase with a more expressive range [message #1831570 is a reply to message #1831568] Mon, 24 August 2020 01:56 Go to previous messageGo to next message
Eclipse UserFriend
I was able to get rid of all the warnings by fixing the grammar as below (notice the right hand sides of `open` and `close` are now in ( ).

@Override
XOtherOperatorExpression returns XExpression:
	{Range}
	(open=('('|'[') left=XAdditiveExpression  '..' right=XAdditiveExpression) close= (')'|']') | super
;


But the error is still there:
error(211): ../..omitted../parser/antlr/internal/InternalScg.g:238:2: [fatal] rule ruleXOtherOperatorExpression has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
3916 [main] INFO  text.xtext.generator.XtextGenerator  - Generating common infrastructure
...
Re: Extend xbase with a more expressive range [message #1831571 is a reply to message #1831569] Mon, 24 August 2020 02:17 Go to previous messageGo to next message
Eclipse UserFriend
Chris,

Thanks for the suggestion. Could you elaborate it a little bit more? I want to use ( and ) to indicate exclusive boundaries, [ and ] to indicate inclusive boundaries.

Also, how do I overload operator_upTo etc?

Thanks.
Re: Extend xbase with a more expressive range [message #1831586 is a reply to message #1831571] Mon, 24 August 2020 05:58 Go to previous message
Eclipse UserFriend
would add a
>..< for both exlusive
operator mapping is done through

org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedFeatures.getExtensionClasses()
which registers the methods in IntegerExtensions
Previous Topic:How can I override STRING?
Next Topic:Autocomplete suggestion files in project
Goto Forum:
  


Current Time: Wed Jul 23 16:21:12 EDT 2025

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

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

Back to the top