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 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); }