~ubuntu-branches/ubuntu/gutsy/jflex/gutsy

« back to all changes in this revision

Viewing changes to src/JFlex/anttask/JFlexTask.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2002-02-16 13:38:21 UTC
  • Revision ID: james.westby@ubuntu.com-20020216133821-5wsdprpt9xl7ondr
Tags: upstream-1.3.5
ImportĀ upstreamĀ versionĀ 1.3.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
2
 * JFlex Anttask                                                           *
 
3
 * Copyright (C) 2001       Rafal Mantiuk <Rafal.Mantiuk@bellstream.pl>    *
 
4
 * All rights reserved.                                                    *
 
5
 *                                                                         *
 
6
 * This program is free software; you can redistribute it and/or modify    *
 
7
 * it under the terms of the GNU General Public License. See the file      *
 
8
 * COPYRIGHT for more information.                                         *
 
9
 *                                                                         *
 
10
 * This program is distributed in the hope that it will be useful,         *
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 
13
 * GNU General Public License for more details.                            *
 
14
 *                                                                         *
 
15
 * You should have received a copy of the GNU General Public License along *
 
16
 * with this program; if not, write to the Free Software Foundation, Inc., *
 
17
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 
18
 *                                                                         *
 
19
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
20
 
 
21
package JFlex.anttask;
 
22
 
 
23
import java.io.*;
 
24
 
 
25
/**
 
26
 * JFlex task class
 
27
 *
 
28
 * @author Rafal Mantiuk
 
29
 * @version JFlex 1.3.5, $Revision: 1.6 $, $Date: 2001/10/08 10:08:11 $
 
30
 */
 
31
public class JFlexTask {
 
32
 
 
33
    private File destinationDir;
 
34
    private File inputFile;
 
35
    private JFlexWrapper wrapper = new JFlexWrapper();
 
36
 
 
37
    private boolean verbose = false;
 
38
    private boolean generateDot = false; //write graphviz .dot files for the generated automata (alpha)
 
39
    private boolean skipMin = false; //skip minimization step (alpha status, use with care
 
40
    private boolean displayTime = false; //display generation time statistics
 
41
    private File skeletonFile = null;
 
42
 
 
43
    public void execute() throws BuildException {
 
44
        
 
45
        try {
 
46
            
 
47
            if( inputFile == null ) {
 
48
                throw new BuildException("You must specify the input file for JFlex!");
 
49
            }
 
50
            
 
51
            processFile( inputFile );
 
52
            
 
53
        }
 
54
        catch( JFlex.GeneratorException e ) {
 
55
            throw new BuildException( "JFlex: generation failed!" );
 
56
        }
 
57
    }
 
58
 
 
59
    public void setDestdir( File destinationDir ) {
 
60
        this.destinationDir = destinationDir;
 
61
    }
 
62
    
 
63
    public void setFile( File file ) {
 
64
        this.inputFile = file;
 
65
    }
 
66
 
 
67
    public void setGenerateDot( boolean genDot )
 
68
    {
 
69
        this.generateDot = genDot;
 
70
    }
 
71
 
 
72
    public void setTimeStatistics( boolean displayTime )
 
73
    {
 
74
        this.displayTime = displayTime;
 
75
    }
 
76
 
 
77
    public void setVerbose( boolean verbose )
 
78
    {
 
79
        this.verbose = verbose;
 
80
    }
 
81
 
 
82
    public void setSkeleton( File skeleton )
 
83
    {
 
84
        this.skeletonFile = skeleton;
 
85
    }
 
86
    
 
87
    public void setSkipMinimization( boolean skipMin )
 
88
    {
 
89
        this.skipMin = skipMin;
 
90
    }
 
91
 
 
92
    protected void processFile( File file ) throws BuildException
 
93
    {
 
94
        try {
 
95
 
 
96
            // find name of the package and class in jflex source file
 
97
            String packageName = null;
 
98
            String className = null;
 
99
            {
 
100
            LineNumberReader reader = new LineNumberReader( new InputStreamReader( new FileInputStream( file ) ) );
 
101
 
 
102
 
 
103
            for( ; ; )
 
104
            {
 
105
                String line = reader.readLine();
 
106
                if( line == null ) break;
 
107
 
 
108
                if( packageName == null ) {
 
109
                    int index  = line.indexOf( "package" );
 
110
                    if( index != -1 ) {
 
111
                        index += 7;
 
112
 
 
113
                        int end = line.indexOf( ';', index );
 
114
                        if( end != -1 ) {
 
115
                            packageName = line.substring( index, end );
 
116
                            packageName = packageName.trim();
 
117
                        }
 
118
 
 
119
                    }
 
120
                }
 
121
 
 
122
                if( className == null ) {
 
123
                    int index  = line.indexOf( "%class" );
 
124
                    if( index != -1 ) {
 
125
                        index += 6;
 
126
 
 
127
                        className = line.substring( index );
 
128
                        className = className.trim();
 
129
 
 
130
                    }
 
131
                }
 
132
 
 
133
                // We have all the necessary information. No further parsing is necessary
 
134
                if( className != null && packageName != null ) break;
 
135
 
 
136
 
 
137
            }
 
138
            if( className == null )
 
139
              className = "Yylex";
 
140
 
 
141
            }
 
142
 
 
143
            // find out what the destination directory is. Append packageName to dest dir.
 
144
            StringBuffer destDir = new StringBuffer();
 
145
            {
 
146
 
 
147
                if( destinationDir != null ) {
 
148
                    destDir.append( destinationDir.getAbsolutePath() );
 
149
 
 
150
                    if( packageName != null ) {
 
151
                        destDir.append( File.separatorChar );
 
152
                        String path = packageName.replace( '.', File.separatorChar );
 
153
                        destDir.append( path );
 
154
                    }
 
155
 
 
156
                } else { //save parser to the same dir as .flex
 
157
                    destDir.append( file.getParent() );
 
158
                }
 
159
            }
 
160
 
 
161
            File destFile = new File( destDir.toString() + File.separator + className + ".java" );
 
162
 
 
163
            if( file.lastModified() > destFile.lastModified() ) {
 
164
 
 
165
                //configure JFlex
 
166
                wrapper.setTimeStatistics( displayTime );
 
167
                wrapper.setVerbose( verbose );
 
168
                wrapper.setGenerateDot( generateDot );
 
169
                wrapper.setSkipMinimization( skipMin );
 
170
                wrapper.setSkeleton( skeletonFile );
 
171
                wrapper.setDestinationDir( destDir.toString() );
 
172
 
 
173
                wrapper.generate( file );
 
174
 
 
175
                if( !verbose ) System.out.println( "Generated: " + destFile.getName() );
 
176
            }
 
177
        }
 
178
        catch( IOException e )
 
179
        {
 
180
            throw new BuildException( "IOException: " + e.toString() );
 
181
        }
 
182
    }
 
183
 
 
184
}
 
185