Esempio n. 1
0
 /// <summary>Writes the header to a C# file generated from a .g4 file.</summary>
 /// <param name="sourceCode">Code writer into which the header shall be written.</param>
 /// <param name="sourcePath">Full pathname of the source g4 file.</param>
 /// <param name="metaData">Metadata describing the whole data model.</param>
 public static void WriteHeader(CodeWriter sourceCode, string sourcePath, DataModelMetadata metaData)
 {
     sourceCode.WriteLine("// *********************************************************");
     sourceCode.WriteLine("// *                                                       *");
     sourceCode.WriteLine("// *   Copyright (C) Microsoft. All rights reserved.       *");
     sourceCode.WriteLine("// *                                                       *");
     sourceCode.WriteLine("// *********************************************************");
     sourceCode.WriteLine();
     sourceCode.WriteLine("//----------------------------------------------------------");
     sourceCode.WriteLine("// <auto-generated>");
     sourceCode.WriteLine("//     This code was generated by a tool.");
     sourceCode.WriteLine("//     Input Grammar      : " + metaData.Name);
     sourceCode.WriteLine("//     Input Grammar file : " + sourcePath);
     sourceCode.WriteLine("//     ");
     sourceCode.WriteLine("//     Changes to this file may cause incorrect behavior and ");
     sourceCode.WriteLine("//     will be lost when the code is regenerated.");
     sourceCode.WriteLine("// </auto-generated>");
     sourceCode.WriteLine("//----------------------------------------------------------");
     sourceCode.WriteLine();
     sourceCode.WriteLine("using System;");
     sourceCode.WriteLine("using System.Collections.Generic;");
     sourceCode.WriteLine("using System.Globalization;");
     sourceCode.WriteLine("using System.Linq;");
     sourceCode.WriteLine("using System.Runtime.CompilerServices;");
     sourceCode.WriteLine("using System.Runtime.Serialization;");
     sourceCode.WriteLine("using System.Text;");
     sourceCode.WriteLine();
     sourceCode.OpenBrace("namespace " + metaData.Namespace);
 }
        public DataModel Link(string sourceFilePath, DataModelMetadata metaData)
        {
            this.AnnotateDerivedTypes();
            if (metaData.GenerateLocations)
            {
                this.AddLocationsToTypes();
            }

            return(new DataModel(
                       sourceFilePath,
                       metaData,
                       this.BuildTypes()
                       ));
        }
Esempio n. 3
0
        public DataModel(string sourceFilePath, DataModelMetadata metaData, IEnumerable <DataModelType> types)
        {
            this.SourceFilePath = sourceFilePath;
            this.MetaData       = metaData;

            _g4Lookup = ImmutableSortedDictionary.CreateRange(
                StringComparer.Ordinal, types.Select(type => Pair.Make(type.G4DeclaredName, type)));
            ImmutableSortedDictionary <string, DataModelType> .Builder csTypes = ImmutableSortedDictionary.CreateBuilder <string, DataModelType>(StringComparer.Ordinal);
            foreach (DataModelType type in _g4Lookup.Values)
            {
                string key = type.CSharpName;
                if (csTypes.ContainsKey(key))
                {
                    csTypes[key] = null;
                }
                else
                {
                    csTypes.Add(key, type);
                }
            }
        }
        //private static IDisposable sw;

        /// <summary>Main entry-point for this application.</summary>
        /// <param name="args">Array of command-line argument strings.</param>
        /// <returns>Exit-code for the process - 0 for success, else an error code.</returns>
        public static int Main(string[] args)
        {
            using (var config = new DataModelGeneratorConfiguration())
            {
                if (!config.ParseArgs(args))
                {
                    return(1);
                }

                string inputText;
                using (var sr = new StreamReader(config.InputStream))
                {
                    inputText = sr.ReadToEnd();
                }

                GrammarSymbol grammar;
                try
                {
                    var factory = new TokenTextIndex(inputText);
                    System.Collections.Immutable.ImmutableArray <Token> tokens = Lexer.Lex(factory);
                    var parser = new Parser(tokens);
                    grammar = parser.ParseGrammar();
                }
                catch (G4ParseFailureException ex)
                {
                    Console.Error.WriteLine(ex.Message);
                    return(1);
                }

                var builder = new DataModelBuilder();
                for (int idx = 1; idx < grammar.Children.Length; ++idx)
                {
                    try
                    {
                        GrammarSymbol prod = grammar.Children[idx];
                        builder.CompileProduction(prod);
                    }
                    catch (G4ParseFailureException ex)
                    {
                        Console.WriteLine(Strings.FailedToCompileProduction + ex.Message);
                    }
                }

                string generatedCSharp;
                string generatedJsonSchema;
                try
                {
                    DataModel model = builder.Link(config.InputFilePath, DataModelMetadata.FromGrammar(grammar));
                    model = MemberHoister.HoistTypes(model);
                    var cr = new CodeWriter();
                    CodeGenerator.WriteDataModel(cr, model);
                    generatedCSharp = cr.ToString();

                    cr = new CodeWriter(2, ' ');
                    JsonSchemaGenerator.WriteSchema(cr, model);
                    generatedJsonSchema = cr.ToString();
                }
                catch (G4ParseFailureException ex)
                {
                    Console.Error.WriteLine(ex.Message);
                    return(1);
                }

                if (config.ToConsole)
                {
                    Console.WriteLine(generatedCSharp);
                }

                using (var sw = new StreamWriter(config.OutputStream))
                {
                    sw.Write(generatedCSharp);
                }

                string jsonSchemaPath = Path.GetFileNameWithoutExtension(config.OutputFilePath);
                jsonSchemaPath = Path.Combine(Path.GetDirectoryName(config.OutputFilePath), jsonSchemaPath + ".schema.json");

                File.WriteAllText(jsonSchemaPath, generatedJsonSchema);

                return(0);
            }
        }
Esempio n. 5
0
 private static void WriteHeader(CodeWriter codeWriter, string sourceFilePath, DataModelMetadata metaData)
 {
     // TODO this file shouldn't contain any references whatsoever to SARIF
     codeWriter.OpenBrace();
     codeWriter.WriteLine(@"""$schema"": ""http://json-schema.org/draft-04/schema#"",");
     codeWriter.WriteLine(@"""title"": ""Static Analysis Results Format (SARIF) Version 1.0 JSON Schema (Draft 0.4)"",");
 }