protected void Generate(string name) { PatternBufferSchemaParser loader = new PatternBufferSchemaParser(); string pbFile = @"..\..\Generation\" + name + @".pb"; string schemaSource = File.ReadAllText(pbFile); PatternBufferSchema schema = loader.Parse(schemaSource); Console.WriteLine(schema.Name); PatternBufferCompiler compiler = new PatternBufferCompiler("Test." + name); Dictionary <string, string> files = compiler.Compile(schema); Console.WriteLine(files.Count + " files generated"); string directory = @"..\..\..\PatternBufferGeneratedTest\" + name + @"\Generated"; if (Directory.Exists(directory)) { Directory.Delete(directory, true); } Directory.CreateDirectory(directory); foreach (string fileName in files.Keys) { Console.WriteLine("writing to " + fileName); File.WriteAllText(directory + @"\" + fileName, files[fileName]); } }
public void TestCommentsToEolWork() { string s = @" // foobara123 90030 name MySchema; // foobarb enum E1 { // here // here E1A, // here E1B // here } // also here // foobarc "; PatternBufferSchema schema = this.Parse(s); Assert.That(schema.Name, Is.EqualTo("MySchema")); Assert.That(schema.Types.Count, Is.EqualTo(0)); Assert.That(schema.Enums.Count, Is.EqualTo(1)); Assert.That(schema.Hints.Count, Is.EqualTo(0)); Assert.That(schema.Enums[0].Name, Is.EqualTo("E1")); Assert.That(schema.Enums[0].Values[0], Is.EqualTo("E1A")); Assert.That(schema.Enums[0].Values[1], Is.EqualTo("E1B")); }
public void TestReferenceFieldWorks() { string s = @" name MySchema; enum MyEnum1 { value1, value2, value3 } type(234) MyObject { MyEnum1 enumValue; } "; s = s.Replace('X', '"'); PatternBufferSchema schema = this.Parse(s); Assert.That(schema.Name, Is.EqualTo("MySchema")); Assert.That(schema.Types.Count, Is.EqualTo(1)); Assert.That(schema.Enums.Count, Is.EqualTo(1)); Assert.That(schema.Hints.Count, Is.EqualTo(0)); PatternBufferType t = schema.Types[0]; Assert.That(t.Name, Is.EqualTo("MyObject")); Assert.That(t.TypeId, Is.EqualTo(234)); Assert.That(t.BaseType, Is.Null); Assert.That(HasReferenceField(t, "enumValue", "MyEnum1"), Is.True); }
public void TestASimpleTypeWorks() { string s = @" // comment name MySchema; type(123) MyType { [foo.bar = XwhateverX] int a; bool b; } "; s = s.Replace('X', '"'); PatternBufferSchema schema = this.Parse(s); Assert.That(schema.Name, Is.EqualTo("MySchema")); Assert.That(schema.Enums.Count, Is.EqualTo(0)); Assert.That(schema.Types.Count, Is.EqualTo(1)); Assert.That(schema.Hints.Count, Is.EqualTo(0)); PatternBufferType t = schema.Types[0]; Assert.That(t.HintCount, Is.EqualTo(1)); Assert.That(t.Name, Is.EqualTo("MyType")); Assert.That(t.TypeId, Is.EqualTo(123)); Assert.That(t.Fields.Count, Is.EqualTo(2)); Assert.That(HasPrimitiveField(t, "a", PrimitiveType.Int), Is.True); Assert.That(HasPrimitiveField(t, "b", PrimitiveType.Bool), Is.True); Assert.That(HasHint(t, "foo.bar", "whatever"), Is.True); }
public void TestVariantTypesWork() { string s = @" name MySchema; type(1) MyType { vushort vushortValue; vint vintValue; vuint vuintValue; vlong vlongValue; vulong vulongValue; } "; s = s.Replace('X', '"'); PatternBufferSchema schema = this.Parse(s); Assert.That(schema.Name, Is.EqualTo("MySchema")); Assert.That(schema.Enums.Count, Is.EqualTo(0)); Assert.That(schema.Types.Count, Is.EqualTo(1)); Assert.That(schema.Hints.Count, Is.EqualTo(0)); PatternBufferType t = schema.Types[0]; Assert.That(t.HintCount, Is.EqualTo(0)); Assert.That(t.Name, Is.EqualTo("MyType")); Assert.That(t.TypeId, Is.EqualTo(1)); Assert.That(t.Fields.Count, Is.EqualTo(5)); Assert.That(HasPrimitiveField(t, "vushortValue", PrimitiveType.VUShort), Is.True); Assert.That(HasPrimitiveField(t, "vintValue", PrimitiveType.VInt), Is.True); Assert.That(HasPrimitiveField(t, "vuintValue", PrimitiveType.VUInt), Is.True); Assert.That(HasPrimitiveField(t, "vlongValue", PrimitiveType.VLong), Is.True); Assert.That(HasPrimitiveField(t, "vulongValue", PrimitiveType.VULong), Is.True); }
public void TestParentingATypeToItselfThrows() { string s = @" name MySchema; type(1) T1 : T1 { int a; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestUndefinedReferenceFieldTypeThrows() { string s = @" name MySchema; type(1) T1 { DoesntExist lol; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestEmbeddedAggregateValueArgThrows() { string s = @" name MySchema; type(1) T1 { list<set<int>> no; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestUndefinedSetArgThrows() { string s = @" name MySchema; type(1) T1 { set<Lol> no; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestUndefinedMapValueArgThrows() { string s = @" name MySchema; type(1) T1 { map<int,Lol> no; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestUndefinedAncestorThrows() { string s = @" name MySchema; type(1) T1 : T2 { int a; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestDuplicateEnumValueThrows() { string s = @" name MySchema; enum E { A, A } "; PatternBufferSchema schema = this.Parse(s); }
public void TestDuplicateFieldNameInATypeThrows() { string s = @" name MySchema; type(1) T1 { int a; int a; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestABadTypeNameThrows() { string s = @" name MySchema; type(123) _MyType { int a; } "; s = s.Replace('X', '"'); PatternBufferSchema schema = this.Parse(s); }
public void TestDuplicateAncestorTypeFieldNameThrows() { string s = @" name MySchema; type(1) T1 : T2 { int a; } type(2) T2 { int a; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestMissingHintQuoteThrows() { string s = @" name MySchema; type(1) T1 { [foobar.schema1=X12345] map<int,Lol> no; } "; s = s.Replace('X', '"'); PatternBufferSchema schema = this.Parse(s); }
public void TestDuplicateTypeIdThrows() { string s = @" name MySchema; type(1) T1 { int a; } type(1) T2 { int b; } "; PatternBufferSchema schema = this.Parse(s); }
/** * Parses a schema string into a PatternBufferSchema. */ protected PatternBufferSchema Parse(string schema) { PatternBufferSchema s = null; try { PatternBufferSchemaParser loader = new PatternBufferSchemaParser(@"..\..\..\..\..\PatternBuffer.cgt"); s = loader.Parse(schema); } catch (Exception x) { //Console.WriteLine(x.StackTrace); throw x; } return(s); }
public void TestDuplicateTypeAndEnumDefinitionThrows() { string s = @" name MySchema; type(1) T1 { int a; } enum T1 { foo, bar } "; PatternBufferSchema schema = this.Parse(s); }
public void TestDuplicateEnumDefinitionThrows() { string s = @" name MySchema; enum E1 { E1A, E1B } enum E1 { E2A, E2B } "; PatternBufferSchema schema = this.Parse(s); }
public void TestCircularTypeAncestryThrows() { string s = @" name MySchema; type(1) T1 : T2 { int a; } type(2) T2 : T3 { int b; } type(3) T3 : T1 { int c; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestCommentsOnTypeFieldToEolWork() { string s = @" name MySchema; type(1) T1 { int a; // foobar1 WRGCJ@(#U@(#3vt#V@ @#TV VT#3tv.t23v.23tv<#$V> int b; } "; PatternBufferSchema schema = this.Parse(s); Assert.That(schema.Name, Is.EqualTo("MySchema")); Assert.That(schema.Types.Count, Is.EqualTo(1)); Assert.That(schema.Enums.Count, Is.EqualTo(0)); Assert.That(schema.Hints.Count, Is.EqualTo(0)); Assert.That(schema.Types[0].Fields.Count, Is.EqualTo(2)); }
public void TestEmptyBaseTypeWorks() { string s = @" name MySchema; type() T1 { } type(2) T2 : T1 { int a; } "; PatternBufferSchema schema = this.Parse(s); Assert.That(schema.Name, Is.EqualTo("MySchema")); Assert.That(schema.Types.Count, Is.EqualTo(2)); Assert.That(schema.Enums.Count, Is.EqualTo(0)); Assert.That(schema.Hints.Count, Is.EqualTo(0)); }
public void TestMultipleAbstractObjectDontConflict() { string s = @" name MySchema; type() T1 { int a; } type(2) T2 : T1 { int b; } type() S1 { int c; } type(3) S2 : S1 { int d; } "; PatternBufferSchema schema = this.Parse(s); }
public void TestFinalFlagIsSetProperly() { string s = @" name MySchema; type(1) T1 { int a; } type(2) T2 : T1 { int b; } "; PatternBufferSchema schema = this.Parse(s); Assert.That(schema.Name, Is.EqualTo("MySchema")); Assert.That(schema.Types.Count, Is.EqualTo(2)); Assert.That(schema.Enums.Count, Is.EqualTo(0)); Assert.That(schema.Hints.Count, Is.EqualTo(0)); Assert.That(schema.GetPatternBufferType("T1").IsFinal, Is.False); Assert.That(schema.GetPatternBufferType("T2").IsFinal, Is.True); }
public void TestAComplexTypeWorks() { string s = @" // comment name MySchema; [foobar.schema1=X12345X] [foobar.schema2=XABCDEX] enum MyEnum1 { [foobar.enum=X12345X] value1, value2, value3 } enum MyEnum2 { valueA, valueB, valueC, valueD } /* * Comment */ type(234) MyObject { [foobar.type=X12345X] short shortValue; ushort ushortValue; int intValue; uint uintValue; long longValue; ulong ulongValue; float floatValue; double doubleValue; bool boolValue; byte byteValue; DerivedObject otherObjectValue; MyEnum1 enumValue; list<int> intList; list<DerivedObject> otherObjectList; list<MyEnum1> myEnumList; set<int> intSet; set<DerivedObject> otherObjectSet; set<MyEnum1> myEnumSet; map<int,string> intStringMap; map<string,DerivedObject> stringOtherObjectMap; map<int,MyEnum1> intMyEnumMap; } type() AbstractObject { int intValue1; } type(789) DerivedObject : AbstractObject { int intValue2; } "; s = s.Replace('X', '"'); PatternBufferSchema schema = this.Parse(s); Assert.That(schema.Name, Is.EqualTo("MySchema")); Assert.That(schema.Types.Count, Is.EqualTo(3)); Assert.That(schema.Enums.Count, Is.EqualTo(2)); Assert.That(schema.Hints.Count, Is.EqualTo(2)); // Name Assert.That(schema.Name, Is.EqualTo("MySchema")); // Hints Assert.That(schema.Hints.Count, Is.EqualTo(2)); Assert.That(schema.Hints.ContainsKey("foobar.schema1"), Is.True); Assert.That(schema.Hints["foobar.schema1"], Is.EqualTo("12345")); Assert.That(schema.Hints.ContainsKey("foobar.schema2"), Is.True); Assert.That(schema.Hints["foobar.schema2"], Is.EqualTo("ABCDE")); // Enums foreach (PatternBufferEnum e in schema.Enums) { Assert.IsTrue("MyEnum1".Equals(e.Name) || "MyEnum2".Equals(e.Name)); if ("MyEnum1".Equals(e.Name)) { Assert.That(e.Values.Count, Is.EqualTo(3)); Assert.That(e.Values[0], Is.EqualTo("value1")); Assert.That(e.Values[1], Is.EqualTo("value2")); Assert.That(e.Values[2], Is.EqualTo("value3")); Assert.That(e.HintCount, Is.EqualTo(1)); Assert.That(e.GetHint("foobar.enum"), Is.EqualTo("12345")); } else { Assert.That(e.Values.Count, Is.EqualTo(4)); Assert.That(e.Values[0], Is.EqualTo("valueA")); Assert.That(e.Values[1], Is.EqualTo("valueB")); Assert.That(e.Values[2], Is.EqualTo("valueC")); Assert.That(e.Values[3], Is.EqualTo("valueD")); Assert.That(e.HintCount, Is.EqualTo(0)); } } // Types PatternBufferType abstractObjectPatternBufferType = null; foreach (PatternBufferType t in schema.Types) { Assert.That("MyObject".Equals(t.Name) || "AbstractObject".Equals(t.Name) || "DerivedObject".Equals(t.Name), Is.True); if ("MyObject".Equals(t.Name)) { Assert.That(t.TypeId, Is.EqualTo(234)); Assert.That(t.BaseType, Is.Null); Assert.That(t.Fields.Count, Is.EqualTo(21)); Assert.That(HasPrimitiveField(t, "shortValue", PrimitiveType.Short), Is.True); Assert.That(HasPrimitiveField(t, "ushortValue", PrimitiveType.UShort), Is.True); Assert.That(HasPrimitiveField(t, "intValue", PrimitiveType.Int), Is.True); Assert.That(HasPrimitiveField(t, "uintValue", PrimitiveType.UInt), Is.True); Assert.That(HasPrimitiveField(t, "longValue", PrimitiveType.Long), Is.True); Assert.That(HasPrimitiveField(t, "ulongValue", PrimitiveType.ULong), Is.True); Assert.That(HasPrimitiveField(t, "floatValue", PrimitiveType.Float), Is.True); Assert.That(HasPrimitiveField(t, "doubleValue", PrimitiveType.Double), Is.True); Assert.That(HasPrimitiveField(t, "boolValue", PrimitiveType.Bool), Is.True); Assert.That(HasPrimitiveField(t, "byteValue", PrimitiveType.Byte), Is.True); Assert.That(HasReferenceField(t, "otherObjectValue", "DerivedObject"), Is.True); Assert.That(HasReferenceField(t, "enumValue", "MyEnum1"), Is.True); Assert.That(HasPrimitiveListField(t, "intList", PrimitiveType.Int), Is.True); Assert.That(HasReferenceListField(t, "otherObjectList", "DerivedObject"), Is.True); Assert.That(HasReferenceListField(t, "myEnumList", "MyEnum1"), Is.True); Assert.That(HasPrimitiveSetField(t, "intSet", PrimitiveType.Int), Is.True); Assert.That(HasReferenceSetField(t, "otherObjectSet", "DerivedObject"), Is.True); Assert.That(HasReferenceSetField(t, "myEnumSet", "MyEnum1"), Is.True); Assert.That(HasMapField(t, "intStringMap", PrimitiveType.Int, PrimitiveType.String), Is.True); Assert.That(HasMapField(t, "stringOtherObjectMap", PrimitiveType.String, "DerivedObject"), Is.True); Assert.That(HasMapField(t, "stringOtherObjectMap", PrimitiveType.Int, "MyEnum1"), Is.True); Assert.That(t.HintCount, Is.EqualTo(1)); Assert.That(t.GetHint("foobar.type"), Is.EqualTo("12345")); } else if ("AbstractObject".Equals(t.Name)) { abstractObjectPatternBufferType = t; Assert.That(t.TypeId, Is.EqualTo(0)); Assert.That(t.BaseType, Is.Null); Assert.That(t.Fields.Count, Is.EqualTo(1)); Assert.That(HasPrimitiveField(t, "intValue1", PrimitiveType.Int), Is.True); Assert.That(t.HintCount, Is.EqualTo(0)); } else if ("DerivedObject".Equals(t.Name)) { Assert.That(t.TypeId, Is.EqualTo(789)); Assert.That(t.BaseType, Is.EqualTo(abstractObjectPatternBufferType)); Assert.That(HasPrimitiveField(t, "intValue2", PrimitiveType.Int), Is.True); Assert.That(t.HintCount, Is.EqualTo(0)); } } }
/** * Converts a schema's name into the PatternBuffer's exception name. */ public static string ToSchemaExceptionName(PatternBufferSchema schema) { return(ToCSharpPropertyName(schema.Name) + "PatternBufferException"); }
/** * The main method that performs cues up the compiler args, gathers files, and feeds it all * to the actual compiler object. */ static void Main(string[] args) { // For timing purposes. It's like totally fast, though, don't worry. Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // Gather the command line args PatternBufferCompilerCommandLine commandLine = new PatternBufferCompilerCommandLine(args); if (commandLine.DoPrintUsage || commandLine.HasMissingRequiredCommandLineOptions) { Environment.Exit(0); } // Load the specified file(s) IList <string> schemaFiles = GatherSchemaFiles(commandLine); if (schemaFiles.Count == 0) { Console.WriteLine("No input file(s) found."); Environment.Exit(1); } // Verify the output path exists string outputDirectory = commandLine.OutputDirectory; if (commandLine.OutputDirectory == null) { outputDirectory = "./"; } else if (Directory.Exists(commandLine.OutputDirectory)) { outputDirectory = commandLine.OutputDirectory; } else { Console.WriteLine("Specified output directory does not exist."); Environment.Exit(1); } // Append a directory separator if it's missing if (!(outputDirectory.EndsWith("" + Path.AltDirectorySeparatorChar) || outputDirectory.EndsWith("" + Path.DirectorySeparatorChar))) { outputDirectory += Path.AltDirectorySeparatorChar; } // Concatenate all the schema files string schemaText = ""; // A little flag to ignore multiple "name" directives. bool nameFound = false; foreach (string file in schemaFiles) { Console.WriteLine(file); string[] allLines = File.ReadAllLines(file); foreach (string line in allLines) { string temp = line.Trim(); // Remember the schema name if you haven't seen one yet. if (temp.StartsWith("name")) { if (!nameFound) { schemaText += line; schemaText += "\r\n"; nameFound = true; } } // Otherwise, not a name line... append and move on. else { schemaText += line; schemaText += "\r\n"; } } } // Load the schema PatternBufferSchemaParser loader = new PatternBufferSchemaParser(); PatternBufferSchema schema = loader.Parse(schemaText); // Create a compiler with the proper namespace string @namespace = commandLine.CSharpNamespace != null ? commandLine.CSharpNamespace : schema.Name; PatternBufferCompiler compiler = new PatternBufferCompiler(@namespace); // Set the custom boilerplate, if any. if (commandLine.BoilerplateFilePath != null) { compiler.Boilerplate = File.ReadAllText(commandLine.BoilerplateFilePath); } // Set the type safety flag. compiler.MakeThreadsafe = commandLine.MakeThreadsafe; // Compile Dictionary <string, string> files = compiler.Compile(schema); // Save all the files. foreach (string fileName in files.Keys) { Console.WriteLine("writing to " + outputDirectory + fileName); if (fileName.Contains("/")) { string subdirectory = outputDirectory + fileName; subdirectory = subdirectory.Substring(0, subdirectory.LastIndexOf("/")); if (!Directory.Exists(subdirectory)) { Console.WriteLine("creating " + subdirectory); Directory.CreateDirectory(subdirectory); } } File.WriteAllText(outputDirectory + fileName, files[fileName]); } stopwatch.Stop(); Console.WriteLine("\nCompiled " + files.Count + " files in " + stopwatch.ElapsedMilliseconds + " ms."); }
/** * Converts a schema's name into the schema object type name. */ public static string ToSchemaObjectTypeName(PatternBufferSchema schema) { return("I" + ToCSharpPropertyName(schema.Name) + "Object"); }