1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2005-2008 Terence Parr
4 * All rights reserved.
5 *
6 * Conversion to C#:
7 * Copyright (c) 2008-2009 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    using ConditionalAttribute = System.Diagnostics.ConditionalAttribute;
35
36    /** <summary>
37     *  A parser for TokenStreams.  "parser grammars" result in a subclass
38     *  of this.
39     *  </summary>
40     */
41    public class Parser : BaseRecognizer {
42        public ITokenStream input;
43
44        public Parser(ITokenStream input)
45            : base() {
46            //super(); // highlight that we go to super to set state object
47            TokenStream = input;
48        }
49
50        public Parser(ITokenStream input, RecognizerSharedState state)
51            : base(state) // share the state object with another parser
52        {
53            this.input = input;
54        }
55
56        public override void Reset() {
57            base.Reset(); // reset all recognizer state variables
58            if (input != null) {
59                input.Seek(0); // rewind the input
60            }
61        }
62
63        protected override object GetCurrentInputSymbol(IIntStream input) {
64            return ((ITokenStream)input).LT(1);
65        }
66
67        protected override object GetMissingSymbol(IIntStream input,
68                                          RecognitionException e,
69                                          int expectedTokenType,
70                                          BitSet follow) {
71            string tokenText = null;
72            if (expectedTokenType == TokenTypes.EndOfFile)
73                tokenText = "<missing EOF>";
74            else
75                tokenText = "<missing " + TokenNames[expectedTokenType] + ">";
76            CommonToken t = new CommonToken(expectedTokenType, tokenText);
77            IToken current = ((ITokenStream)input).LT(1);
78            if (current.Type == TokenTypes.EndOfFile) {
79                current = ((ITokenStream)input).LT(-1);
80            }
81            t.Line = current.Line;
82            t.CharPositionInLine = current.CharPositionInLine;
83            t.Channel = DefaultTokenChannel;
84            return t;
85        }
86
87        /** <summary>Gets or sets the token stream; resets the parser upon a set.</summary> */
88        public virtual ITokenStream TokenStream {
89            get {
90                return input;
91            }
92            set {
93                input = null;
94                Reset();
95                input = value;
96            }
97        }
98
99        public override string SourceName {
100            get {
101                return input.SourceName;
102            }
103        }
104
105        [Conditional("ANTLR_TRACE")]
106        public virtual void TraceIn(string ruleName, int ruleIndex) {
107            base.TraceIn(ruleName, ruleIndex, input.LT(1));
108        }
109
110        [Conditional("ANTLR_TRACE")]
111        public virtual void TraceOut(string ruleName, int ruleIndex) {
112            base.TraceOut(ruleName, ruleIndex, input.LT(1));
113        }
114    }
115}
116