public void Parse(string code) { DirectiveValues.Clear(); List <string> lines = code.Replace("\r", "").Split(new[] { '\n' }, StringSplitOptions.None).ToList(); // Find all directives for (int c = 0; c < lines.Count; c++) { if (lines[c].StartsWith("#")) { int firstSpace = lines[c].IndexOf(' '); if (firstSpace != -1) { string directive = lines[c].Substring(1, firstSpace - 1); if (_preprocessor.ContainsDirective(directive)) { DirectiveValues.Add(new DirectiveValue(c + 1, directive, lines[c].Substring(firstSpace + 1).Trim())); lines[c] = "//" + lines[c]; // Just comment out the directive so it still shows in script code exports and influences the line number } } } } Code = string.Join(Environment.NewLine, lines); }
public Section(int index, string text, SectionType type, DirectiveValues values) { Index = index; Text = text; Type = type; Values = values; }
private static string ParseDirectives(SectionList list, string page) { // finds directives //<%\s*@\s*(?<directive>[\w]*)[^%]*%> //<%\s*@\s*(?<directive>[\w]*)\s*(?<p1>[\w]*)\s*=(?<v1>[^\s%]*)[^%]*%> // parses the directive and up to 4 name/value pairs //<%\s*@\s*(?<directive>[\w]*)\s*((?<p1>[\w]*)\s*=(?<v1>[^\s%]*))?\s*((?<p2>[\w]*)\s*=(?<v2>[^\s%]*))?\s*((?<p3>[\w]*)\s*=(?<v3>[^\s%]*))?\s*((?<p4>[\w]*)\s*=(?<v4>[^\s%]*))?[^%]*%> StringBuilder result = new StringBuilder(); //Regex reg =new Regex(@"<%\s*@\s*(?<directive>[\w]*)[^%]*%>", RegexOptions.IgnoreCase); Regex reg = new Regex(@"<%\s*@\s*(?<directive>[\w]*)\s*((?<p1>[\w]*)\s*=(?<v1>[^\s%]*))?\s*((?<p2>[\w]*)\s*=(?<v2>[^\s%]*))?\s*((?<p3>[\w]*)\s*=(?<v3>[^\s%]*))?\s*((?<p4>[\w]*)\s*=(?<v4>[^\s%]*))?[^%]*%>", RegexOptions.IgnoreCase); MatchCollection mc = reg.Matches(page); int index = 0; foreach (Match m in mc) { result.Append(page.Substring(index, m.Index - index)); DirectiveValues dvs = new DirectiveValues(m.Groups["directive"].Value.ToLower()); if (m.Groups["p1"].Value != string.Empty) { dvs.Add(m.Groups["p1"].Value.ToLower(), StripQuotes(m.Groups["v1"].Value)); } if (m.Groups["p2"].Value != string.Empty) { dvs.Add(m.Groups["p2"].Value.ToLower(), StripQuotes(m.Groups["v2"].Value)); } if (m.Groups["p3"].Value != string.Empty) { dvs.Add(m.Groups["p3"].Value.ToLower(), StripQuotes(m.Groups["v3"].Value)); } if (m.Groups["p4"].Value != string.Empty) { dvs.Add(m.Groups["p4"].Value.ToLower(), StripQuotes(m.Groups["v4"].Value)); } Section s = new Section(list.Count, page.Substring(m.Index, m.Length), SectionType.Directive, dvs); list.Add(s); index = m.Index + m.Length; } result.Append(page.Substring(index)); return(result.ToString()); }