示例#1
0
        public static void Main(string[] args)
        {
            if (args.Length < 7)
            {
                Console.Error.WriteLine("org.antlr.codebuff.Tool -g grammar-name -rule start-rule -corpus root-dir-of-samples \\\n" + "   [-files file-extension] [-indent num-spaces] \\" + "   [-comment line-comment-name] [-o output-file] file-to-format");
                return;
            }

            formatted_output = null;
            string outputFileName  = "";
            string grammarName     = null;
            string startRule       = null;
            string corpusDir       = null;
            string indentS         = "4";
            string commentS        = null;
            string input_file_name = null;
            string fileExtension   = null;
            int    i = 0;

            while (i < args.Length && args[i].StartsWith("-", StringComparison.Ordinal))
            {
                switch (args[i])
                {
                case "-g":
                    i++;
                    grammarName = args[i++];
                    break;

                case "-rule":
                    i++;
                    startRule = args[i++];
                    break;

                case "-corpus":
                    i++;
                    corpusDir = args[i++];
                    break;

                case "-files":
                    i++;
                    fileExtension = args[i++];
                    break;

                case "-indent":
                    i++;
                    indentS = args[i++];
                    break;

                case "-comment":
                    i++;
                    commentS = args[i++];
                    break;

                case "-o":
                    i++;
                    outputFileName = args[i++];
                    break;

                case "-inoutstring":
                    i++;
                    formatted_output = "";
                    outputFileName   = null;
                    break;
                }
            }
            input_file_name = args[i];             // must be last

            Console.WriteLine("gramm: " + grammarName);
            string parserClassName = grammarName + "Parser";
            string lexerClassName  = grammarName + "Lexer";
            Type   parserClass     = null;
            Type   lexerClass      = null;
            Lexer  lexer           = null;

            try
            {
                parserClass = (Type)Type.GetType(parserClassName);
                lexerClass  = (Type)Type.GetType(lexerClassName);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Can't load " + parserClassName + " or maybe " + lexerClassName);
                Console.Error.WriteLine("Make sure they are generated by ANTLR, compiled, and in CLASSPATH");
                System.Console.WriteLine(e.StackTrace);
            }
            if (parserClass == null | lexerClass == null)
            {
                return;                 // don't return from catch!
            }
            int indentSize            = int.Parse(indentS);
            int singleLineCommentType = -1;

            if (!string.ReferenceEquals(commentS, null))
            {
                try
                {
                    lexer = getLexer(lexerClass, null);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Can't instantiate lexer " + lexerClassName);
                    System.Console.WriteLine(e.StackTrace);
                }
                if (lexer == null)
                {
                    return;
                }
                IDictionary <string, int> tokenTypeMap = lexer.TokenTypeMap;
                if (tokenTypeMap.ContainsKey(commentS))
                {
                    singleLineCommentType = tokenTypeMap[commentS];
                }
            }
            string fileRegex = null;

            if (!string.ReferenceEquals(fileExtension, null))
            {
                fileRegex = ".*\\." + fileExtension;
            }
            LangDescriptor language = new LangDescriptor(grammarName, corpusDir, fileRegex, lexerClass, parserClass, startRule, indentSize, singleLineCommentType);

            ////////
            // load all corpus files up front
            IList <string>        allFiles  = getFilenames(language.corpusDir, language.fileRegex);
            IList <InputDocument> documents = load(allFiles, language);

            // Handle formatting of document if it's passed as a string or not.
            if (unformatted_input == null)
            {
                // Don't include file to format in corpus itself.
                string path = System.IO.Path.GetFullPath(input_file_name);
                IList <InputDocument> others = BuffUtils.filter(documents, d => !d.fileName.Equals(path));
                // Perform training of formatter.
                Corpus corpus = new Corpus(others, language);
                corpus.train();

                // Parse code contained in file.
                InputDocument unformatted_document = parse(input_file_name, language);

                // Format document.
                Formatter formatter = new Formatter(corpus, language.indentSize, Formatter.DEFAULT_K, Trainer.FEATURES_INJECT_WS, Trainer.FEATURES_HPOS);
                formatted_output = formatter.format(unformatted_document, false);
            }
            else
            {
                // Perform training of formatter.
                Corpus corpus = new Corpus(documents, language);
                corpus.train();

                // Parse code that was represented as a string.
                InputDocument unformatted_document = parse(input_file_name, unformatted_input, language);

                // Format document.
                Formatter formatter = new Formatter(corpus, language.indentSize, Formatter.DEFAULT_K, Trainer.FEATURES_INJECT_WS, Trainer.FEATURES_HPOS);
                formatted_output = formatter.format(unformatted_document, false);
            }
            ///////
            if (outputFileName != null && outputFileName == "")
            {
                System.Console.WriteLine(formatted_output);
            }
            else if (!string.IsNullOrEmpty(outputFileName))
            {
                org.antlr.codebuff.misc.Utils.writeFile(outputFileName, formatted_output);
            }
        }