Beispiel #1
0
        private static RootNodeDefinition ParseSyntax(
            string fbsPath,
            IIncludeLoader includeLoader)
        {
            string rootPath = Path.GetFullPath(fbsPath);

            // First, visit includes. We need to figure out which files warrant a thorough look.
            HashSet <string> includes = new HashSet <string>()
            {
                rootPath
            };
            Queue <string> visitOrder = new Queue <string>();

            visitOrder.Enqueue(rootPath);

            var rootNode      = new RootNodeDefinition(rootPath);
            var schemaVisitor = new SchemaVisitor(rootNode);

            // SHA256 -> 32 bytes.
            byte[] hash = new byte[32];

            string asmVersion = typeof(FlatSharpCompiler).Assembly.GetCustomAttribute <AssemblyFileVersionAttribute>()?.Version ?? "unknown";

            Encoding.UTF8.GetBytes(asmVersion, 0, asmVersion.Length, hash, 0);

            while (visitOrder.Count > 0)
            {
                string next = visitOrder.Dequeue();
                string fbs  = includeLoader.LoadInclude(next);

                using (var sha256 = SHA256Managed.Create())
                {
                    byte[] componentHash = sha256.ComputeHash(Encoding.UTF8.GetBytes(fbs));
                    for (int i = 0; i < hash.Length; ++i)
                    {
                        hash[i] ^= componentHash[i];
                    }
                }

                schemaVisitor.CurrentFileName = next;

                // Traverse the graph of includes.
                var includeVisitor = new IncludeVisitor(next, includes, visitOrder);

                ErrorContext.Current.WithScope(next, () =>
                {
                    var schema = GetParser(fbs).schema();
                    ErrorContext.Current.ThrowIfHasErrors();

                    includeVisitor.Visit(schema);
                    ErrorContext.Current.ThrowIfHasErrors();

                    schemaVisitor.Visit(schema);
                    ErrorContext.Current.ThrowIfHasErrors();
                });
            }

            rootNode.InputHash = $"{asmVersion}.{Convert.ToBase64String(hash)}";
            return(rootNode);
        }
        private static BaseSchemaMember ParseSyntax(string fbsSchema, string inputHash)
        {
            AntlrInputStream  input       = new AntlrInputStream(fbsSchema);
            FlatBuffersLexer  lexer       = new FlatBuffersLexer(input);
            CommonTokenStream tokenStream = new CommonTokenStream(lexer);
            FlatBuffersParser parser      = new FlatBuffersParser(tokenStream);

            parser.AddErrorListener(new CustomErrorListener());

            SchemaVisitor    visitor  = new SchemaVisitor(inputHash);
            BaseSchemaMember rootNode = visitor.Visit(parser.schema());

            return(rootNode);
        }