com.borland.primetime.editor
Class AbstractScanner

java.lang.Object
  |
  +--com.borland.primetime.editor.AbstractScanner

public abstract class AbstractScanner
extends java.lang.Object
implements Scanner

This class is the abstract base for language scanner classes used for syntax highlighting.

The scanner operates on one line at a time, and it's built to be able to do incremental updates of the syntax information. It starts scanning on the line on which the change began, and stops scanning when it reaches a line that has no style changes from what it was in the previous scan.

Subclasses will override this class to deal with the keywords specific to the language the scanner is being built for. Note that all of the token recognition methods were built for the Java language, but all of them can be overridden in a subclass to deal with languages that have different rules. For example, if the language has different comment rules, the checkComment(...) method will also need to be overridden.

See Also:
EditorDocument.StyledLeafElement, EditorDocument.RunInfo, ScannerJava

Field Summary
protected  int bp
          The buffer pointer.
protected  char[] buf
          The input buffer.
protected  char ch
          The last character read.
static char EOF
           
protected static int IN_COMMENT
          In a comment
protected static int NORMAL
          Not in a comment
protected  int stateFlags
          The scanner state after the last token was read.
 
Constructor Summary
AbstractScanner()
           
 
Method Summary
protected  int checkComment(int initialState)
          Check to see if the buffer pointer is on a comment character.
protected  int checkIdentifier(int initialState)
          Check to see if the buffer pointer is on the start of an identifier.
protected  int checkNumber(int initialState)
          Check to see if the buffer pointer is on a numeric character.
protected  int checkString(int initialState)
          Check to see if the buffer pointer is on a string character.
protected  int checkSymbol(int initialState)
          Check to see if the buffer pointer is on a symbol character.
protected  int checkWhitespace(int initialState)
          Check to see if the buffer pointer is on a whitespace character.
protected  void initialize(javax.swing.text.Segment text)
          initialize is called before starting a scan.
protected  void initialize(javax.swing.text.Segment text, javax.swing.text.Segment text2)
           
protected static int initMap(java.util.HashMap map, java.lang.String[] words, boolean caseSensitive)
          Internal routine to intialize a HashMap with words to look up and associated integer values
protected abstract  boolean isExtendedKeyword(java.lang.String str)
          The default implementation of checkIdentifier will call this method to determine whether or not a particular string is an extended keyword.
protected abstract  boolean isKeyword(java.lang.String str)
          The default implementation of checkIdentifier will call this method to determine whether or not a particular string is a keyword.
protected  boolean isSymbol(char ch)
          The default implementation of checkSymbol will call this method to determine whether or not a particular character is a valid symbol.
protected  boolean isValidIdentifierPart(char ch)
          The default implementation of checkIdentifier will call this method to determine whether or not a particular character can be a part of an identifier.
protected  boolean isValidIdentifierStart(char ch)
          The default implementation of checkIdentifier will call this method to determine whether or not a particular character can be the start of an identifier.
protected  int nextToken(int initialState)
          nextToken is called by scanLine to read the next run.
 void parse(javax.swing.event.DocumentEvent e)
          the parse method is called externally to start the scanner.
protected  boolean scanLine(EditorDocument.StyledLeafElement leaf, EditorDocument.StyledLeafElement leaf2, int initialState)
           
protected  boolean scanLine(EditorDocument.StyledLeafElement leaf, int initialState)
          scanLine is called by parse to actually scan a leaf of the document (which corresponds to a line of text).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOF

public static final char EOF

NORMAL

protected static final int NORMAL
Not in a comment

IN_COMMENT

protected static final int IN_COMMENT
In a comment

buf

protected char[] buf
The input buffer.

bp

protected int bp
The buffer pointer.

ch

protected char ch
The last character read.

stateFlags

protected int stateFlags
The scanner state after the last token was read. This is set by the individual check<...> methods and is passed as a parameter to each check<...> method that gets called. The stateFlags that are set after the last run on a line is read is stored with the line element.
Constructor Detail

AbstractScanner

public AbstractScanner()
Method Detail

initMap

protected static int initMap(java.util.HashMap map,
                             java.lang.String[] words,
                             boolean caseSensitive)
Internal routine to intialize a HashMap with words to look up and associated integer values
Parameters:
map - the HashMap to fill
words - the words to put into the hash map as keys
caseSensitive - the words to put into the hash map as keys
Returns:
the length of the longest word in the map which can be used to optimize whether to even bother looking up the word.

parse

public void parse(javax.swing.event.DocumentEvent e)
the parse method is called externally to start the scanner.
Specified by:
parse in interface Scanner
Parameters:
e - The DocumentEvent that generated this parse call. From this event, the scanner determines where to start parsing and how much of the document must be processed.

scanLine

protected boolean scanLine(EditorDocument.StyledLeafElement leaf,
                           int initialState)
scanLine is called by parse to actually scan a leaf of the document (which corresponds to a line of text). It will call nextToken until the end of the line is reached. This method handles adding the data to the RunInfo object, and assigning the RunInfo object to the leaf.
Parameters:
leaf - the leaf of the document that needs to be scanned.
stateFlags - The flags in effect at the start of this line. This state carries over from previous lines, and retains information such as whether the scanner should treat this line as part of a multi-line comment block.
Returns:
The return value of this method indicates whether or not parsing should continue with the next line.

scanLine

protected boolean scanLine(EditorDocument.StyledLeafElement leaf,
                           EditorDocument.StyledLeafElement leaf2,
                           int initialState)

initialize

protected void initialize(javax.swing.text.Segment text)
initialize is called before starting a scan. It copies characters into the character buffer (buf) and reads the first char.
Parameters:
text - A Segment object that contains the text that needs to be scanned.

initialize

protected void initialize(javax.swing.text.Segment text,
                          javax.swing.text.Segment text2)

nextToken

protected int nextToken(int initialState)
nextToken is called by scanLine to read the next run.
Parameters:
initalState - The state of the scanner when nextToken was called.
Returns:
the type of run that was just read.

isKeyword

protected abstract boolean isKeyword(java.lang.String str)
The default implementation of checkIdentifier will call this method to determine whether or not a particular string is a keyword. This method should take into account whether or not the language is case-sensitive.
Parameters:
str - the string to be checked
Returns:
true if str is a keyword.
See Also:
checkIdentifier

isExtendedKeyword

protected abstract boolean isExtendedKeyword(java.lang.String str)
The default implementation of checkIdentifier will call this method to determine whether or not a particular string is an extended keyword. This method should take into account whether or not the language is case-sensitive.
Parameters:
str - the string to be checked
Returns:
true if str is an extended keyword.
See Also:
checkIdentifier

isValidIdentifierStart

protected boolean isValidIdentifierStart(char ch)
The default implementation of checkIdentifier will call this method to determine whether or not a particular character can be the start of an identifier.
Parameters:
ch - the char to be checked
Returns:
true if ch can be the start of an identifier.
See Also:
checkIdentifier

isValidIdentifierPart

protected boolean isValidIdentifierPart(char ch)
The default implementation of checkIdentifier will call this method to determine whether or not a particular character can be a part of an identifier.
Parameters:
ch - the char to be checked
Returns:
true if ch can be a part of an identifier.
See Also:
checkIdentifier

isSymbol

protected boolean isSymbol(char ch)
The default implementation of checkSymbol will call this method to determine whether or not a particular character is a valid symbol.
Parameters:
ch - the char to be checked
Returns:
true if ch is a valid symbol
See Also:
checkSymbol

checkWhitespace

protected int checkWhitespace(int initialState)
Check to see if the buffer pointer is on a whitespace character. If so, advance the buffer point past all of the whitespace characters in this run.
Returns:
The defined WHITESPACE constant if bp is on a whitespace character, -1 otherwise.

checkComment

protected int checkComment(int initialState)
Check to see if the buffer pointer is on a comment character. If so, advance the buffer point past the entire comment.
Returns:
The defined COMMENT constant if bp is part of a comment section, -1 otherwise.

checkIdentifier

protected int checkIdentifier(int initialState)
Check to see if the buffer pointer is on the start of an identifier. If so, advance the buffer point past the identifier. This method is also responsible for checking to see if the identifier is a keyword or an extended keyword.

Returns:
The defined constant, usually RESERVED_WORD, EXTRA_KEYWORD, or IDENTIFIER, if bp is part of an identifier, -1 otherwise.
See Also:
isValidIdentifierStart, isValidIdentifierPart, isKeyword, isExtendedKeyword

checkNumber

protected int checkNumber(int initialState)
Check to see if the buffer pointer is on a numeric character. If so, advance the buffer point past the entire number.
Returns:
The defined NUMBER constant if bp is part of a comment section, -1 otherwise.

checkString

protected int checkString(int initialState)
Check to see if the buffer pointer is on a string character. If so, advance the buffer point past the entire string sequence.
Returns:
The defined STRING constant if bp is part of a comment section, -1 otherwise.

checkSymbol

protected int checkSymbol(int initialState)
Check to see if the buffer pointer is on a symbol character. If so, advance the buffer point past the entire symbol sequence.
Returns:
The defined SYMBOL constant if bp is part of a comment section, -1 otherwise.
See Also:
isSymbol