- FAQ 319: What is eScript?
- FAQ 320: Language integration phase 1: How do I compile and build programs?
- FAQ 321: How do I load source files edited outside Eclipse?
- FAQ 322: How do I run an external builder on my source files?
- FAQ 323: How do I implement a compiler that runs inside Eclipse?
- FAQ 324: How do I react to changes in source files?
- FAQ 325: How do I implement an Eclipse builder?
- FAQ 326: Where are project build specifications stored?
- FAQ 327: How do I add a builder to a given project?
- FAQ 328: How do I implement an incremental project builder?
- FAQ 329: How do I handle setup problems for a given builder?
- FAQ 330: How do I make my compiler incremental?
- FAQ 331: Language integration phase 2: How do I implement a DOM?
- FAQ 332: How do I implement a DOM for my language?
- FAQ 333: How can I ensure that my model is scalable?
- FAQ 334: Language integration phase 3: How do I edit programs?
- FAQ 335: How do I write an editor for my own language?
- FAQ 336: How do I add Content Assist to my language editor?
- FAQ 337: How do I add hover support to my text editor?
- FAQ 338: How do I create problem markers for my compiler?
- FAQ 339: How do I implement Quick Fixes for my own language?
- FAQ 340: How do I support refactoring for my own language?
- FAQ 341: How do I create an Outline view for my own language editor?
- FAQ 342: Language integration phase 4: What are the finishing touches?
- FAQ 343: What wizards do I define for my own language?
- FAQ 344: When does my language need its own nature?
- FAQ 345: When does my language need its own perspective?
- FAQ 346: How do I add documentation and help for my own language?
- FAQ 347: How do I support source-level debugging for my own language?
FAQ 332: 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).
-
A DOM is used for defining context for Content Assist (see FAQ 336).
-
A DOM is useful for generating text hovers (see FAQ 337).
-
Creating outline views without a DOM is difficult (see FAQ 341).
-
A DOM is essential in architecting and implementing support for refactoring (see FAQ 340).
-
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.
-
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.
-
A DOM can be generated in two modes:
-
The same compiler that also compiles source code can save its abstract syntax tree (AST) and expose it for use by the editor. Using an AST is a pretty standard way to implement a computer language, and piggybacking on that infrastructure makes life a lot easier when writing your editor. This is the way the eScript editor works.
-
If the underlying AST is not accessible or is to too fine-grained for use in an editor, you may decide to implement a lightweight parser and generate a DOM more efficiently. This is the way the JDT implements its Java model.
-
Figure 19.2 shows the inheritance hierarchy for the DOM that was develloped for the eScript’s language. As you can see, each node in the DOM extends Element. It defines the following fields:
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....
Figure 19.2 Inheritance hierarchy for eScript’s DOM
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.
Note
FAQ 333 How can I ensure that my model is scalable?