org.openbp.common.string.parser
Class StringParser

java.lang.Object
  extended by org.openbp.common.string.parser.StringParser

public class StringParser
extends java.lang.Object

Generic string parser class. Provides methods to scan a string for identifiers, numbers etc.

Author:
Heiko Erhardt

Constructor Summary
StringParser(java.lang.String strArg)
          Constructor.
 
Method Summary
 java.lang.String errorMsg(java.lang.String msg)
          Returns an error message prefixed by the standard error prefix and a file/line specification (see the setFileName(java.lang.String) methods).
 char getChar()
          Get the current character.
 char getChar(int i)
          Get a character relative to the current position.
 java.lang.String getExtraIdentifierChars()
          Gets the valid characters in identifiers beside letter, digit or underscore.
 java.lang.String getFileName()
          Gets the file name.
 java.lang.String getIdentifier()
          Get the identifier at the current position.
 int getLength()
          Gets the length of the string.
 int getLineNr()
          Gets the current line number.
 java.lang.String getNonWhitespace()
          Get the text until the next white space at the current position.
 java.lang.String getNumber()
          Get the number constant at the current position.
 java.util.List getOptionalParameters()
          Get the parameters enclosed in () as an array of strings.
 java.lang.String getOptionArgument()
          Get the string constant at the current position.
 int getPos()
          Gets the current character position.
 java.lang.String getQualifiedIdentifier()
          Get the (qualified) identifier at the current position.
 java.lang.String getSourceString()
          Gets the source string.
 java.lang.String getString(char delimChar)
          Get the string constant at the current position.
 java.lang.String getUntilEndOfLine()
          Get the text until the end of the line.
 boolean isEnd()
          Checks if we are at the end of the str.
 void nextChar()
          Proceed to the next character.
 void nextChar(int n)
          Skip characters.
 void printError(java.lang.String msg)
          Prints an error message
 void reInit(java.lang.String str)
          Re-inits the string parse.
 void restorePos(int pos)
          Restores the current character position to a previously saved state.
 void rewindChar()
          Rewind to the previous character.
 void rewindChar(int n)
          Rewind several characters.
 void setExtraIdentifierChars(java.lang.String extraIdentifierChars)
          Sets the valid characters in identifiers beside letter, digit or underscore.
 void setFileName(java.lang.String errorFileName)
          Sets the file name to prepend to error messages.
 void setLineNr(int line)
          Sets the current line number.
 void setPos(int pos)
          Sets the current character position.
 boolean skipAssignmentOperator(java.lang.String ident)
          Skips an assignemnt operator (" = ")
 void skipNoNewlineSpace()
          Skips spaces and tabs only, no newlines
 void skipSpace()
          Skips all white spaces.
 void skipUntil(java.lang.String pattern, boolean skipPattern)
          Skips until a pattern is found.
 boolean startsWith(java.lang.String pattern)
          Check if 'pattern' matches the string at the current position.
 java.lang.String substring()
          Performs a substring operation on the string from the current position up to the end of the string.
 java.lang.String substring(int begin)
          Performs a substring operation on the string.
 java.lang.String substring(int begin, int end)
          Performs a substring operation on the string.
 void throwError(java.lang.String msg)
          Throws an error runtime exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringParser

public StringParser(java.lang.String strArg)
Constructor.

Parameters:
strArg - String to parse
Method Detail

getExtraIdentifierChars

public java.lang.String getExtraIdentifierChars()
Gets the valid characters in identifiers beside letter, digit or underscore.


setExtraIdentifierChars

public void setExtraIdentifierChars(java.lang.String extraIdentifierChars)
Sets the valid characters in identifiers beside letter, digit or underscore.


reInit

public void reInit(java.lang.String str)
Re-inits the string parse. Sets the string to parse and resets the current index and line number.

Parameters:
str - String to parse

setFileName

public void setFileName(java.lang.String errorFileName)
Sets the file name to prepend to error messages. If an error file name is set, every error message will be prepended by "errorFileName (linenr): ".

Parameters:
errorFileName - File name or null

getChar

public char getChar()
Get the current character.

Returns:
The current character. The current index is not advanced.

getChar

public char getChar(int i)
Get a character relative to the current position.

Parameters:
i - The index of the character to return from the current position
Returns:
The character. The current index is not advanced.

nextChar

public void nextChar()
Proceed to the next character.


nextChar

public void nextChar(int n)
Skip characters.

Parameters:
n - Number of characters to skip

rewindChar

public void rewindChar()
Rewind to the previous character.


rewindChar

public void rewindChar(int n)
Rewind several characters.

Parameters:
n - Number of characters to rewind

skipSpace

public void skipSpace()
Skips all white spaces.


skipNoNewlineSpace

public void skipNoNewlineSpace()
Skips spaces and tabs only, no newlines


skipAssignmentOperator

public boolean skipAssignmentOperator(java.lang.String ident)
Skips an assignemnt operator (" = ")

Parameters:
ident - The identifier before the operator (for error messages)
Returns:
true The operator was skipped successfully
false There is no assignment operator.
In this case, an error is printed.

isEnd

public boolean isEnd()
Checks if we are at the end of the str.

Returns:
true The end of the string is reached
false The end of the string is not yet reached

getPos

public int getPos()
Gets the current character position.

Returns:
The character position, starting with 0

setPos

public void setPos(int pos)
Sets the current character position.

Parameters:
pos - The character position, starting with 0

restorePos

public void restorePos(int pos)
Restores the current character position to a previously saved state. The current line number is corrected accordingly.

Parameters:
pos - The character position to restore

getLength

public int getLength()
Gets the length of the string.

Returns:
The string length

getFileName

public java.lang.String getFileName()
Gets the file name.

Returns:
The file name if one was assigned using setFileName(java.lang.String)

getLineNr

public int getLineNr()
Gets the current line number.

Returns:
The file name if one was assigned using setFileName(java.lang.String)

setLineNr

public void setLineNr(int line)
Sets the current line number.

Parameters:
line - The new line number

getUntilEndOfLine

public java.lang.String getUntilEndOfLine()
Get the text until the end of the line. The current position will be at the first character after the end of line characters.

Returns:
The identifier

getIdentifier

public java.lang.String getIdentifier()
Get the identifier at the current position. The current position will be at the first character after the identifier.

Returns:
The identifier

getQualifiedIdentifier

public java.lang.String getQualifiedIdentifier()
Get the (qualified) identifier at the current position. The current position will be at the first character after the identifier.

Returns:
The identifier

getNonWhitespace

public java.lang.String getNonWhitespace()
Get the text until the next white space at the current position. The current position will be at the first white space character.

Returns:
The text

getNumber

public java.lang.String getNumber()
Get the number constant at the current position. The current position will be at the first character after the number.

Returns:
The number. If there is no number, null is returned.

getOptionArgument

public java.lang.String getOptionArgument()
Get the string constant at the current position. The string is delimited by \" or " The current position will be at the first character after the string.

Returns:
The string

getString

public java.lang.String getString(char delimChar)
Get the string constant at the current position. The string is delimited by \" or " The current position will be at the first character after the string.

Parameters:
delimChar - Specifies the delimiter character that should enclose the returned string.
If you want the string returned without delimiter characters, use '\0' for this parameter.
Returns:
The string

getOptionalParameters

public java.util.List getOptionalParameters()
Get the parameters enclosed in () as an array of strings. The parameters are separated by ','. The current position will be at the first character after the ')'.

Returns:
A list of parameter strings or null if there is no parameter list
Note: In order to reduce object allocation, in case of an empty parameter list, a static reference to an empty list will be returned. So do not modify the return value or subsequent calls to this method may not produce the desired results.

startsWith

public boolean startsWith(java.lang.String pattern)
Check if 'pattern' matches the string at the current position.

Parameters:
pattern - The pattern to search for
Returns:
true The match succeeded
false The match failed

skipUntil

public void skipUntil(java.lang.String pattern,
                      boolean skipPattern)
Skips until a pattern is found.

Parameters:
pattern - The pattern to search for
skipPattern - true The pattern itself is skipped. The current position will be at the first character after the pattern
false The current position will be at the first character of the pattern.

substring

public java.lang.String substring(int begin,
                                  int end)
Performs a substring operation on the string.

Parameters:
begin - The start index
end - The end index
Returns:
The sub string

substring

public java.lang.String substring(int begin)
Performs a substring operation on the string.

Parameters:
begin - The start index
Returns:
The sub string

substring

public java.lang.String substring()
Performs a substring operation on the string from the current position up to the end of the string.

Returns:
The sub string

throwError

public void throwError(java.lang.String msg)
Throws an error runtime exception.

Parameters:
msg - The error message

printError

public void printError(java.lang.String msg)
Prints an error message

Parameters:
msg - The error message

getSourceString

public java.lang.String getSourceString()
Gets the source string.


errorMsg

public java.lang.String errorMsg(java.lang.String msg)
Returns an error message prefixed by the standard error prefix and a file/line specification (see the setFileName(java.lang.String) methods).

Parameters:
msg - The 'raw' error message
Returns:
The formatted error message


Copyright © 2011. All Rights Reserved.