本文整理汇总了C#中Grammar.GetTemplatePath方法的典型用法代码示例。如果您正苦于以下问题:C# Grammar.GetTemplatePath方法的具体用法?C# Grammar.GetTemplatePath怎么用?C# Grammar.GetTemplatePath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Grammar
的用法示例。
在下文中一共展示了Grammar.GetTemplatePath方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Generate
public string Generate(Grammar Grammar, bool Debug)
{
if (string.IsNullOrEmpty(Grammar.GetTemplatePath()))
return null;
// generate the parser file
StringBuilder parsers = new StringBuilder();
string parser = File.ReadAllText(Grammar.GetTemplatePath() + templateName);
// build non terminal tokens
foreach (NonTerminalSymbol s in Grammar.GetNonTerminals())
{
string method = GenerateParseMethod(s);
parsers.Append(method);
}
if (Debug)
{
parser = parser.Replace(@"<%Imports%>", "Imports TinyPG.Debug");
parser = parser.Replace(@"<%Namespace%>", "TinyPG.Debug");
parser = parser.Replace(@"<%IParser%>", "\r\n Implements IParser\r\n");
parser = parser.Replace(@"<%IParseTree%>", "IParseTree");
}
else
{
parser = parser.Replace(@"<%Imports%>", "");
parser = parser.Replace(@"<%Namespace%>", Grammar.Directives["TinyPG"]["Namespace"]);
parser = parser.Replace(@"<%IParser%>", "");
parser = parser.Replace(@"<%IParseTree%>", "ParseTree");
}
parser = parser.Replace(@"<%ParseNonTerminals%>", parsers.ToString());
return parser;
}
示例2: 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 + ");");
}
if (Grammar.FileAndLine != null)
skiplist.AppendLine(" FileAndLine = TokenType." + Grammar.FileAndLine.Name + ";");
// build system tokens
tokentype.AppendLine("\r\n //Non terminal tokens:");
tokentype.AppendLine(Helper.Outline("_NONE_", 3, "= 0,", 5));
tokentype.AppendLine(Helper.Outline("_UNDETERMINED_", 3, "= 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, 3, "= " + 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())
{
regexps.Append(" regex = new Regex(" + s.Expression.ToString() + ");\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, 3, "= " + 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(@"<%Namespace%>", "TinyPG.Debug");
scanner = scanner.Replace(@"<%IToken%>", " : TinyPG.Debug.IToken");
}
else
{
scanner = scanner.Replace(@"<%Namespace%>", Grammar.Directives["TinyPG"]["Namespace"]);
scanner = scanner.Replace(@"<%IToken%>", "");
}
return scanner;
}
示例3: Generate
public string Generate(Grammar Grammar, bool Debug)
{
if (string.IsNullOrEmpty(Grammar.GetTemplatePath()))
return null;
// copy the parse tree file (optionally)
string parsetree = File.ReadAllText(Grammar.GetTemplatePath() + templateName);
StringBuilder evalsymbols = new StringBuilder();
StringBuilder evalmethods = new StringBuilder();
// build non terminal tokens
foreach (Symbol s in Grammar.GetNonTerminals())
{
evalsymbols.AppendLine(" case TokenType." + s.Name + ":");
evalsymbols.AppendLine(" Value = Eval" + s.Name + "(tree, paramlist);");
//evalsymbols.AppendLine(" Value = Token.Text;");
evalsymbols.AppendLine(" break;");
evalmethods.AppendLine(" protected virtual object Eval" + s.Name + "(ParseTree tree, params object[] paramlist)");
evalmethods.AppendLine(" {");
if (s.CodeBlock != null)
{
// paste user code here
evalmethods.AppendLine(FormatCodeBlock(s as NonTerminalSymbol));
}
else
{
if (s.Name == "Start") // return a nice warning message from root object.
evalmethods.AppendLine(" return \"Could not interpret input; no semantics implemented.\";");
else
evalmethods.AppendLine(" foreach (var node in Nodes)\r\n" +
" node.Eval(tree, paramlist);\r\n" +
" return null;");
// otherwise simply not implemented!
}
evalmethods.AppendLine(" }\r\n");
}
if (Debug)
{
parsetree = parsetree.Replace(@"<%Namespace%>", "TinyPG.Debug");
parsetree = parsetree.Replace(@"<%ParseError%>", " : TinyPG.Debug.IParseError");
parsetree = parsetree.Replace(@"<%ParseErrors%>", "List<TinyPG.Debug.IParseError>");
parsetree = parsetree.Replace(@"<%IParseTree%>", ", TinyPG.Debug.IParseTree");
parsetree = parsetree.Replace(@"<%IParseNode%>", " : TinyPG.Debug.IParseNode");
parsetree = parsetree.Replace(@"<%ITokenGet%>", "public IToken IToken { get {return (IToken)Token;} }");
string inodes = "public List<IParseNode> INodes {get { return nodes.ConvertAll<IParseNode>( new Converter<ParseNode, IParseNode>( delegate(ParseNode n) { return (IParseNode)n; })); }}\r\n\r\n";
parsetree = parsetree.Replace(@"<%INodesGet%>", inodes);
}
else
{
parsetree = parsetree.Replace(@"<%Namespace%>", Grammar.Directives["TinyPG"]["Namespace"]);
parsetree = parsetree.Replace(@"<%ParseError%>", "");
parsetree = parsetree.Replace(@"<%ParseErrors%>", "List<ParseError>");
parsetree = parsetree.Replace(@"<%IParseTree%>", "");
parsetree = parsetree.Replace(@"<%IParseNode%>", "");
parsetree = parsetree.Replace(@"<%ITokenGet%>", "");
parsetree = parsetree.Replace(@"<%INodesGet%>", "");
}
parsetree = parsetree.Replace(@"<%EvalSymbols%>", evalsymbols.ToString());
parsetree = parsetree.Replace(@"<%VirtualEvalMethods%>", evalmethods.ToString());
return parsetree;
}
示例4: 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;
}