FAQ How do I implement a DOM for my language?
A DOM represents the structure of your programming language. Its design and implementation are dependent of the target language and follow a few simple guidelines:
- The DOM is hierarchical in nature and directly represents concrete elements in the
program it represents
(such as the program itself and its functions, declarations, and statements). </li>
- A DOM is used for defining context for Content Assist
- A DOM is useful for generating text hovers
- Creating outline views without a DOM is difficult
- A DOM is essential in architecting and implementing support for refactoring
- A program may be represented with various DOMs. In the case of eScript we have
a DOM for describing the program structure and a second DOM for the method bodies. </li>
- A DOM is implemented as a data structure with access API. In the case of eScript, we
move Content Assist and text-hover support into the DOM nodes. This makes handling those in the editor very easy. </li>
- A DOM can be generated in two modes:
- The same compiler that also compiles source code can save its abstract syntax tree
- If the underlying AST is not accessible or is to too fine-grained for use in an
int startOffset, endOffset; // source positions Hashtable attributes; // things like ID, label, ... ArrayList children; // children of this element Element parent; // the owner of this element String hoverHelp; // cached value of hover help ...more fields....
The subclasses of Element implement useful methods:
public String getAttributeValue(String name) public String getHoverHelp() public void getContentProposals(...., ArrayList result)
For instance, the getHoverHelp method easily allows us to use the DOM to find the element at a given offset and then ask it for what hover help is appropriate.
Figure 19.2 Inheritance hierarchy for eScripts DOM
This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.