private static string[] ReadUntil(StreamReaderParser fe, string cad) { var res = new List<string>(); var c = fe.ReadLineTrim(); while ((c!= null) && !c.StartsWith(cad, StringComparison.CurrentCulture)) { res.Add(c); c = fe.ReadLineTrim(); } return res.ToArray(); }
public ReturnCode Render(string f) { var fe = new StreamReaderParser(f,a=>!a.StartsWith("//", StringComparison.Ordinal)); var fs = new StreamWriter(f.LeftToFar(".pre.cs") + ".tsk.cs",false, Encoding.UTF8); var tokens = new TokenStream<TokenizerCSharp>(fe.ReadLines()); var linetoken= tokens.ReadLine(); while (!linetoken.ContainsToken(TokenKind.ReservedWord,SubKindCSharp.Namespace)) { if (linetoken.ContainsToken(TokenKind.ReservedWord,SubKindCSharp.Using)) { fs.WriteLine(linetoken.Line); } linetoken = tokens.ReadLine(); } if (!linetoken.ContainsToken(TokenKind.ReservedWord, SubKindCSharp.Namespace)) return ReturnCode.Prefile_Error; var name = linetoken.Find(Token.Create(TokenKind.ReservedWord, SubKindCSharp.Namespace)).Next.Value; var tasknamespace = new TaskNamespace(name.Value,new string[] { }); var parent = tasknamespace; fs.WriteLine("namespace "+name.Value); fs.WriteLine("{"); foreach (var cad in PreCode) fs.WriteLine(cad); while (!tokens.EOF) { var line = tokens.ReadLine(); var strtokens = line.Line.Trim(); Switch.On(strtokens.Trim(), (a, b) => a.StartsWith(b, StringComparison.CurrentCulture)) .Case("[TaskClass(").Do(() => { var cad = strtokens.Between("(\"", "\")"); if (!TaskClasses.ContainsKey(cad)) { fs.Write("//Error: Task Clase " + cad + " no encontrada"); } else { var currentclass = TaskClasses[cad]; line = tokens.ReadLine(); currentclass.Render(parent, line, fs); } }) .Case("//!", "using").Do(() => fs.WriteLine(line)) .Case("[TaskMember(").Do(() => { var cad = strtokens.Between("(\"", "\")"); if (!TaskMembers.ContainsKey(cad)) { fs.Write("//Error: Task Member " + cad + " no encontrado"); } else { var currentclass = TaskMembers[cad]; line = tokens.ReadLine(); currentclass.Render(parent, new TokenLine(line), fs); } }); } fs.WriteLine("}"); fs.Close(); return ReturnCode.Ok; }
public TaskFile(string file) { var reader = new StreamReaderParser(file, (a, i) => { switch (i) { case 0: return (a.StartsWith("///<Task>", StringComparison.CurrentCulture)) ? 1 : -1; case 1: if (a.StartsWith("///</Task>", StringComparison.CurrentCulture)) return 3; if (a.StartsWith("///!", StringComparison.CurrentCulture)) return 4; return 2; case 2: return (a.StartsWith("///</Task>", StringComparison.CurrentCulture)) ? 3 : 2; case 4: if (a.StartsWith("///</Task>", StringComparison.CurrentCulture)) return 3; if (a.StartsWith("///!", StringComparison.CurrentCulture)) return 4; return 2; default: return -1; } }, (a, i) => { switch (i) { case 2: return a.Trim().RemoveLeft("///",No).RemoveLeft("//",No); case 4: return a.Trim(); default: return null; } } ); while (!reader.EndOfStream) { var linea = reader.ReadLineTrim(); if (linea == null) break; Switch.OnTrue() .Case(linea.StartsWith("///!", StringComparison.CurrentCulture)).Do(()=>PreCode.Add(linea)) .Case(linea.StartsWith("<TaskClass:", StringComparison.CurrentCulture)).Do(()=> { var c = new TaskClass(linea.Between("<TaskClass:",">"),ReadUntil(reader,"</TaskClass")); if (string.IsNullOrEmpty(c.AttrName)) throw new Exception("Fichero mal formado"); TaskClasses.Add(c.AttrName, c); }) .Case(linea.StartsWith("<TaskMember:", StringComparison.CurrentCulture)).Do(() => { var c = new TaskMember(linea.Between("<TaskMember:", ">"), ReadUntil(reader, "</TaskMember")); if (string.IsNullOrEmpty(c.AttrName)) throw new Exception("Fichero mal formado"); TaskMembers.Add(c.AttrName, c); }) .Default(()=> { throw new Exception("Fichero mal formado"); }); } }