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


C# ParserState.SetParameters方法代码示例

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


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

示例1: TestParseErrorGetsReported

        public void TestParseErrorGetsReported()
        {
            // Create an instance of our language grammar and set handlers for its rules.
            LanguageGrammar<Expression> g = new LanguageGrammar<Expression>();

            g.DivideRule.Action = (e1, e2) => Expression.Divide(e1, e2);
            g.DoubleLiteralRule.Action = (d) => Expression.Constant(d);
            g.IntLiteralRule.Action = (i) => Expression.Constant(i);
            g.StringLiteralRule.Action = (s) => Expression.Constant(s);
            g.MinusRule.Action = (e1, e2) => Expression.Subtract(e1, e2);
            g.ModRule.Action = (e1, e2) => Expression.Modulo(e1, e2);
            g.MultiplyRule.Action = (e1, e2) => Expression.Multiply(e1, e2);
            g.NegateRule.Action = (e) => Expression.Negate(e);
            g.PlusRule.Action = (e1, e2) => Expression.Add(e1, e2);
            g.VariableRefRule.SetStatefulAction<ParserState>((s, name) => s.GetVariable(name));

            // Spin up a parser for our language.
            // TODO: Package this up and simplify it.
            var expressionHelper = new ExpressionHelper();
            var classifierSession = new TerminalClassifierSession<char, ParserState, int>()
                .AddSkipTerminal(new Terminal<char> { Name = "Whitespace", InitialState = RegexCharNFABuilder.RegexCompiler(@"\s+") })
                .CurrentCharExprIs(x => x.CurrentChar())
                .GetFromMarkExprIs(x => x.GetFromMarkedPos())
                .HasCurrentCharExprIs(x => x.HasCurrentChar())
                .MarkPosExprIs(x => x.MarkPos())
                .MoveNextCharExprIs(x => x.MoveNextChar())
                .GetLocationIs(x=>new ParseLocation {Line = 1, Column = x.GetPos()})
                .ErrorCollectionIs(x=>x.Errors)
                .UnmarkPosExprIs(x => x.UnmarkPos());

            var parserGen = new ParserGenerator<char>(expressionHelper);
            var parseTableBuilder = new LRParseTableBuilder();
            var parseTable = parseTableBuilder.BuildParseTable(g);
            var session = parserGen.NewSession<ParserState>()
                .NonTerminalValueExprIs<object>(x => x.NonTerminalValue)
                .TerminalValueExprIs<string>(x => x.TerminalStringValue)
                .TerminalValueExprIs<int>(x => x.TerminalIntValue)
                .TerminalValueExprIs<double>(x => x.TerminalDoubleValue)
                .TerminalIs(x => x.Terminal)
                .NonTerminalIs(x => x.NonTerminal)
                .IncludeSymbols(true)
                .DebugOutputIs(x => Debug.WriteLine(x))
                .Generate("LanguageParser", parseTable, classifierSession);
            // At this point, session is an Expression<ParserState,int> representing our parser.
            // We can compile it into a delegate or a MethodBuilder.  For the examples, we'll use a delegate.
            var compiler = session.Compile();

            // Create a parser state object and initialize it.
            ParserState ps = new ParserState("x*y +* 2.0");
            ps.SetParameters(
                Expression.Parameter(typeof(double), "x"),
                Expression.Parameter(typeof(double), "y"));
            Assert.AreNotEqual(0, compiler(ps));
            Assert.AreNotEqual(0, ps.Errors.Count);
            Assert.Less(ps.Errors.First().ExpectedTerminalNames.Count, g.Terminals.Count);
        }
开发者ID:kennethuil,项目名称:LINQParserGenerator,代码行数:56,代码来源:GrammarTests.cs


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