当前位置: 首页>>代码示例>>C#>>正文


C# Grammar.GetRule方法代码示例

本文整理汇总了C#中Antlr3.Tool.Grammar.GetRule方法的典型用法代码示例。如果您正苦于以下问题:C# Grammar.GetRule方法的具体用法?C# Grammar.GetRule怎么用?C# Grammar.GetRule使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Antlr3.Tool.Grammar的用法示例。


在下文中一共展示了Grammar.GetRule方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestBracketArgParsing

        public void TestBracketArgParsing()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            // now check in actual grammar.
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "a[String[\\] ick, int i]\n" +
                "        : A \n" +
                "        ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            Rule r = g.GetRule( "a" );
            AttributeScope parameters = r.ParameterScope;
            var attrs = parameters.Attributes;
            Assert.AreEqual("String[] ick", attrs.ElementAt(0).Decl.ToString(), "attribute mismatch");
            Assert.AreEqual("ick", attrs.ElementAt(0).Name, "parameter name mismatch");
            Assert.AreEqual("String[]", attrs.ElementAt(0).Type, "declarator mismatch");

            Assert.AreEqual("int i", attrs.ElementAt(1).Decl.ToString(), "attribute mismatch");
            Assert.AreEqual("i", attrs.ElementAt(1).Name, "parameter name mismatch");
            Assert.AreEqual("int", attrs.ElementAt(1).Type, "declarator mismatch");

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
开发者ID:JSchofield,项目名称:antlrcs,代码行数:28,代码来源:TestAttributes.cs

示例2: TestCStyleReturnInitValue

        public void TestCStyleReturnInitValue()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : r ;\n" +
                "r returns [int (*x)()=NULL] : 'a' ;\n" );
            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);

            Rule r = g.GetRule( "r" );
            AttributeScope retScope = r.ReturnScope;
            var parameters = retScope.Attributes;
            Assert.IsNotNull(parameters, "missing return action");
            Assert.AreEqual( 1, parameters.Count );
            string found = parameters.ElementAt( 0 ).ToString();
            string expecting = "int (*)() x=NULL";
            Assert.AreEqual( expecting, found );
        }
开发者ID:JSchofield,项目名称:antlrcs,代码行数:19,代码来源:TestAttributes.cs

示例3: TestGenericsAsReturnValue

        public void TestGenericsAsReturnValue()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            string grammar =
                "parser grammar T;\n" +
                "a returns [HashMap<String,String> foo] : ;\n";
            Grammar g = new Grammar( grammar );
            Rule ra = g.GetRule( "a" );
            var attrs = ra.ReturnScope.Attributes;
            Assert.AreEqual("HashMap<String,String> foo", attrs.ElementAt(0).Decl.ToString(), "attribute mismatch");
            Assert.AreEqual("foo", attrs.ElementAt(0).Name, "parameter name mismatch");
            Assert.AreEqual("HashMap<String,String>", attrs.ElementAt(0).Type, "declarator mismatch");

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
开发者ID:JSchofield,项目名称:antlrcs,代码行数:16,代码来源:TestAttributes.cs

示例4: TestMultipleReturnInitValue

        public void TestMultipleReturnInitValue()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : r ;\n" +
                "r returns [int x=0, int y, String s=new String(\"foo\")] : 'a' {$x = 4;} ;\n" );
            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);

            Rule r = g.GetRule( "r" );
            AttributeScope retScope = r.ReturnScope;
            var parameters = retScope.Attributes;
            Assert.IsNotNull(parameters, "missing return action");
            Assert.AreEqual( 3, parameters.Count );
            Assert.AreEqual( "int x=0", parameters.ElementAt( 0 ).ToString() );
            Assert.AreEqual( "int y", parameters.ElementAt( 1 ).ToString() );
            Assert.AreEqual( "String s=new String(\"foo\")", parameters.ElementAt( 2 ).ToString() );
        }
开发者ID:JSchofield,项目名称:antlrcs,代码行数:19,代码来源:TestAttributes.cs

示例5: TestGenericsAsArgumentDefinition2

        public void TestGenericsAsArgumentDefinition2()
        {
            string action = "$foo.get(\"ick\"); x=3;";
            string expecting = "foo.get(\"ick\"); x=3;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            string grammar =
                "parser grammar T;\n" +
                "a[HashMap<String,String> foo, int x, List<String> duh]\n" +
                "        : {" + action + "}\n" +
                "        ;";
            Grammar g = new Grammar( grammar );
            Rule ra = g.GetRule( "a" );
            var attrs = ra.ParameterScope.Attributes;

            Assert.AreEqual("HashMap<String,String> foo", attrs.ElementAt(0).Decl.ToString().Trim(), "attribute mismatch");
            Assert.AreEqual("foo", attrs.ElementAt(0).Name, "parameter name mismatch");
            Assert.AreEqual("HashMap<String,String>", attrs.ElementAt(0).Type, "declarator mismatch");

            Assert.AreEqual("int x", attrs.ElementAt(1).Decl.ToString().Trim(), "attribute mismatch");
            Assert.AreEqual("x", attrs.ElementAt(1).Name, "parameter name mismatch");
            Assert.AreEqual("int", attrs.ElementAt(1).Type, "declarator mismatch");

            Assert.AreEqual("List<String> duh", attrs.ElementAt(2).Decl.ToString().Trim(), "attribute mismatch");
            Assert.AreEqual("duh", attrs.ElementAt(2).Name, "parameter name mismatch");
            Assert.AreEqual("List<String>", attrs.ElementAt(2).Type, "declarator mismatch");

            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
开发者ID:JSchofield,项目名称:antlrcs,代码行数:39,代码来源:TestAttributes.cs

示例6: TestReturnInitValue

        public void TestReturnInitValue()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : r ;\n" +
                "r returns [int x=0] : 'a' {$x = 4;} ;\n" );
            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );

            Rule r = g.GetRule( "r" );
            AttributeScope retScope = r.returnScope;
            var parameters = retScope.Attributes;
            assertNotNull( "missing return action", parameters );
            assertEquals( 1, parameters.Count );
            string found = parameters.ElementAt( 0 ).ToString();
            string expecting = "int x=0";
            assertEquals( expecting, found );
        }
开发者ID:bszafko,项目名称:antlrcs,代码行数:19,代码来源:TestAttributes.cs

示例7: TestGenericsAsArgumentDefinition

        public void TestGenericsAsArgumentDefinition()
        {
            string action = "$foo.get(\"ick\");";
            string expecting = "foo.get(\"ick\");";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            string grammar =
                "parser grammar T;\n" +
                "a[HashMap<String,String> foo]\n" +
                "        : {" + action + "}\n" +
                "        ;";
            Grammar g = new Grammar( grammar );
            Rule ra = g.GetRule( "a" );
            var attrs = ra.parameterScope.Attributes;
            assertEquals( "attribute mismatch", "HashMap<String,String> foo", attrs.ElementAt( 0 ).Decl.ToString() );
            assertEquals( "parameter name mismatch", "foo", attrs.ElementAt( 0 ).Name );
            assertEquals( "declarator mismatch", "HashMap<String,String>", attrs.ElementAt( 0 ).Type );

            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) );
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.ToString();
            assertEquals( expecting, found );

            assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
        }
开发者ID:bszafko,项目名称:antlrcs,代码行数:35,代码来源:TestAttributes.cs

示例8: checkSymbols

        //throws Exception
        protected void checkSymbols( Grammar g,
                                    string rulesStr,
                                    string tokensStr )
        {
            var tokens = g.GetTokenDisplayNames();

            // make sure expected tokens are there
            //StringTokenizer st = new StringTokenizer( tokensStr, ", " );
            //while ( st.hasMoreTokens() )
            foreach ( string tokenName in tokensStr.Split( new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries ) )
            {
                //String tokenName = st.nextToken();
                Assert.IsTrue(g.GetTokenType(tokenName) != Label.INVALID, "token " + tokenName + " expected");
                tokens.Remove( tokenName );
            }
            // make sure there are not any others (other than <EOF> etc...)
            foreach ( string tokenName in tokens )
            {
                Assert.IsTrue( g.GetTokenType( tokenName ) < Label.MIN_TOKEN_TYPE, "unexpected token name " + tokenName );
            }

            // make sure all expected rules are there
            //st = new StringTokenizer( rulesStr, ", " );
            int n = 0;
            //while ( st.hasMoreTokens() )
            foreach ( string ruleName in rulesStr.Split( new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries ) )
            {
                //String ruleName = st.nextToken();
                Assert.IsNotNull(g.GetRule(ruleName), "rule " + ruleName + " expected");
                n++;
            }
            var rules = g.Rules;
            //System.out.println("rules="+rules);
            // make sure there are no extra rules
            Assert.AreEqual(n, rules.Count, "number of rules mismatch; expecting " + n + "; found " + rules.Count);
        }
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:37,代码来源:TestSymbolDefinitions.cs

示例9: checkPlusEqualsLabels

 //throws Exception
 protected void checkPlusEqualsLabels( Grammar g,
                                      string ruleName,
                                      string tokenLabelsStr,
                                      string ruleLabelsStr )
 {
     // make sure expected += labels are there
     Rule r = g.GetRule( ruleName );
     StringTokenizer st = new StringTokenizer( tokenLabelsStr, ", " );
     ICollection<string> tokenLabels = null;
     while ( st.hasMoreTokens() )
     {
         if ( tokenLabels == null )
         {
             tokenLabels = new List<string>();
         }
         string labelName = st.nextToken();
         tokenLabels.Add( labelName );
     }
     ICollection<string> ruleLabels = null;
     if ( ruleLabelsStr != null )
     {
         st = new StringTokenizer( ruleLabelsStr, ", " );
         ruleLabels = new List<string>();
         while ( st.hasMoreTokens() )
         {
             string labelName = st.nextToken();
             ruleLabels.Add( labelName );
         }
     }
     Assert.IsTrue((tokenLabels != null && r.TokenListLabels != null) ||
                (tokenLabels == null && r.TokenListLabels == null),
                "token += labels mismatch; " + tokenLabels + "!=" + r.TokenListLabels);
     Assert.IsTrue((ruleLabels != null && r.RuleListLabels != null) ||
                (ruleLabels == null && r.RuleListLabels == null),
                "rule += labels mismatch; " + ruleLabels + "!=" + r.RuleListLabels);
     if ( tokenLabels != null )
     {
         Assert.IsTrue( tokenLabels.SequenceEqual( r.TokenListLabels.Keys ) );
     }
     if ( ruleLabels != null )
     {
         Assert.IsTrue( ruleLabels.SequenceEqual( r.RuleListLabels.Keys ) );
     }
 }
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:45,代码来源:TestSymbolDefinitions.cs


注:本文中的Antlr3.Tool.Grammar.GetRule方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。