Overview

 
Package  Use  Tree  Serialized  Deprecated  Index  Help 
SWT LPEX
v3.0.0
 PREV CLASS   NEXT CLASS FRAMES    NO FRAMES  
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD


 

com.ibm.lpex.samples
Class TestParser2

java.lang.Object
  extended bycom.ibm.lpex.core.LpexCommonParser
      extended bycom.ibm.lpex.samples.TestParser2

All Implemented Interfaces:
LpexConstants, LpexParser


public class TestParser2
extends LpexCommonParser

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;

See Also:
LpexView


Field Summary
 
Fields inherited from class com.ibm.lpex.core.LpexCommonParser
ATTRIBUTES_COMMENT, ATTRIBUTES_COMMENT_KEYWORD, ATTRIBUTES_COMMENT1, ATTRIBUTES_DEFAULT, ATTRIBUTES_DIRECTIVE, ATTRIBUTES_ERROR, ATTRIBUTES_KEYWORD, ATTRIBUTES_KEYWORD1, ATTRIBUTES_LIBRARY, ATTRIBUTES_NONSOURCE, ATTRIBUTES_NUMERAL, ATTRIBUTES_STRING, ATTRIBUTES_STRING1, BACKGROUND_COLOR, CLASS_MESSAGE, LANGUAGE_CCPP, LANGUAGE_CICS, LANGUAGE_CL, LANGUAGE_COBOL, LANGUAGE_DDS, LANGUAGE_FORTRAN, LANGUAGE_HLASM, LANGUAGE_HTML, LANGUAGE_JAVA, LANGUAGE_JCL, LANGUAGE_LISP, LANGUAGE_PERL, LANGUAGE_PLI, LANGUAGE_REXX, LANGUAGE_RPG, LANGUAGE_SABRETALK, LANGUAGE_SQL, LANGUAGE_XMI, LANGUAGE_XML, LANGUAGE_XSL, LEXER_RC_END, LEXER_RC_EOF, LEXER_RC_MORE, LEXER_RC_OK, POPUP_END, POPUP_FILTERVIEW, POPUP_SOURCE, POPUP_TOP, PROTOKEY_EMPTY, STYLE_MESSAGE, STYLE_NAME, view
 
Fields inherited from interface com.ibm.lpex.core.LpexConstants
HELP_COMMAND_MAP, LPEX_VERSION, MSG_POPUP_ERRORS, MSG_POPUP_EXCLUDESELECTION, MSG_POPUP_FILTERVIEWMENU, MSG_POPUP_INSERTMENU, MSG_POPUP_SELECTEDMENU, MSG_POPUP_SHOWALL, MSG_POPUP_SOURCEMENU, PARSE_PENDING_CHANGE_MASK, PARSE_PENDING_NEXT_DELETED_MASK, PARSE_PENDING_NEXT_SHOW_DELETED_MASK, PARSE_PENDING_PREV_DELETED_MASK, PARSE_PENDING_PREV_SHOW_DELETED_MASK, PLATFORM_AWT, PLATFORM_SWT, PLATFORM_SWT_KEY, STATUS_FILE_ERRORREADING, STATUS_FILE_INCORRECTENCODING, STATUS_FILE_NOTFOUND, STATUS_FINDTEXT_INVALIDPATTERN, STATUS_FINDTEXT_NOTFOUND, STATUS_FINDTEXT_ONLYOCCURRENCE, STATUS_FINDTEXT_READONLY, STATUS_FINDTEXT_WRAPPED, STATUS_LOCATE_NOSEQUENCETEXT, STATUS_LOCATE_NOTFOUND, STATUS_LOCATE_WRAPPED, STATUS_SAVE_CANCELLED, STATUS_SAVE_FAILED, STATUS_TEXTLIMIT_ENFORCED
 
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

 

 

TestParser2

public TestParser2(LpexView lpexView)

Constructor for the parser. In adition to calling superclass LpexCommonParser's constructor, we must also initialize what's needed for parsing and tokenizing the document: we define two style display attributes 't' and 'c', for text and comments, enable the element classes, and query their allocated bit-masks.

 
 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;
 }

Parameters:
lpexView - the LpexView object associated with this parser object.
See Also:
LpexCommonParser.setStyle(java.lang.String, java.lang.String), LpexView.doDefaultCommand(java.lang.String), LpexView.classMask(java.lang.String)
Method Detail

 

 

parseAll

public void parseAll()

Parse the entire document.

 
 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.

Specified by:
parseAll in class LpexCommonParser

See Also:
Total and incremental parse


 

 

parseElement

public void parseElement(int element)

Parse a change in the document.

 
 public void parseElement(int element) 
 {
  parseOneElement(element);
 }

Specified by:
parseElement in class LpexCommonParser

Parameters:
element - the element whose committed change triggered the parse, or the element that precedes / follows a deleted block.
See Also:
LpexView.parsePending(int), LpexView.elementParsed(int), Total and incremental parse


 

 

parseOneElement

public void parseOneElement(int element)

Parse one element in the document. Our own method (which could very well be declared 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);
 } 

Parameters:
element - one document element to parse
See Also:
LpexCommonParser.styleString(char, int), LpexView.elementText(int), LpexView.elementClasses(int), LpexView.setElementStyle(int, java.lang.String), LpexView.setElementClasses(int, long)


 

Overview

 
Package  Use  Tree  Serialized  Deprecated  Index  Help 
SWT LPEX
v3.0.0
 PREV CLASS   NEXT CLASS FRAMES    NO FRAMES  
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD