SlimDX.Generator.HeaderParser.Parse C# (CSharp) Method

Parse() public method

Runs a custom "C++ Header" grammar on a preprocessed and transformed file to produce a tree of source elements in memory.
public Parse ( string file ) : SlimDX.Generator.NonTerminal
file string The preprocessed source file.
return SlimDX.Generator.NonTerminal
        public NonTerminal Parse(string file)
        {
            using (var stream = new StreamReader(file))
            {
                var parser = new Parser(stream, grammar);
                parser.TrimReductions = true;

                while (true)
                {
                    var result = parser.Parse();
                    switch (result)
                    {
                        case ParseMessage.InternalError:
                            throw new InvalidOperationException("Internal error in parser");

                        case ParseMessage.LexicalError:
                            throw new InvalidOperationException(string.Format("Lexical error: (line:{0}) {1}", parser.TokenLineNumber, parser.TokenText));

                        case ParseMessage.SyntaxError:
                            throw new InvalidOperationException(string.Format("Syntax error: (line:{0}) {1}\n -- Expected: {2}", parser.LineNumber, parser.LineText,
                                string.Join<Symbol>(", ", parser.GetExpectedTokens())));

                        case ParseMessage.TokenRead:
                            var terminal = new Terminal(parser.TokenSymbol, parser.TokenText);
                            parser.TokenSyntaxNode = terminal;
                            break;

                        case ParseMessage.Reduction:
                            var nonTerminal = new NonTerminal(parser.ReductionRule);
                            parser.TokenSyntaxNode = nonTerminal;
                            for (int i = 0; i < parser.ReductionCount; i++)
                                nonTerminal.Add(parser.GetReductionSyntaxNode(i));
                            break;

                        case ParseMessage.Accept:
                            return parser.TokenSyntaxNode as NonTerminal;
                    }
                }
            }
        }

Usage Example

Example #1
0
        static JObject RunParser(Configuration configuration)
        {
            // run boost::wave on the primary source file to get a preprocessed file and a list of macros
            var preprocessor = new Preprocessor(configuration);

            Console.WriteLine(preprocessor.Run());

            // before parsing, run some transformations on the preprocessed file to cut down on the size needed to be examined
            // this includes dropping any source that is not from the given primary or ancillary
            // sources, which is indicated in the preprocessed file by #line directives
            var source          = preprocessor.Source;
            var rawSources      = configuration.AdditionalHeaders.Concat(new[] { Path.Combine(Directory.GetCurrentDirectory(), source) });
            var relevantSources = new List <string>();

            foreach (string s in rawSources)
            {
                relevantSources.Add(Environment.ExpandEnvironmentVariables(s));
            }

            source = Path.ChangeExtension(source, ".i");
            Preprocessor.PostTransform(source, new HashSet <string>(relevantSources));

            // run the parser on the preprocessed file to generate a model of the file in memory
            var grammarFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "header_grammar.cgt");
            var parser      = new HeaderParser(grammarFile);
            var root        = parser.Parse(source).ToXml();
            var json        = ModelXml.Transform(root);

            // add a dependency to the base SlimDX.json file
            json.Add(new JProperty("dependencies", new JArray(new JValue("../SlimDX/SlimDX.json"))));

            return(json);
        }
All Usage Examples Of SlimDX.Generator.HeaderParser::Parse