|
LPEX 3.6.7 |
||||||||
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
public class 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 extensions (parsers, 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; |
LpexView
Field Summary |
---|
Constructor Summary | |
---|---|
TestParser2(LpexView lpexView)
Constructor. |
Method Summary | |
---|---|
protected void |
initParser()
Parser initialization. |
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 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); } |
lpexView
- the LpexView object associated with this parser object.Method Detail |
---|
protected void initParser()
protected void initParser() { // 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; } |
initParser
in class LpexCommonParser
LpexCommonParser.setStyle(java.lang.String, java.lang.String)
,
LpexView.doDefaultCommand(java.lang.String)
,
LpexView.classMask(java.lang.String)
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 the editor, the style and elementClasses being set in the view for each element.
parseAll
in class LpexCommonParser
public void parseElement(int element)
public void parseElement(int element) { parseOneElement(element); } |
parseElement
in class LpexCommonParser
element
- the element whose committed change triggered the parse,
or the element that precedes / follows a deleted block.LpexView.parsePending(int)
,
LpexView.elementParsed(int)
,
Total and incremental parsepublic void parseOneElement(int element)
private
),
called by parseAll() and parseElement().
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); } |
element
- one document element to parseLpexCommonParser.styleString(char, int)
,
LpexView.elementText(int)
,
LpexView.elementClasses(int)
,
LpexView.setElementStyle(int, java.lang.String)
,
LpexView.setElementClasses(int, long)
|
LPEX 3.6.7 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |