Example #1
0
        /// <summary>Test the hard coded rules.
        /// One rule is testet with the load function.</summary>
        /// <param name="code">String to parse.</param>
        /// <param name="markup">Expected markup from rule output.</param>
        /// <param name="rules">List of rules to test. The first rule is loaded.</param>
        public static void ParserLoadRule(List <Rule> rules, string code, string markup)
        {
            var parser      = new Parser();
            var outElements = new List <TextElement>();

            TextBuffer buffer = Util.NewBufferWs(code);

            parser.Rules = rules.Select(r => r.CloneForParse(buffer) as Rule).ToList();
            ParserBuilder.InitializeGrammar(parser, parser.Rules, buffer.Status);
            ParserBuilder.ValidateGrammar(parser, buffer.Status);

            string syntax = parser.GetGrammar();

            Rule rule = parser.Rules[0];

            Assert.AreEqual(true, rule.Load(outElements, 0), string.Format("rule '{0}': cant read", rule.Name));
            string actual = outElements[0].ToMarkupProtected(string.Empty);

            Assert.AreEqual(markup, actual, "Equation TestOption: document fail");
        }
Example #2
0
        /// <summary>Read from a text buffer and create a text document.</summary>
        /// <param name="buffer">The text buffer.</param>
        /// <returns>A text document, or null if error.</returns>
        /// <exclude/>
        internal CodeDocument ParseString(TextBuffer buffer)
        {
            if (buffer.Status.Error != null)
            {
                return(null);
            }

            List <Rule> procesRules = Rules.Select(r => r.CloneForParse(buffer) as Rule).ToList();

            if (!ParserBuilder.InitializeGrammar(this, procesRules, buffer.Status))
            {
                return(null);
            }
            var  elements = new List <TextElement>();
            bool ok;

            var resultDoc = new CodeDocument(elements, procesRules[0].Name);

            try
            {
                // skip preceding white spaces and comments
                buffer.FindNextWord(resultDoc.ChildNodes, false);
                //buffer.InsertComments(elements);

                ok = procesRules[0].Load(elements, 0);

                buffer.FindNextWord(elements, false);
                //buffer.InsertComments(elements);
            }
            catch (Exception e)
            {
                buffer.Status.AddException(e, () => MessageRes.itc12, e.Message);
                return(null);
            }

            if (buffer.Status.Error != null)
            {
                return(null);
            }

            if (!ok)
            {
                buffer.Status.AddParseError(() => MessageRes.itc12, procesRules[0].Name);
            }
            else if (!buffer.IsEnd())
            {
                if (elements != null && elements.Count() > 0)
                {
                    CodeElement last  = elements.OfType <CodeElement>().Last();
                    string      debug = last.ToMarkupProtected(string.Empty);
                    buffer.GetLoopLast(null);
                    procesRules[0].ResolveErrorsLast(last, 0);
                }
                buffer.Status.AddSyntaxErrorEof(() => MessageRes.itc13);
            }
            else if (elements.OfType <CodeElement>().Count() == 1)
            {
                resultDoc.AddRange(elements.OfType <CodeElement>().First().ChildNodes);
                resultDoc.AddRange(elements.OfType <CommentElement>());
                return(resultDoc);
            }
            else
            {
                buffer.Status.AddParseError(() => MessageRes.itc20, procesRules[0].Name);
            }

            return(null);
        }