Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
SCOPING TIPS & TRICKS
SEBASTIAN ZARNEKOW
IN COMPUTER PROGRAMMING, THE SCOPE OF A NAME BINDING – AN
ASSOCIATION OF A NAME TO AN ENTITY, SUCH AS A VARIABLE – IS
THE ...
IN COMPUTER PROGRAMMING, THE SCOPE OF A NAME BINDING – AN
ASSOCIATION OF A NAME TO AN ENTITY, SUCH AS A VARIABLE – IS
THE ...
IScope and its Clients
Linking:

getSingleElement(QualifiedName)

ContentAssist:

getAllElements()

Serialization:

getEle...
IScope Disassembled
Multimap-like Structure

Two Different Keys

Multimap<QualifiedName, IEObjectDescription>

Multimap<EObj...
Multimaps from FOP View
Multimap<K, V>
~= Map<K, Collection<V>>
~= get(K): Collection<V>
~= (K) -> V*
IScope from FOP View
{

(QualifiedName) -> IEObjectDescription*,

(EObject) -> IEObjectDescription*

() -> IEObjectDescrip...
IEObjectDescription
{ Name, QualifiedName, URI, String* }
#getEObjectOrProxy
IScopeProvider
getScope(EObject ctx, EReference ref):IScope
EObject ctx
Linking: Owner of Reference

Content Assist: Best ...
PUTTING IT TOGETHER
Embrace (QN)->IEOD
Decorate the Produced Scope, e.g

return new SimpleScope(orig) {..}

Override #getSingleElement(Qualifi...
Validation Meets Scoping
Don’t Limit Content of Scope ‘Arbitrarily’

Include Potentially Invalid Descriptions

Improve Use...
Validation Meets Scoping
public class ErrorAddingDescription

extends AliasedEObjectDescription {
ErrorAddingDescription(E...
Detect Unused Locals
Mark Local Declarations as Unused

Local Vars

Import Declarations

Potentially Expensive Validation
...
public class MarkAsUsedDescription

extends AliasedEObjectDescription {
MarkAsUsedDescription(EObject ctx,

IEObjectDescri...
@Check

public void markAsUnused(LocalVar var) {
if (UsageMarkerAdapter.isUnused(var)) {
addWarning(var, ..)
}
}
Detect Un...
Avoid Follow-up Errors
Encountered EMF Proxy During getScope(..)

Often Follow-Up Error Situation

Return Scope with Dummy...
public class ErrorScope implements IScope {
ErrorScope(EObject ctx, EReference ref) { .. }

public IEObjectDescription get...
Avoid Follow-up Errors
public class ErrorDescription

extends AliasedEObjectDescription {
ErrorDescription(QualifiedName n...
Dynamic Scopes
Dynamic Languages Allow to Refer Anything

Returns Dynamic Description if Name is Unknown

Record Obtained ...
COMMON PITFALLS
This is not the context
you’re looking for
getScope(EObject ctx, EReference ref)
Content Assist Works on Invalid Documents...
Don’t Enumerate the World
Avoid eResource().getResourceSet()
and eAllContents()
Embrace the Defaults

Index Queries

Local...
Get Rid of Reflective API
Extend

o.e.x.s.impl.DelegatingScopeProvider

Implement #getScope(..) based on EReference

if (re...
Avoid Loading the World
Put Necessary Information Into Index

User Data Makes Great Filter-Criteria

Index Objects with Al...
Q & A
Próxima SlideShare
Cargando en…5
×

Scoping Tips and Tricks

1.147 visualizaciones

Publicado el

Tips to customize the scoping in Xtext and examples for common use cases.

Publicado en: Software
  • Sé el primero en comentar

Scoping Tips and Tricks

  1. 1. SCOPING TIPS & TRICKS SEBASTIAN ZARNEKOW
  2. 2. IN COMPUTER PROGRAMMING, THE SCOPE OF A NAME BINDING – AN ASSOCIATION OF A NAME TO AN ENTITY, SUCH AS A VARIABLE – IS THE PART OF A COMPUTER PROGRAM WHERE THE BINDING IS VALID: WHERE THE NAME CAN BE USED TO REFER TO THE ENTITY. IN OTHER PARTS OF THE PROGRAM THE NAME MAY REFER TO A DIFFERENT ENTITY (IT MAY HAVE A DIFFERENT BINDING), OR TO NOTHING AT ALL (IT MAY BE UNBOUND). THE SCOPE OF A BINDING IS ALSO KNOWN AS THE VISIBILITY OF AN ENTITY, PARTICULARLY IN OLDER OR MORE TECHNICAL LITERATURE – THIS IS FROM THE PERSPECTIVE OF THE REFERENCED ENTITY, NOT THE REFERENCING NAME. [..] THE TERM "SCOPE" IS ALSO USED TO REFER TO THE SET OF ALL ENTITIES THAT ARE VISIBLE OR NAMES THAT ARE VALID WITHIN A PORTION OF THE PROGRAM OR AT A GIVEN POINT IN A PROGRAM, WHICH IS MORE CORRECTLY REFERRED TO AS CONTEXT OR ENVIRONMENT. http://en.wikipedia.org/wiki/Scope_%28computer_science%29
  3. 3. IN COMPUTER PROGRAMMING, THE SCOPE OF A NAME BINDING – AN ASSOCIATION OF A NAME TO AN ENTITY, SUCH AS A VARIABLE – IS THE PART OF A COMPUTER PROGRAM WHERE THE BINDING IS VALID: WHERE THE NAME CAN BE USED TO REFER TO THE ENTITY. IN OTHER PARTS OF THE PROGRAM THE NAME MAY REFER TO A DIFFERENT ENTITY (IT MAY HAVE A DIFFERENT BINDING), OR TO NOTHING AT ALL (IT MAY BE UNBOUND). THE SCOPE OF A BINDING IS ALSO KNOWN AS THE VISIBILITY OF AN ENTITY, PARTICULARLY IN OLDER OR MORE TECHNICAL LITERATURE – THIS IS FROM THE PERSPECTIVE OF THE REFERENCED ENTITY, NOT THE REFERENCING NAME. [..] THE TERM "SCOPE" IS ALSO USED TO REFER TO THE SET OF ALL ENTITIES THAT ARE VISIBLE OR NAMES THAT ARE VALID WITHIN A PORTION OF THE PROGRAM OR AT A GIVEN POINT IN A PROGRAM, WHICH IS MORE CORRECTLY REFERRED TO AS CONTEXT OR ENVIRONMENT. http://en.wikipedia.org/wiki/Scope_%28computer_science%29
  4. 4. IScope and its Clients Linking:
 getSingleElement(QualifiedName) ContentAssist:
 getAllElements() Serialization:
 getElements(EObject)
  5. 5. IScope Disassembled Multimap-like Structure Two Different Keys Multimap<QualifiedName, IEObjectDescription> Multimap<EObject, IEObjectDescription> Typically Stacked Shadowing Semantics
  6. 6. Multimaps from FOP View Multimap<K, V> ~= Map<K, Collection<V>> ~= get(K): Collection<V> ~= (K) -> V*
  7. 7. IScope from FOP View {
 (QualifiedName) -> IEObjectDescription*,
 (EObject) -> IEObjectDescription*
 () -> IEObjectDescription*
 }
  8. 8. IEObjectDescription { Name, QualifiedName, URI, String* } #getEObjectOrProxy
  9. 9. IScopeProvider getScope(EObject ctx, EReference ref):IScope EObject ctx Linking: Owner of Reference Content Assist: Best Known Container EReference ref Reflective representation, e.g. TYPE__SUPER_TYPE for Type.getSuperType()
  10. 10. PUTTING IT TOGETHER
  11. 11. Embrace (QN)->IEOD Decorate the Produced Scope, e.g
 return new SimpleScope(orig) {..} Override #getSingleElement(QualifiedName) Decorate the IEObjectDescription
 return new AliasedEObjectDescription {..} Perform Side-Effect on 
 #getEObjectOrProxy
  12. 12. Validation Meets Scoping Don’t Limit Content of Scope ‘Arbitrarily’ Include Potentially Invalid Descriptions Improve User Experience 
 (Navigation, Error Messages) Add Error or Warning, if Description is Invalid
 ctx.eResource().getErrors().add(..)
  13. 13. Validation Meets Scoping public class ErrorAddingDescription
 extends AliasedEObjectDescription { ErrorAddingDescription(EObject ctx,
 IEObjectDescription desc) {..} @Override
 public EObject getEObjectOrProxy() { ctx.eResource().getErrors().add(
 new EObjectDiagnosticImpl(..)) return super.getEObjectOrProxy(); } }
  14. 14. Detect Unused Locals Mark Local Declarations as Unused Local Vars Import Declarations Potentially Expensive Validation Install EMF Adapter on Used Instances
  15. 15. public class MarkAsUsedDescription
 extends AliasedEObjectDescription { MarkAsUsedDescription(EObject ctx,
 IEObjectDescription desc) { .. } @Override
 public EObject getEObjectOrProxy() { EObject result = super.getEObjectOrProxy(); if (result.eResource() == ctx.eResource())
 UsageMarkerAdapter.markAsUsed(result); return result; } } Detect Unused Locals
  16. 16. @Check
 public void markAsUnused(LocalVar var) { if (UsageMarkerAdapter.isUnused(var)) { addWarning(var, ..) } } Detect Unused Locals
  17. 17. Avoid Follow-up Errors Encountered EMF Proxy During getScope(..) Often Follow-Up Error Situation Return Scope with Dummy Description Don’t Produce Unnecessary Errors Avoid Subsequent Resolution Attempts
  18. 18. public class ErrorScope implements IScope { ErrorScope(EObject ctx, EReference ref) { .. }
 public IEObjectDescription getSingleElement(
 QualifiedName name) { return new ErrorDescription(name, ctx, ref); } .. } Avoid Follow-up Errors
  19. 19. Avoid Follow-up Errors public class ErrorDescription
 extends AliasedEObjectDescription { ErrorDescription(QualifiedName name, EObject ctx,
 EReference ref) { .. } @Override
 public EObject getEObjectOrProxy() { return (EObject) ctx.eGet(ref, false); // simplified } }
  20. 20. Dynamic Scopes Dynamic Languages Allow to Refer Anything Returns Dynamic Description if Name is Unknown Record Obtained Descriptions for Later Reuse Special Case of ErrorScope
  21. 21. COMMON PITFALLS
  22. 22. This is not the context you’re looking for getScope(EObject ctx, EReference ref) Content Assist Works on Invalid Documents Parser Produces Trees rather than a Forest Best Effort Context Computation Beware of ClassCastException in getScope(..)
  23. 23. Don’t Enumerate the World Avoid eResource().getResourceSet() and eAllContents() Embrace the Defaults Index Queries Local Resource Contents is Indexed & Cached Cache Scopes if Necessary (e.g. MapBasedScope) !
  24. 24. Get Rid of Reflective API Extend
 o.e.x.s.impl.DelegatingScopeProvider Implement #getScope(..) based on EReference
 if (ref == MyDslPackage.Literals…) Generally Faster & Easier to Debug
  25. 25. Avoid Loading the World Put Necessary Information Into Index User Data Makes Great Filter-Criteria Index Objects with Aliases / Multiple Names Use AliasedEObjectDescription for Local Synonyms If you invoke getEObjectOrProxy() you’re doing it wrong. Usually.
  26. 26. Q & A

×