SetTriviaTokens() public method

public SetTriviaTokens ( ) : void
return void
示例#1
0
        public void SetTriviaTokens(params int[] triviaTokenIndices)
        {
            if (m_lookAheadQueue.Count > 0)
            {
                throw new InvalidOperationException("The skip tokens are not allowed to be set when the look ahead queue not empty");
            }

            m_masterScanner.SetTriviaTokens(triviaTokenIndices);
        }
示例#2
0
        public ForkableScanner Create(SourceReader source)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            Scanner masterScanner = new Scanner(ScannerInfo);
            masterScanner.SetSource(source);
            masterScanner.SetTriviaTokens(m_triviaTokens);
            masterScanner.ErrorManager = ErrorManager;
            masterScanner.RecoverErrors = RecoverErrors;
            masterScanner.LexicalErrorId = LexicalErrorId;

            return ForkableScanner.Create(masterScanner);
        }
示例#3
0
        public ForkableScanner Create(SourceReader source)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            Scanner masterScanner = new Scanner(ScannerInfo);

            masterScanner.SetSource(source);
            masterScanner.SetTriviaTokens(m_triviaTokens);
            masterScanner.ErrorList      = ErrorList;
            masterScanner.RecoverErrors  = RecoverErrors;
            masterScanner.LexicalErrorId = LexicalErrorId;

            return(ForkableScanner.Create(masterScanner));
        }
示例#4
0
        public void CompactCharSetTest()
        {
            Lexicon lexicon = new Lexicon();
            Lexer global = lexicon.Lexer;
            Lexer keywords = global.CreateSubLexer();
            Lexer xml = keywords.CreateSubLexer();

            var lettersCategories = new[] { UnicodeCategory.LetterNumber,
                                            UnicodeCategory.LowercaseLetter,
                                            UnicodeCategory.ModifierLetter,
                                            UnicodeCategory.OtherLetter,
                                            UnicodeCategory.TitlecaseLetter,
                                            UnicodeCategory.UppercaseLetter};

            var RE_IDCHAR = RE.CharsOf(c => lettersCategories.Contains(Char.GetUnicodeCategory(c)));

            var ID = global.DefineToken(RE_IDCHAR.Concat(
                (RE_IDCHAR | RE.Range('0', '9')).Many()));
            var NUM = global.DefineToken(RE.Range('0', '9').Many1());
            var WHITESPACE = global.DefineToken(RE.Symbol(' ').Many());

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

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

            var scannerInfo = lexicon.CreateScannerInfo();
            scannerInfo.CurrentLexerIndex = xml.Index;

            Scanner s = new Scanner(scannerInfo);

            string source = "xmlns 你好吗1 123 蘏臦囧綗 ABCD if";

            SourceReader sr = new SourceReader(new StringReader(source));

            s.SetSource(sr);
            s.SetTriviaTokens(WHITESPACE.Index);

            var l1 = s.Read();
            Assert.AreEqual(XMLNS.Index, l1.TokenIndex);

            var l2 = s.Read();
            Assert.AreEqual(ID.Index, l2.TokenIndex);

            var l3 = s.Read();
            Assert.AreEqual(NUM.Index, l3.TokenIndex);

            var l4 = s.Read();
            Assert.AreEqual(ID.Index, l4.TokenIndex);

            var l5 = s.Read();
            Assert.AreEqual(ID.Index, l5.TokenIndex);

            var l6 = s.Read();
            Assert.AreEqual(IF.Index, l6.TokenIndex);
        }
示例#5
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);
        }
示例#6
0
        public void ErrorRecoveryTest()
        {
            Lexicon lexicon = new Lexicon();
            Lexer global = lexicon.Lexer;


            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());

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

            string source = "asdf04a 1107 !@#$!@ Z if vvv xmlns 772737";
            StringReader sr = new StringReader(source);

            scanner.SetSource(new SourceReader(sr));
            scanner.SetTriviaTokens(WHITESPACE.Index);
            scanner.RecoverErrors = true;

            CompilationErrorManager em = new CompilationErrorManager();
            em.DefineError(101, 0, CompilationStage.Scanning, "Invalid token: {0}");

            var el = em.CreateErrorList();

            scanner.ErrorList = el;
            scanner.LexicalErrorId = 101;

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

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

            Assert.AreEqual(0, el.Count);

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

            Assert.IsTrue(el.Count > 0);
            Assert.AreEqual(101, el[0].Info.Id);
        }