Package jpicedt.format.input.util
Class Context
- java.lang.Object
-
- jpicedt.format.input.util.Context
-
- All Implemented Interfaces:
ExpressionConstants
public class Context extends java.lang.Object implements ExpressionConstants
A class that stores context information about the parsing process, like : current line number, current parsed substring, block markers, stack for markers...By convention, end markers (EOF, EndOfBlocks, ...) always refer to a position one character ahead of the last character (e.g. of the block), so that String.substring() works properly w/o adding 1 to the end-index.
Context also offers a "convenience" marker which may be handled by using mark() and reset().
- Since:
- jpicedt 1.3 [refactored from scratch as of 1.3.1]
- Version:
- $Id: Context.java,v 1.5.2.1 2007/09/02 11:56:13 reynal Exp $
- Author:
- Sylvain Reynal
-
-
Field Summary
-
Fields inherited from interface jpicedt.format.input.util.ExpressionConstants
ANY_CHAR, ANY_CHAR_EOL, ANY_SIGN, AT_LEAST, AT_MOST, DEBUG, DOUBLE, EOL, EXACTLY, INTEGER, NEGATIVE, NON_NULL, POSITIVE, STRICTLY_NEGATIVE, STRICTLY_POSITIVE, WHITE_SPACES, WHITE_SPACES_OR_EOL
-
-
Constructor Summary
Constructors Constructor Description Context(java.io.Reader reader)build a new Context fed by the given reader
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidenterBlock(int blockEnd)save current markers in the stack, and set new block boundaries : new endBlockMarker is set to the given position new beginBlockMarker is set to the current caret position convenience marker is saved, and a new one is initialized to the beginning of the (new) current block.voidexitBlock()restore old markers (and forget current value), then move caret to the end of the block we just went out.java.lang.Stringfind(java.util.regex.Pattern re)Attempts to find the next subsequence of the current block that matches the given pattern, starting at the caret position, then moves the caret past the matched string if found.intgetBeginningOfBlock()Return the position of the beginning of the current blockjava.lang.StringgetBlockContent()Return the content of the current block.intgetBOL()Returns the position of the beginning of the current line, or the beginning of the current block if the caret is located b/w the B.of.Block and the first CR inside the current block.java.lang.StringgetBuffer()Return the entire bufferintgetCaretPosition()Return the start-index of the remaining substring to be analysed.java.lang.CharactergetCharAt(int index)Return the character at the given position, or null if index is negative, or past EOFjava.lang.StringgetCurrentLine()intgetEndOfBlock()Return the position of the end of the current block, i.e.intgetEOF()Return the position of the end of the buffer, i.e.intgetEOL()Returns the position of the end of the current line, i.e.intgetLineNumber()Return the current line number ; beware that first line is numbered "0" !!! A line is terminated by a '\n'.java.util.regex.MatchergetMatcher(java.util.regex.Pattern re)Returns the matcher built by matching the given pattern against the current block, starting at the current caret position.intindexOf(java.lang.String s)Return the index of the first occurence of the given String, starting from the current caret position.intindexOf(java.lang.String s, int fromIndex)Return the index of the first occurence of the given String in the current block, starting from the given positionintindexOfBeforeEOL(java.lang.String s)Return the index of the first occurence of the given String, starting from the current caret position, ending at getEndOfBlock() (not included!).booleanisAtBeginningOfBlock()Return true if the caret is located at the beginning of the current blockbooleanisAtEndOfBlock()Return true if the caret is located at the end of the current blockbooleanisAtEOF()Return true if the caret is located at the end of the entire bufferbooleanlineFeed()Move cursor to the beginning of the next line, EVEN if we aren't at an EOLbooleanlookingAt(java.util.regex.Pattern re)match the given RE pattern on the current block, starting at the caret position, then move the caret past the matched string if found.static voidmain(java.lang.String[] arg)context standalone testvoidmark()Set the convenience marker to the current caret position.booleanmatchAndMove(java.lang.String s)Test if the string at the caret position starts with the given string, and in case of success moves the caret past this string ; return false otherwise.booleanmoveCaretBy(int increment)Move cursor position forward by the given increment, possibly proceeding to linefeeds.booleanmoveCaretTo(int newPos)Move cursor position forward to the given position, possibly proceeding to linefeeds.booleanpushBack()After a read(), pushes back the char that has been readjava.lang.Characterread()read a character, and increment the caret position by one, if this is possible.java.lang.Stringread(int n)read the given number of character, wrapped in a String, and move the caret past the String being returned.java.lang.StringreadTo(int pos)Return the remaining substring up to the given position if (pos < caret) return "".java.lang.StringreadToEOL()Returns the remaining substring up to the end of the current line, EOL NOT INCLUDED ! Doesn't proceed to any linefeed, i.e.static java.lang.StringremoveLineFeeds(java.lang.String s)Remove '\n' and '\r' from a given string, and return a new string.static java.lang.StringremoveRedundantWhiteSpaces(java.lang.String s)Replace " " (double-space) or tabs by ' ' ; always return a new String.voidreset()reset caret position to the convenience marker position, or to the beginning of the current block if no marker has been set.booleanstartsWith(java.lang.String s)Return whether the string at the caret position starts with the given string or not.java.lang.StringtoString()
-
-
-
Constructor Detail
-
Context
public Context(java.io.Reader reader) throws REParserException.EOFbuild a new Context fed by the given reader- Parameters:
r- a reader encapsulating the text to be parsed- Throws:
REParserException.EOF
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getLineNumber
public int getLineNumber()
Return the current line number ; beware that first line is numbered "0" !!! A line is terminated by a '\n'.
-
getCaretPosition
public int getCaretPosition()
Return the start-index of the remaining substring to be analysed.Ranges from 0 to currentLine.length()-1
-
getBOL
public int getBOL()
Returns the position of the beginning of the current line, or the beginning of the current block if the caret is located b/w the B.of.Block and the first CR inside the current block.
-
getEOL
public int getEOL()
Returns the position of the end of the current line, i.e. the location of the "\n" character, OR the end of the current block, if the caret is located b/w the E.of.Block and the last CR inside the block.
-
isAtEOF
public boolean isAtEOF()
Return true if the caret is located at the end of the entire buffer
-
getEOF
public int getEOF()
Return the position of the end of the buffer, i.e. of the last character in the buffer.
-
moveCaretBy
public boolean moveCaretBy(int increment) throws REParserException.EOFMove cursor position forward by the given increment, possibly proceeding to linefeeds.- Returns:
- false if at EOF, or the end of the current block, was reached.
- Throws:
REParserException.EOF
-
moveCaretTo
public boolean moveCaretTo(int newPos) throws REParserException.EOFMove cursor position forward to the given position, possibly proceeding to linefeeds.In any case, caret CAN'T move out of the current block.
- Parameters:
newPos- new caret position ; can be negative, null, or positive.- Returns:
- false if EOF, or the end of the current block, was reached.
- Throws:
REParserException.EOF- if EOF was reached.
-
lineFeed
public boolean lineFeed() throws REParserException.EOFMove cursor to the beginning of the next line, EVEN if we aren't at an EOL- Returns:
- FALSE if at EOF, or if we went past the current sub-buffer (enclosing expression)
- Throws:
REParserException.EOF- if there's nothing to be read from the reader (EOF)
-
mark
public void mark()
Set the convenience marker to the current caret position.
-
reset
public void reset()
reset caret position to the convenience marker position, or to the beginning of the current block if no marker has been set.There's one convenience marker per block, i.e. marker defaults to beginning-of-block when entering a block.
-
getBuffer
public java.lang.String getBuffer()
Return the entire buffer
-
getCharAt
public java.lang.Character getCharAt(int index)
Return the character at the given position, or null if index is negative, or past EOF
-
getCurrentLine
public java.lang.String getCurrentLine()
- Returns:
- the current line, i.e. substring from BOL to EOL markers, w/o the trailing "\n" Doesn't move the caret !!! If we're inside a block, BOL and EOL have different meaning (see getBOL() and getEOL()).
-
read
public java.lang.Character read() throws REParserException.EOFread a character, and increment the caret position by one, if this is possible.- Returns:
- null if we've reached the end of the current block (which can be EOF)
- Throws:
REParserException.EOF
-
pushBack
public boolean pushBack() throws REParserException.EOFAfter a read(), pushes back the char that has been read- Throws:
REParserException.EOF
-
read
public java.lang.String read(int n) throws REParserException.EOFread the given number of character, wrapped in a String, and move the caret past the String being returned. May return "" if we're inside a block, and end-of-block has been reached.- Throws:
REParserException.EOF
-
readToEOL
public java.lang.String readToEOL() throws REParserException.EOFReturns the remaining substring up to the end of the current line, EOL NOT INCLUDED ! Doesn't proceed to any linefeed, i.e. simply move the caret to EOL!!! If we're inside a block, and current EOL is greater than getEndOfBlock(), getEndOfBlock() is taken as the EOL (see getEOL()). Note that this wouldn't make sense otherwise...
- Returns:
- NULL if EOF has been reached or "" if at EOL.
- Throws:
REParserException.EOF
-
readTo
public java.lang.String readTo(int pos) throws REParserException.EOFReturn the remaining substring up to the given position if (pos < caret) return "". Move the caret just past the String being returned.- Throws:
REParserException.EOF
-
matchAndMove
public boolean matchAndMove(java.lang.String s) throws REParserException.EOFTest if the string at the caret position starts with the given string, and in case of success moves the caret past this string ; return false otherwise.- Throws:
REParserException.EOF
-
indexOf
public int indexOf(java.lang.String s)
Return the index of the first occurence of the given String, starting from the current caret position.- Returns:
- -1 if not found inside the current block
-
indexOf
public int indexOf(java.lang.String s, int fromIndex)Return the index of the first occurence of the given String in the current block, starting from the given position- Returns:
- -1 if not found inside the current block
-
indexOfBeforeEOL
public int indexOfBeforeEOL(java.lang.String s)
Return the index of the first occurence of the given String, starting from the current caret position, ending at getEndOfBlock() (not included!).- Returns:
- -1 if not found
-
startsWith
public boolean startsWith(java.lang.String s)
Return whether the string at the caret position starts with the given string or not.
-
removeLineFeeds
public static java.lang.String removeLineFeeds(java.lang.String s)
Remove '\n' and '\r' from a given string, and return a new string.
-
removeRedundantWhiteSpaces
public static java.lang.String removeRedundantWhiteSpaces(java.lang.String s)
Replace " " (double-space) or tabs by ' ' ; always return a new String.
-
getMatcher
public java.util.regex.Matcher getMatcher(java.util.regex.Pattern re)
Returns the matcher built by matching the given pattern against the current block, starting at the current caret position. Hence indices returned by the produced matcher must be incremented by getCaretPosition() to become meaningful.
-
lookingAt
public boolean lookingAt(java.util.regex.Pattern re)
match the given RE pattern on the current block, starting at the caret position, then move the caret past the matched string if found.- Returns:
- false if no match, true if the given pattern matches the beginning of the current block
-
find
public java.lang.String find(java.util.regex.Pattern re)
Attempts to find the next subsequence of the current block that matches the given pattern, starting at the caret position, then moves the caret past the matched string if found.- Returns:
- null if no match, of the string from caret position to the start of the match, if found (aka "swallowed string")
-
getBeginningOfBlock
public int getBeginningOfBlock()
Return the position of the beginning of the current block
-
getEndOfBlock
public int getEndOfBlock()
Return the position of the end of the current block, i.e. the position of the last char in the block, plus one (so that for example substring(getBeginningOfBlock(), getEndOfBlock()) returns the entire block string.
-
isAtBeginningOfBlock
public boolean isAtBeginningOfBlock()
Return true if the caret is located at the beginning of the current block
-
isAtEndOfBlock
public boolean isAtEndOfBlock()
Return true if the caret is located at the end of the current block
-
getBlockContent
public java.lang.String getBlockContent()
Return the content of the current block. Note : this is an expensive operation since it creates a new String.
-
enterBlock
public void enterBlock(int blockEnd)
save current markers in the stack, and set new block boundaries :- new endBlockMarker is set to the given position
- new beginBlockMarker is set to the current caret position
- convenience marker is saved, and a new one is initialized to the beginning of the (new) current block.
-
exitBlock
public void exitBlock()
restore old markers (and forget current value), then move caret to the end of the block we just went out.
-
main
public static void main(java.lang.String[] arg)
context standalone test- Parameters:
arg- [0] name of file to parse.
-
-