Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Deep dive into Xtext scoping local and global scopes explained

1.297 Aufrufe

Veröffentlicht am

In Xtext's grammar language syntax and structure of models are defined at a single location in a very concise way. The nesting structure and the references between objects are expressed which goes beyond plain abstract syntax trees. By just pointing to a type of an object, we describe the shape of a syntax graph. But the grammar alone has no meaning to describe the visibility rules inside of a resource or across resource boundaries. Therefore Xtext has a concept of scopes that are modeled as a chain of responsibility. Xtext comes with a good default scoping semantic that fits for most simple problems, but when the amount of models grows or your problem cannot be handled by the default you have to get in touch with the scoping API. Or as others have said it: Beyond here are dragons.

Most Xtext users know how to implement scoping rules that are applied within the same resource. After all, the API is pretty straight forward. Referencing objects located in other resources is usually more tricky. Here the so called global scope provider is doing it’s job. By default, the global scope contains every object that has a name. Again, that’s good as a starting point and will work in most of the cases, but for serious languages, it doesn't cut it. Memory consumption and performance will play a bigger role as the amount of models grows. There is no turning back: As a language engineer, you really need to understand what’s going on under the covers to define scaling implementations.

In this session we will provide a thorough explaination of the most important concepts of Xtext’s way of scoping. You will learn, that an object might have different names in different contexts and what's behind nested scopes, shadowing and filtering. We will discuss typical problems and solutions along with best practices for scoping. In the end, it's about the right compromise between implementation effort, memory consumption and performance.

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

Deep dive into Xtext scoping local and global scopes explained

  1. 1. Deep dive into Xtext scoping local and global scopes explained
  2. 2. Sebastian Holger Who are these guys?
  3. 3. Scoping with Xtext
  4. 4. What is scoping?
  5. 5. What is scoping?
  6. 6. What is scoping? Convention
  7. 7. What is scoping? QualifiedName
  8. 8. What is scoping? QualifiedName grandfather father son1 son2 - grandfather.father.son1 - grandfather.father.son2
  9. 9. What is scoping? Simple name
  10. 10. What is scoping?
  11. 11. What is scoping? Same EMF Container
  12. 12. What is scoping?
  13. 13. What is scoping? PrefixMatcher does it’s job
  14. 14. What is scoping?
  15. 15. What is scoping? Not same container
  16. 16. Imports?
  17. 17. Imports? org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider
  18. 18. Imports? org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider
  19. 19. Imports? org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider Feels wrong
  20. 20. Implicit imports
  21. 21. Implicit imports
  22. 22. Implicit imports
  23. 23. Implicit imports Not called for root object!
  24. 24. Implicit imports
  25. 25. Implicit imports
  26. 26. Implicit imports
  27. 27. So far easy…
  28. 28. So far easy… But what is a scope?
  29. 29. So far easy… But what is a scope? It describes the reachable Elements…
  30. 30. So far easy… But what is a scope? …depending on the context… It describes the reachable Elements…
  31. 31. So far easy… But what is a scope? …depending on the context… …and maps name to EObject… It describes the reachable Elements…
  32. 32. So far easy… But what is a scope? …depending on the context… …and maps name to EObject… …in a chain of scopes. It describes the reachable Elements…
  33. 33. Simply spoken Scope EObjectDescription EObjectDescription … EObjectDescription QualifiedName URI UserData
  34. 34. A chain of responsibility Scope EObjectDescription EObjectDescription … Scope EObjectDescription EObjectDescription … outer Index global ResourceDescription … Simply spoken
  35. 35. A chain of responsibility Scope EObjectDescription EObjectDescription … Scope EObjectDescription EObjectDescription … outer Index global ResourceDescription … Simply spoken
  36. 36. Index ResourceDescription … ResourceDescription URI ExportedObjects ReferencedObjects ImportedNames - List of EObjectDescriptions - List of ReferenceDesciptions - All the names Simply spoken
  37. 37. Local vs. global scoping Local
  38. 38. Local vs. global scoping Local Global Other files in project and more…
  39. 39. Who creates Scopes? Local Scopes Global Scope aka. the Index
  40. 40. Who creates Scopes? Local Scopes Global Scope aka. the Index YOU! The builder!
  41. 41. Override getScope! Don’t use the declarative stuff any more!
  42. 42. Simple Example
  43. 43. Simple Example
  44. 44. Simple Example
  45. 45. Collect all fields
  46. 46. Collect all fields
  47. 47. Collect all fields
  48. 48. More logic
  49. 49. More logic Walkthrough Walkthrough Field
  50. 50. More logic Walkthrough Walkthrough Field
  51. 51. More logic Walkthrough Walkthrough Field Field
  52. 52. More logic
  53. 53. More logic
  54. 54. More logic
  55. 55. More logic context.getField cannot be called! CyclicLinkingException
  56. 56. Objects can have different names in different contexts Function to transform the name The implementation creates new EObjectDescriptions
  57. 57. Objects can have different names in different contexts org.eclipse.xtext.resource.impl.AliasedEObjectDescription org.eclipse.xtext.scoping.impl.ImportScope
  58. 58. Don’t create too tight scopes
  59. 59. Don’t create too tight scopes Use validations instead! They can produce more meaningful messages then!
  60. 60. The NodeModel EObject CompositeNode Adapter LeafNode LeafNode CompositeNode LeafNode LeafNode
  61. 61. EObject CompositeNode Adapter LeafNode LeafNode CompositeNode LeafNode LeafNode The NodeModel Don’trelyontheNodeModelduring scoping
  62. 62. Never load resources explicitly There is a global scope
  63. 63. Global Scoping
  64. 64. Global Scoping ResourceDescriptions Constructed when the builder runs…
  65. 65. Global Scoping ResourceDescriptions Constructed when the builder runs… org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider …accessible through… IResourceDescriptions - getResourceDescriptions(ResourceSet)
  66. 66. Global Scoping ResourceDescriptions Constructed when the builder runs… org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider …accessible through… IResourceDescriptions - getResourceDescriptions(ResourceSet) …depending on the LoadOptions… LIVE_SCOPE NAMED_BUILDER_SCOPE
  67. 67. Information hiding Not everything should be referable
  68. 68. Information hiding Not everything should be referable Save memory and time!
  69. 69. EObjectDescription(QualifiedName qualifiedName, EObject element, Map<String, String> userData) UserData Serializable stuff Informations without resolving the proxy
  70. 70. Index in Standalone ResourceSetGlobalScopeProvider Long time the was the default.
  71. 71. Index in Standalone ResourceSetGlobalScopeProvider Long time the was the default. Today we can do better but you need to use it and know that there is something new… For Maven- and Gradle it’s done.
  72. 72. Index in Standalone Fill the index and install it on the ResourceSet! Load each resource and ask the ResourceDescriptionManager to create a ResourceDescription…
  73. 73. Index in Standalone Fill the index and install it on the ResourceSet! Load each resource and ask the ResourceDescriptionManager to create a ResourceDescription… Install it on the ResourceSet
  74. 74. Lessons learned Never use #getResource Avoid the Node Model CyclicResolutionExceptions! UserData helps Never use #getResource! Don’t Filter - But Validate!

×