Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xbase] Scoping for local variables when extending Xbase expressions
[Xbase] Scoping for local variables when extending Xbase expressions [message #1711543] Sat, 17 October 2015 00:31 Go to next message
Steven Derrien is currently offline Steven DerrienFriend
Messages: 50
Registered: July 2009
Member
Hi,

I am working on a Xbase language extension to support tree based pattern matching, as illustrated in the example below.
As suggested in earlier post, I extended the XBase classes set with a few extra XExpression subtypes to support new
language constructs.

// Patterns definitions
pattern sub : Node (Node l,Node r) { ...}
pattern add : Node (Node l,Node r) { ...}
pattern ival : Node (int v) { ...}

class Example {

def Node foobar (Node x) {
match(x) {
// pattern matching rule
sub(add(ival(a),ival(b)),ival(c)) : {
// a, b,c are subclasses of XVariableDeclarations
ival(a+b-c);
}
}
}

def void test() {
val input = sub(add(ival(4),ival(5)),ival(8))
println(input+" = "+foobar(input));
}
}

However, I am stuck with scoping/typing for local variables with these classes.
In particular, I can’t make local XVariableDefinition (the free variables a,b,c from the pattern)
visible in the body of the rule (i.e the one that computes ival (a+b-c)).

sub(add(ival(a),ival(b)),ival(c)) : {
// a, b,c are subclasses of XVariableDeclarations
ival(a+b-c);
}

Whenever I try to refer to one of this variable I get the following error message : « Couldn't resolve reference to
JvmIdentifiableElement 'a'”. After dwelving in the xtext 2.8 source code, I was able to understand that local variable
scoping was apparently managed by ITypeComputer. I then wrote my own type computer by extending XBaseTypeComputer and
using the same API calls to register local variable (ITypeComputationState.addLocalToCurrentScope()) and manage scope
(ITypeComputationState.withinScope()).

Although I tried to mimic as much as possible the API usage pattern found in the code (for XBlockExpression in
particular), I still can’t make these local variable visible. The function I use to do so is given below. The
addLocalToCurrentScope call happen as expected, but this does not seem to have any impact.

Any help is welcome : did I miss something obvious, am looking at the wrong place ?


def dispatch computeXTomTypes(XTomMatchingRule rule, ITypeComputationState state) {
// XTomMatchingRule extends XBlockExpression
val children = rule.expressions
// rule patterns sub(add(a,b),c) is a single pattern
val rulePatterns = children.subList(0, children.size - 1)
// rule body (what is executed id the pattern matches)
val ruleBody= children.last
//var thenState = state.withTypeCheckpoint(ruleBody)
var thenState = state.withoutExpectation;
thenState.withinScope(rule)
thenState.computeTypes(ruleBody)
for (XExpression e : rulePatterns) {
// search for all XTomVariableDeclaration in patterns
// in our example, we get three declarations for a,b,c
val decList = e.eAllContents.filter(XTomVariableDeclaration).toList;
if (decList.size > 0) {
for (XTomVariableDeclaration dec : decList) {
// we add the local variables to the scope
addLocalToCurrentScope(dec, thenState)
}
}
}
}

Thank you very much in advance,

Steven
Re: [Xbase] Scoping for local variables when extending Xbase expressions [message #1711597 is a reply to message #1711543] Sat, 17 October 2015 21:52 Go to previous message
Steven Derrien is currently offline Steven DerrienFriend
Messages: 50
Registered: July 2009
Member
Hi,

Problem solved, local variable need to be added to the scope *before* we computing their types.
The correct code is given below just in case ...

def dispatch computeXTomTypes(XTomMatchingRule rule, ITypeComputationState state) {
// XTomMatchingRule extends XBlockExpression
val children = rule.expressions>
// rule patterns sub(add(a,b),c) is a single pattern
val rulePatterns = children.subList(0, children.size - 1)
// rule body (what is executed id the pattern matches)
val ruleBody= children.last
//var thenState = state.withTypeCheckpoint(ruleBody)>
var thenState = state.withoutExpectation;
thenState.withinScope(rule)
for (XExpression e : rulePatterns) {
// search for all XTomVariableDeclaration in patterns
// in our example, we get three declarations for a,b,c
val decList = e.eAllContents.filter(XTomVariableDeclaration).toList;
if (decList.size > 0) {
for (XTomVariableDeclaration dec : decList) {
// we add the local variables to the scope
addLocalToCurrentScope(dec, thenState)
}
}
}
// MOVED HERE
thenState.computeTypes(ruleBody)
}


Le 17/10/2015 02:31, Steven Derrien a écrit :
> Hi,
>
> I am working on a Xbase language extension to support tree based pattern matching, as illustrated in the example below.
> As suggested in earlier post, I extended the XBase classes set with a few extra XExpression subtypes to support new
> language constructs.
>
> // Patterns definitions
> pattern sub : Node (Node l,Node r) { ...}
> pattern add : Node (Node l,Node r) { ...}
> pattern ival : Node (int v) { ...}
>
> class Example {
>
> def Node foobar (Node x) {
> match(x) {
> // pattern matching rule
> sub(add(ival(a),ival(b)),ival(c)) : {
> // a, b,c are subclasses of XVariableDeclarations
> ival(a+b-c);
> }
> }
> }
>
> def void test() {
> val input = sub(add(ival(4),ival(5)),ival(8))
> println(input+" = "+foobar(input));
> }
> }
>
> However, I am stuck with scoping/typing for local variables with these classes.
> In particular, I can’t make local XVariableDefinition (the free variables a,b,c from the pattern)
> visible in the body of the rule (i.e the one that computes ival (a+b-c)).
>
> sub(add(ival(a),ival(b)),ival(c)) : {
> // a, b,c are subclasses of XVariableDeclarations
> ival(a+b-c);
> }
>
> Whenever I try to refer to one of this variable I get the following error message : « Couldn't resolve reference to
> JvmIdentifiableElement 'a'”. After dwelving in the xtext 2.8 source code, I was able to understand that local variable
> scoping was apparently managed by ITypeComputer. I then wrote my own type computer by extending XBaseTypeComputer and
> using the same API calls to register local variable (ITypeComputationState.addLocalToCurrentScope()) and manage scope
> (ITypeComputationState.withinScope()).
>
> Although I tried to mimic as much as possible the API usage pattern found in the code (for XBlockExpression in
> particular), I still can’t make these local variable visible. The function I use to do so is given below. The
> addLocalToCurrentScope call happen as expected, but this does not seem to have any impact.
>
> Any help is welcome : did I miss something obvious, am looking at the wrong place ?
>
>
> def dispatch computeXTomTypes(XTomMatchingRule rule, ITypeComputationState state) {
> // XTomMatchingRule extends XBlockExpression
> val children = rule.expressions
> // rule patterns sub(add(a,b),c) is a single pattern
> val rulePatterns = children.subList(0, children.size - 1)
> // rule body (what is executed id the pattern matches)
> val ruleBody= children.last
> //var thenState = state.withTypeCheckpoint(ruleBody)
> var thenState = state.withoutExpectation;
> thenState.withinScope(rule)
> thenState.computeTypes(ruleBody)
> for (XExpression e : rulePatterns) {
> // search for all XTomVariableDeclaration in patterns
> // in our example, we get three declarations for a,b,c
> val decList = e.eAllContents.filter(XTomVariableDeclaration).toList;
> if (decList.size > 0) {
> for (XTomVariableDeclaration dec : decList) {
> // we add the local variables to the scope
> addLocalToCurrentScope(dec, thenState)
> }
> }
> }
> }
>
> Thank you very much in advance,
>
> Steven
Previous Topic:2.5.0: Problems using Standalone Xtend with Xpand code
Next Topic:add certain files to global scope
Goto Forum:
  


Current Time: Tue Apr 23 06:29:32 GMT 2024

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

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

Back to the top