コード例 #1
0
 /**
  * Resets this tokenizer for usage with another input stream.
  * This method will clear all the internal state in the
  * tokenizer as well as close the previous input stream. It
  * is normally called in order to reuse a parser and
  * tokenizer pair with multiple input streams, thereby
  * avoiding the cost of re-analyzing the grammar structures.
  *
  * @param input          the new input stream to read
  *
  * @see Parser#reset(Reader)
  *
  * @since 1.5
  */
 public void Reset(TextReader input)
 {
     buffer.Dispose();
     buffer        = new ReaderBuffer(input);
     previousToken = null;
     lastMatch.Clear();
 }
コード例 #2
0
ファイル: Tokenizer.cs プロジェクト: nkchinh/grammatica
        /**
         * Finds the next token on the stream. This method will return
         * null when end of file has been reached. It will return a
         * parse exception if no token matched the input stream.
         *
         * @return the next token found, or
         *         null if end of file was encountered
         *
         * @throws ParseException if the input stream couldn't be read or
         *             parsed correctly
         */
        private Token NextToken()
        {
            string str;
            int    line;
            int    column;

            try {
                lastMatch.Clear();
                stringDfaMatcher.Match(buffer, lastMatch);
                nfaMatcher.Match(buffer, lastMatch);
                regExpMatcher.Match(buffer, lastMatch);
                if (lastMatch.Length > 0)
                {
                    line   = buffer.LineNumber;
                    column = buffer.ColumnNumber;
                    str    = buffer.Read(lastMatch.Length);
                    return(NewToken(lastMatch.Pattern, str, line, column));
                }
                else if (buffer.Peek(0) < 0)
                {
                    return(null);
                }
                else
                {
                    line   = buffer.LineNumber;
                    column = buffer.ColumnNumber;
                    throw new ParseException(
                              ParseException.ErrorType.UNEXPECTED_CHAR,
                              buffer.Read(1),
                              line,
                              column);
                }
            } catch (IOException e) {
                throw new ParseException(ParseException.ErrorType.IO,
                                         e.Message,
                                         -1,
                                         -1);
            }
        }