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


C# Grammar.Preprocess方法代码示例

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


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

示例1: Compile

        public void Compile(Grammar grammar)
        {
            IsParsed = false;
            IsCompiled = false;
            Errors = new List<string>();
            if (grammar == null) throw new ArgumentNullException("grammar", "Grammar may not be null");

            Grammar = grammar;
            grammar.Preprocess();
            IsParsed = true;

            BuildCode();
            if (Errors.Count == 0)
                IsCompiled = true;
        }
开发者ID:hvacengi,项目名称:TinyPG,代码行数:15,代码来源:Compiler.cs

示例2: CompileGrammar

        private void CompileGrammar()
        {
            DateTime starttimer = DateTime.Now;

            if (string.IsNullOrEmpty(GrammarFile))
                SaveGrammarAs();

            if (string.IsNullOrEmpty(GrammarFile))
                return;

            compiler = new TinyPG.Compiler.Compiler();
            StringBuilder output = new StringBuilder();

            // clear tree
            tvParsetree.Nodes.Clear();

            string input = textEditor.Text;
            Scanner scanner = new Scanner();
            Parser parser = new Parser(scanner);
            ParseTree tree = parser.Parse(input, GrammarFile, new GrammarTree());

            if (tree.Errors.Count > 0)
            {
                foreach (ParseError error in tree.Errors)
                    output.AppendLine(string.Format("({0},{1}): {2}", error.Line, error.Column, error.Message));
                output.AppendLine("Syntax errors in grammar found.");

                if (tree.Errors.Count > 0)
                    textEditor.Select(tree.Errors[0].Position, tree.Errors[0].Length > 0 ? tree.Errors[0].Length : 1);
            }
            else
            {

                grammar = (Grammar)tree.Eval();
                grammar.Preprocess();

                if (tree.Errors.Count == 0)
                {
                    output.AppendLine(grammar.PrintGrammar());
                    output.AppendLine(grammar.PrintFirsts());

                    output.AppendLine("Parse successful!\r\n");
                }
            }

            if (grammar != null)
            {
                SetHighlighterLanguage(grammar.Directives["TinyPG"]["Language"]);
                if (tree.Errors.Count > 0)
                {
                    foreach (ParseError error in tree.Errors)
                        output.AppendLine(string.Format("({0},{1}): {2}", error.Line, error.Column, error.Message));
                    output.AppendLine("Semantic errors in grammar found.");
                    if (tree.Errors.Count > 0)
                        textEditor.Select(tree.Errors[0].Position, tree.Errors[0].Length > 0 ? tree.Errors[0].Length : 1);

                }
                else
                {
                    output.AppendLine("Building code...");
                    compiler.Compile(grammar);
                    if (!compiler.IsCompiled)
                    {
                        foreach (string err in compiler.Errors)
                            output.AppendLine(err);
                        output.AppendLine("Compilation contains errors, could not compile.");
                    }
                    else
                    {
                        TimeSpan span = DateTime.Now.Subtract(starttimer);
                        output.AppendLine("Compilation successfull in " + span.TotalMilliseconds + "ms.");

                    }
                }
            }
            textOutput.Text = output.ToString();
            textOutput.Select(textOutput.Text.Length, 0);
            textOutput.ScrollToCaret();

            if (grammar != null && tree.Errors.Count == 0)
            {
                ICodeGenerator generator;

                string language = grammar.Directives["TinyPG"]["Language"];
                foreach (Directive d in grammar.Directives)
                {
                    generator = CodeGeneratorFactory.CreateGenerator(d.Name, language);
                    if (generator != null && d.ContainsKey("FileName"))
                        generator.FileName = d["FileName"];

                    if (generator != null && d["Generate"].ToLower() == "true")
                        File.WriteAllText(grammar.GetOutputPath() + generator.FileName, generator.Generate(grammar, false));
                }
            }
        }
开发者ID:jj-jabb,项目名称:TinyPG,代码行数:95,代码来源:MainForm.cs


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