00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 namespace Antlr.Runtime.Tree
00037 {
00038 using System;
00039 using Antlr.Runtime;
00040
00046 public class TreeParser : BaseRecognizer
00047 {
00048 public const int DOWN = Token.DOWN;
00049 public const int UP = Token.UP;
00050
00051 public TreeParser(ITreeNodeStream input)
00052 : base()
00053 {
00054 TreeNodeStream = input;
00055 }
00056
00057 public TreeParser(ITreeNodeStream input, RecognizerSharedState state)
00058 : base(state)
00059 {
00060 TreeNodeStream = input;
00061 }
00062
00064 virtual public ITreeNodeStream TreeNodeStream
00065 {
00066 get { return input; }
00067 set { this.input = value; }
00068 }
00069
00070 override public string SourceName {
00071 get { return input.SourceName; }
00072 }
00073
00074 protected override object GetCurrentInputSymbol(IIntStream input) {
00075 return ((ITreeNodeStream)input).LT(1);
00076 }
00077
00078 protected override object GetMissingSymbol(IIntStream input,
00079 RecognitionException e,
00080 int expectedTokenType,
00081 BitSet follow) {
00082 string tokenText = "<missing " + TokenNames[expectedTokenType] + ">";
00083 return new CommonTree(new CommonToken(expectedTokenType, tokenText));
00084 }
00085
00087 override public void Reset()
00088 {
00089 base.Reset();
00090 if ( input != null )
00091 {
00092 input.Seek(0);
00093 }
00094 }
00095
00104 override public void MatchAny(IIntStream ignore )
00105 {
00106 state.errorRecovery = false;
00107 state.failed = false;
00108 object look = input.LT(1);
00109 if ( input.TreeAdaptor.GetChildCount(look) == 0 )
00110 {
00111 input.Consume();
00112 return;
00113 }
00114
00115
00116 int level = 0;
00117 int tokenType = input.TreeAdaptor.GetNodeType(look);
00118 while ( (tokenType != Token.EOF) && !( (tokenType == UP) && (level == 0) ) )
00119 {
00120 input.Consume();
00121 look = input.LT(1);
00122 tokenType = input.TreeAdaptor.GetNodeType(look);
00123 if ( tokenType == DOWN )
00124 {
00125 level++;
00126 }
00127 else if ( tokenType == UP )
00128 {
00129 level--;
00130 }
00131 }
00132 input.Consume();
00133 }
00134
00135 override public IIntStream Input
00136 {
00137 get { return input; }
00138 }
00139
00140 protected internal ITreeNodeStream input;
00141
00146 protected internal override void Mismatch(IIntStream input, int ttype, BitSet follow) {
00147 throw new MismatchedTreeNodeException(ttype, (ITreeNodeStream)input);
00148 }
00149
00155 override public string GetErrorHeader(RecognitionException e)
00156 {
00157 return GrammarFileName + ": node from "
00158 + (e.approximateLineInfo ? "after " : "")
00159 + "line " + e.Line + ":" + e.CharPositionInLine;
00160 }
00161
00167 override public string GetErrorMessage(RecognitionException e, string[] tokenNames)
00168 {
00169 if ( this is TreeParser )
00170 {
00171 ITreeAdaptor adaptor = ((ITreeNodeStream)e.Input).TreeAdaptor;
00172 e.Token = adaptor.GetToken(e.Node);
00173 if ( e.Token == null )
00174 {
00175 e.Token = new CommonToken(adaptor.GetNodeType(e.Node), adaptor.GetNodeText(e.Node));
00176 }
00177 }
00178 return base.GetErrorMessage(e, tokenNames);
00179 }
00180
00181 public virtual void TraceIn(string ruleName, int ruleIndex)
00182 {
00183 base.TraceIn(ruleName, ruleIndex, input.LT(1));
00184 }
00185
00186 public virtual void TraceOut(string ruleName, int ruleIndex)
00187 {
00188 base.TraceOut(ruleName, ruleIndex, input.LT(1));
00189 }
00190
00191 }
00192 }