View Javadoc

    1   package org.codehaus.groovy.antlr;
    2
    3   import java.awt.event.WindowAdapter;
    4   import java.awt.event.WindowEvent;
    5   import java.io.File;
    6   import java.io.FileReader;
    7
    8   import org.codehaus.groovy.antlr.parser.GroovyLexer;
    9   import org.codehaus.groovy.antlr.parser.GroovyRecognizer;
    10
    11  import antlr.ASTFactory;
    12  import antlr.CommonAST;
    13  import antlr.Token;
    14  import antlr.collections.AST;
    15  import antlr.debug.misc.ASTFrame;
    16
    17  class Main {
    18
    19      static boolean whitespaceIncluded = false;
    20
    21  	static boolean showTree = false;
    22      //static boolean xml = false;
    23  	static boolean verbose = false;
    24      public static void main(String[] args) {
    25  		// Use a try/catch block for parser exceptions
    26  		try {
    27  			// if we have at least one command-line argument
    28  			if (args.length > 0 ) {
    29  				System.err.println("Parsing...");
    30
    31  				// for each directory/file specified on the command line
    32  				for(int i=0; i< args.length;i++) {
    33  					if ( args[i].equals("-showtree") ) {
    34  						showTree = true;
    35  					}
    36                      //else if ( args[i].equals("-xml") ) {
    37                      //    xml = true;
    38                      //}
    39  					else if ( args[i].equals("-verbose") ) {
    40  						verbose = true;
    41  					}
    42  					else if ( args[i].equals("-trace") ) {
    43  						GroovyRecognizer.tracing = true;
    44  						GroovyLexer.tracing = true;
    45  					}
    46  					else if ( args[i].equals("-traceParser") ) {
    47  						GroovyRecognizer.tracing = true;
    48  					}
    49  					else if ( args[i].equals("-traceLexer") ) {
    50  						GroovyLexer.tracing = true;
    51  					}
    52                                          else if ( args[i].equals("-whitespaceIncluded") ) {
    53                                              whitespaceIncluded = true;
    54                                          }
    55                                          else {
    56  						doFile(new File(args[i])); // parse it
    57  					}
    58  				} }
    59  			else
    60  				System.err.println("Usage: java -jar groovyc.jar [-showtree] [-verbose] [-trace{,Lexer,Parser}]"+
    61                                     "<directory or file name>");
    62  		}
    63  		catch(Exception e) {
    64  			System.err.println("exception: "+e);
    65  			e.printStackTrace(System.err);   // so we can get stack trace
    66  		}
    67  	}
    68
    69
    70  	// This method decides what action to take based on the type of
    71  	//   file we are looking at
    72  	public static void doFile(File f)
    73  							  throws Exception {
    74  		// If this is a directory, walk each file/dir in that directory
    75  		if (f.isDirectory()) {
    76  			String files[] = f.list();
    77  			for(int i=0; i < files.length; i++)
    78  				doFile(new File(f, files[i]));
    79  		}
    80
    81  		// otherwise, if this is a groovy file, parse it!
    82  		else if (f.getName().endsWith(".groovy")) {
    83  			System.err.println(" --- "+f.getAbsolutePath());
    84  			// parseFile(f.getName(), new FileInputStream(f));
    85              SourceBuffer sourceBuffer = new SourceBuffer();
    86              UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new FileReader(f),sourceBuffer);
    87              GroovyLexer lexer = new GroovyLexer(unicodeReader);
    88              unicodeReader.setLexer(lexer);
    89  			parseFile(f.getName(),lexer,sourceBuffer);
    90  		}
    91  	}
    92
    93  	// Here's where we do the real work...
    94  	public static void parseFile(String f, GroovyLexer l, SourceBuffer sourceBuffer)
    95  								 throws Exception {
    96  		try {
    97  			// Create a parser that reads from the scanner
    98  			GroovyRecognizer parser = GroovyRecognizer.make(l);
    99              parser.setSourceBuffer(sourceBuffer);
    100 			parser.setFilename(f);
    101
    102                         if (whitespaceIncluded) {
    103                             GroovyLexer lexer = parser.getLexer();
    104                             lexer.setWhitespaceIncluded(true);
    105                             while (true) {
    106                                 Token t = lexer.nextToken();
    107                                 System.out.println(t);
    108                                 if (t == null || t.getType() == Token.EOF_TYPE)  break;
    109                             }
    110                             return;
    111                         }
    112
    113 			// start parsing at the compilationUnit rule
    114 			parser.compilationUnit();
    115
    116 			System.out.println("parseFile "+f+" => "+parser.getAST());
    117
    118 			// do something with the tree
    119 			doTreeAction(f, parser.getAST(), parser.getTokenNames());
    120 		}
    121 		catch (Exception e) {
    122 			System.err.println("parser exception: "+e);
    123 			e.printStackTrace();   // so we can get stack trace
    124 		}
    125 	}
    126
    127 	public static void doTreeAction(String f, AST t, String[] tokenNames) {
    128 		if ( t==null ) return;
    129 		if ( showTree ) {
    130 			CommonAST.setVerboseStringConversion(true, tokenNames);
    131 			ASTFactory factory = new ASTFactory();
    132 			AST r = factory.create(0,"AST ROOT");
    133 			r.setFirstChild(t);
    134 			final ASTFrame frame = new ASTFrame("Groovy AST", r);
    135 			frame.setVisible(true);
    136 			frame.addWindowListener(
    137 				new WindowAdapter() {
    138                    public void windowClosing (WindowEvent e) {
    139                        frame.setVisible(false); // hide the Frame
    140                        frame.dispose();
    141                        System.exit(0);
    142                    }
    143 		        }
    144 			);
    145 			if (verbose)  System.out.println(t.toStringList());
    146 		}
    147         /*if ( xml ) {
    148 			((CommonAST)t).setVerboseStringConversion(true, tokenNames);
    149 			ASTFactory factory = new ASTFactory();
    150 			AST r = factory.create(0,"AST ROOT");
    151 			r.setFirstChild(t);
    152             XStream xstream = new XStream();
    153             xstream.alias("ast", CommonAST.class);
    154 			try {
    155                 xstream.toXML(r,new FileWriter(f + ".xml"));
    156                 System.out.println("Written AST to " + f + ".xml");
    157             } catch (Exception e) {
    158                 System.out.println("couldn't write to " + f + ".xml");
    159                 e.printStackTrace();
    160             }
    161 			//if (verbose)  System.out.println(t.toStringList());
    162 		}*/
    163 	/*@todo
    164 		GroovyTreeParser tparse = new GroovyTreeParser();
    165 		try {
    166 			tparse.compilationUnit(t);
    167 			if (verbose)  System.out.println("successful walk of result AST for "+f);
    168 		}
    169 		catch (RecognitionException e) {
    170 			System.err.println(e.getMessage());
    171 			e.printStackTrace();
    172 		}
    173 	@todo*/
    174
    175 	}
    176 }
    177