http://www.jpicedt.org

jpicedt.format.input.util
Class Context

java.lang.Object
  extended by jpicedt.format.input.util.Context

public class Context
extends Object

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.10 2013/03/31 06:59:54 vincentb1 Exp $
Author:
Sylvain Reynal

Constructor Summary
Context(Reader reader)
          Build a new Context fed by the given reader.
 
Method Summary
 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.
 void exitBlock()
          Restore old markers (and forget current value), then move caret to the end of the block we just went out.
 String find(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.
 int getBeginningOfBlock()
          Return the position of the beginning of the current block
 String getBlockContent()
          Return the content of the current block.
 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.
 String getBuffer()
          Return the entire buffer
 int getCaretPosition()
          Return the start-index of the remaining substring to be analysed.
 Character getCharAt(int index)
          Return the character at the given position, or null if index is negative, or past EOF
 String getCurrentLine()
           
 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.
 int getEOF()
          Return the position of the end of the buffer, i.e. of the last character in the buffer.
 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.
 int getLineNumber()
          Return the current line number ; beware that first line is numbered "0" !!!
 Matcher getMatcher(Pattern re)
          Returns the matcher built by matching the given pattern against the current block, starting at the current caret position.
 int indexOf(String s)
          Return the index of the first occurence of the given String, starting from the current caret position.
 int indexOf(String s, int fromIndex)
          Return the index of the first occurence of the given String in the current block, starting from the given position
 int indexOfBeforeEOL(String s)
          Return the index of the first occurence of the given String, starting from the current caret position, ending at getEndOfBlock() (not included!).
 boolean isAtBeginningOfBlock()
          Return true if the caret is located at the beginning of the current block
 boolean isAtEndOfBlock()
          Return true if the caret is located at the end of the current block
 boolean isAtEOF()
          Return true if the caret is located at the end of the entire buffer
 boolean lineFeed()
          Move cursor to the beginning of the next line, EVEN if we aren't at an EOL
 boolean lookingAt(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 void main(String[] arg)
          Context standalone test
 void mark()
          Set the convenience marker to the current caret position.
 boolean matchAndMove(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.
 boolean moveCaretBy(int increment)
          Move cursor position forward by the given increment, possibly proceeding to linefeeds.
 boolean moveCaretTo(int newPos)
          Move cursor position forward to the given position, possibly proceeding to linefeeds.
 boolean pushBack()
          After a read(), pushes back the char that has been read
 Character read()
          read a character, and increment the caret position by one, if this is possible.
 String read(int n)
          read the given number of character, wrapped in a String, and move the caret past the String being returned.
 String readTo(int pos)
          Return the remaining substring up to the given position if (pos < caret) return "".
 String readToEOL()
          Returns the remaining substring up to the end of the current line, EOL NOT INCLUDED !
static String removeLineFeeds(String s)
          Remove '\n' and '\r' from a given string, and return a new string.
static String removeRedundantWhiteSpaces(String s)
          Replace " " (double-space) or tabs by ' ' ; always return a new String.
 void reset()
          reset caret position to the convenience marker position, or to the beginning of the current block if no marker has been set.
 boolean startsWith(String s)
          Return whether the string at the caret position starts with the given string or not.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Context

public Context(Reader reader)
        throws REParserException.EOF
Build a new Context fed by the given reader.

Parameters:
reader - a reader encapsulating the text to be parsed
Throws:
REParserException.EOF
Method Detail

toString

public String toString()
Overrides:
toString in class 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.EOF
Move 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.EOF
Move 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.EOF
Move 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 String getBuffer()
Return the entire buffer


getCharAt

public Character getCharAt(int index)
Return the character at the given position, or null if index is negative, or past EOF


getCurrentLine

public 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 Character read()
               throws REParserException.EOF
read 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.EOF
After a read(), pushes back the char that has been read

Throws:
REParserException.EOF

read

public String read(int n)
            throws REParserException.EOF
read 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 String readToEOL()
                 throws REParserException.EOF
Returns 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 String readTo(int pos)
              throws REParserException.EOF
Return 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(String s)
                     throws REParserException.EOF
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.

Throws:
REParserException.EOF

indexOf

public int indexOf(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(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(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(String s)
Return whether the string at the caret position starts with the given string or not.


removeLineFeeds

public static String removeLineFeeds(String s)
Remove '\n' and '\r' from a given string, and return a new string.


removeRedundantWhiteSpaces

public static String removeRedundantWhiteSpaces(String s)
Replace " " (double-space) or tabs by ' ' ; always return a new String.


getMatcher

public Matcher getMatcher(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(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 String find(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 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 : If the new endBlockMarker position lies outside the range of the old current block, it's trimmed, that is, a new block CAN'T stick out of the block in which it was created.


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(String[] arg)
Context standalone test

Parameters:
arg - arg[0] is the name of file to parse.

http://www.jpicedt.org

Submit a bug : syd@jpicedt.org