private string[] ParseKeyedValues(VPPTextParser parser, string key) { parser.SetPosition(0); int defIndex; List <string> ret = new(); while ((defIndex = parser.Seek(key)) != -1) { parser.Eat(key); parser.EatWhiteSpace(); if (parser.Is('\"')) { continue; } int pstart = parser.Eat('<'); int end = parser.EatUntil('>'); parser.SetPosition(pstart + 1); ret.Add(parser.Get(end - pstart - 1)); parser.Set(pstart, '\"'); parser.Set(end, '\"'); } return(ret.ToArray()); }
private static bool ResolveDirective(VPPDirective directive, List <VPPMakro> makros, VPPTextParser parser) { parser.SetPosition(0); int idx; bool resolved = false; while ((idx = parser.Seek(directive.DirectiveName)) != -1) { parser.Eat(directive.DirectiveName); if (!parser.IsValidPreWordCharacter(idx - 1) || !parser.IsValidPostWordCharacter(idx + directive.DirectiveName.Length)) { continue; } parser.EatWhiteSpace(); if (parser.Is('(')) { parser.Eat('('); List <string> p = ParseList(parser, x => x.Is(')')); int end = parser.Eat(')'); parser.SetPosition(idx); parser.Remove(end + 1 - idx); if (directive.ResolveParameters) { for (int i = 0; i < p.Count; i++) { VPPTextParser arg = new VPPTextParser(p[i]); while (ResolveMakros(arg, makros)) { } p[i] = arg.ToString(); } } string resolvedString = directive.Resolve(makros, parser, p.ToArray()); parser.Insert(resolvedString); } else { parser.SetPosition(idx); parser.Remove(directive.DirectiveName.Length); parser.Insert(directive.Resolve(makros, parser, new string[0])); } resolved = true; } return(resolved); }
private static bool ResolveMakro(VPPMakro makro, VPPTextParser parser) { parser.SetPosition(0); int idx; bool resolved = false; while ((idx = parser.Seek(makro.Name)) != -1) { parser.Eat(makro.Name); if (!parser.IsValidPreWordCharacter(idx - 1) || !parser.IsValidPostWordCharacter(idx + makro.Name.Length)) { continue; } parser.EatWhiteSpace(); if (parser.Is('(')) { parser.Eat('('); List <string> p = ParseList(parser, x => x.Is(')')); int end = parser.Eat(')'); parser.SetPosition(idx); parser.Remove(end + 1 - idx); parser.Insert(makro.GenerateValue(p.ToArray())); } else { parser.SetPosition(idx); parser.Remove(makro.Name.Length); parser.Insert(makro.GenerateValue(new string[0])); } resolved = true; } return(resolved); }
private List <VPPMakro> ParseDefines(VPPTextParser parser) { int defIndex; List <VPPMakro> ret = new(); while ((defIndex = parser.Seek("#define")) != -1) { parser.Eat("#define"); parser.EatWhiteSpace(); string var = parser.EatWord(); parser.EatWhiteSpaceUntilNewLine(); if (parser.Is('(')) { ret.Add(ParseFuncDefine(defIndex, var, parser)); continue; } string value = "1"; if (parser.Is('\n')) { parser.Eat('\n'); parser.RemoveReverse(defIndex); } else { value = parser.EatUntilWhitespace(); parser.RemoveReverse(defIndex); } ret.Add( new VPPTextMakro(var, value, new List <VPPMakroParameter>()) ); } return(ret); }
private VPPMakro ParseFuncDefine(int start, string var, VPPTextParser parser) { parser.Eat('('); List <string> p = ParseList(parser, x => x.Is(')')); parser.Eat(')'); parser.EatWhiteSpace(); int pStart = parser.Eat('{'); int end = parser.FindClosing('{', '}'); parser.SetPosition(pStart + 1); string block = parser.Get(end - pStart - 1).Trim(); parser.SetPosition(start); parser.Remove(end + 1 - start); return(new VPPTextMakro(var, block, p.Select(x => new VPPMakroParameter { Name = x }).ToList())); }
private void ApplyParameter(VPPTextParser parser, VPPMakroParameter p, string v) { parser.SetPosition(0); int idx; while ((idx = parser.Seek(p.Name)) != -1) { if (parser.IsValidPreWordCharacter(idx - 1) && parser.IsValidPostWordCharacter(idx + p.Name.Length)) { parser.Remove(p.Name.Length); parser.Insert(v); } else { parser.Eat(); } } }
private static List <string> ParseList(VPPTextParser parser, Func <VPPTextParser, bool> isEnd) { List <string> p = new List <string>(); while (true) { if (isEnd(parser)) { break; } p.Add(parser.EatAnyUntil(isEnd, x => x.Is(','))); if (isEnd(parser)) { break; } parser.Eat(','); parser.EatWhiteSpace(); } return(p); }