Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Default Scope Shadowing
Default Scope Shadowing [message #1716439] Thu, 03 December 2015 15:26 Go to next message
Eclipse UserFriend
I'm working through Lorenzo Bettini's book, and have gotten to the section on scoping. I'm reading the ebook, and at location 4124, he briefly discusses shadowing and proposes this test to verify that the default ScopeProvider will properly provide shadowing.

	@Test def void testVariableShadowsParamLinking() {
		'''
		class A {
			A m(A a) {
				A a = null;
				return a;
			}
		}
		'''.parse.classes.head.methods.head => [
			assertNoErrors
			// the local variable should shadow the param
			body.statements.head.assertSame(
				(returnStatement.expression as SJSymbolRef).symbol)
		]
	}


However, this test fails for me with the default provider. Am I missing something, or is this just an incorrect statement from the book, or something that worked "out of the box" in prior versions of Xtext? The test passes with the custom ScopeProvider implementation presented later in the chapter.

Thanks!
Re: Default Scope Shadowing [message #1716448 is a reply to message #1716439] Thu, 03 December 2015 18:57 Go to previous messageGo to next message
Eclipse UserFriend
Specifically, implementation of this code in SmallJavaScopeProvider makes things behavior as expected:


	def scope_SJSymbolRef_symbol(SJExpression context, EReference r) {
		context.eContainer.symbolsDefinedBefore(context)
	}
	
	def dispatch IScope symbolsDefinedBefore(EObject cont, EObject o) {
		cont.eContainer.symbolsDefinedBefore(o.eContainer)
	}
	
	def dispatch IScope symbolsDefinedBefore(SJMethod m, EObject o) {
		Scopes::scopeFor(m.params) // ends recursion
	}
	
	def dispatch IScope symbolsDefinedBefore(SJBlock b, EObject o) {
		Scopes::scopeFor(b.statements.variablesDeclaredBefore(o),
			// outer scope
			b.eContainer.symbolsDefinedBefore(o.eContainer))
	}
	
	def private variablesDeclaredBefore(List<SJStatement> list, EObject o) {
		list.subList(0,list.indexOf(o)).filter(SJVariableDeclaration)
	}

Re: Default Scope Shadowing [message #1716483 is a reply to message #1716439] Fri, 04 December 2015 05:24 Go to previous messageGo to next message
Eclipse UserFriend
On 03/12/2015 21:26, Larry LeBron wrote:
> I'm working through Lorenzo Bettini's book, and have gotten to the
> section on scoping. I'm reading the ebook, and at location 4124, he
> briefly discusses shadowing and proposes this test to verify that the
> default ScopeProvider will properly provide shadowing.
>
>
> @Test def void testVariableShadowsParamLinking() {
> '''
> class A {
> A m(A a) {
> A a = null;
> return a;
> }
> }
> '''.parse.classes.head.methods.head => [
> assertNoErrors
> // the local variable should shadow the param
> body.statements.head.assertSame(
> (returnStatement.expression as SJSymbolRef).symbol)
> ]
> }
>
>
> However, this test fails for me with the default provider. Am I missing
> something, or is this just an incorrect statement from the book, or
> something that worked "out of the box" in prior versions of Xtext? The
> test passes with the custom ScopeProvider implementation presented later
> in the chapter.

Hi

how does this test fail with the default scope provider? I mean, does
assertNoErrors fail or the other assert?

cheers
Lorenzo


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book
Re: Default Scope Shadowing [message #1717223 is a reply to message #1716483] Thu, 10 December 2015 15:28 Go to previous messageGo to next message
Eclipse UserFriend
Sorry, I didn't see that you had responded here. It is the second assert which reports the error:

body.statements.head.assertSame((returnStatement.expression as SJSymbolRef).symbol)


The error is:

java.lang.AssertionError: expected same:<org.example.smalljava.smallJava.impl.SJVariableDeclarationImpl@235a0c16 (name: a)> was not:<org.example.smalljava.smallJava.impl.SJParameterImpl@4b9df8a (name: a)>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotSame(Assert.java:828)
at org.junit.Assert.assertSame(Assert.java:771)
at org.junit.Assert.assertSame(Assert.java:782)
at org.example.smalljava.tests.SmallJavaScopeProviderTest.lambda$2(SmallJavaScopeProviderTest.java:159)
at org.example.smalljava.tests.SmallJavaScopeProviderTest$$Lambda$5/1426725223.apply(Unknown Source)
at org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_doubleArrow(ObjectExtensions.java:139)
at org.example.smalljava.tests.SmallJavaScopeProviderTest.testVariableShadowsParamLinking(SmallJavaScopeProviderTest.java:161)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.eclipse.xtext.junit4.XtextRunner$1.evaluate(XtextRunner.java:49)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)




Re: Default Scope Shadowing [message #1729455 is a reply to message #1716439] Thu, 14 April 2016 05:40 Go to previous message
Eclipse UserFriend
On 03/12/2015 21:26, Larry LeBron wrote:
> I'm working through Lorenzo Bettini's book, and have gotten to the
> section on scoping. I'm reading the ebook, and at location 4124, he
> briefly discusses shadowing and proposes this test to verify that the
> default ScopeProvider will properly provide shadowing.
>
>
> @Test def void testVariableShadowsParamLinking() {
> '''
> class A {
> A m(A a) {
> A a = null;
> return a;
> }
> }
> '''.parse.classes.head.methods.head => [
> assertNoErrors
> // the local variable should shadow the param
> body.statements.head.assertSame(
> (returnStatement.expression as SJSymbolRef).symbol)
> ]
> }
>
>
> However, this test fails for me with the default provider. Am I missing
> something, or is this just an incorrect statement from the book, or
> something that worked "out of the box" in prior versions of Xtext? The
> test passes with the custom ScopeProvider implementation presented later
> in the chapter.
>
> Thanks!

Hi

while working on this chapter for the 2nd edition I confirm that with
the default implementation of scope provider the test fails; probably
something has changed in Xtext after the book was written, sorry about
that. Of course, I fixed that in the new edition :)

cheers
Lorenzo

--
Prof. Lorenzo Bettini, Computer Science, DISIA, Univ. Firenze
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book
Previous Topic:specify STRING in my DSL, so I can use templates inside the string
Next Topic:Ambiguous binary operator in a PLSQL grammar
Goto Forum:
  


Current Time: Fri Jul 25 21:06:33 EDT 2025

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

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

Back to the top