|
SWT LPEX v3.0.0 | |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.ibm.lpex.core.LpexCommonParser
com.ibm.lpex.samples.TestParser2
Sample document parser: STEP 2 - tokenize.
This takes the TestParser1 sample a little further, setting a few element classes and styles. Our document parser will be activated by the loading in the editor of file types that it is associated with, and display them in two colours: regular text (blue), and comments (green).
A parser communicates with its associated document view in the editor through public methods in class LpexView. This is the main class of the editor API (Application Programming Interface) for parsers and user-defined commands, actions, and user profiles. LpexView allows a parser class to access the text of the document loaded in the editor, set display styles for the elements, assign element classes to the elements, and query them as reference points during incremental parsing.
Element classes are used to roughly classify document structures, or identify other points of interest inside the parsed document. An element can be assigned any combination of element classes. Here, we'll define two such classes: one for elements that contain text, and one for elements which (also) contain C++-style comments (those starting with a double-slash "//").
Element classes are defined and registered by name. LPEX allocates view-scoped bits for the classes enabled, which bit-masks may be used to query and set classes in the elements via the API.
An element's styles string 'parallels' its text string: each character in the styles string determines how the corresponding character in the text will display (foreground and background colour, regular or underlined, etc.).
// two element classes static final String CLASS_TEXT = "text", CLASS_COMMENT = "comment"; // the two element classes as bit-masks, and a mask for all our classes private long classText, classComment, classAll; |
Field Summary |
---|
Constructor Summary | |
---|---|
TestParser2(LpexView lpexView)
Constructor for the parser. |
Method Summary | |
---|---|
void | parseAll()
Parse the entire document. |
void | parseElement(int element)
Parse a change in the document. |
void | parseOneElement(int element)
Parse one element in the document. |
Methods inherited from class com.ibm.lpex.core.LpexCommonParser |
---|
addMessage, addMessage, addMessage, blockMarkWord, cursorIndent, expandProtoKeyword, getCommentStyleCharacters, getInstallStyleAttributes, getLanguage, getLanguage, getLshToken, getPopupItems, getPopupParserItems, getPopupViewItems, getProfile, getProperty, getStyleName, getStyles, getTextIndent, getToken, getTokenLocation, indentText, indentText, indentText, isDebuggable, isTokenDelimiter, lineComment, lpexView, matchToken, newLine, openLine, parse, removeMessages, removeMessages, resetParser, setProperty, setStyle, splitLine, styleString, terminateParser, tokenBegin, tokenEnd, totalParse |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public TestParser2(LpexView lpexView)
public TestParser2(LpexView lpexView) { super(lpexView); // get active palette's background color String toBackground = LpexPaletteAttributes.background(view); // convert text style attributes (blue/white) to active palette String attributes = LpexPaletteAttributes.convert("0 0 255 255 255 255", "255 255 255", toBackground); setStyle("t", attributes); // comment style (defined as green/white) attributes = LpexPaletteAttributes.convert("0 128 128 255 255 255", "255 255 255", toBackground); setStyle("c", attributes); // define the "text" and "comment" element classes, // and save the bits allocated for them classText = view.registerClass(CLASS_TEXT); classComment = view.registerClass(CLASS_COMMENT); // keep a bit-mask of all our element classes classAll = classText | classComment; } |
Method Detail |
public void parseAll()
public void parseAll() { for (int element = 1; element <= view.elements(); element++) { parseOneElement(element); } } |
During the development of a parser, one can easily verify the operation of the parser by querying, from the command line of Test, the style and elementClasses being set in the document for each element.
public void parseElement(int element)
public void parseElement(int element) { parseOneElement(element); } |
public void parseOneElement(int element)
We query the text of the element, analyze it, and set display styles for the text and comment portions. We also set the element classes of the element according to its contents. LpexCommonParser's utility method styleString is used to create the style strings for the element.
public void parseOneElement(int element) { // query element's text and current classes (as possibly set by others) String text = view.elementText(element); long classes = view.elementClasses(element) & ~classAll; // establish the new styles, and which of our element classes to set String styles = ""; int i = text.indexOf("//"); if (i < 0) // 1.- no comment found { if (text.length() > 0) // any regular text? { styles = styleString('t', text.length()); classes |= classText; } } else // 2.- there is a comment { styles = styleString('t', i) + styleString('c', text.length() - i); classes |= classComment; if (i > 0) // any regular text? { classes |= classText; } } // set the element's display styles, and our element classes view.setElementStyle(element, styles); view.setElementClasses(element, classes); } |
|
SWT LPEX v3.0.0 | |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |