public FreeTextLiteralTestGrammar(Terminal terminal)
   : base(caseSensitive: true) {
   var rule = new BnfExpression(terminal);
   MarkReservedWords(Terminator);
   rule += Terminator;
   base.Root = new NonTerminal("Root");
   Root.Rule = rule;
 }
 public TerminalTestGrammar(Terminal terminal, string terminator = null) : base(caseSensitive: true) {
   Terminator = terminator; 
   var rule = new BnfExpression(terminal);
   if (Terminator != null) {
     MarkReservedWords(Terminator);
     rule += Terminator; 
   }
   base.Root = new NonTerminal("Root");
   Root.Rule = rule;
 }
示例#3
0
        private void CollectTermsRecursive(BnfTerm term)
        {
            if (_grammarData.AllTerms.Contains(term))
            {
                return;
            }
            _grammarData.AllTerms.Add(term);
            NonTerminal nt = term as NonTerminal;

            if (nt == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(nt.Name))
            {
                if (nt.Rule != null && !string.IsNullOrEmpty(nt.Rule.Name))
                {
                    nt.Name = nt.Rule.Name;
                }
                else
                {
                    nt.Name = "Unnamed" + (_unnamedCount++);
                }
            }
            if (nt.Rule == null)
            {
                _language.Errors.AddAndThrow(GrammarErrorLevel.Error, null, Resources.ErrNtRuleIsNull, nt.Name);
            }
            //check all child elements
            foreach (BnfTermList elemList in nt.Rule.Data)
            {
                for (int i = 0; i < elemList.Count; i++)
                {
                    BnfTerm child = elemList[i];
                    if (child == null)
                    {
                        _language.Errors.Add(GrammarErrorLevel.Error, null, Resources.ErrRuleContainsNull, nt.Name, i);
                        continue; //for i loop
                    }
                    //Check for nested expression - convert to non-terminal
                    BnfExpression expr = child as BnfExpression;
                    if (expr != null)
                    {
                        child       = new NonTerminal(null, expr);
                        elemList[i] = child;
                    }
                    CollectTermsRecursive(child);
                } //for i
            }
        }         //method
示例#4
0
            public TerminalTestGrammar(Terminal terminal, string terminator = null) : base(caseSensitive: true)
            {
                Terminator = terminator;
                var rule = new BnfExpression(terminal);

                if (Terminator != null)
                {
                    MarkReservedWords(Terminator);
                    rule += Terminator;
                }
                Root = new NonTerminal("Root")
                {
                    Rule = rule
                };
            }
示例#5
0
 private static NonTerminal NonTerminal <T>(string name, BnfExpression rule, Func <ParseTreeNode, T> creator)
 {
     return(new NonTerminal(name, (context, n) =>
     {
         try
         {
             n.AstNode = creator(n);
         }
         catch (Exception e)
         {
             var input = GetTokens(n).JoinStrings(" ");
             const string messageFormat = "exception creating ast node from node {0} [{1}]";
             throw new InvalidOperationException(string.Format(messageFormat, n, input), e);
         }
     })
     {
         Rule = rule
     });
 }
示例#6
0
        BnfTerm MultiWordTermial(string term)
        {
            var parts = term.Split(' ');

            if (parts.Length == 1)
            {
                return(ToTerm(term));
            }

            var           nonterm = new NonTerminal(term, typeof(MultiWordTermialNode));
            BnfExpression expr    = new BnfExpression(ToTerm(parts[0]));

            foreach (var part in parts.Skip(1))
            {
                expr += ToTerm(part);
            }
            nonterm.Rule = expr;
            return(nonterm);
        }
示例#7
0
        private void ProcessUnparseHints(BnfExpression rule)
        {
            if (rule != null)
            {
                for (int childBnfTermListIndex = 0; childBnfTermListIndex < rule.Data.Count; childBnfTermListIndex++)
                {
                    BnfTermList bnfTermList = rule.Data[childBnfTermListIndex];

                    try
                    {
                        UnparseHint unparseHint = (UnparseHint)bnfTermList.SingleOrDefault(bnfTerm => bnfTerm is UnparseHint);
                        childBnfTermListIndexToUnparseHint.Add(childBnfTermListIndex, unparseHint);
                    }
                    catch (InvalidOperationException)
                    {
                        GrammarHelper.ThrowGrammarErrorException(
                            GrammarErrorLevel.Error,
                            "NonTerminal '{0}' has more than one UnparseHint on its {1}. childrenlist. Only one UnparseHint is allowed per childrenlist.", this, childBnfTermListIndex + 1
                            );
                    }
                }
            }
        }
示例#8
0
        private BnfExpression TerminalsFromEnum <TEnum>(string groupName = null)
            where TEnum : struct, Enum
        {
            BnfExpression expr = null;

            var names = Enum.GetNames <TEnum>();

            KeyTerm toTerm(string name) => groupName is null?ToTerm(name) : ToTerm(name, groupName);

            foreach (var name in names)
            {
                if (expr is null)
                {
                    expr = toTerm(name);
                }
                else
                {
                    expr |= toTerm(name);
                }
            }

            return(expr);
        }
示例#9
0
        protected BnfExpression CreateRuleFromObjectTypes <T>(IEnumerable <T> types) where T : ObjectType
        {
            BnfExpression rule = null;

            foreach (var type in types)
            {
                if (rule == null)
                {
                    rule = TypeName(type.Name);
                }
                else
                {
                    rule |= TypeName(type.Name);
                }

                // Add alternative names as well
                foreach (var alternativeName in type.AlternativeNames)
                {
                    rule |= TypeName(alternativeName);
                }
            }
            return(rule);
        }
示例#10
0
        NonTerminal BuildTerminal <TNode>(string name, string filename)
            where TNode : MultiWordTermialNode
        {
            const string ResourcePrefix = "Shakespeare.include.";
            var          termList       = new NonTerminal(name, typeof(TNode));
            var          strm           = Assembly.GetExecutingAssembly().GetManifestResourceStream(ResourcePrefix + filename.ToLowerInvariant());
            var          sw             = new StreamReader(strm);
            var          block          = sw.ReadToEnd();
            var          lines          = block.Split('\n', '\r');

            if (lines.Any())
            {
                BnfExpression expr = new BnfExpression(MultiWordTermial(lines[0]));
                foreach (var line in lines.Skip(1))
                {
                    if (!String.IsNullOrWhiteSpace(line))
                    {
                        expr |= MultiWordTermial(line);
                    }
                }
                termList.Rule = expr;
            }
            return(termList);
        }
示例#11
0
        public QueryGrammar()
            : base(false)
        {
            LanguageFlags = LanguageFlags.CreateAst;
            not           = Transient("not", ToTerm("NOT") | "НЕ");
            by            = NonTerminal("by", ToTerm("by") | "ПО", TermFlags.NoAstNode);
            distinctOpt   = NonTerminal("distinctOpt", null);
            var identifier = Identifier();

            var root            = NonTerminal("root", null, ToSqlQuery);
            var selectStatement = NonTerminal("selectStmt", null, ToSelectClause);
            var expression      = Expression(identifier, root);

            var asOpt    = NonTerminal("asOpt", Empty | "AS" | "КАК");
            var alias    = NonTerminal("alias", null, TermFlags.IsTransient);
            var aliasOpt = NonTerminal("aliasOpt", null, TermFlags.IsTransient);

            var columnItem = NonTerminal("columnItem", null, ToSelectFieldExpression);

            var tableDeclaration = NonTerminal("joinSource", null, ToTableDeclaration);

            var columnSource   = NonTerminal("columnSource", null, TermFlags.IsTransient);
            var joinItemList   = Join(columnSource, expression);
            var whereClauseOpt = NonTerminal("whereClauseOpt", null,
                                             node => node.ChildNodes.Count == 0 ? null : node.ChildNodes[1].AstNode);

            var groupClauseOpt  = GroupBy(expression);
            var orderClauseOpt  = OrderBy(expression);
            var havingClauseOpt = NonTerminal("havingClauseOpt", Empty | ToTerm("having") + expression,
                                              node => node.ChildNodes.Count == 0 ? null : node.ChildNodes[1].AstNode);
            var columnItemList = NonTerminal("columnItemList", null);

            var topOpt       = NonTerminal("topOpt", null);
            var selectList   = NonTerminal("selectList", null);
            var unionStmtOpt = NonTerminal("unionStmt", null, ToUnionType);
            var unionList    = NonTerminal("unionList", null, ToUnionList);

            var subqueryTable = NonTerminal("subQuery", null, ToSubqueryTable);

            //rules
            selectStatement.Rule = Transient("select", ToTerm("SELECT") | "ВЫБРАТЬ")
                                   + topOpt
                                   + distinctOpt
                                   + selectList
                                   + Transient("from", ToTerm("FROM") | ToTerm("ИЗ")) + columnSource
                                   + joinItemList + whereClauseOpt
                                   + groupClauseOpt + havingClauseOpt;
            selectList.Rule  = columnItemList | "*";
            topOpt.Rule      = Empty | Transient("top", ToTerm("TOP") | "ПЕРВЫЕ") + numberLiteral;
            distinctOpt.Rule = Empty | Transient("distinct", ToTerm("DISTINCT") | "РАЗЛИЧНЫЕ");

            columnSource.Rule     = tableDeclaration | subqueryTable;
            alias.Rule            = asOpt + identifier;
            aliasOpt.Rule         = Empty | alias;
            columnItem.Rule       = expression + aliasOpt;
            tableDeclaration.Rule = identifier + aliasOpt;
            columnItemList.Rule   = MakePlusRule(columnItemList, ToTerm(","), columnItem);
            subqueryTable.Rule    = ToTerm("(") + root + ")" + alias;
            whereClauseOpt.Rule   = Empty | (Transient("where", ToTerm("WHERE") | "ГДЕ") + expression);
            unionStmtOpt.Rule     = Empty |
                                    (Transient("union", ToTerm("UNION") | "ОБЪЕДИНИТЬ") +
                                     Transient("unionAllModifier", Empty | "ALL" | "ВСЕ"));
            unionList.Rule = MakePlusRule(unionList, null,
                                          NonTerminal("unionListElement", selectStatement + unionStmtOpt));
            root.Rule = unionList + orderClauseOpt;

            RegisterOperators <SqlBinaryOperator>();
            RegisterOperators <UnaryOperator>();
            MarkPunctuation(",", "(", ")");
            MarkPunctuation(asOpt);
            AddOperatorReportGroup("operator");
            AddToNoReportGroup("as", "КАК");

            Root = root;
        }
示例#12
0
 public static NonTerminal Named(string name, BnfExpression expr)
 {
     var nt = new NonTerminal(name);
     nt.Rule = expr;
     return nt;
 }
示例#13
0
文件: Pattern.cs 项目: msallin/irony
 public ListPattern(string name, string separator = null, BnfExpression elementRule = null) : base(name) {
   if (separator != null)
     Separators = new string[] { separator };
   ElementRule = elementRule;
 }
示例#14
0
 protected static BnfTerm WithStar(BnfExpression expression)
 {
     return ToElement(expression).Star();
 }
示例#15
0
 protected static BnfTerm ToElement(BnfExpression expression)
 {
     string name = expression.ToString();
       return new NonTerminal(name, expression);
 }
示例#16
0
 protected BnfiExpression()
 {
     this.bnfExpression = new BnfExpression();
 }
示例#17
0
 protected BnfiExpressionConversion(BnfExpression bnfExpression)
     : base(bnfExpression)
 {
 }
示例#18
0
 protected BnfiExpression(BnfExpression bnfExpression)
 {
     this.bnfExpression = bnfExpression;
 }
 static NonTerminal CreateNonTerminal(string name, BnfExpression rule)
 {
     var nt = new NonTerminal(name);
     nt.Rule = rule;
     return nt;
 }
示例#20
0
 protected BnfiExpression(BnfTerm bnfTerm)
 {
     this.bnfExpression = bnfTerm.ToBnfExpression();
 }
示例#21
0
        protected void CreateBNFRule(BnfExpression myBnfExpression)
        {
            CLICommandNonTerminal.Rule = myBnfExpression;
            CLICommandNonTerminal.GraphOptions.Add(GraphOption.IsCommandRoot);

            //ToDo: Via reflection

            // Non-terminal integration
            _CommandNonTerminals.Add(CLICommandNonTerminal);

            // Symbol integration
            _CommandSymbolTerminal.Add(CLICommandSymbolTerminal);
        }
示例#22
0
 protected BnfiExpression()
 {
     this.bnfExpression = new BnfExpression();
 }
示例#23
0
 protected BnfiExpression(BnfExpression bnfExpression)
 {
     this.bnfExpression = bnfExpression;
 }
示例#24
0
 protected BnfiExpression(BnfTerm bnfTerm)
 {
     this.bnfExpression = bnfTerm.ToBnfExpression();
 }
示例#25
0
 internal static IEnumerable <BnfTermList> GetBnfTermsList(this BnfExpression bnfExpression)
 {
     return(bnfExpression.Data);
 }
示例#26
0
 protected BnfiExpressionConversion(BnfExpression bnfExpression)
     : base(bnfExpression)
 {
 }
示例#27
0
 public static NonTerminal WithRule(this NonTerminal @this, BnfExpression expression)
 {
     @this.Rule = expression;
     return @this;
 }
示例#28
0
        public XbnfGrammar(Mode mode)
        {
            this.mode = mode;

            var alternationChar = (mode == Mode.Strict) ? "/" : "|";

            var rulename    = new IdentifierTerminal("rulename", "-_", null);
            var funcname    = new IdentifierTerminal("funcname", ".", null);
            var newrulename = new IdentifierTerminal("newrulename", "-_", null);
            var comment     = new CommentTerminal("comment", "/*", "*/");
            var bindig1     = new NumberLiteral("bindig", NumberOptions.Binary | NumberOptions.IntOnly);
            var bindig2     = new NumberLiteral("bindig", NumberOptions.Binary | NumberOptions.IntOnly);
            var hexdig1     = new NumberLiteral("hexdig", NumberOptions.Hex | NumberOptions.IntOnly);
            var hexdig2     = new NumberLiteral("hexdig", NumberOptions.Hex | NumberOptions.IntOnly);
            var decdig1     = new NumberLiteral("decvalue", NumberOptions.IntOnly);
            var decdig2     = new NumberLiteral("decvalue", NumberOptions.IntOnly);
            var charval     = new StringLiteral("charval", "\"", StringOptions.NoEscapes);
            var repeat1     = new NumberLiteral("repeat1", NumberOptions.IntOnly);
            var repeat2     = new NumberLiteral("repeat2", NumberOptions.IntOnly);
            var minus       = ToTerm("-", "minus");
            var point       = ToTerm(".", "point");

            bindig1.AddPrefix("b", NumberOptions.None);
            hexdig1.AddPrefix("x", NumberOptions.None);
            decdig1.AddPrefix("d", NumberOptions.None);

            base.NonGrammarTerminals.Add(comment);


            // NON TERMINALS
            var numval = new NonTerminal("numval");

            var hexval   = new NonTerminal("hexval");
            var hexvalp  = new NonTerminal("hexvalpoint");
            var hexvalps = new NonTerminal("hexvalpointstar");

            var binval   = new NonTerminal("binval");
            var binvalp  = new NonTerminal("binvalpoint");
            var binvalps = new NonTerminal("binvalpointstar");

            var decval   = new NonTerminal("decval");
            var decvalp  = new NonTerminal("decvalpoint");
            var decvalps = new NonTerminal("decvalpointstar");

            var rule          = new NonTerminal("rule");
            var rulelist      = new NonTerminal("rulelist");
            var alternation   = new NonTerminal("alternation");
            var concatenation = new NonTerminal("concatenation");
            var substraction  = new NonTerminal("substraction");
            var repetition    = new NonTerminal("repetition");
            var repeat        = new NonTerminal("repeat");
            var element       = new NonTerminal("element");
            var elements      = new NonTerminal("elements");
            var group         = new NonTerminal("group");
            var option        = new NonTerminal("option");
            var func          = new NonTerminal("func");
            var funcarg       = new NonTerminal("funcarg");
            var funcargs      = new NonTerminal("funcargs");



            // RULES
            hexval.Rule   = hexdig1 + (hexvalps | (minus + hexdig2) | Empty);
            hexvalp.Rule  = point + hexdig2;
            hexvalps.Rule = MakePlusRule(hexvalps, hexvalp);

            binval.Rule   = bindig1 + (binvalps | (minus + bindig2) | Empty);
            binvalp.Rule  = point + bindig2;
            binvalps.Rule = MakePlusRule(binvalps, binvalp);

            decval.Rule   = decdig1 + (decvalps | (minus + decdig2) | Empty);
            decvalp.Rule  = point + decdig2;
            decvalps.Rule = MakePlusRule(decvalps, decvalp);

            numval.Rule = ToTerm("%") + (binval | hexval | decval);

            BnfExpression rp = ToTerm("*");

            if (mode == Mode.HttpCompatible)
            {
                rp = rp | "#";
            }

            repeat.Rule = ((repeat1) | ((repeat1 | Empty) + rp + (repeat2 | Empty)));
            group.Rule  = ToTerm("(") + alternation + ")";
            option.Rule = ToTerm("[") + alternation + "]";

            funcarg.Rule  = alternation;
            funcargs.Rule = MakePlusRule(funcargs, ToTerm(","), funcarg);
            func.Rule     = ToTerm("{") + funcname + "," + funcargs + "}";

            //alternation.Rule = MakePlusRule(alternation, ToTerm(alternationChar), concatenation);
            //concatenation.Rule = MakePlusRule(concatenation, repetition);
            alternation.Rule  = MakePlusRule(alternation, ToTerm(alternationChar), substraction);
            substraction.Rule = MakePlusRule(substraction, ToTerm("&!"), concatenation);
            //concatenation + ((ToTerm("--") + concatenation) | Empty);
            concatenation.Rule = MakePlusRule(concatenation, repetition);

            repetition.Rule = (Empty | repeat) + element;
            element.Rule    = rulename | group | option | numval | charval | func;

            elements.Rule = alternation;
            rule.Rule     = newrulename + (ToTerm("=") | ToTerm("=" + alternationChar)) + elements + ";";
            rulelist.Rule = MakeStarRule(rulelist, rule);

            base.Root           = rulelist;
            base.LanguageFlags |= LanguageFlags.CanRunSample;
        }
示例#29
0
 protected static BnfTerm WithPlus(BnfExpression expression)
 {
     return ToElement(expression).Plus();
 }
示例#30
0
 private NonTerminal _(string name, BnfExpression rule)
 {
     return new NonTerminal(name) {Rule = rule};
 }
示例#31
0
文件: Pattern.cs 项目: msallin/irony
 public Pattern(string name, BnfExpression rule = null) : base(name)  {
   Rule = rule; 
 }
示例#32
0
 private void _(NonTerminal nonTerminal, BnfExpression rule)
 {
     nonTerminal.Rule = rule;
 }
示例#33
0
文件: GraphCLI.cs 项目: TheByte/sones
        private void BuildGraphCLIGrammar()
        {

            #region global grammar options

            GraphCLIGrammar.CaseSensitive = false;

            NonTerminal _Command = new NonTerminal("Command");

            List<ABnfTerm> _SubtreeCommandGrammarRoot = new List<ABnfTerm>();

            #endregion

            #region get command specific grammars

            foreach (KeyValuePair<String, AllCLICommands> _aCommand in Commands)
            {
                //TODO: check if the grammar is valid. if not --> handle error
                Grammar _aCommandGrammar = _aCommand.Value.CommandGrammar;
                LanguageCompiler _testCompiler = new LanguageCompiler(_aCommandGrammar);
                Boolean isGrammarValid = true;

                foreach (String aErrorString in _testCompiler.Grammar.Errors)
                {
                    if (aErrorString.StartsWith("Error"))
                    {
                        WriteLine(aErrorString);
                        isGrammarValid = false;
                    }
                }

                if (isGrammarValid)
                {

                    GraphCLIGrammar.NonTerminals.AddRange(_aCommandGrammar.NonTerminals);

                    GraphCLIGrammar.Terminals.AddRange(_aCommandGrammar.Terminals);

                    _SubtreeCommandGrammarRoot.Add(_aCommandGrammar.Root);
                }
                else
                {
                    WriteLine("ERROR! The command \"{0}\" is defined by an incorrect BNF grammar. It is not integrated into the GraphCLI!");

                }
            }

            #endregion

            #region build new root

            for (int i = 0; i < _SubtreeCommandGrammarRoot.Count; i++)
            {
                BnfExpression _exprNew = new BnfExpression(_SubtreeCommandGrammarRoot[i]);

                if (!i.Equals(0))
                {
                    BnfExpression _exprActual = new BnfExpression(_Command.Rule);
                    
                    _Command.Rule = _exprActual | _exprNew;
                }
                else
                {
                    _Command.Rule = _exprNew;
                }
            }
 
            GraphCLIGrammar.NonTerminals.Add(_Command);

            GraphCLIGrammar.Root = _Command;

            #endregion

        }
示例#34
0
 public static BnfExpression Q(this BnfExpression bnf, string name)
 {
     bnf      = bnf.Q();
     bnf.Name = name;
     return(bnf);
 }