// $ANTLR 2.7.4: "csv.g" -> "CSVParser.cs"$

namespace net.ivoa.data
{
	// Generate the header common to all output files.
	using System;
	
	using TokenBuffer              = antlr.TokenBuffer;
	using TokenStreamException     = antlr.TokenStreamException;
	using TokenStreamIOException   = antlr.TokenStreamIOException;
	using ANTLRException           = antlr.ANTLRException;
	using LLkParser = antlr.LLkParser;
	using Token                    = antlr.Token;
	using TokenStream              = antlr.TokenStream;
	using RecognitionException     = antlr.RecognitionException;
	using NoViableAltException     = antlr.NoViableAltException;
	using MismatchedTokenException = antlr.MismatchedTokenException;
	using SemanticException        = antlr.SemanticException;
	using ParserSharedInputState   = antlr.ParserSharedInputState;
	using BitSet                   = antlr.collections.impl.BitSet;
	
	public 	class CSVParser : antlr.LLkParser
	{
		public const int EOF = 1;
		public const int NULL_TREE_LOOKAHEAD = 3;
		public const int NEWLINE = 4;
		public const int POUND = 5;
		public const int RECORD = 6;
		public const int COMMA = 7;
		public const int WS = 8;
		
		public CSVWrapper csvw;		
		protected void initialize()
		{
			tokenNames = tokenNames_;
		}
		
		
		protected CSVParser(TokenBuffer tokenBuf, int k) : base(tokenBuf, k)
		{
			initialize();
		}
		
		public CSVParser(TokenBuffer tokenBuf) : this(tokenBuf,2)
		{
		}
		
		protected CSVParser(TokenStream lexer, int k) : base(lexer,k)
		{
			initialize();
		}
		
		public CSVParser(TokenStream lexer) : this(lexer,2)
		{
		}
		
		public CSVParser(ParserSharedInputState state) : base(state,2)
		{
			initialize();
		}
		
	public void file() //throws RecognitionException, TokenStreamException
{
		
		csvw= new CSVWrapper();
		
		try {      // for error handling
			{
				line();
				{    // ( ... )*
					for (;;)
					{
						if ((LA(1)==NEWLINE) && (LA(2)==POUND||LA(2)==RECORD))
						{
							match(NEWLINE);
							line();
						}
						else
						{
							goto _loop4_breakloop;
						}
						
					}
_loop4_breakloop:					;
				}    // ( ... )*
				{
					switch ( LA(1) )
					{
					case NEWLINE:
					{
						match(NEWLINE);
						break;
					}
					case EOF:
					{
						break;
					}
					default:
					{
						throw new NoViableAltException(LT(1), getFilename());
					}
					 }
				}
				match(Token.EOF_TYPE);
			}
		}
		catch (RecognitionException ex)
		{
			reportError(ex);
			consume();
			consumeUntil(tokenSet_0_);
		}
	}
	
	public void line() //throws RecognitionException, TokenStreamException
{
		
		csvw.NewRow();
		
		try {      // for error handling
			{
				{ // ( ... )+
				int _cnt9=0;
				for (;;)
				{
					if ((LA(1)==POUND||LA(1)==RECORD))
					{
						record();
					}
					else
					{
						if (_cnt9 >= 1) { goto _loop9_breakloop; } else { throw new NoViableAltException(LT(1), getFilename());; }
					}
					
					_cnt9++;
				}
_loop9_breakloop:				;
				}    // ( ... )+
			}
		}
		catch (RecognitionException ex)
		{
			reportError(ex);
			consume();
			consumeUntil(tokenSet_1_);
		}
	}
	
	public void record() //throws RecognitionException, TokenStreamException
{
		
		Token  r = null;
		
		try {      // for error handling
			{
				{
					switch ( LA(1) )
					{
					case POUND:
					{
						match(POUND);
						break;
					}
					case RECORD:
					{
						break;
					}
					default:
					{
						throw new NoViableAltException(LT(1), getFilename());
					}
					 }
				}
				{
					r = LT(1);
					match(RECORD);
				}
				{
					switch ( LA(1) )
					{
					case COMMA:
					{
						match(COMMA);
						break;
					}
					case EOF:
					case NEWLINE:
					case POUND:
					case RECORD:
					{
						break;
					}
					default:
					{
						throw new NoViableAltException(LT(1), getFilename());
					}
					 }
				}
			}
			csvw.AddColumn(r.getText());
		}
		catch (RecognitionException ex)
		{
			reportError(ex);
			consume();
			consumeUntil(tokenSet_2_);
		}
	}
	
	private void initializeFactory()
	{
	}
	
	public static readonly string[] tokenNames_ = new string[] {
		@"""<0>""",
		@"""EOF""",
		@"""<2>""",
		@"""NULL_TREE_LOOKAHEAD""",
		@"""NEWLINE""",
		@"""POUND""",
		@"""RECORD""",
		@"""COMMA""",
		@"""WS"""
	};
	
	private static long[] mk_tokenSet_0_()
	{
		long[] data = { 2L, 0L};
		return data;
	}
	public static readonly BitSet tokenSet_0_ = new BitSet(mk_tokenSet_0_());
	private static long[] mk_tokenSet_1_()
	{
		long[] data = { 18L, 0L};
		return data;
	}
	public static readonly BitSet tokenSet_1_ = new BitSet(mk_tokenSet_1_());
	private static long[] mk_tokenSet_2_()
	{
		long[] data = { 114L, 0L};
		return data;
	}
	public static readonly BitSet tokenSet_2_ = new BitSet(mk_tokenSet_2_());
	
}
}
