Пример #1
0
        [Test] public void SrcStack()
        {
            const string str1  = "one";
            const string str2  = "two";
            var          src1  = new StringSrc(str1);
            var          src2  = new StringSrc(str2);
            var          stack = new SrcStack(src1);

            for (int i = 0; i != 2; ++i, stack.Next())
            {
                Assert.Equal(str1[i], stack.Peek);
            }

            stack.Push(src2);

            for (int i = 0; i != 3; ++i, stack.Next())
            {
                Assert.Equal(str2[i], stack.Peek);
            }

            for (int i = 2; i != 3; ++i, stack.Next())
            {
                Assert.Equal(str1[i], stack.Peek);
            }

            Assert.Equal((char)0, stack.Peek);
        }
Пример #2
0
            public AutoComplete(string?templates = null)
            {
                Templates = new List <Template>();
                var lookup = new TemplateLookup();

                // Parse the templates
                templates ??= AutoCompleteTemplates;
                using var src = new StringSrc(templates);
                foreach (var tok in Parse(src, true))
                {
                    if (tok is Template template)
                    {
                        // Add 'TemplateMark' templates to the list of root-level templates
                        if (!template.Flags.HasFlag(ETemplateFlags.Hidden))
                        {
                            Templates.Add(template);
                        }

                        // Add the template to the lookup map from keyword to template.
                        // All root level templates must be unique.
                        if (!template.Flags.HasFlag(ETemplateFlags.RootLevelOnly))
                        {
                            lookup.Add(template.Keyword.ToLower(), template);
                        }
                    }
                    else
                    {
                        throw new ScriptException(Script.EResult.SyntaxError, src.Location, $"Unexpected root level token: {tok.Description}");
                    }
                }

                // Replace template references. '*!' templates can existing in 'Templates' and not in 'lookup'
                foreach (var kv in lookup)
                {
                    ReplaceTemplateReferences(kv.Value, lookup);
                }
                foreach (var tp in Templates)
                {
                    ReplaceTemplateReferences(tp, lookup);
                }

                // Sort the top level templates to allow binary search
                Templates.Sort();
            }
Пример #3
0
        public void StripCppComments()
        {
            const string str_in =
                "123// comment         \n" +
                "456/* block */789     \n" +
                "// many               \n" +
                "// lines              \n" +
                "// \"string\"         \n" +
                "/* \"string\" */      \n" +
                "\"string \\\" /*a*/ //b\"  \n" +
                "/not a comment\n" +
                "/*\n" +
                "  more lines\n" +
                "*/\n" +
                "/*back to*//*back*/ comment\n";
            const string str_out =
                "123\n" +
                "456789     \n" +
                "\n" +
                "\n" +
                "\n" +
                "      \n" +
                "\"string \\\" /*a*/ //b\"  \n" +
                "/not a comment\n" +
                "\n" +
                " comment\n";

            var src   = new StringSrc(str_in);
            var strip = new StripComments(src);

            for (int i = 0; i != str_out.Length; ++i, strip.Next())
            {
                if (str_out[i] == strip.Peek)
                {
                    continue;
                }
                Assert.Equal(str_out[i], strip.Peek);
            }
            Assert.Equal('\0', strip.Peek);
        }
Пример #4
0
        public void StripAsmComments()
        {
            const string str_in =
                "; asm comments start with a ; character\r\n" +
                "mov 43 2\r\n" +
                "ldr $a 2 ; imaginary asm";
            const string str_out =
                "\r\n" +
                "mov 43 2\r\n" +
                "ldr $a 2 ";

            var src   = new StringSrc(str_in);
            var strip = new StripComments(src, comment_patterns: new InComment.Patterns(";", "\r\n", "", ""));

            for (int i = 0; i != str_out.Length; ++i, strip.Next())
            {
                if (str_out[i] == strip.Peek)
                {
                    continue;
                }
                Assert.Equal(str_out[i], strip.Peek);
            }
            Assert.Equal('\0', strip.Peek);
        }