Peek2() 공개 메소드

public Peek2 ( ) : int
리턴 int
예제 #1
0
        public void SkipTokenTest()
        {
            Lexicon lexicon = new Lexicon();
            Lexer global = lexicon.Lexer;
            Lexer keywords = global.CreateSubLexer();
            Lexer xml = keywords.CreateSubLexer();

            var ID = global.DefineToken(RE.Range('a', 'z').Concat(
                (RE.Range('a', 'z') | RE.Range('0', '9')).Many()));
            var NUM = global.DefineToken(RE.Range('0', '9').Many1());
            var WHITESPACE = global.DefineToken(RE.Symbol(' ').Many());
            var ERROR = global.DefineToken(RE.Range(Char.MinValue, (char)255));

            var IF = keywords.DefineToken(RE.Literal("if"));
            var ELSE = keywords.DefineToken(RE.Literal("else"));

            var XMLNS = xml.DefineToken(RE.Literal("xmlns"));

            ScannerInfo info = lexicon.CreateScannerInfo();
            Scanner scanner = new Scanner(info);

            string source = "asdf04a 1107 else Z if vvv xmlns 772737";
            StringReader sr = new StringReader(source);

            scanner.SetSource(new SourceReader(sr));
            scanner.SetTriviaTokens(WHITESPACE.Index, ERROR.Index);
            info.CurrentLexerIndex = xml.Index;

            Lexeme l1 = scanner.Read();
            Assert.AreEqual(ID.Index, l1.TokenIndex);
            Assert.AreEqual("asdf04a", l1.Value.Content);
            Assert.AreEqual(0, l1.PrefixTrivia.Count);

            Lexeme l2 = scanner.Read();
            Assert.AreEqual(NUM.Index, l2.TokenIndex);
            Assert.AreEqual("1107", l2.Value.Content);
            Assert.AreEqual(1, l2.PrefixTrivia.Count);

            Lexeme l3 = scanner.Read();
            Assert.AreEqual(ELSE.Index, l3.TokenIndex);
            Assert.AreEqual("else", l3.Value.Content);
            Assert.AreEqual(1, l2.PrefixTrivia.Count);

            Lexeme l4 = scanner.Read();
            Assert.AreEqual(IF.Index, l4.TokenIndex);
            Assert.AreEqual("if", l4.Value.Content);
            Assert.AreEqual(3, l4.PrefixTrivia.Count);
            

            int p1 = scanner.Peek();
            Assert.AreEqual(ID.Index, p1);

            int p2 = scanner.Peek2();
            int p3 = scanner.Peek(3);
            int peof = scanner.Peek(4);
            Assert.AreEqual(info.EndOfStreamTokenIndex, peof);

            Lexeme l6 = scanner.Read();
            Lexeme l7 = scanner.Read();
            Assert.AreEqual(XMLNS.Index, l7.TokenIndex);

            Lexeme l8 = scanner.Read();
            Assert.AreEqual(NUM.Index, l8.TokenIndex);

            Lexeme leof = scanner.Read();
            Assert.AreEqual(info.EndOfStreamTokenIndex, leof.TokenIndex);
            Assert.AreEqual(leof.Value.Span.StartLocation.CharIndex, leof.Value.Span.EndLocation.CharIndex);
            Assert.AreEqual(source.Length, leof.Value.Span.StartLocation.CharIndex);
        }
예제 #2
0
        public void ScannerTest()
        {
            Lexicon lexicon = new Lexicon();
            Lexer global = lexicon.Lexer;
            Lexer keywords = global.CreateSubLexer();
            Lexer xml = keywords.CreateSubLexer();

            var ID = global.DefineToken(RE.Range('a', 'z').Concat(
                (RE.Range('a', 'z') | RE.Range('0', '9')).Many()));
            var NUM = global.DefineToken(RE.Range('0', '9').Many1());
            var WHITESPACE = global.DefineToken(RE.Symbol(' ').Many());
            var ERROR = global.DefineToken(RE.Range(Char.MinValue, (char)255));

            var IF = keywords.DefineToken(RE.Literal("if"));
            var ELSE = keywords.DefineToken(RE.Literal("else"));

            var XMLNS = xml.DefineToken(RE.Literal("xmlns"));

            ScannerInfo info = lexicon.CreateScannerInfo();
            Scanner scanner = new Scanner(info);

            string source = "asdf04a 1107 else Z if vvv xmlns 772737";
            StringReader sr = new StringReader(source);

            scanner.SetSource(new SourceReader(sr));

            Lexeme l1 = scanner.Read();
            Assert.AreEqual(ID.Index, l1.TokenIndex);
            Assert.AreEqual("asdf04a", l1.Value.Content);
            Assert.AreEqual(0, l1.Value.Span.StartLocation.Column);
            Assert.AreEqual(6, l1.Value.Span.EndLocation.Column);

            Lexeme l2 = scanner.Read();
            Assert.AreEqual(WHITESPACE.Index, l2.TokenIndex);
            Assert.AreEqual(" ", l2.Value.Content);

            Lexeme l3 = scanner.Read();
            Assert.AreEqual(NUM.Index, l3.TokenIndex);
            Assert.AreEqual("1107", l3.Value.Content);

            Lexeme l4 = scanner.Read();
            Assert.AreEqual(WHITESPACE.Index, l4.TokenIndex);

            Lexeme l5 = scanner.Read();
            Assert.AreEqual(ID.Index, l5.TokenIndex);

            int p1 = scanner.Peek();
            Assert.AreEqual(WHITESPACE.Index, p1);

            int p2 = scanner.Peek2();
            Assert.AreEqual(ERROR.Index, p2);

            int p3 = scanner.Peek(3);
            Assert.AreEqual(WHITESPACE.Index, p3);

            int p4 = scanner.Peek(4);
            Assert.AreEqual(ID.Index, p4);

            int p5 = scanner.Peek(5);
            Assert.AreEqual(WHITESPACE.Index, p5);

            Lexeme l6 = scanner.Read();
            Lexeme l7 = scanner.Read();

            Assert.AreEqual(ERROR.Index, l7.TokenIndex);

            int p3_2 = scanner.Peek();
            Assert.AreEqual(p3, p3_2);

            Lexeme l8 = scanner.Read(); // whitespace
            Lexeme l9 = scanner.Read(); // ID:if
            Lexeme l10 = scanner.Read(); // whitespace
            Lexeme l11 = scanner.Read(); // ID:vvv
            Lexeme l12 = scanner.Read(); // whitespace
            Lexeme l13 = scanner.Read(); // ID:xmlns
            Lexeme l14 = scanner.Read(); // whitespace
            Lexeme l15 = scanner.Read(); // NUM:772737
            Lexeme leof = scanner.Read(); // eof

            Assert.AreEqual(info.EndOfStreamTokenIndex, leof.TokenIndex);
            Assert.AreEqual(leof.Value.Span.StartLocation.CharIndex, leof.Value.Span.EndLocation.CharIndex);
            Assert.AreEqual(source.Length, leof.Value.Span.StartLocation.CharIndex);

            Lexeme leof2 = scanner.Read(); //after eof, should return eof again

            Assert.AreEqual(info.EndOfStreamTokenIndex, leof2.TokenIndex);
            Assert.AreEqual(leof.Value.Span.StartLocation.CharIndex, leof2.Value.Span.StartLocation.CharIndex);
        }