本文整理汇总了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;
}
示例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));
}
}
}