public static Proto Parse(string path) { Proto p = new Proto (); string t = ""; using (TextReader reader = new StreamReader(path, Encoding.UTF8)) { while (true) { string line = reader.ReadLine (); if (line == null) break; t += line + "\n"; } } TokenReader tr = new TokenReader (t); ProtoFormatException e = null; try { ParseMessages (tr, p); return p; } catch (ProtoFormatException pfe) { e = pfe; } Console.Write (tr.Parsed); Console.WriteLine (" <---"); Console.WriteLine (e.Message); return null; }
public static void Prepare(Proto proto) { foreach (Message m in proto.Messages) PrepareMessage (m); }
/// <summary> /// Generate code for reading and writing protocol buffer messages /// </summary> public static void Save(Proto proto, MessageCode codeGen, string csPath) { string ext = Path.GetExtension (csPath); string prefix = csPath.Substring (0, csPath.Length - ext.Length); //Basic structures using (TextWriter codeWriter = new StreamWriter(csPath, false, Encoding.UTF8)) { codeWriter.WriteLine (@"// // You may customize this code as you like // Report bugs to: https://silentorbit.com/protobuf/ // // Generated by ProtocolBuffer // - a pure c# code generation implementation of protocol buffers // using System; using System.Collections.Generic; "); foreach (Message m in proto.Messages) { codeWriter.WriteLine ("namespace " + m.Namespace); codeWriter.WriteLine ("{"); codeWriter.WriteLine (Code.Indent (1, codeGen.GenerateClass (m))); codeWriter.WriteLine ("}"); } } //.Serializer.cs //Code for Reading/Writing using (TextWriter codeWriter = new StreamWriter(prefix + ".Serializer" + ext, false, Encoding.UTF8)) { codeWriter.WriteLine (@"// // This is the backend code for reading and writing // Report bugs to: https://silentorbit.com/protobuf/ // // Generated by ProtocolBuffer // - a pure c# code generation implementation of protocol buffers // using System; using System.IO; using System.Text; using System.Collections.Generic; using ProtocolBuffers;"); foreach (Message m in proto.Messages) { codeWriter.WriteLine ("namespace " + m.Namespace); codeWriter.WriteLine ("{"); codeWriter.WriteLine (Code.Indent (1, SerializerCode.GenerateClassSerializer (m))); codeWriter.WriteLine ("}"); } codeWriter.WriteLine (@" namespace ProtocolBuffers { public static partial class Serializer {"); foreach (Message m in proto.Messages) codeWriter.WriteLine (Code.Indent (2, SerializerCode.GenerateGenericClassSerializer (m))); codeWriter.WriteLine (@" } }"); } string libPath = Path.Combine (Path.GetDirectoryName (csPath), "ProtocolParser.cs"); using (TextWriter codeWriter = new StreamWriter(libPath, false, Encoding.UTF8)) { ReadCode (codeWriter, "ProtocolParser", true); ReadCode (codeWriter, "ProtocolParserFixed", false); ReadCode (codeWriter, "ProtocolParserKey", false); ReadCode (codeWriter, "ProtocolParserVarInt", false); ReadCode (codeWriter, "ProtocolParserCustom", false); } }
static void ParseMessages(TokenReader tr, Proto p) { while (true) { string token; try { token = tr.ReadNext (); } catch (EndOfStreamException) { return; } if (ParseComment (token)) continue; switch (token) { case "message": p.Messages.Add (ParseMessage (tr, p)); break; case "option": //Save options ParseOption (tr, p); break; case "import": //Ignored tr.ReadNext (); tr.ReadNextOrThrow (";"); break; case "package": string pkg = tr.ReadNext (); tr.ReadNextOrThrow (";"); p.OptionNamespace = pkg; break; default: throw new ProtoFormatException ("Unexpected/not implemented: " + token); } } }