Example #1
0
        static void Main(string[] args)
        {
            bool show_help = false;
            bool xref = false;
            bool print_ast = false;
            string action = "compile";
            var names = new List<string>();

            var p = new OptionSet()
            {
                { "h|help", "show this message and exit", v => show_help = v != null },
                { "x|xref", "perform a cross-reference of identifiers", v => xref = v != null },
                { "i|intermediate", "print the parse tree of the input", v => print_ast = v != null },
                { "a|action=", "`compile' or `interpret' the input", v => action = v },
            };

            List<string> extra;
            try
            {
                extra = p.Parse(args);
            }
            catch (OptionException option_exception)
            {
                Console.Write("dradis: ");
                Console.WriteLine(option_exception.Message);
                Console.WriteLine("Try `dradis --help' for more information.");
                return;
            }

            if (show_help)
            {
                ShowHelp(p);
                return;
            }

            if (extra.Count == 0)
            {
                Console.WriteLine("dradis: ");
                Console.WriteLine("No source file given.");
                Console.WriteLine("Try `dradis --help' for more information.");
                return;
            }

            string source_path = extra[0];
            try
            {
                using (StreamReader reader = new StreamReader(source_path))
                {
                    SourceMessageObserver sourceObserver = new SourceMessageObserver();
                    Source source = new Source(reader);
                    source.Add(sourceObserver);

                    Scanner scanner = new Scanner(source);
                    Parser parser = new Parser(scanner);
                    ParserMessageObserver parserObserver = new ParserMessageObserver();
                    parser.Add(parserObserver);

                    var result = parser.Parse();
                    if (parser.ErrorCount > 0)
                    {
                        ICode icode = result.Item1;
                        SymbolTableStack symtabstack = result.Item2;

                        if (xref)
                        {
                            XRef.Print(symtabstack);
                        }

                        if (print_ast)
                        {
                            ParseTreePrinter printer = new ParseTreePrinter(Console.Out);
                            printer.Print(icode);
                        }

                        Backend backend = Backend.Create(action);
                        if (backend == null)
                        {
                            throw new Exception("`action' can be either `compile' or 'interpret'!");
                        }
                        backend.Add(new BackendMessageObserver());
                        backend.Process(icode, symtabstack);
                    }
                }
            } catch(Exception ex)
            {
                Console.WriteLine("dradis: ");
                Console.WriteLine("FATAL ERROR: " + ex.Message);
                Console.WriteLine("TRACE: " + ex.StackTrace);
            }
        }
Example #2
0
 public Scanner(Source s)
 {
     Contract.Requires(s != null);
     source = s;
 }