Class 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
    • 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
      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.
      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.
      int getBeginningOfBlock()
      Return the position of the beginning of the current block
      java.lang.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.
      java.lang.String getBuffer()
      Return the entire buffer
      int getCaretPosition()
      Return the start-index of the remaining substring to be analysed.
      java.lang.Character getCharAt​(int index)
      Return the character at the given position, or null if index is negative, or past EOF
      java.lang.String getCurrentLine()  
      int getEndOfBlock()
      Return the position of the end of the current block, i.e.
      int getEOF()
      Return the position of the end of the buffer, i.e.
      int getEOL()
      Returns the position of the end of the current line, i.e.
      int getLineNumber()
      Return the current line number ; beware that first line is numbered "0" !!! A line is terminated by a '\n'.
      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.
      int indexOf​(java.lang.String s)
      Return the index of the first occurence of the given String, starting from the current caret position.
      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
      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!).
      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​(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 void main​(java.lang.String[] arg)
      context standalone test
      void mark()
      Set the convenience marker to the current caret position.
      boolean matchAndMove​(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.
      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
      java.lang.Character read()
      read a character, and increment the caret position by one, if this is possible.
      java.lang.String read​(int n)
      read the given number of character, wrapped in a String, and move the caret past the String being returned.
      java.lang.String readTo​(int pos)
      Return the remaining substring up to the given position if (pos < caret) return "".
      java.lang.String readToEOL()
      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.String removeLineFeeds​(java.lang.String s)
      Remove '\n' and '\r' from a given string, and return a new string.
      static java.lang.String removeRedundantWhiteSpaces​(java.lang.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​(java.lang.String s)
      Return whether the string at the caret position starts with the given string or not.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Context

        public Context​(java.io.Reader reader)
                throws REParserException.EOF
        build 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:
        toString in class java.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.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 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.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
      • read

        public java.lang.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 java.lang.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 java.lang.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​(java.lang.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​(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.
        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​(java.lang.String[] arg)
        context standalone test
        Parameters:
        arg - [0] name of file to parse.