예제 #1
0
        public virtual void Match(string s)
        {
            int i = 0;

            while (i < s.Length)
            {
                if (input.LA(1) != s[i])
                {
                    if (state.backtracking > 0)
                    {
                        state.failed = true;
                        return;
                    }
                    MismatchedTokenException mte =
                        new MismatchedTokenException(s[i], input)
                    {
                        tokenNames = GetTokenNames()
                    };
                    Recover(mte);
                    throw mte;
                }
                i++;
                input.Consume();
                state.failed = false;
            }
        }
예제 #2
0
        public void Consume()
        {
            if (_la1 != '\\')
            {
                _source.Consume();
                _la1        = _source.La(1);
                _range      = Math.Max(_range, _source.Index);
                _slashCount = 0;
                return;
            }

            // make sure the next character has been processed
            this.La(1);

            if (_escapeListIndex >= _escapeIndexes.Count || _escapeIndexes[_escapeListIndex] != Index)
            {
                _source.Consume();
                _slashCount++;
            }
            else
            {
                int indirectionLevel = _escapeIndirectionLevels[_escapeListIndex];
                for (int i = 0; i < 6 + indirectionLevel; i++)
                {
                    _source.Consume();
                }

                _escapeListIndex++;
                _slashCount = 0;
            }

            _la1 = _source.La(1);
            Debug.Assert(_range >= Index);
        }
예제 #3
0
 /// <summary>
 /// Lexers can normally match any char in it's vocabulary after matching
 /// a token, so do the easy thing and just kill a character and hope
 /// it all works out.
 /// </summary>
 /// <remarks>
 /// Lexers can normally match any char in it's vocabulary after matching
 /// a token, so do the easy thing and just kill a character and hope
 /// it all works out.  You can instead use the rule invocation stack
 /// to do sophisticated error recovery if you are in a fragment rule.
 /// </remarks>
 public virtual void Recover(RecognitionException re)
 {
     //System.out.println("consuming char "+(char)input.LA(1)+" during recovery");
     //re.printStackTrace();
     // TODO: Do we lose character or line position information?
     _input.Consume();
 }
예제 #4
0
        public static bool ConsumeNewLine(this ICharStream stream)
        {
            var c = stream.Next;

            if (c == '\r')
            {
                stream.Consume();
                c = stream.Next;
            }

            if (c == '\n')
            {
                stream.Consume();
                return(true);
            }

            return(false);
        }
예제 #5
0
        public virtual void Consume(ICharStream input)
        {
            int curChar = input.La(1);

            if (curChar == '\n')
            {
                _line++;
                charPositionInLine = 0;
            }
            else
            {
                charPositionInLine++;
            }
            input.Consume();
        }
예제 #6
0
        public void Consume(ICharStream input)
        {
            int curChar = input.LA(1);

            if (curChar == '\n')
            {
                thisLine++;
                charPositionInLine = 0;
            }
            else
            {
                charPositionInLine++;
            }
            input.Consume();
        }
예제 #7
0
        public static bool ConsumeSlComment(this ICharStream stream, IPairFactory pairFactory, Pair parent)
        {
            if (stream.Next != '\'')
            {
                return(false);
            }
            if (stream.La(2) != '\'')
            {
                return(false);
            }
            if (stream.La(3) != '\'')
            {
                return(false);
            }

            stream.Consume();
            var begin = new CharLocation(stream);

            stream.Consume();
            stream.Consume();
            var c = stream.Next;

            while (!c.IsNewLineCharacter() && c != -1)
            {
                stream.Consume();
                c = stream.Next;
            }

            var comment = pairFactory.ProcessComment((ITextSource)stream, 1, new Interval(begin, new CharLocation(stream)));

            if (comment != null)
            {
                pairFactory.AppendChild(parent, comment);
            }
            return(true);
        }
예제 #8
0
        public IToken NextToken()
        {
            IToken token = null;

            do
            {
                int position = _input.Index;
                token = NextTokenCore();
                // ensure progress
                if (position == _input.Index)
                {
                    _input.Consume();
                }
            } while (token == null || token.Type == ChapelCodeClassifierLexer.NEWLINE);

            return(token);
        }
예제 #9
0
        public static bool ConsumeSpaces(this ICharStream stream)
        {
            var c = stream.Next;

            if (!c.IsSpaceCharacter())
            {
                return(false);
            }

            while (true)
            {
                stream.Consume();
                c = stream.Next;
                if (!c.IsSpaceCharacter())
                {
                    return(true);
                }
            }
        }
예제 #10
0
        public static bool ConsumeMlComment(this ICharStream stream, IPairFactory pairFactory, Pair parent)
        {
            if (stream.Next != '\"')
            {
                return(false);
            }
            if (stream.La(2) != '\"')
            {
                return(false);
            }
            if (stream.La(3) != '\"')
            {
                return(false);
            }

            stream.Consume();
            var begin = new CharLocation(stream);

            stream.Consume();
            stream.Consume();
            while (!(stream.Next == '\"' && stream.La(2) == '\"' && stream.La(3) == '\"') && stream.Next != -1)
            {
                stream.Consume();
            }

            if (stream.Next == '\"')
            {
                stream.Consume();
                stream.Consume();
                stream.Consume();
            }
            var comment = pairFactory.ProcessComment((ITextSource)stream, 2, new Interval(begin, new CharLocation(stream)));

            if (comment != null)
            {
                pairFactory.AppendChild(parent, comment);
            }
            return(true);
        }
예제 #11
0
 public static void consume(this ICharStream stream)
 {
     stream.Consume();
 }
예제 #12
0
 public void Consume() => internalStream.Consume();
 public void Consume()
 {
     stream.Consume();
 }
예제 #14
0
        public static void Check_CobolCharStream()
        {
            // Test file properties
            string         relativePath   = @"Compiler\Parser\Samples";
            string         textName       = "MSVCOUT";
            DocumentFormat documentFormat = DocumentFormat.RDZReferenceFormat;

            // Compile test file
            CompilationDocument compilationDocument = ParserUtils.ScanCobolFile(relativePath, textName, documentFormat);

            // Create a token iterator on top of tokens lines
            TokensLinesIterator tokensIterator = new TokensLinesIterator(
                compilationDocument.TokensDocumentSnapshot.TextSourceInfo.Name,
                compilationDocument.TokensDocumentSnapshot.Lines,
                null,
                Token.CHANNEL_SourceTokens);

            // Crate an Antlr compatible token source on top a the token iterator
            TokensLinesTokenSource tokenSource = new TokensLinesTokenSource(
                compilationDocument.TokensDocumentSnapshot.TextSourceInfo.Name,
                tokensIterator);

            tokenSource.NextToken();

            // Get underlying CharStream
            ICharStream charStream = tokenSource.InputStream;

            if (charStream.Index != 0)
            {
                throw new Exception("Char stream index should start at 0");
            }
            if (charStream.La(0) != 0)
            {
                throw new Exception("La(0) should be 0");
            }
            if (charStream.La(1) != '0')
            {
                throw new Exception("La(1) should be 0");
            }
            if (charStream.La(4) != '1')
            {
                throw new Exception("La(4) should be 1");
            }
            if (charStream.La(5) != '6')
            {
                throw new Exception("La(5) should be 6");
            }

            charStream.Consume();
            if (charStream.Index != 1)
            {
                throw new Exception("Char stream index should be 1 after consume");
            }
            if (charStream.La(4) != '6')
            {
                throw new Exception("La(4) should be 6 after consume");
            }
            if (charStream.La(80) != IntStreamConstants.Eof)
            {
                throw new Exception("La(80) should be Eof");
            }

            charStream.Seek(12);
            if (charStream.Index != 12)
            {
                throw new Exception("Char stream index should be 12 after seek");
            }
            if (charStream.La(-1) != ':')
            {
                throw new Exception("La(-1) should be : after seek");
            }
            if (charStream.La(1) != 'M')
            {
                throw new Exception("La(1) should be M after seek");
            }
            // should do nothing
            int marker = charStream.Mark();

            charStream.Release(marker);
            if (charStream.La(2) != 'S')
            {
                throw new Exception("La(2) should be S after release");
            }

            string text = charStream.GetText(new Interval(11, 18));

            if (text != ":MSVCOUT")
            {
                throw new Exception("Char stream GetText method KO");
            }

            if (charStream.Size != 80)
            {
                throw new Exception("Char stream size KO");
            }
        }
예제 #15
0
 protected virtual void consume()
 {
     input.Consume();
     c = (char)input.LA(1);
 }
예제 #16
0
 private bool ConsumeEol()
 {
     if (_input.Next == '\r')
     {
         _input.Consume();
     }
     if (_input.Next != '\n')
     {
         return(false);
     }
     _input.Consume();
     return(true);
 }
예제 #17
0
 public virtual void Consume(ICharStream input)
 {
     int curChar = input.La(1);
     if (curChar == '\n')
     {
         line++;
         charPositionInLine = 0;
     }
     else
     {
         charPositionInLine++;
     }
     input.Consume();
 }