예제 #1
0
        public void FewLinesFragmentTextTest()
        {
            string source = "1\n22\n\n4";

            iOrigin = CreateOriginTest(source);
            iReader = iOrigin.GetReader();

            iReader.MoveNext(); // after '1'
            ILocation loc1 = iReader.CurrentLocation;

            iReader.MoveNext(); // after '\n'
            iReader.MoveNext(); // after '2'
            ILocation loc2 = iReader.CurrentLocation;

            iReader.MoveNext(); // after '2'

            Assert.AreEqual(iOrigin.MakeFragment(loc1, loc2).GetOriginText(), "\n2");

            ILocation loc3 = iReader.CurrentLocation;

            iReader.MoveNext(); // after '\n'
            iReader.MoveNext(); // after '\n'
            ILocation loc4 = iReader.CurrentLocation;

            Assert.AreEqual(iOrigin.MakeFragment(loc3, loc4).GetOriginText(), "\n\n");

            iReader.MoveNext(); // after '4'
            ILocation loc5 = iReader.CurrentLocation;

            Assert.AreEqual(iOrigin.MakeFragment(loc4, loc5).GetOriginText(), "4");
        }
예제 #2
0
        public void FewLinesFragmentPositionsTest()
        {
            string source = "1\n22\n\n4";

            iOrigin = CreateOriginTest(source);
            iReader = iOrigin.GetReader();

            iReader.MoveNext();
            ILocation loc1 = iReader.CurrentLocation;

            iReader.MoveNext();
            iReader.MoveNext();
            ILocation loc2 = iReader.CurrentLocation;

            iReader.MoveNext();

            IFragment fr = iOrigin.MakeFragment(loc1, loc2);

            Assert.AreEqual(fr.GetBeginOriginPosition(), loc1.GetOriginPosition());
            Assert.AreEqual(fr.GetEndOriginPosition(), loc2.GetOriginPosition());

            iReader.MoveNext();
            iReader.MoveNext();
            ILocation loc3 = iReader.CurrentLocation;

            IFragment fr2 = iOrigin.MakeFragment(loc2, loc3);

            Assert.AreEqual(fr2.GetBeginOriginPosition(), loc2.GetOriginPosition());
            Assert.AreEqual(fr2.GetEndOriginPosition(), loc3.GetOriginPosition());
        }
예제 #3
0
        public static IEnumerable <Tuple <IFragment, IEnumerable <int> > > SanitizedTokens(IOrigin origin)
        {
            var categories           = NicodemGrammarProductions.TokenCategories;
            var whiteSpaceCategories = NicodemGrammarProductions.WhiteSpaceTokenCategories;

            var forbidden = new List <int>(whiteSpaceCategories.Count);

            for (int i = 0; i < categories.Count; ++i)
            {
                if (whiteSpaceCategories.Contains(categories[i]))
                {
                    forbidden.Add(i);
                }
            }

            var lexer = Lexer.Lexer.GetCompiledLexer("NicodemLexer", categories);

            var tokenizerResult = lexer.Process(origin);

            // TODO improve error handling
            if (!tokenizerResult.LastParsedLocation.EqualsLocation(tokenizerResult.FailedAtLocation))
            {
                throw new LexerFailure(origin.MakeFragment(tokenizerResult.LastParsedLocation, tokenizerResult.FailedAtLocation));
            }
            var tokens          = tokenizerResult.Tokens.ToArray();
            var sanitizedTokens = tokens.Where(a => a.Item2.All(c => !forbidden.Contains(c)));

            return(sanitizedTokens);
        }
예제 #4
0
        public void EmptySourceFragmentTextTest()
        {
            string source = "";

            iOrigin = CreateOriginTest(source);
            iReader = iOrigin.GetReader();

            ILocation loc1 = iReader.CurrentLocation;

            iReader.MoveNext();
            ILocation loc2 = iReader.CurrentLocation;

            IFragment fr = iOrigin.MakeFragment(loc1, loc2);

            Assert.AreEqual(fr.GetOriginText(), "");
        }
예제 #5
0
        public void WholeSourceFragmentTextTest()
        {
            string source = "I like reading sources!\nEspecially in tests.";

            iOrigin = CreateOriginTest(source);
            iReader = iOrigin.GetReader();

            ILocation locBeg = iReader.CurrentLocation;

            while (iReader.MoveNext())
            {
                ;                        // move to the end of source
            }
            ILocation locEnd = iReader.CurrentLocation;

            IFragment frAll = iOrigin.MakeFragment(locBeg, locEnd);

            Assert.AreEqual(frAll.GetOriginText(), source);
        }