Thread: [Adapdev-commits] Adapdev/src/Adapdev.NVelocity/Runtime/Parser CharStream.cs,1.2,1.3 JJTParserState.
Status: Beta
Brought to you by:
intesar66
From: Sean M. <int...@us...> - 2005-11-16 05:45:34
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev.NVelocity/Runtime/Parser In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22005/src/Adapdev.NVelocity/Runtime/Parser Added Files: CharStream.cs JJTParserState.cs ParseException.cs Parser.cs ParserConstants.cs ParserTokenManager.cs ParserTreeConstants.cs Token.cs TokenMgrError.cs VelocityCharStream.cs Log Message: --- NEW FILE: ParserConstants.cs --- namespace NVelocity.Runtime.Parser { using System; /* Generated By:JJTree&JavaCC: Do not edit this line. ParserConstants.java */ public class ParserConstants { public const int EOF = 0; public const int LBRACKET = 1; public const int RBRACKET = 2; public const int COMMA = 3; public const int DOUBLEDOT = 4; public const int LPAREN = 5; public const int RPAREN = 6; public const int REFMOD2_RPAREN = 7; public const int ESCAPE_DIRECTIVE = 8; public const int SET_DIRECTIVE = 9; public const int DOLLAR = 10; public const int DOLLARBANG = 11; public const int HASH = 15; public const int DOUBLE_ESCAPE = 16; public const int ESCAPE = 17; public const int TEXT = 18; public const int SINGLE_LINE_COMMENT = 19; public const int FORMAL_COMMENT = 20; public const int MULTI_LINE_COMMENT = 21; public const int WHITESPACE = 23; public const int STRING_LITERAL = 24; public const int TRUE = 25; public const int FALSE = 26; public const int NEWLINE = 27; public const int MINUS = 28; public const int PLUS = 29; public const int MULTIPLY = 30; public const int DIVIDE = 31; public const int MODULUS = 32; public const int LOGICAL_AND = 33; public const int LOGICAL_OR = 34; public const int LOGICAL_LT = 35; public const int LOGICAL_LE = 36; public const int LOGICAL_GT = 37; public const int LOGICAL_GE = 38; public const int LOGICAL_EQUALS = 39; public const int LOGICAL_NOT_EQUALS = 40; public const int LOGICAL_NOT = 41; public const int EQUALS = 42; public const int END = 43; public const int IF_DIRECTIVE = 44; public const int ELSEIF_DIRECTIVE = 45; public const int ELSE_DIRECTIVE = 46; public const int STOP_DIRECTIVE = 47; public const int DIGIT = 48; public const int NUMBER_LITERAL = 49; public const int LETTER = 50; public const int DIRECTIVE_CHAR = 51; public const int WORD = 52; public const int ALPHA_CHAR = 53; public const int ALPHANUM_CHAR = 54; public const int IDENTIFIER_CHAR = 55; public const int IDENTIFIER = 56; public const int DOT = 57; public const int LCURLY = 58; public const int RCURLY = 59; public const int REFERENCE_TERMINATOR = 60; public const int DIRECTIVE_TERMINATOR = 61; public const int DIRECTIVE = 0; public const int REFMOD2 = 1; public const int REFMODIFIER = 2; public const int DEFAULT = 3; public const int PRE_DIRECTIVE = 4; public const int REFERENCE = 5; public const int IN_MULTI_LINE_COMMENT = 6; public const int IN_FORMAL_COMMENT = 7; public const int IN_SINGLE_LINE_COMMENT = 8; public static readonly String[] tokenImage = new String[] {"<EOF>", "\"[\"", "\"]\"", "\",\"", "\"..\"", "\"(\"", "<RPAREN>", "\")\"", "<ESCAPE_DIRECTIVE>", "<SET_DIRECTIVE>", "<DOLLAR>", "<DOLLARBANG>", "\"##\"", "<token of kind 13>", "\"#*\"", "\"#\"", "\"\\\\\\\\\"", "\"\\\\\"", "<TEXT>", "<SINGLE_LINE_COMMENT>", "\"*#\"", "\"*#\"", "<token of kind 22>", "<WHITESPACE>", "<STRING_LITERAL>", "\"true\"", "\"false\"", "<NEWLINE>", "\"-\"", "\"+\"", "\"*\"", "\"/\"", "\"%\"", "\"&&\"", "\"||\"", "\"<\"", "\"<=\"", "\">\"", "\">=\"", "\"==\"", "\"!=\"", "\"!\"", "\"=\"", "<END>", "\"if\"", "\"elseif\"", "<ELSE_DIRECTIVE>", "\"stop\"", "<DIGIT>", "<NUMBER_LITERAL>", "<LETTER>", "<DIRECTIVE_CHAR>", "<WORD>", "<ALPHA_CHAR>", "<ALPHANUM_CHAR>", "<IDENTIFIER_CHAR>", "<IDENTIFIER>", "<DOT>", "\"{\"", "\"}\"", "<REFERENCE_TERMINATOR>", "<DIRECTIVE_TERMINATOR>"}; } } --- NEW FILE: ParseException.cs --- /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 2.1 */ namespace NVelocity.Runtime.Parser { using System; using System.Text; /// <summary> This exception is thrown when parse errors are encountered. /// You can explicitly create objects of this exception type by /// calling the method generateParseException in the generated /// parser. /// * /// You can modify this class to customize your error reporting /// mechanisms so long as you retain the public fields. /// </summary> public class ParseException : Exception { /// <summary> The end of line string for this machine. /// </summary> protected internal String eol = Environment.NewLine; public override String Message { get { if (!specialConstructor) { return base.Message; } String expected = ""; int maxSize = 0; for (int i = 0; i < expectedTokenSequences.Length; i++) { if (maxSize < expectedTokenSequences[i].Length) { maxSize = expectedTokenSequences[i].Length; } for (int j = 0; j < expectedTokenSequences[i].Length; j++) { expected += tokenImage[expectedTokenSequences[i][j]] + " "; } if (expectedTokenSequences[i][expectedTokenSequences[i].Length - 1] != 0) { expected += "..."; } expected += eol + " "; } String retval = "Encountered \""; Token tok = currentToken.next; for (int i = 0; i < maxSize; i++) { if (i != 0) retval += " "; if (tok.kind == 0) { retval += tokenImage[0]; break; } retval += add_escapes(tok.image); tok = tok.next; } retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; retval += "." + eol; if (expectedTokenSequences.Length == 1) { retval += "Was expecting:" + eol + " "; } else { retval += "Was expecting one of:" + eol + " "; } retval += expected; return retval; } } /// <summary> This constructor is used by the method "generateParseException" /// in the generated parser. Calling this constructor generates /// a new object of this type with the fields "currentToken", /// "expectedTokenSequences", and "tokenImage" set. The boolean /// flag "specialConstructor" is also set to true to indicate that /// this constructor was used to create this object. /// This constructor calls its super class with the empty string /// to force the "toString" method of parent class "Throwable" to /// print the error message in the form: /// ParseException: <result of getMessage> /// </summary> public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String[] tokenImageVal) : base("") { specialConstructor = true; currentToken = currentTokenVal; expectedTokenSequences = expectedTokenSequencesVal; tokenImage = tokenImageVal; } /// <summary> The following constructors are for use by you for whatever /// purpose you can think of. Constructing the exception in this /// manner makes the exception behave in the normal way - i.e., as /// documented in the class "Throwable". The fields "errorToken", /// "expectedTokenSequences", and "tokenImage" do not contain /// relevant information. The JavaCC generated code does not use /// these constructors. /// </summary> public ParseException() : base() { specialConstructor = false; } public ParseException(String message) : base(message) { specialConstructor = false; } /// <summary> This variable determines which constructor was used to create /// this object and thereby affects the semantics of the /// "getMessage" method (see below). /// </summary> protected internal bool specialConstructor; /// <summary> This is the last token that has been consumed successfully. If /// this object has been created due to a parse error, the token /// followng this token will (therefore) be the first error token. /// </summary> public Token currentToken; /// <summary> Each entry in this array is an array of integers. Each array /// of integers represents a sequence of tokens (by their ordinal /// values) that is expected at this point of the parse. /// </summary> public int[][] expectedTokenSequences; /// <summary> This is a reference to the "tokenImage" array of the generated /// parser within which the parse error occurred. This array is /// defined in the generated ...Constants interface. /// </summary> public String[] tokenImage; /// <summary> This method has the standard behavior when this object has been /// created using the standard constructors. Otherwise, it uses /// "currentToken" and "expectedTokenSequences" to generate a parse /// error message and returns it. If this object has been created /// due to a parse error, and you do not catch it (it gets thrown /// from the parser), then this method is called during the printing /// of the final stack trace, and hence the correct error message /// gets displayed. /// </summary> /// <summary> Used to convert raw characters to their escaped version /// when these raw version cannot be used as part of an ASCII /// string literal. /// </summary> protected internal virtual String add_escapes(String str) { StringBuilder retval = new StringBuilder(); char ch; for (int i = 0; i < str.Length; i++) { switch (str[i]) { case (char) (0): continue; case '\b': retval.Append("\\b"); continue; case '\t': retval.Append("\\t"); continue; case '\n': retval.Append("\\n"); continue; case '\f': retval.Append("\\f"); continue; case '\r': retval.Append("\\r"); continue; case '\"': retval.Append("\\\""); continue; case '\'': retval.Append("\\\'"); continue; case '\\': retval.Append("\\\\"); continue; default: if ((ch = str[i]) < 0x20 || ch > 0x7e) { String s = "0000" + Convert.ToString(ch, 16); retval.Append("\\u" + s.Substring(s.Length - 4, (s.Length) - (s.Length - 4))); } else { retval.Append(ch); } continue; } } return retval.ToString(); } } } --- NEW FILE: JJTParserState.cs --- /* Generated By:JJTree: Do not edit this line. JJTParserState.java */ namespace NVelocity.Runtime.Parser { using System; using System.Collections; using NVelocity.Runtime.Parser.Node; internal class JJTParserState { private Stack nodes; private Stack marks; private int sp; // number of nodes on stack private int mk; // current mark private bool node_created; internal JJTParserState() { nodes = new Stack(); marks = new Stack(); sp = 0; mk = 0; } /* Determines whether the current node was actually closed and pushed. This should only be called in the final user action of a node scope. */ internal virtual bool nodeCreated() { return node_created; } /* Call this to reinitialize the node stack. It is called automatically by the parser's ReInit() method. */ internal virtual void reset() { nodes.Clear(); marks.Clear(); sp = 0; mk = 0; } /* Returns the root node of the AST. It only makes sense to call this after a successful parse. */ internal virtual INode rootNode() { return (INode) (nodes.ToArray())[nodes.Count - (0 + 1)]; } /* Pushes a node on to the stack. */ internal virtual void pushNode(INode n) { Object temp_object; temp_object = n; Object generatedAux = temp_object; nodes.Push(temp_object); ++sp; } /* Returns the node on the top of the stack, and remove it from the stack. */ internal virtual INode popNode() { if (--sp < mk) { mk = ((Int32) marks.Pop()); } return (INode) nodes.Pop(); } /* Returns the node currently on the top of the stack. */ internal virtual INode peekNode() { return (INode) nodes.Peek(); } /* Returns the number of children on the stack in the current node scope. */ internal virtual int nodeArity() { return sp - mk; } internal virtual void clearNodeScope(INode n) { while (sp > mk) { popNode(); } mk = ((Int32) marks.Pop()); } internal virtual void openNodeScope(INode n) { Object temp_object; temp_object = mk; Object generatedAux = temp_object; marks.Push(temp_object); mk = sp; n.jjtOpen(); } /* A definite node is constructed from a specified number of children. That number of nodes are popped from the stack and made the children of the definite node. Then the definite node is pushed on to the stack. */ internal virtual void closeNodeScope(INode n, int num) { mk = ((Int32) marks.Pop()); while (num-- > 0) { INode c = popNode(); c.jjtSetParent(n); n.jjtAddChild(c, num); } n.jjtClose(); pushNode(n); node_created = true; } /* A conditional node is constructed if its condition is true. All the nodes that have been pushed since the node was opened are made children of the the conditional node, which is then pushed on to the stack. If the condition is false the node is not constructed and they are left on the stack. */ internal virtual void closeNodeScope(INode n, bool condition) { if (condition) { int a = nodeArity(); mk = ((Int32) marks.Pop()); while (a-- > 0) { INode c = popNode(); c.jjtSetParent(n); n.jjtAddChild(c, a); } n.jjtClose(); pushNode(n); node_created = true; } else { mk = ((Int32) marks.Pop()); node_created = false; } } } } --- NEW FILE: ParserTokenManager.cs --- namespace NVelocity.Runtime.Parser { using System; using System.Collections; using System.IO; using System.Text; /* Generated By:JJTree&JavaCC: Do not edit this line. ParserTokenManager.java */ public class ParserTokenManager : ParserConstants { private int fileDepth = 0; private int lparen = 0; private int rparen = 0; private Stack stateStack = new Stack(); public bool debugPrint = false; [...4273 lines suppressed...] matchedToken.image = "."; if (debugPrint) Console.Out.Write("DOT : switching to " + REFMODIFIER); SwitchTo(ParserConstants.REFMODIFIER); break; case 59: if (image == null) image = new StringBuilder(new String(input_stream.GetSuffix(jjimageLen + (lengthOfMatch = jjmatchedPos + 1)))); else image.Append(input_stream.GetSuffix(jjimageLen + (lengthOfMatch = jjmatchedPos + 1))); stateStackPop(); break; default: break; } } } } --- NEW FILE: Parser.cs --- /* Generated By:JJTree&JavaCC: Do not edit this line. Parser.java */ namespace NVelocity.Runtime.Parser { using System; using System.Collections; using System.IO; using NVelocity.Runtime.Directive; using NVelocity.Runtime.Parser.Node; using NVelocity.Util; /// <summary> This class is responsible for parsing a Velocity /// template. This class was generated by JavaCC using /// the JJTree extension to produce an Abstract /// Syntax Tree (AST) of the template. /// /// Please look at the Parser.jjt file which is /// what controls the generation of this class. /// </summary> [...5189 lines suppressed...] { p = p.next = new JJCalls(); break; } p = p.next; } p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla; } private sealed class JJCalls { internal int gen; internal Token first; internal int arg; internal JJCalls next; } } } --- NEW FILE: TokenMgrError.cs --- /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 2.1 */ namespace NVelocity.Runtime.Parser { using System; using System.Text; public class TokenMgrError : ApplicationException { public override String Message { get { return base.Message; } } /* * Ordinals for various reasons why an Error of this type can be thrown. */ /// <summary> Lexical error occured. /// </summary> internal const int LEXICAL_ERROR = 0; /// <summary> An attempt wass made to create a second instance of a static token manager. /// </summary> internal const int STATIC_LEXER_ERROR = 1; /// <summary> Tried to change to an invalid lexical state. /// </summary> internal const int INVALID_LEXICAL_STATE = 2; /// <summary> Detected (and bailed out of) an infinite loop in the token manager. /// </summary> internal const int LOOP_DETECTED = 3; /// <summary> Indicates the reason why the exception is thrown. It will have /// one of the above 4 values. /// </summary> internal int errorCode; /// <summary> Replaces unprintable characters by their espaced (or unicode escaped) /// equivalents in the given string /// </summary> protected internal static String addEscapes(String str) { StringBuilder retval = new StringBuilder(); char ch; for (int i = 0; i < str.Length; i++) { switch (str[i]) { case (char) (0): continue; case '\b': retval.Append("\\b"); continue; case '\t': retval.Append("\\t"); continue; case '\n': retval.Append("\\n"); continue; case '\f': retval.Append("\\f"); continue; case '\r': retval.Append("\\r"); continue; case '\"': retval.Append("\\\""); continue; case '\'': retval.Append("\\\'"); continue; case '\\': retval.Append("\\\\"); continue; default: if ((ch = str[i]) < 0x20 || ch > 0x7e) { String s = "0000" + Convert.ToString(ch, 16); retval.Append("\\u" + s.Substring(s.Length - 4, (s.Length) - (s.Length - 4))); } else { retval.Append(ch); } continue; } } return retval.ToString(); } /// <summary> Returns a detailed message for the Error when it is thrown by the /// token manager to indicate a lexical error. /// Parameters : /// EOFSeen : indicates if EOF caused the lexicl error /// curLexState : lexical state in which this error occured /// errorLine : line number when the error occured /// errorColumn : column number when the error occured /// errorAfter : prefix that was seen before this error occured /// curchar : the offending character /// Note: You can customize the lexical error message by modifying this method. /// </summary> private static String LexicalError(bool EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) { return ("Lexical error at line " + errorLine + ", column " + errorColumn + ". Encountered: " + (EOFSeen ? "<EOF> " : ("\"" + addEscapes(curChar.ToString()) + "\"") + " (" + (int) curChar + "), ") + "after : \"" + addEscapes(errorAfter) + "\""); } /// <summary> You can also modify the body of this method to customize your error messages. /// For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not /// of end-users concern, so you can return something like : /// * /// "Internal Error : Please file a bug report .... " /// * /// from this method for such cases in the release version of your parser. /// </summary> /* * Constructors of various flavors follow. */ public TokenMgrError() { } public TokenMgrError(String message, int reason) : base(message) { errorCode = reason; } public TokenMgrError(bool EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) : this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason) { } } } --- NEW FILE: ParserTreeConstants.cs --- namespace NVelocity.Runtime.Parser { using System; /* Generated By:JJTree: Do not edit this line. ParserTreeConstants.java */ public class ParserTreeConstants { /// <summary> /// private constructor as class is meant to hold constants only. /// Class was orginally an interface in Java, but as C# does not support Fields in an interface and /// the jjtNodeName field, I converted it to a class with no constructor. /// </summary> private ParserTreeConstants() { } public const int JJTPROCESS = 0; public const int JJTVOID = 1; public const int JJTESCAPEDDIRECTIVE = 2; public const int JJTESCAPE = 3; public const int JJTCOMMENT = 4; public const int JJTNUMBERLITERAL = 5; public const int JJTSTRINGLITERAL = 6; public const int JJTIDENTIFIER = 7; public const int JJTWORD = 8; public const int JJTDIRECTIVE = 9; public const int JJTBLOCK = 10; public const int JJTOBJECTARRAY = 11; public const int JJTINTEGERRANGE = 12; public const int JJTMETHOD = 13; public const int JJTREFERENCE = 14; public const int JJTTRUE = 15; public const int JJTFALSE = 16; public const int JJTTEXT = 17; public const int JJTIFSTATEMENT = 18; public const int JJTELSESTATEMENT = 19; public const int JJTELSEIFSTATEMENT = 20; public const int JJTSETDIRECTIVE = 21; public const int JJTEXPRESSION = 22; public const int JJTASSIGNMENT = 23; public const int JJTORNODE = 24; public const int JJTANDNODE = 25; public const int JJTEQNODE = 26; public const int JJTNENODE = 27; public const int JJTLTNODE = 28; public const int JJTGTNODE = 29; public const int JJTLENODE = 30; public const int JJTGENODE = 31; public const int JJTADDNODE = 32; public const int JJTSUBTRACTNODE = 33; public const int JJTMULNODE = 34; public const int JJTDIVNODE = 35; public const int JJTMODNODE = 36; public const int JJTNOTNODE = 37; public static readonly String[] jjtNodeName = new String[] {"process", "void", "EscapedDirective", "Escape", "Comment", "NumberLiteral", "StringLiteral", "Identifier", "Word", "Directive", "Block", "ObjectArray", "IntegerRange", "Method", "Reference", "True", "False", "Text", "IfStatement", "ElseStatement", "ElseIfStatement", "SetDirective", "Expression", "Assignment", "OrNode", "AndNode", "EQNode", "NENode", "LTNode", "GTNode", "LENode", "GENode", "AddNode", "SubtractNode", "MulNode", "DivNode", "ModNode", "NotNode"}; } } --- NEW FILE: CharStream.cs --- /* Generated By:JavaCC: Do not edit this line. CharStream.java Version 2.1 */ namespace NVelocity.Runtime.Parser { using System; /// <summary> This interface describes a character stream that maintains line and /// column number positions of the characters. It also has the capability /// to backup the stream to some extent. An implementation of this /// interface is used in the TokenManager implementation generated by /// JavaCCParser. /// /// All the methods except backup can be implemented in any fashion. backup /// needs to be implemented correctly for the correct operation of the lexer. /// Rest of the methods are all used to get information like line number, /// column number and the String that constitutes a token and are not used /// by the lexer. Hence their implementation won't affect the generated lexer's /// operation. /// </summary> public interface CharStream { int Column { get; } int Line { get; } int EndColumn { get; } int EndLine { get; } int BeginColumn { get; } int BeginLine { get; } /// <summary> Returns the next character from the selected input. The method /// of selecting the input is the responsibility of the class /// implementing this interface. Can throw any java.io.IOException. /// </summary> char readChar(); /// <summary> Returns the column position of the character last read. /// </summary> /// <deprecated> /// </deprecated> /// <seealso cref=" #getEndColumn /// /// "/> /// <summary> Returns the line number of the character last read. /// </summary> /// <deprecated> /// </deprecated> /// <seealso cref=" #getEndLine /// /// "/> /// <summary> Returns the column number of the last character for current token (being /// matched after the last call to BeginTOken). /// </summary> /// <summary> Returns the line number of the last character for current token (being /// matched after the last call to BeginTOken). /// </summary> /// <summary> Returns the column number of the first character for current token (being /// matched after the last call to BeginTOken). /// </summary> /// <summary> Returns the line number of the first character for current token (being /// matched after the last call to BeginTOken). /// </summary> /// <summary> Backs up the input stream by amount steps. Lexer calls this method if it /// had already read some characters, but could not use them to match a /// (longer) token. So, they will be used again as the prefix of the next /// token and it is the implemetation's responsibility to do this right. /// </summary> void backup(int amount); /// <summary> Returns the next character that marks the beginning of the next token. /// All characters must remain in the buffer between two successive calls /// to this method to implement backup correctly. /// </summary> char BeginToken(); /// <summary> Returns a string made up of characters from the marked token beginning /// to the current buffer position. Implementations have the choice of returning /// anything that they want to. For example, for efficiency, one might decide /// to just return null, which is a valid implementation. /// </summary> String GetImage(); /// <summary> Returns an array of characters that make up the suffix of length 'len' for /// the currently matched token. This is used to build up the matched string /// for use in actions in the case of MORE. A simple and inefficient /// implementation of this is as follows : /// /// { /// String t = GetImage(); /// return t.substring(t.length() - len, t.length()).toCharArray(); /// } /// </summary> char[] GetSuffix(int len); /// <summary> The lexer calls this function to indicate that it is done with the stream /// and hence implementations can free any resources held by this class. /// Again, the body of this function can be just empty and it will not /// affect the lexer's operation. /// </summary> void Done(); } } --- NEW FILE: VelocityCharStream.cs --- namespace NVelocity.Runtime.Parser { using System; using System.IO; /// <summary> NOTE : This class was originally an ASCII_CharStream autogenerated /// by Javacc. It was then modified via changing class name with appropriate /// fixes for CTORS, and mods to readChar(). /// /// This is safe because we *always* use Reader with this class, and never a /// InputStream. This guarantees that we have a correct stream of 16-bit /// chars - all encoding transformations have been done elsewhere, so we /// believe that there is no risk in doing this. Time will tell :) /// </summary> /// <summary> An implementation of interface CharStream, where the stream is assumed to /// contain only ASCII characters (without unicode processing). /// </summary> public sealed class VelocityCharStream : CharStream { public int Column { get { return bufcolumn[bufpos]; } } public int Line { get { return bufline[bufpos]; } } public int EndColumn { get { return bufcolumn[bufpos]; } } public int EndLine { get { return bufline[bufpos]; } } public int BeginColumn { get { return bufcolumn[tokenBegin]; } } public int BeginLine { get { return bufline[tokenBegin]; } } public const bool staticFlag = false; internal int bufsize; internal int available; internal int tokenBegin; public int bufpos = - 1; private int[] bufline; private int[] bufcolumn; private int column = 0; private int line = 1; private bool prevCharIsCR = false; private bool prevCharIsLF = false; private TextReader inputStream; private char[] buffer; private int maxNextCharInd = 0; private int inBuf = 0; private void ExpandBuff(bool wrapAround) { char[] newbuffer = new char[bufsize + 2048]; int[] newbufline = new int[bufsize + 2048]; int[] newbufcolumn = new int[bufsize + 2048]; //UPGRADE_NOTE: Exception 'java.lang.Throwable' was converted to ' ' which has different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1100"' try { if (wrapAround) { Array.Copy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); Array.Copy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos); buffer = newbuffer; Array.Copy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); Array.Copy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); bufline = newbufline; Array.Copy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); Array.Copy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); bufcolumn = newbufcolumn; maxNextCharInd = (bufpos += (bufsize - tokenBegin)); } else { Array.Copy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); buffer = newbuffer; Array.Copy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); bufline = newbufline; Array.Copy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); bufcolumn = newbufcolumn; maxNextCharInd = (bufpos -= tokenBegin); } } catch (Exception t) { throw new ApplicationException(t.Message); } bufsize += 2048; available = bufsize; tokenBegin = 0; } private void FillBuff() { if (maxNextCharInd == available) { if (available == bufsize) { if (tokenBegin > 2048) { bufpos = maxNextCharInd = 0; available = tokenBegin; } else if (tokenBegin < 0) bufpos = maxNextCharInd = 0; else ExpandBuff(false); } else if (available > tokenBegin) available = bufsize; else if ((tokenBegin - available) < 2048) ExpandBuff(true); else available = tokenBegin; } int i; try { // NOTE: java streams return -1 when done // NOTE: java throws java.io.IOException when anything goes wrong - .Net will throw // a System.ObjectDisposedException when the reader is closed try { i = inputStream.Read(buffer, maxNextCharInd, available - maxNextCharInd); } catch (Exception ex) { throw new IOException("exception reading from inputStream", ex); } if (i <= 0) { inputStream.Close(); throw new IOException(); } else maxNextCharInd += i; return; } catch (IOException e) { --bufpos; backup(0); if (tokenBegin == - 1) tokenBegin = bufpos; throw e; } } public char BeginToken() { tokenBegin = - 1; char c = readChar(); tokenBegin = bufpos; return c; } private void UpdateLineColumn(char c) { column++; if (prevCharIsLF) { prevCharIsLF = false; line += (column = 1); } else if (prevCharIsCR) { prevCharIsCR = false; if (c == '\n') { prevCharIsLF = true; } else line += (column = 1); } switch (c) { case '\r': prevCharIsCR = true; break; case '\n': prevCharIsLF = true; break; case '\t': column--; column += (8 - (column & 7)); break; default: break; } bufline[bufpos] = line; bufcolumn[bufpos] = column; } public char readChar() { if (inBuf > 0) { --inBuf; /* * was : return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]); */ return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]; } if (++bufpos >= maxNextCharInd) FillBuff(); /* * was : char c = (char)((char)0xff & buffer[bufpos]); */ char c = buffer[bufpos]; UpdateLineColumn(c); return (c); } /// <deprecated> /// </deprecated> /// <seealso cref=" #getEndColumn /// /// "/> /// <deprecated> /// </deprecated> /// <seealso cref=" #getEndLine /// /// "/> public void backup(int amount) { inBuf += amount; if ((bufpos -= amount) < 0) bufpos += bufsize; } public VelocityCharStream(TextReader dstream, int startline, int startcolumn, int buffersize) { inputStream = dstream; line = startline; column = startcolumn - 1; available = bufsize = buffersize; buffer = new char[buffersize]; bufline = new int[buffersize]; bufcolumn = new int[buffersize]; } public VelocityCharStream(TextReader dstream, int startline, int startcolumn) : this(dstream, startline, startcolumn, 4096) { } public void ReInit(TextReader dstream, int startline, int startcolumn, int buffersize) { inputStream = dstream; line = startline; column = startcolumn - 1; if (buffer == null || buffersize != buffer.Length) { available = bufsize = buffersize; buffer = new char[buffersize]; bufline = new int[buffersize]; bufcolumn = new int[buffersize]; } prevCharIsLF = prevCharIsCR = false; tokenBegin = inBuf = maxNextCharInd = 0; bufpos = - 1; } public void ReInit(TextReader dstream, int startline, int startcolumn) { ReInit(dstream, startline, startcolumn, 4096); } // TODO - I am still not sure what place regular Streams will play - so I will just stick with Text* streams // public VelocityCharStream(System.IO.Stream dstream, int startline, int startcolumn, int buffersize):this(new System.IO.TextReader(dstream), startline, startcolumn, 4096) { // } // // public VelocityCharStream(System.IO.Stream dstream, int startline, int startcolumn):this(dstream, startline, startcolumn, 4096) { // } // // public void ReInit(System.IO.Stream dstream, int startline, int startcolumn, int buffersize) { // ReInit(new System.IO.TextReader(dstream), startline, startcolumn, 4096); // } // public void ReInit(System.IO.Stream dstream, int startline, int startcolumn) { // ReInit(dstream, startline, startcolumn, 4096); // } public String GetImage() { if (bufpos >= tokenBegin) { Int32 len = (bufpos - tokenBegin + 1) > buffer.Length ? buffer.Length : (bufpos - tokenBegin + 1); //return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); return new String(buffer, tokenBegin, len); } else { return new String(buffer, tokenBegin, bufsize - tokenBegin) + new String(buffer, 0, bufpos + 1); } } public char[] GetSuffix(int len) { char[] ret = new char[len]; if ((bufpos + 1) >= len) Array.Copy(buffer, bufpos - len + 1, ret, 0, len); else { Array.Copy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1); Array.Copy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); } return ret; } public void Done() { buffer = null; bufline = null; bufcolumn = null; } /// <summary> Method to adjust line and column numbers for the start of a token.<BR> /// </summary> public void adjustBeginLineColumn(int newLine, int newCol) { int start = tokenBegin; int len; if (bufpos >= tokenBegin) { len = bufpos - tokenBegin + inBuf + 1; } else { len = bufsize - tokenBegin + bufpos + 1 + inBuf; } int i = 0, j = 0, k = 0; int nextColDiff = 0, columnDiff = 0; while (i < len && bufline[j = start%bufsize] == bufline[k = ++start%bufsize]) { bufline[j] = newLine; nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; bufcolumn[j] = newCol + columnDiff; columnDiff = nextColDiff; i++; } if (i < len) { bufline[j] = newLine++; bufcolumn[j] = newCol + columnDiff; while (i++ < len) { if (bufline[j = start%bufsize] != bufline[++start%bufsize]) bufline[j] = newLine++; else bufline[j] = newLine; } } line = bufline[j]; column = bufcolumn[j]; } } } --- NEW FILE: Token.cs --- /* Generated By:JavaCC: Do not edit this line. Token.java Version 2.1 */ namespace NVelocity.Runtime.Parser { using System; /// <summary> Describes the input token stream. /// </summary> public class Token { /// <summary> An integer that describes the kind of this token. This numbering /// system is determined by JavaCCParser, and a table of these numbers is /// stored in the file ...Constants.java. /// </summary> public int kind; /// <summary> beginLine and beginColumn describe the position of the first character /// of this token; endLine and endColumn describe the position of the /// last character of this token. /// </summary> public int beginLine, beginColumn, endLine, endColumn; /// <summary> The string image of the token. /// </summary> public String image; /// <summary> A reference to the next regular (non-special) token from the input /// stream. If this is the last token from the input stream, or if the /// token manager has not read tokens beyond this one, this field is /// set to null. This is true only if this token is also a regular /// token. Otherwise, see below for a description of the contents of /// this field. /// </summary> public Token next; /// <summary> This field is used to access special tokens that occur prior to this /// token, but after the immediately preceding regular (non-special) token. /// If there are no such special tokens, this field is set to null. /// When there are more than one such special token, this field refers /// to the last of these special tokens, which in turn refers to the next /// previous special token through its specialToken field, and so on /// until the first special token (whose specialToken field is null). /// The next fields of special tokens refer to other special tokens that /// immediately follow it (without an intervening regular token). If there /// is no such token, this field is null. /// </summary> public Token specialToken; /// <summary> Returns the image. /// </summary> public override String ToString() { return image; } /// <summary> Returns a new Token object, by default. However, if you want, you /// can create and return subclass objects based on the value of ofKind. /// Simply add the cases to the switch for all those special cases. /// For example, if you have a subclass of Token called IDToken that /// you want to create if ofKind is ID, simlpy add something like : /// * /// case MyParserConstants.ID : return new IDToken(); /// * /// to the following switch statement. Then you can cast matchedToken /// variable to the appropriate type and use it in your lexical actions. /// </summary> public static Token newToken(int ofKind) { switch (ofKind) { default: return new Token(); } } } } |