本文整理汇总了C#中Grammar.GetNonTerminals方法的典型用法代码示例。如果您正苦于以下问题:C# Grammar.GetNonTerminals方法的具体用法?C# Grammar.GetNonTerminals怎么用?C# Grammar.GetNonTerminals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Grammar
的用法示例。
在下文中一共展示了Grammar.GetNonTerminals方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}