    001    /* Copyright 2000, 2001, Compaq Computer Corporation */
    002
    003    package javafe;
    004
    005    import java.io.IOException;
    006    import javafe.ast.*;
    007    import javafe.genericfile.*;
    008    import javafe.parser.Lex;
    009    import javafe.parser.Parse;
    010    import javafe.util.CorrelatedReader;
    011    import javafe.util.ErrorSet;
    012    import javafe.util.FileCorrelatedReader;
    013
    014    public class CountLines
    015    {
    016        //@ requires \nonnullelements(argv);
    017        public static void main(String[] argv) throws IOException {
    018            String spc = "            ";
    019            String[] indent = new String[spc.length()];
    020            //@ assume indent.length == 12;
    021            for(int i = 0; i < indent.length; i++)
    022                indent[i] = spc.substring(0, indent.length - i);
    023
    024            Lex l = new Lex(null, true);
    025            Parse p = new Parse();
    026            long total = 0;
    027
    028            try {
    029                for(int i = 0; i<argv.length; i++) {
    030                    CorrelatedReader in =
    031                        new FileCorrelatedReader(new NormalGenericFile(argv[1]));
    032                    l.restart(in);
    033                    int thisFile = count( p.parseCompilationUnit(l, false) );
    034                    in.close();
    035
    036                    String tf = Integer.toString(thisFile);
    037                    System.out.println(indent[Math.min(indent.length-1, tf.length())]
    038                                       + tf + " " + argv[i]);
    039                    total += thisFile;
    040                }
    041            } catch(IOException e) {
    042                e.printStackTrace();
    043                ErrorSet.fatal(e.getMessage());
    044            }
    045            String tf = Long.toString(total);
    046            System.out.println(indent[Math.min(indent.length-1, tf.length())]
    047                               + tf + " total");
    048        }
    049
    050      public static int count(/*@ non_null @*/ ASTNode n) {
    051        int result = 0;
    052        if (n instanceof TypeDecl || n instanceof TypeDeclElem|| n instanceof Stmt)
    053          result = 1;
    054            else result = 0;
    055
    056            for(int i = 0; i < n.childCount(); i++) {
    057                Object c = n.childAt(i);
    058                if (c instanceof ASTNode)
    059                    result += count((ASTNode)c);
    060            }
    061
    062            return result;
    063        }
    064    }