com.taco.text
Class CPunctuationState

java.lang.Object
  extended by com.taco.text.CPunctuationState
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class CPunctuationState
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

An object that represents the state within a string that uses C-style punctuation (braces, brackets, parentheses, escape characters, quotations). Note that, unlike C, single quotes can contain multiple characters.

See Also:
Serialized Form

Field Summary
static int BLOCK_COMMENT_STATE_END_STAR
          Value for blockCommentState that means we are in a a comment block, after an eligible '*' to start the termination sequence.
static int BLOCK_COMMENT_STATE_FIRST_SLASH
          Value for blockCommentState that means we have encountered the first slash for a comment block.
static int BLOCK_COMMENT_STATE_MIDDLE
          Value for blockCommentState that means we are in the middle of a comment block, before both the terminating '*' and '/'.
static int BLOCK_COMMENT_STATE_NOT_IN_BLOCK
          Value for blockCommentState that means we have not started comment block.
 int blockCommentState
          The state of being in block comment.
 int braces
          The number of opening brace ('{') yet to be closed.
 int brackets
          The number of opening brackets ('[') yet to be closed.
 java.lang.StringBuffer digitBuffer
          The digits used for a octal or unicode escape, encountered so far.
 boolean doubleQuoted
          True if this instance is in a double-quoted context.
 boolean escaped
          True if this instance is in an escaped context.
static java.lang.String HEX_DIGITS
          A string of all possible hex digits.
 boolean inLineComment
          True if this instance is in a line comment.
 char lastOctalChar
          Because octal escapes have a variable length, a character that ends an octal escape may result in two output characters: one for the octal escape that has ended and one for the original character.
static java.lang.String OCTAL_DIGITS
          A string of all possible octal digits.
 boolean octalEscaped
          True if this instance is in a octal escaped context.
 char outChar
          If set to something other than Character.MAX_VALUE, the character that was represented by the last character scanned.
 int parentheses
          The number of opening parentheses ('(') yet to be closed.
 boolean singleQuoted
          True if this instance is in a single-quoted context.
 boolean unicodeEscaped
          True if this instance is in a Unicode escaped context.
 
Constructor Summary
CPunctuationState()
           
 
Method Summary
protected  void _finishOctalEscape(int index)
          Finish an octal escape sequence, using digitBuffer to compute the last octal character (saved in outChar), and reseting the state.
protected static char _hexDigitsToChar(java.lang.CharSequence cs, int index)
          Convert the char sequence which is a sequence of hex digits to a character.
protected static char _octalDigitsToChar(java.lang.CharSequence cs, int index)
          Convert the char sequence which is a sequence of octal digits to a character.
 java.lang.Object clone()
           
 boolean inComment()
          Return true if we are in a line or block comment.
 boolean isClosed()
          Return true if the number of unclosed parentheses, brackets, and braces is 0, and the state is not in an escaped or quoted context.
static boolean isClosingPunctuation(char c)
          Return true if c is one of ')', ']', '}', '\'', or '"'.
static boolean isOpeningPunctuation(char c)
          Return true if c is one of '(', '[', '{', '\'', or '"'.
static char matchingPunctuation(char c)
          Return the punctuation character that either opens or closes the argument character.
 int matchPunctuation(java.lang.CharSequence cs, int startIndex)
          Assuming cs has C-style syntax, and using this instance as the punctuation state, return the index of the punctuation symbol that closes the opening punctuation symbol at startIndex (either '{' , '[', '(', '\'', or '"').
 void reset()
          Reset the state to be as it was just after construction.
 java.lang.String toString()
           
 char update(char c, int index)
          Update the state using the next character.
 java.lang.String update(java.lang.CharSequence cs)
          Update the state with the characters in cs.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

parentheses

public int parentheses
The number of opening parentheses ('(') yet to be closed.


brackets

public int brackets
The number of opening brackets ('[') yet to be closed.


braces

public int braces
The number of opening brace ('{') yet to be closed.


escaped

public boolean escaped
True if this instance is in an escaped context.


octalEscaped

public boolean octalEscaped
True if this instance is in a octal escaped context.


unicodeEscaped

public boolean unicodeEscaped
True if this instance is in a Unicode escaped context.


inLineComment

public boolean inLineComment
True if this instance is in a line comment.


blockCommentState

public int blockCommentState
The state of being in block comment.


digitBuffer

public java.lang.StringBuffer digitBuffer
The digits used for a octal or unicode escape, encountered so far. If null, we are not in an octal or unicode escape.


lastOctalChar

public char lastOctalChar
Because octal escapes have a variable length, a character that ends an octal escape may result in two output characters: one for the octal escape that has ended and one for the original character. But update() can only return one character, so the octal character built is saved here. If equal to Character.MAX_VALUE, the last character did not end an octal sequence.


doubleQuoted

public boolean doubleQuoted
True if this instance is in a double-quoted context.


singleQuoted

public boolean singleQuoted
True if this instance is in a single-quoted context.


outChar

public char outChar
If set to something other than Character.MAX_VALUE, the character that was represented by the last character scanned. Ordinary, this is the same as the last character, but in an escaped context, the last character might mean another character. If equal to Character.MAX_VALUE, the last character was escaped so no character was represented.


BLOCK_COMMENT_STATE_NOT_IN_BLOCK

public static final int BLOCK_COMMENT_STATE_NOT_IN_BLOCK
Value for blockCommentState that means we have not started comment block.

See Also:
Constant Field Values

BLOCK_COMMENT_STATE_FIRST_SLASH

public static final int BLOCK_COMMENT_STATE_FIRST_SLASH
Value for blockCommentState that means we have encountered the first slash for a comment block.

See Also:
Constant Field Values

BLOCK_COMMENT_STATE_MIDDLE

public static final int BLOCK_COMMENT_STATE_MIDDLE
Value for blockCommentState that means we are in the middle of a comment block, before both the terminating '*' and '/'.

See Also:
Constant Field Values

BLOCK_COMMENT_STATE_END_STAR

public static final int BLOCK_COMMENT_STATE_END_STAR
Value for blockCommentState that means we are in a a comment block, after an eligible '*' to start the termination sequence. If we encounter a '/', we will leave the block.

See Also:
Constant Field Values

OCTAL_DIGITS

public static final java.lang.String OCTAL_DIGITS
A string of all possible octal digits.

See Also:
Constant Field Values

HEX_DIGITS

public static final java.lang.String HEX_DIGITS
A string of all possible hex digits.

See Also:
Constant Field Values
Constructor Detail

CPunctuationState

public CPunctuationState()
Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

reset

public void reset()
Reset the state to be as it was just after construction.


update

public char update(char c,
                   int index)
            throws java.text.ParseException
Update the state using the next character. Return the character represented by the character, or Character.MAX_VALUE if the state is in an escaped context. If the character is a closing punctuation mark, throw a ParseException using the argument index as the index of error.

Throws:
java.text.ParseException

update

public java.lang.String update(java.lang.CharSequence cs)
                        throws java.text.ParseException
Update the state with the characters in cs. Output the unescaped characters.

Throws:
java.text.ParseException

isClosed

public boolean isClosed()
Return true if the number of unclosed parentheses, brackets, and braces is 0, and the state is not in an escaped or quoted context.


inComment

public boolean inComment()
Return true if we are in a line or block comment.


matchPunctuation

public int matchPunctuation(java.lang.CharSequence cs,
                            int startIndex)
                     throws java.text.ParseException

Assuming cs has C-style syntax, and using this instance as the punctuation state, return the index of the punctuation symbol that closes the opening punctuation symbol at startIndex (either '{' , '[', '(', '\'', or '"'). If the character at startIndex is not an opening punctation symbol, throw an IllegalArgumentException. If a matching symbol cannot be found by the end of cs, throw a ParseException. Also, if a closing punctuation symbol is encountered before its corresponding opening punctuation symbol, throw a ParseException. Characters before startIndex are not scanned, so it is assumed this instance accurately reflects the punctuation state at startIndex.

As a side effect, this instance's state is updated with the characters scanned, even if an exception is thrown. It may be prudent to call this method on a clone of an existing state.

Throws:
java.text.ParseException

_finishOctalEscape

protected void _finishOctalEscape(int index)
                           throws java.text.ParseException
Finish an octal escape sequence, using digitBuffer to compute the last octal character (saved in outChar), and reseting the state.

Throws:
java.text.ParseException

_octalDigitsToChar

protected static char _octalDigitsToChar(java.lang.CharSequence cs,
                                         int index)
                                  throws java.text.ParseException
Convert the char sequence which is a sequence of octal digits to a character.

Throws:
java.text.ParseException

_hexDigitsToChar

protected static char _hexDigitsToChar(java.lang.CharSequence cs,
                                       int index)
                                throws java.text.ParseException
Convert the char sequence which is a sequence of hex digits to a character.

Throws:
java.text.ParseException

isOpeningPunctuation

public static final boolean isOpeningPunctuation(char c)
Return true if c is one of '(', '[', '{', '\'', or '"'.


isClosingPunctuation

public static final boolean isClosingPunctuation(char c)
Return true if c is one of ')', ']', '}', '\'', or '"'.


matchingPunctuation

public static final char matchingPunctuation(char c)
Return the punctuation character that either opens or closes the argument character. If c is not a valid punctuation character, throw an IllegalArgumentException.