private void SetupDirectives() { Directive d = Directives.Find("TinyPG"); if (d == null) { d = new Directive("TinyPG"); Directives.Insert(0, d); } if (!d.ContainsKey("Namespace")) { d["Namespace"] = "TinyPG"; // set default namespace } if (!d.ContainsKey("OutputPath")) { d["OutputPath"] = "./"; // write files to current path } if (!d.ContainsKey("Language")) { d["Language"] = "C#"; // set default language } if (!d.ContainsKey("TemplatePath")) { switch (d["Language"].ToLower(CultureInfo.InvariantCulture)) { // set the default templates directory case "visualbasic": case "vbnet": case "vb.net": case "vb": d["TemplatePath"] = AppDomain.CurrentDomain.BaseDirectory + @"Templates\VB\"; break; default: d["TemplatePath"] = AppDomain.CurrentDomain.BaseDirectory + @"Templates\C#\"; break; } } d = Directives.Find("Parser"); if (d == null) { d = new Directive("Parser"); Directives.Insert(1, d); } if (!d.ContainsKey("Generate")) { d["Generate"] = "True"; // generate parser by default } d = Directives.Find("Scanner"); if (d == null) { d = new Directive("Scanner"); Directives.Insert(1, d); } if (!d.ContainsKey("Generate")) { d["Generate"] = "True"; // generate scanner by default } d = Directives.Find("ParseTree"); if (d == null) { d = new Directive("ParseTree"); Directives.Add(d); } if (!d.ContainsKey("Generate")) { d["Generate"] = "True"; // generate parsetree by default } d = Directives.Find("TextHighlighter"); if (d == null) { d = new Directive("TextHighlighter"); Directives.Add(d); } if (!d.ContainsKey("Generate")) { d["Generate"] = "False"; // do NOT generate a text highlighter by default } }
protected override object EvalNameValue(ParseTree tree, params object[] paramlist) { Grammar grammer = (Grammar)paramlist[0]; Directive directive = (Directive)paramlist[1]; GrammarNode node = (GrammarNode)paramlist[2]; string key = node.Nodes[0].Token.Text; string value = node.Nodes[2].Token.Text.Substring(1, node.Nodes[2].Token.Text.Length - 2); if (value.StartsWith("\"")) { value = value.Substring(1); } directive[key] = value; List <string> names = new List <string>(new string[] { "Namespace", "OutputPath", "TemplatePath", }); List <string> languages = new List <string>(new string[] { "c#", "cs", "csharp", "vb", "vb.net", "vbnet", "visualbasic" }); switch (directive.Name) { case "TinyPG": names.Add("Namespace"); names.Add("OutputPath"); names.Add("TemplatePath"); names.Add("Language"); if (key == "TemplatePath") { if (grammer.GetTemplatePath() == null) { tree.Errors.Add(new ParseError("Template path '" + value + "' does not exist", 0x1060, node.Nodes[2])); } } if (key == "OutputPath") { if (grammer.GetOutputPath() == null) { tree.Errors.Add(new ParseError("Output path '" + value + "' does not exist", 0x1061, node.Nodes[2])); } } if (key == "Language") { if (!languages.Contains(value.ToLower(CultureInfo.InvariantCulture))) { tree.Errors.Add(new ParseError("Language '" + value + "' is not supported", 0x1062, node.Nodes[2])); } } break; case "Parser": case "Scanner": case "ParseTree": case "TextHighlighter": names.Add("Generate"); names.Add("FileName"); break; default: return(null); } if (!names.Contains(key)) { tree.Errors.Add(new ParseError("Directive attribute '" + key + "' is not supported", 0x1034, node.Nodes[0])); } return(null); }