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


C# Grammar.GetTerminals方法代码示例

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


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

示例1: Generate

        public string Generate(Grammar Grammar, bool Debug)
        {
            if (string.IsNullOrEmpty(Grammar.GetTemplatePath()))
                return null;

            string scanner = File.ReadAllText(Grammar.GetTemplatePath() + templateName);

            int counter = 2;
            StringBuilder tokentype = new StringBuilder();
            StringBuilder regexps = new StringBuilder();
            StringBuilder skiplist = new StringBuilder();

            foreach (TerminalSymbol s in Grammar.SkipSymbols)
            {
                skiplist.AppendLine("            SkipList.Add(TokenType." + s.Name + ")");
            }

            // build system tokens
            tokentype.AppendLine("\r\n        'Non terminal tokens:");
            tokentype.AppendLine(Helper.Outline("_NONE_", 2, "= 0", 5));
            tokentype.AppendLine(Helper.Outline("_UNDETERMINED_", 2, "= 1", 5));

            // build non terminal tokens
            tokentype.AppendLine("\r\n        'Non terminal tokens:");
            foreach (Symbol s in Grammar.GetNonTerminals())
            {
                tokentype.AppendLine(Helper.Outline(s.Name, 2, "= " + String.Format("{0:d}", counter), 5));
                counter++;
            }

            // build terminal tokens
            tokentype.AppendLine("\r\n        'Terminal tokens:");
            bool first = true;
            foreach (TerminalSymbol s in Grammar.GetTerminals())
            {
                string vbexpr = s.Expression.ToString();
                if (vbexpr.StartsWith("@"))
                    vbexpr = vbexpr.Substring(1);
                regexps.Append("            regex = new Regex(" + vbexpr + ", RegexOptions.Compiled)\r\n");
                regexps.Append("            Patterns.Add(TokenType." + s.Name + ", regex)\r\n");
                regexps.Append("            Tokens.Add(TokenType." + s.Name + ")\r\n\r\n");

                if (first) first = false;
                else tokentype.AppendLine("");

                tokentype.Append(Helper.Outline(s.Name, 2, "= " + String.Format("{0:d}", counter), 5));
                counter++;
            }

            scanner = scanner.Replace(@"<%SkipList%>", skiplist.ToString());
            scanner = scanner.Replace(@"<%RegExps%>", regexps.ToString());
            scanner = scanner.Replace(@"<%TokenType%>", tokentype.ToString());

            if (Debug)
            {
                scanner = scanner.Replace(@"<%Imports%>", "Imports TinyPG.Debug");
                scanner = scanner.Replace(@"<%Namespace%>", "TinyPG.Debug");
                scanner = scanner.Replace(@"<%IToken%>", "\r\n        Implements IToken");
                scanner = scanner.Replace(@"<%ImplementsITokenStartPos%>", " Implements IToken.StartPos");
                scanner = scanner.Replace(@"<%ImplementsITokenEndPos%>", " Implements IToken.EndPos");
                scanner = scanner.Replace(@"<%ImplementsITokenLength%>", " Implements IToken.Length");
                scanner = scanner.Replace(@"<%ImplementsITokenText%>", " Implements IToken.Text");
                scanner = scanner.Replace(@"<%ImplementsITokenToString%>", " Implements IToken.ToString");

            }
            else
            {
                scanner = scanner.Replace(@"<%Imports%>", "");
                scanner = scanner.Replace(@"<%Namespace%>", Grammar.Directives["TinyPG"]["Namespace"]);
                scanner = scanner.Replace(@"<%IToken%>", "");
                scanner = scanner.Replace(@"<%ImplementsITokenStartPos%>", "");
                scanner = scanner.Replace(@"<%ImplementsITokenEndPos%>", "");
                scanner = scanner.Replace(@"<%ImplementsITokenLength%>", "");
                scanner = scanner.Replace(@"<%ImplementsITokenText%>", "");
                scanner = scanner.Replace(@"<%ImplementsITokenToString%>", "");
            }

            return scanner;
        }
开发者ID:hvacengi,项目名称:TinyPG,代码行数:79,代码来源:ScannerGenerator.cs

示例2: Generate

        public string Generate(Grammar Grammar, bool Debug)
        {
            if (string.IsNullOrEmpty(Grammar.GetTemplatePath()))
                return null;

            string generatedtext = File.ReadAllText(Grammar.GetTemplatePath() + templateName);
            StringBuilder tokens = new StringBuilder();
            StringBuilder colors = new StringBuilder();

            int colorindex = 1;
            foreach (TerminalSymbol t in Grammar.GetTerminals())
            {
                if (!t.Attributes.ContainsKey("Color"))
                    continue;

                tokens.AppendLine(Helper.Indent(5) + "case TokenType." + t.Name + ":");
                tokens.AppendLine(Helper.Indent(6) + @"sb.Append(@""{{\cf" + colorindex + @" "");");
                tokens.AppendLine(Helper.Indent(6) + "break;");

                int red = 0;
                int green = 0;
                int blue = 0;
                int len = t.Attributes["Color"].Length;
                if (len == 1)
                {
                    if (t.Attributes["Color"][0] is long)
                    {
                        int v = Convert.ToInt32(t.Attributes["Color"][0]);
                        red = (v >> 16) & 255;
                        green = (v >> 8) & 255;
                        blue = v & 255;
                    }
                }
                else if (len == 3)
                {
                    if (t.Attributes["Color"][0] is int || t.Attributes["Color"][0] is long)
                        red = Convert.ToInt32(t.Attributes["Color"][0]) & 255;
                    if (t.Attributes["Color"][1] is int || t.Attributes["Color"][1] is long)
                        green = Convert.ToInt32(t.Attributes["Color"][1]) & 255;
                    if (t.Attributes["Color"][2] is int || t.Attributes["Color"][2] is long)
                        blue = Convert.ToInt32(t.Attributes["Color"][2]) & 255;
                }

                colors.Append(String.Format(@"\red{0}\green{1}\blue{2};", red, green, blue));
                colorindex++;
            }

            generatedtext = generatedtext.Replace(@"<%HightlightTokens%>", tokens.ToString());
            generatedtext = generatedtext.Replace(@"<%RtfColorPalette%>", colors.ToString());

            if (Debug)
            {
                generatedtext = generatedtext.Replace(@"<%Namespace%>", "TinyPG.Debug");
            }
            else
            {
                generatedtext = generatedtext.Replace(@"<%Namespace%>", Grammar.Directives["TinyPG"]["Namespace"]);
            }

            return generatedtext;
        }
开发者ID:hvacengi,项目名称:TinyPG,代码行数:61,代码来源:TextHighlighterGenerator.cs


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