1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2005-2011 Terence Parr
4 * All rights reserved.
5 *
6 * Conversion to C#:
7 * Copyright (c) 2008-2011 Sam Harwell, Pixel Mine, Inc.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33namespace Antlr.Runtime
34{
35    using ConditionalAttribute = System.Diagnostics.ConditionalAttribute;
36
37    /** <summary>
38     *  A parser for TokenStreams.  "parser grammars" result in a subclass
39     *  of this.
40     *  </summary>
41     */
42    public class Parser : BaseRecognizer
43    {
44        public ITokenStream input;
45
46        public Parser( ITokenStream input )
47            : base()
48        {
49            //super(); // highlight that we go to super to set state object
50            TokenStream = input;
51        }
52
53        public Parser( ITokenStream input, RecognizerSharedState state )
54            : base(state) // share the state object with another parser
55        {
56            this.input = input;
57        }
58
59        public override void Reset()
60        {
61            base.Reset(); // reset all recognizer state variables
62            if ( input != null )
63            {
64                input.Seek( 0 ); // rewind the input
65            }
66        }
67
68        protected override object GetCurrentInputSymbol( IIntStream input )
69        {
70            return ( (ITokenStream)input ).LT( 1 );
71        }
72
73        protected override object GetMissingSymbol( IIntStream input,
74                                          RecognitionException e,
75                                          int expectedTokenType,
76                                          BitSet follow )
77        {
78            string tokenText = null;
79            if ( expectedTokenType == TokenTypes.EndOfFile )
80                tokenText = "<missing EOF>";
81            else
82                tokenText = "<missing " + TokenNames[expectedTokenType] + ">";
83            CommonToken t = new CommonToken( expectedTokenType, tokenText );
84            IToken current = ( (ITokenStream)input ).LT( 1 );
85            if ( current.Type == TokenTypes.EndOfFile )
86            {
87                current = ( (ITokenStream)input ).LT( -1 );
88            }
89            t.Line = current.Line;
90            t.CharPositionInLine = current.CharPositionInLine;
91            t.Channel = DefaultTokenChannel;
92            t.InputStream = current.InputStream;
93            return t;
94        }
95
96        /** <summary>Gets or sets the token stream; resets the parser upon a set.</summary> */
97        public virtual ITokenStream TokenStream
98        {
99            get
100            {
101                return input;
102            }
103            set
104            {
105                input = null;
106                Reset();
107                input = value;
108            }
109        }
110
111        public override string SourceName
112        {
113            get
114            {
115                return input.SourceName;
116            }
117        }
118
119        [Conditional("ANTLR_TRACE")]
120        public virtual void TraceIn( string ruleName, int ruleIndex )
121        {
122            base.TraceIn( ruleName, ruleIndex, input.LT( 1 ) );
123        }
124
125        [Conditional("ANTLR_TRACE")]
126        public virtual void TraceOut( string ruleName, int ruleIndex )
127        {
128            base.TraceOut( ruleName, ruleIndex, input.LT( 1 ) );
129        }
130    }
131}
132