Esempio n. 1
0
        public static string Substring([NotNull] this string str, StringOrigin origin, int length)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            // Fast path
            var strLen = str.Length;

            if (strLen == 0 || length == 0)
            {
                return("");
            }
            if (length >= strLen)
            {
                return(str);
            }
            switch (origin)
            {
            case StringOrigin.Begin:
                return(str.Substring(0, length));

            case StringOrigin.End:
                return(str.Substring(strLen - length, length));

            default:
                throw new ArgumentOutOfRangeException(nameof(origin), origin, null);
            }
        }
        public static string Substring([NotNull] this string str, StringOrigin origin, int length)
        {
            Code.NotNull(str, nameof(str));

            // Fast path
            var strLen = str.Length;

            if (strLen == 0 || length == 0)
            {
                return("");
            }
            if (length >= strLen)
            {
                return(str);
            }
            switch (origin)
            {
            case StringOrigin.Begin:
                return(str.Substring(0, length));

            case StringOrigin.End:
                return(str.Substring(strLen - length, length));

            default:
                throw CodeExceptions.Argument(nameof(origin), $"Invalid {nameof(StringOrigin)} value.");
            }
        }
        public void PrintMessageTest()
        {
            string        source = "OK. Error <- here is an error!";
            IOriginReader reader = new StringOrigin(source).GetReader();

            for (int i = 0; i < 4; i++)
            {
                reader.MoveNext(); // 'OK. '
            }
            ILocation locBeg = reader.CurrentLocation;

            for (int i = 0; i < 5; i++)
            {
                reader.MoveNext(); // 'Error'
            }
            ILocation locEnd = reader.CurrentLocation;

            IFragment fr = locBeg.Origin.MakeFragment(locBeg, locEnd);

            SourceDiagnostic sd  = new SourceDiagnostic();
            string           res = sd.PrepareMessage("warning", fr, "found an error in your source");

            sd.PrintMessage("warning", fr, "found an error in your source");
            Assert.AreEqual(res, "warning at line 1:4 \"Error\" - found an error in your source");
        }
Esempio n. 4
0
        internal static IEnumerable <Tuple <string, IEnumerable <int> > > Process(this Nicodem.Lexer.Lexer lexer, string s)
        {
            var origin = new StringOrigin(s);

            return(lexer
                   .ProcessBare(origin)
                   .Select(t => new Tuple <string, IEnumerable <int> >(origin.GetText(t.Item1), t.Item2)));
        }
        protected IEnumerable <ParseLeaf <CharSymbol> > BuildCode(string[] input)
        {
            var origin = new StringOrigin("");

            return(input.Select(k => {
                return new ParseLeaf <CharSymbol>(
                    new OriginFragment(origin,
                                       new OriginPosition(),
                                       new OriginPosition()),
                    _builder.GetSymbolForTerm(k));
            }));
        }
Esempio n. 6
0
        //[Test()]
        public void Sample01Test() // bad test -> strings not implemented
        {
            var s = new StringBuilder();

            s.AppendLine("main(int x) -> void");
            s.AppendLine("{");
            s.AppendLine("    print(\"Hello, World!\\n\");");
            s.AppendLine("}");
            var inputFile        = new StringOrigin(s.ToString());
            var parseTree        = CSTBuilder.Build(inputFile);
            var backendFunctions = new Frontend().FromParseTreeToBackend(parseTree);

            foreach (var f in backendFunctions)
            {
                Console.WriteLine(f);
            }
        }
Esempio n. 7
0
        public static string Substring(this string str, StringOrigin origin, int length)
        {
            Code.NotNull(str, nameof(str));

            // Fast path
            var strLen = str.Length;

            if (strLen == 0 || length == 0)
            {
                return("");
            }
            if (length >= strLen)
            {
                return(str);
            }
            return
                (origin switch
            {
                StringOrigin.Begin => str.Substring(0, length),
                StringOrigin.End => str.Substring(strLen - length, length),
                _ => throw CodeExceptions.Argument(nameof(origin), $"Invalid {nameof(StringOrigin)} value.")
            });
        public void PrintFragmentInLineTest()
        {
            string        source = "OK. Error in line 1\nLine 2.";
            IOriginReader reader = new StringOrigin(source).GetReader();

            for (int i = 0; i < 4; i++)
            {
                reader.MoveNext(); // 'OK. '
            }
            ILocation locBeg = reader.CurrentLocation;

            for (int i = 0; i < 5; i++)
            {
                reader.MoveNext(); // 'Error'
            }
            ILocation locEnd = reader.CurrentLocation;
            IFragment fr     = locBeg.Origin.MakeFragment(locBeg, locEnd);

            SourceDiagnostic sd = new SourceDiagnostic();

            sd.PrintFragmentInLine(fr);
        }
 public string SubstringOrigin([NotNull] string str, StringOrigin origin, int length) => str.Substring(origin, length);
Esempio n. 10
0
 public string SubstringOrg(string str, StringOrigin origin, int length) => str.Substring(origin, length);
Esempio n. 11
0
 public string SubstringOrigin(string str, StringOrigin origin, [NonNegativeValue] int length) => str.Substring(origin, length);