Exemple #1
0
        public override void DisplayRecognitionError(string[] tokenNames, RecognitionException e)
        {
            string header  = GetErrorHeader(e);
            string message = GetErrorMessage(e, tokenNames);
            Span   span    = new Span();

            object positionNode = null;
            IPositionTrackingStream positionTrackingStream = input as IPositionTrackingStream;

            if (positionTrackingStream != null)
            {
                positionNode = positionTrackingStream.GetKnownPositionElement(false);
                if (positionNode == null)
                {
                    positionNode = positionTrackingStream.GetKnownPositionElement(true);
                }
            }

            if (positionNode != null)
            {
                IToken token = input.TreeAdaptor.GetToken(positionNode);
                if (token != null)
                {
                    span = Span.FromBounds(token.StartIndex, token.StopIndex + 1);
                }
            }
            else if (e.Token != null)
            {
                span = Span.FromBounds(e.Token.StartIndex, e.Token.StopIndex + 1);
            }

            ParseErrorEventArgs args = new ParseErrorEventArgs(message, span);

            OnParseError(args);

            base.DisplayRecognitionError(tokenNames, e);
        }
        protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input)
        {
            this._node = input.LT(1);

            object positionNode = null;
            IPositionTrackingStream positionTrackingStream = input as IPositionTrackingStream;

            if (positionTrackingStream != null)
            {
                positionNode = positionTrackingStream.GetKnownPositionElement(false);
                if (positionNode == null)
                {
                    positionNode = positionTrackingStream.GetKnownPositionElement(true);
                    this._approximateLineInfo = positionNode != null;
                }
            }

            ITokenStreamInformation streamInformation = input as ITokenStreamInformation;

            if (streamInformation != null)
            {
                IToken lastToken     = streamInformation.LastToken;
                IToken lastRealToken = streamInformation.LastRealToken;
                if (lastRealToken != null)
                {
                    this._token = lastRealToken;
                    this._line  = lastRealToken.Line;
                    this._charPositionInLine  = lastRealToken.CharPositionInLine;
                    this._approximateLineInfo = lastRealToken.Equals(lastToken);
                }
            }
            else
            {
                ITreeAdaptor adaptor = input.TreeAdaptor;
                IToken       payload = adaptor.GetToken(positionNode ?? _node);
                if (payload != null)
                {
                    this._token = payload;
                    if (payload.Line <= 0)
                    {
                        // imaginary node; no line/pos info; scan backwards
                        int    i         = -1;
                        object priorNode = input.LT(i);
                        while (priorNode != null)
                        {
                            IToken priorPayload = adaptor.GetToken(priorNode);
                            if (priorPayload != null && priorPayload.Line > 0)
                            {
                                // we found the most recent real line / pos info
                                this._line = priorPayload.Line;
                                this._charPositionInLine  = priorPayload.CharPositionInLine;
                                this._approximateLineInfo = true;
                                break;
                            }

                            --i;
                            try
                            {
                                priorNode = input.LT(i);
                            }
                            catch (NotSupportedException)
                            {
                                priorNode = null;
                            }
                        }
                    }
                    else
                    {
                        // node created from real token
                        this._line = payload.Line;
                        this._charPositionInLine = payload.CharPositionInLine;
                    }
                }
                else if (this._node is Tree.ITree)
                {
                    this._line = ((Tree.ITree) this._node).Line;
                    this._charPositionInLine = ((Tree.ITree) this._node).CharPositionInLine;
                    if (this._node is CommonTree)
                    {
                        this._token = ((CommonTree)this._node).Token;
                    }
                }
                else
                {
                    int    type = adaptor.GetType(this._node);
                    string text = adaptor.GetText(this._node);
                    this._token = new CommonToken(type, text);
                }
            }
        }