Example #1
0
        public void ITC10Load()
        {
            // load symbol
            var word = new WordSymbol("symbol1");

            Util.ParserLoadWord(word, "  symbol1  ", "Abcde", "string", 0, 0, 11);
        }
Example #2
0
        public void ITC11LoadWord()
        {
            //// test loading of basic grammar elements
            var outNo = new List <TextElement>();
            var idn   = new WordIdent();
            var str   = new WordString();
            var sym   = new WordSymbol("symbol1");

            // load string + string + name
            TextBuffer textBuffer = Util.NewBufferWs("  Aname     symbol1      'Fghij'      sym02  ");

            idn.TextBuffer = textBuffer;
            sym.TextBuffer = textBuffer;
            str.TextBuffer = textBuffer;
            idn.TextBuffer = textBuffer;
            Assert.AreEqual(true, idn.Load(outNo, 0), "Can't read a combinded Identifier");
            Assert.AreEqual(true, sym.Load(outNo, 0), "Can't read a combinded Symbol");
            Assert.AreEqual(true, str.Load(outNo, 0), "Can't read a combinded String");
            Assert.AreEqual(true, idn.Load(outNo, 0), "Can't read a combinded Identifier");
            textBuffer.FindNextWord(null, false);
            CodeElement node = outNo[1] as CodeElement;

            Assert.IsNotNull(node, "Can't find node after reading combinded Quote");
            Assert.AreEqual("Fghij", node.Value, "The combinded Quote value is not correct");
            node = outNo[2] as CodeElement;
            Assert.IsNotNull(node, "Can't find node after reading combinded VarName");
            Assert.AreEqual("sym02", node.Value, "The combinded VarName value is not correct");
            Assert.AreEqual(45, textBuffer.PointerNextChar, "The buffer pointer is of after reading combinded values");
        }
Example #3
0
        public void ITC10LoadError()
        {
            string testName = "Symbol";
            var    word     = new WordSymbol("loop");

            //                 "1234  Read 'symbol', EOF error.
            Util.WordLoadError("   ", word, testName,
                               "itc10: Syntax error (testRule). Expecting symbol 'loop', found EOF. Line 1, colomn 4");

            //                 "1234  Read 'symbol', EOF error.
            Util.WordLoadError(" lo", word, testName,
                               "itc10: Syntax error (testRule). Expecting symbol 'loop', found EOF. Line 1, colomn 4");

            //                 "12345678  Read 'symbol',  error.
            Util.WordLoadError(" looP  ", word, testName,
                               "itc07: Syntax error (testRule). Expecting symbol 'loop', found 'looP' Line 1, colomn 2");
        }
Example #4
0
        private void AddAlternatives(Rule ExprRule, ParserElementBase alternative)
        {
            // is it more alternatives?
            var or = alternative as Or;

            if (or != null)
            {
                AddAlternatives(ExprRule, or.ChildNodes[0]);
                AddAlternatives(ExprRule, or.ChildNodes[1]);
                return;
            }

            // is it a binary operator? (depends opun how Or is implemented)
            WordSymbol symbol = null;
            Rule       rule   = null;

            if (IsBinaryAlternative(ExprRule, alternative, out symbol, out rule))
            {
                var wordOp = new WordBinaryOperator(symbol, rule != null ? rule.Name : symbol.Value, TextBuffer);
                _binaryOperators.Add(wordOp);
                if (rule != null)
                {
                    rule.ReplaceSubElement(symbol, wordOp);
                }
                else
                {
                    Add(wordOp);
                }
            }

            // Other forms
            else
            {
                _otherForms.Add(alternative);
            }
        }
Example #5
0
        /// <summary>Initialize rules and elements. Called when creating a new parser and when cloning grammar.</summary>
        /// <param name="parser">The parser.</param>
        /// <param name="rules">Set of rules.</param>
        /// <param name="status">The parser status.</param>
        /// <returns>True: no error. False: error.</returns>
        internal static bool InitializeGrammar(Parser parser, List <Rule> rules, ParserStatus status)
        {
            foreach (Rule rule in rules)
            {
                rule.Parser = parser;

                //string debug1 = "" + parser.Level + ": " + rule.GetGrammar().NL() +
                //    rule.ToMarkupProtected(string.Empty);

                if (rules.Any(r => r != rule && r.Name.ToLower() == rule.Name.ToLower()))
                {
                    status.AddBuildError(() => MessageRes.itc23, rule.DefinitionCodeElement,
                                         rule.Name,
                                         parser.Name);
                }
            }

            foreach (Rule rule in rules)
            {
                InitializeElements(rule.ChildNodes, rules, status);
            }

            // Transformation of syntax: Initialize expressions here
            foreach (Rule rule in rules)
            {
                Or         or     = rule.ChildNodes[0] as Or;
                WordSymbol symbol = null;
                Rule       ruleOp = null;
                if (or != null &&
                    Expression.IsBinaryAlternative(rule, or.ChildNodes[0], out symbol, out ruleOp))
                {
                    rule.ReplaceSubElement(0, new Expression(rule, or));
                }
            }

            if (status.Error == null)
            {
                // Loop check and set RuleLink.Recursive
                var effectiveRules = new List <Rule>();
                parser.Rules[0].InitializeLoop(effectiveRules, new List <ParserElementBase>(), status);

                parser.Rules[0].InitializeLoop(effectiveRules, new List <ParserElementBase>(), status);

                foreach (Rule rule in effectiveRules)
                {
                    if (!rule.LoopHasEnd)
                    {
                        status.AddBuildError(() => MessageRes.itc25, rule.DefinitionCodeElement, rule.Name);
                    }
                }

                foreach (Rule rule in effectiveRules)
                {
                    if (rule.LoopLeftRecursive)
                    {
                        status.AddBuildError(() => MessageRes.itc28, rule.DefinitionCodeElement, rule.Name);
                    }
                }
            }
            return(status.Error == null);
        }
Example #6
0
        internal static bool IsBinaryAlternative(Rule ExprRule, ParserElementBase alternative, out WordSymbol symbol, out Rule rule)
        {
            // is it a binary operator
            // find symbol for 'rule' binary operators
            ParserElementBase binaryOperation = null;

            if (alternative is RuleLink)
            {
                rule            = ((RuleLink)alternative).RuleElement;
                binaryOperation = rule;
            }
            else
            {
                rule = null;
            }

            // find symbol for inline binary operators
            if (alternative is SetOfElements)
            {
                binaryOperation = alternative;
            }

            // Is it a binary operation ?
            if (binaryOperation != null && binaryOperation.ChildNodes.Count == 3)
            {
                var expre1 = binaryOperation.ChildNodes[0] as RuleLink;
                symbol = binaryOperation.ChildNodes[1] as WordSymbol;
                var expre2 = binaryOperation.ChildNodes[2] as RuleLink;

                // add binary operator
                if (expre1 != null && expre1.RuleElement == ExprRule &&
                    symbol != null &&
                    expre2 != null && expre2.RuleElement == ExprRule)
                {
                    return(true);
                }
            }

            symbol = null;
            return(false);
        }