|
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
LpexCommonParser is a base, abstract class for document parsers. It adds several services to the LpexParser interface, simplifying document-parser development, and provides methods for a common look-and-feel to the parsers extending it. Certain methods in the basic LpexParser interface are not used by parsers extending LpexCommonParser.
A document parser is associated with one particular view of a document. Different views of the same document may be handled by different parsers. There is up to one instance of this class for each document view.
Constructor
The parser must have a constructor whose parameter is the associated document view, as shown below:
public class MyLpexParser extends LpexCommonParser { public MyLpexParser(LpexView lpexView) { super(lpexView); // rest of MyLpexParser constructor... } // rest of MyLpexParser... } |
Total and incremental parse
There are two main callback methods in a document parser:
Editor actions
Several default editor actions are modified by parsers that extend LpexCommonParser:
Action and its assigned key added by LpexCommonParser-based parsers:
Keys already defined (for example, by the active base profile) to an action different from nullAction are not redefined in here.
The proto action expands a prototype word at the current document location using the template defined in the active document parser. The cursor is set to the position of the first '?' encountered in the expansion, if any. The expansion string is obtained through a call to getProperty(), with the prototype word found at the cursor (and delimited by white space or token-delimiter characters), prefixed by "proto.", as the key argument. For example, if the cursor is located on the word "if" in a C/C++ source document, the value of the CppParser property "proto.if" is used for the expansion. Argument substitution(s) required in the expansion string should be carried out by the parser, and the final string should be returned by its implementation of method getProperty(). When the document is empty, the proto action uses the key PROTOKEY_EMPTY, if an expansion value is defined for it, to set up an initial text to serve as the basic structure for the document.
Parser properties
Parser properties are parser-specific settings. Certain common parser properties are used by LpexCommonParser:
The default scope of a parser property is the document parser for which it is defined. Any instance of the parser in any document view will use the same property value, unless overridden by a view scoped setting. View scoped parser properties are prefixed with "view.". For example, the value of the autoIndent setting used by a specific instance of a document parser may be changed by the view.autoIndent parser property.
Parser properties may be defined at several levels.
The parser properties are also accessible through the parserProperty editor parameter. A parser may 'listen' to changes effected to any of its properties by extending setProperty().
See getProperty() and setProperty() for additional information on parser properties.
Language-sensitive help
LpexCommonParser provides a few basic services for the implementation of language-sensitive help (LSH) on any platform:
Mixed content documents
Source files opened in the editor commonly contain several distinct content types. Examples of mixed content documents, in which there is a main language and one or more secondary languages or scripts include: C/C++ source code with imbedded SQL and/or CICS statements, Java source code with imbedded SQL, and HTML code with JavaScript sections.
In these cases, the parser for the main language (normally installed through the file name extension - parser association mechanism) invokes the appropriate subparsers (secondary parsers) for those sections of the document that contain other types of content. Several document parsers shipped with LPEX are designed in this manner: C/C++, COBOL, PL/I, and Java document parsers support embedded SQL and CICS statements. The full set of context-sensitive services which is ordinarily provided by a document parser written specifically for a language as the main, or unique, document language becomes thus available for all the different sections of the document.
Being done under the full control of the main document parser, a well-integrated functionality and look is achieved. The document parser defines and registers with the editor the styles and the element classes for use by all the subparsers in the document context supported. A subparser can query the main document language to determine, for example, the end-of-construct it must detect, the type of comments which should be accepted, etc. For example, the SQL subparser may detect the end of imbedded SQL by a semicolon ';' in a C/C++ and PL/I document, and an "END-EXEC" token in a COBOL document. Comments acceptable in the main language, preprocessed (removed) before the source is passed on to the secondary language compilers or interpreters, must also be accepted by the secondary document parsers.
LpexCommonParser defines a generic set of return codes which may be used for communication between the main parser and the subparsers: LEXER_RC_OK, LEXER_RC_EOF, LEXER_RC_END, and LEXER_RC_MORE. Each parser may contribute its own items to the pop-up menu. A method is provided to query the document content type at a specific location.
Multiple document views
As there may be several views opened on the same document, each view being associated with a different parser instance, but most commonly document parsers of the same type, several features should be carefully considered. Remember, both the total and incremental parse callbacks will be invoked in all the parsers associated with the document, on every change to the text of the document.
Related editor parameters: documentId, multipleViews, viewId.
Example
An example of developing a simple document parser is available in the com.ibm.lpex.samples package. See TestParser1 and TestParser2.
Field Summary | |
---|---|
static String | ATTRIBUTES_COMMENT
Style attributes for comment/remark tokens. |
static String | ATTRIBUTES_COMMENT_KEYWORD
Style attributes for special keywords inside comments. |
static String | ATTRIBUTES_COMMENT1
Additional style attributes for comment/remark tokens. |
String | ATTRIBUTES_DEFAULT
Common default style attributes. |
static String | ATTRIBUTES_DIRECTIVE
Style attributes for compiler directives. |
static String | ATTRIBUTES_ERROR
Style attributes for errors. |
static String | ATTRIBUTES_KEYWORD
Style attributes for language keywords. |
static String | ATTRIBUTES_KEYWORD1
Additional style attributes for language keywords. |
static String | ATTRIBUTES_LIBRARY
Style attributes for library-related tokens. |
static String | ATTRIBUTES_NONSOURCE
Style attributes for text which does not constitute part of the source proper. |
static String | ATTRIBUTES_NUMERAL
Style attributes for numerals. |
static String | ATTRIBUTES_STRING
Style attributes for quoted strings. |
static String | ATTRIBUTES_STRING1
Additional style attributes for quoted strings. |
static String | BACKGROUND_COLOR
Common background color. |
static String | CLASS_MESSAGE
Common element class reserved for error messages. |
static String | LANGUAGE_CCPP
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_CICS
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_CL
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_COBOL
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_DDS
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_FORTRAN
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_HLASM
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_HTML
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_JAVA
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_JCL
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_LISP
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_PERL
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_PLI
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_REXX
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_RPG
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_SABRETALK
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_SQL
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_XMI
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_XML
Predefined string for the language or document type supported by a parser. |
static String | LANGUAGE_XSL
Predefined string for the language or document type supported by a parser. |
static int | LEXER_RC_END
Return code from a subparser's token processing indicating the end of its job. |
static int | LEXER_RC_EOF
EOF (end-of-file) flag in the return code from a subparser's token processing. |
static int | LEXER_RC_MORE
More-input flag in the return code from a subparser's token processing. |
static int | LEXER_RC_OK
Successful return code from a subparser's token processing. |
static int | POPUP_END
Indication for parser items at the end of the popup. |
static int | POPUP_FILTERVIEW
Indication for parser items in the "Filter view" submenu of the popup. |
static int | POPUP_SOURCE
Indication for parser items in the "Source" submenu of the popup. |
static int | POPUP_TOP
Indication for parser items at the top of the popup. |
static String | PROTOKEY_EMPTY
Key for action proto in an empty document. |
static char | STYLE_MESSAGE
Common style reserved for parser messages. |
static String | STYLE_NAME
Prefix for the styleName.styleCharacter parser properties. |
protected LpexView | view
The view with which this document parser is associated. |
Constructor Summary | |
---|---|
protected | LpexCommonParser(LpexView lpexView)
LpexCommonParser constructor. |
Method Summary | |
---|---|
protected void | addMessage(int element,
String text)
Displays an embedded parser error or informational message for an element. |
protected void | addMessage(int element,
String text,
long messageClass)
Displays an embedded parser error or informational message for an element. |
protected void | addMessage(int element,
String text,
long messageClass,
char messageStyle)
Displays an embedded parser error or informational message for an element. |
protected void | blockMarkWord()
Overrides the default editor action blockMarkWord. |
int | cursorIndent(int element)
Returns the indent display column of the cursor in the element. |
protected void | expandProtoKeyword(int s,
int e,
String p)
Carries out the keyword expansion for the proto action. |
String | getCommentStyleCharacters()
Returns the style character(s) used by this parser for comments. |
ArrayList | getInstallStyleAttributes()
Returns the install attributes for the style characters defined by this document parser. |
String | getLanguage()
Returns a string identifying the language or document type supported by this document parser. |
String | getLanguage(LpexDocumentLocation loc)
Returns a string identifying the language segment at the specified location. |
String | getLshToken()
Returns the current token for language-sensitive help (LSH). |
protected String | getPopupItems(int popupLocation)
Returns this parser's items for the pop-up (context) menu. |
String | getPopupParserItems()
Deprecated. Return the same string in getPopupItems(POPUP_END). |
String | getPopupViewItems()
Deprecated. Return the same string in getPopupItems(POPUP_FILTERVIEW). |
ResourceBundle | getProfile()
Returns the document parser profile. |
String | getProperty(String key)
Returns a parser property. |
String | getStyleName(char styleCharacter)
Returns the description of a style defined by this document parser. |
String | getStyles()
Returns the style characters defined by this document parser. |
String | getTextIndent(int element)
Returns the desired indent string for the text in the specified element. |
String | getToken(LpexDocumentLocation loc)
Returns the language token at the specified document location. |
LpexDocumentLocation | getTokenLocation(LpexDocumentLocation loc)
Returns the start location of the language token as defined and returned by getToken(). |
int | indentText(int element)
Positions the text of an element to the correct indent value. |
int | indentText(int element,
int desiredIndent)
Positions the text of an element to the specified indent value, given as the column position of the text indented by leading blanks. |
int | indentText(int element,
String desiredIndent)
Positions the text of an element to the specified indentation. |
boolean | isDebuggable(int line)
Query whether the specified document line is debuggable. |
boolean | isTokenDelimiter(char c)
Query whether the specified character is a token delimiter. |
void | lineComment(String text,
boolean comment)
Generic utility method to comment / uncomment lines. |
LpexView | lpexView()
Returns the document view with which this parser is associated. |
protected LpexDocumentLocation | matchToken(LpexDocumentLocation loc)
Matches the token found at the specified document location. |
protected void | newLine()
Overrides the default editor action newLine. |
protected void | openLine()
Overrides the default editor action openLine. |
void | parse(int element)
N/A - Method in the LpexParser interface; a document parser extending LpexCommonParser only implements parseElement(). |
abstract void | parseAll()
Total parse. |
abstract void | parseElement(int element)
Incremental parse. |
protected void | removeMessages(int fromElement,
int toElement)
Removes this parser's default messages for a given range of elements. |
protected void | removeMessages(int fromElement,
int toElement,
long messageClass)
Removes this parser's embedded messages of the given element class(es), for the given range of elements. |
void | resetParser()
N/A - Method in the LpexParser interface; a document parser extending LpexCommonParser only implements, if needed, terminateParser(). |
void | setProperty(String key,
String value)
Sets, updates, or restores a parser property. |
void | setStyle(String styles,
String installAttributes)
Defines the styles used by the parser for a token or set of tokens. |
protected void | splitLine()
Overrides the default editor action splitLine. |
String | styleString(char style,
int len)
Utility method to return a style string for a len-long token. |
void | terminateParser()
Remove all of the parser specifics from the document view. |
protected LpexDocumentLocation | tokenBegin(LpexDocumentLocation loc)
Returns the start of a token at the specified document location. |
protected LpexDocumentLocation | tokenEnd(LpexDocumentLocation loc)
Returns the end of a token at the specified document location. |
void | totalParse()
N/A - Method in the LpexParser interface; a document parser extending LpexCommonParser only implements parseAll(). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected LpexView view
public static final String CLASS_MESSAGE
It is defined as "Message". The parser should not define an element class with this name.
public static final char STYLE_MESSAGE
Defined as 'M'. The parser should not define this style for tokens.
public static final String LANGUAGE_CCPP
public static final String LANGUAGE_CICS
public static final String LANGUAGE_CL
public static final String LANGUAGE_COBOL
public static final String LANGUAGE_DDS
public static final String LANGUAGE_FORTRAN
public static final String LANGUAGE_HLASM
public static final String LANGUAGE_HTML
public static final String LANGUAGE_JAVA
public static final String LANGUAGE_JCL
public static final String LANGUAGE_LISP
public static final String LANGUAGE_PERL
public static final String LANGUAGE_PLI
public static final String LANGUAGE_REXX
public static final String LANGUAGE_RPG
public static final String LANGUAGE_SABRETALK
public static final String LANGUAGE_SQL
public static final String LANGUAGE_XMI
public static final String LANGUAGE_XML
public static final String LANGUAGE_XSL
public static final String PROTOKEY_EMPTY
public static final String STYLE_NAME
public static final String BACKGROUND_COLOR
public final String ATTRIBUTES_DEFAULT
public static final String ATTRIBUTES_COMMENT
public static final String ATTRIBUTES_COMMENT1
public static final String ATTRIBUTES_COMMENT_KEYWORD
public static final String ATTRIBUTES_ERROR
public static final String ATTRIBUTES_KEYWORD
public static final String ATTRIBUTES_KEYWORD1
public static final String ATTRIBUTES_LIBRARY
public static final String ATTRIBUTES_NUMERAL
public static final String ATTRIBUTES_STRING
public static final String ATTRIBUTES_STRING1
public static final String ATTRIBUTES_DIRECTIVE
public static final String ATTRIBUTES_NONSOURCE
public static final int LEXER_RC_OK
public static final int LEXER_RC_EOF
public static final int LEXER_RC_END
public static final int LEXER_RC_MORE
public static final int POPUP_TOP
public static final int POPUP_FILTERVIEW
public static final int POPUP_SOURCE
public static final int POPUP_END
Constructor Detail |
protected LpexCommonParser(LpexView lpexView)
public MyLpexParser(LpexView lpexView) { super(lpexView); // rest of MyLpexParser's constructor code... } |
Method Detail |
public abstract void parseAll()
public abstract void parseElement(int element)
public final LpexView lpexView()
public final void resetParser()
public final void totalParse()
public final void parse(int element)
public String getLanguage()
The document parser extending LpexCommonParser should override this method to return the language it supports. Strings for the most common programming languages are defined in LpexCommonParser. The implementation of this method provided by the LpexCommonParser class returns null.
public String getLanguage(LpexDocumentLocation loc)
The document parser extending LpexCommonParser may override this method to return the particular language segment. Strings for the most common programming languages are defined in LpexCommonParser. The implementation of this method provided by the LpexCommonParser class returns getLanguage().
public ResourceBundle getProfile()
private static ResourceBundle resource = ResourceBundle.getBundle("com.ibm.lpex.java.Profile"); |
The document parser extending LpexCommonParser should override this method to return its own resource bundle. The implementation of this method provided by the LpexCommonParser class does nothing, except return null.
public String getProperty(String key)
Other properties may be set, for example to override the default style attributes used by the document parser for syntax highlighting.
The implementation of this method provided by the LpexCommonParser class searches the key (prefixed with the parser's language) in the editor defaults profile (used to store persistent user settings). If not found, it searches the parser profile (installation settings of parser properties). If the property is not found, it returns null.
If the specified key starts with "view.", then the view scoped setting of the parser property, stored in the associated document view, is searched first.
public void setProperty(String key, String value)
The implementation of this method provided by the LpexCommonParser class sets the key (prefixed with the parser's language) and the specified value in the editor defaults profile (persistent user settings).
If the specified key starts with "view.", then only the instance scoped setting of the parser property is set.
If value is null, the specified view or default setting is removed, and the parser's higher-level definition of the property value, if any, will be returned by a subsequent getProperty() call.
A parser may 'listen' to changes effected to any of its properties by extending this method. The implementation of this method in LpexCommonParser listens to changes to the autoIndent and errorMessages properties.
public String getPopupViewItems()
public String getPopupParserItems()
protected String getPopupItems(int popupLocation)
For example, the Java document parser contributes to the "Filter view" submenu three menu items, defined here by their keys in LPEX's Resources.properties, and their corresponding action names; and to the "Source" submenu four items, defined here by imbedded strings, and their corresponding action names:
protected String getPopupItems(int popupLocation) { switch (popupLocation) { case POPUP_FILTERVIEW: return getLanguage() + ".popup.methods methods " + getLanguage() + ".popup.outline outline " + LpexConstants.MSG_POPUP_ERRORS + " errors"; case POPUP_SOURCE: return "\"&Comment\" comment " + "\"&Uncomment\" uncomment " + "\"&Document\" doc " + "\"&Trace\" trace"; } return null; } |
The implementation of this method provided by the LpexCommonParser class does nothing, except return null.
public void terminateParser()
The document parser extending LpexCommonParser may override this method to do any additional cleanups. For example, it should remove any listeners that it has added. A parser that implements code reformatting or auto-uppercasing may have reset certain editor settings, such as the autoCheck parameter, as it explicitly invokes the undo check command after modifying the source code; it should now restore any such settings, so that the editor may continue to operate properly without the parser.
The implementation of this method provided by the LpexCommonParser class does nothing.
public String getCommentStyleCharacters()
The document parser extending LpexCommonParser should override this method to return the style character(s) it uses for document comments and remarks. The implementation of this method provided by the LpexCommonParser class returns null.
protected void expandProtoKeyword(int s, int e, String p)
public String getToken(LpexDocumentLocation loc)
A token is a string of a consistent style, and delimited by the boundaries of the text element, white space, and characters defined by isTokenDelimiter().
This method returns null if the document location is beyond the element text, is on white space, or loc is incorrect. Token search is restricted to one element. Styles are assumed to be set correctly (i.e., no parse is pending for this element).
If a block is selected in this view and the location points to the start or end of this block, then the selected text will be returned as the token. It is assumed in this case that the text has just been selected, and the selection is deemed intentional for the purpose for which the token is being queried. This overrides the normal definition of a token stated above.
public LpexDocumentLocation getTokenLocation(LpexDocumentLocation loc)
public String getLshToken()
The implementation of this method provided by the LpexCommonParser class returns getToken(view.documentLocation()).
protected void blockMarkWord()
protected LpexDocumentLocation tokenBegin(LpexDocumentLocation loc)
Token search is restricted to one element. Styles are assumed to be set correctly (i.e., no parse is pending for this element).
protected LpexDocumentLocation tokenEnd(LpexDocumentLocation loc)
protected LpexDocumentLocation matchToken(LpexDocumentLocation loc)
If no matching token is returned by this method, the editor tries to match any parenthesis, brace, square bracket, or angle bracket found at loc.
The implementation of this method provided by the LpexCommonParser class does nothing, except return null.
public boolean isDebuggable(int line)
The implementation of this method provided by the LpexCommonParser class returns false. A parser which supports debugging should override this to return correct information.
public boolean isTokenDelimiter(char c)
... (name());in a Java-language source. This method is also used to identify the keyword to expand for the proto action.
The implementation of this method provided by the LpexCommonParser class returns false. The parser should override this to return its particular token delimiters.
public int cursorIndent(int element)
The implementation of this method provided by the LpexCommonParser class returns the display column of the beginning of text in this element, if any, or otherwise the column corresponding to the beginning of text in the preceding non-blank element, if any, or otherwise column 1; 1 is also returned if automatic indentation (the autoIndent parser property) is disabled. Show elements are ignored.
A document parser may override this method, to return a value appropriate to its auto-indentation scheme.
public String getTextIndent(int element)
The implementation of this method provided by the LpexCommonParser class returns the indent string to the beginning of text in the preceding non-blank element, if any, or otherwise an empty string. Show elements are ignored.
A document parser may override this method, to return a value appropriate to its auto-indentation scheme.
protected void newLine()
protected void openLine()
protected void splitLine()
An incremental parse is triggered in here to ensure the text tokenization is current.
public int indentText(int element)
public int indentText(int element, int desiredIndent)
public int indentText(int element, String desiredIndent)
public String styleString(char style, int len)
public final void setStyle(String styles, String installAttributes)
If the style.c property is not set, the style attributes specified by the installAttributes parameter are used. This is the document parser's install (hard-coded) value for the style. This parameter is assumed to be an appropriate color value for the active palette. It must be in a format suitable for the editor command set styleAttributes.c, for example, "0 128 128 255 255 255 underline".
If the parser property tokenHighlight is "off", then ATTRIBUTES_DEFAULT (converted to the active palette) is used instead.
As an example, below is how the style character for comments is defined in the Java document parser. It uses as default the common ATTRIBUTES_COMMENT style attributes, defined for BACKGROUND_COLOR, first converted to the currently active palette.
// get active palette's background color String toBackground = LpexPaletteAttributes.background(view); // convert style attributes from definition background to the active one String attributes = LpexPaletteAttributes.convert(ATTRIBUTES_COMMENT, BACKGROUND_COLOR, toBackground); // define the style character setStyle("c", attributes); |
public String getStyles()
public ArrayList getInstallStyleAttributes()
public String getStyleName(char styleCharacter)
public void lineComment(String text, boolean comment)
protected final void addMessage(int element, String text)
protected final void addMessage(int element, String text, long messageClass)
A document-parser error is usually set with addMessage(int,String), the error message being assigned the default CLASS_MESSAGE element class. This method may be used to customize the error messages, or to allow one to manage the separate addition and removal of error messages from different sources (for example, a lexer, a grammar parser, and a compiler).
protected final void addMessage(int element, String text, long messageClass, char messageStyle)
No imbedded messages are displayed if this feature (the errorMessages parser property) is disabled.
A document-parser error is usually set with addMessage(int,String), the error message being assigned the default CLASS_MESSAGE element class, and set the default STYLE_MESSAGE style character. This method may be used to customize the error messages, or to allow one to manage the separate addition and removal of error messages from different sources (for example, a lexer, a grammar parser, and a compiler).
All the addMessage() methods should only be invoked during the parseAll and parseElement calls. The messages to be inserted during the parse action are stacked. The actual insertion of the show-line messages will be carried out at the end of the current parse action. Several messages for one line will be displayed in the order that they were added.
The text of the message should be obtained from a locale-sensitive resource.
protected final void removeMessages(int fromElement, int toElement)
protected final void removeMessages(int fromElement, int toElement, long messageClass)
A parser normally adds error and informational embedded messages with addMessage(int,String), and removes them with removeMessages(int,int). Both these methods handle default messages. The methods that allow specifying the element class assigned to the message may be used to customize these error messages, or to allow one to manage the separate addition and removal of error messages from different sources (for example, a lexer, a grammar parser, and a compiler).
If using both qualified and default addMessage() calls, then in order to remove the messages of all types in a range, you can use:
removeMessages(fromElement, toElement, lpexView().classMask(CLASS_MESSAGE) | myMessageClassesBitMask);
All the removeMessages() methods should only be invoked during the parseAll and parseElement calls. The actual removal of embedded error messages will be carried out at the end of the current parse action. Several calls to this method during the course of one parse action may extend a range already set. Note that a range is assumed to be continuous.
|
SWT LPEX v3.0.0 | |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |