1 /*
2 *
3 * Copyright 2002-2004 The Ant-Contrib project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.codehaus.mojo.natives.parser;
18
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.util.Vector;
22
23 /**
24 * A parser that extracts INCLUDE statements from a Reader.
25 *
26 * @author Curt Arnold
27 */
28 public final class FortranParser
29 extends AbstractParser
30 implements Parser
31 {
32 /**
33 * List of included filenames.
34 */
35 private final Vector includes = new Vector();
36
37 /**
38 * State that starts consuming content at the beginning of a line.
39 */
40 private final AbstractParserState newLineState;
41
42 /**
43 * Default constructor.
44 */
45 public FortranParser()
46 {
47 AbstractParserState filename = new FilenameState( this, new char[] { '\'', '/' } );
48 AbstractParserState apos = new WhitespaceOrLetterState( this, '\'', filename );
49 AbstractParserState blank = new LetterState( this, ' ', apos, null );
50 AbstractParserState e = new CaseInsensitiveLetterState( this, 'E', blank, null );
51 AbstractParserState d = new CaseInsensitiveLetterState( this, 'D', e, null );
52 AbstractParserState u = new CaseInsensitiveLetterState( this, 'U', d, null );
53 AbstractParserState l = new CaseInsensitiveLetterState( this, 'L', u, null );
54 AbstractParserState c = new CaseInsensitiveLetterState( this, 'C', l, null );
55 AbstractParserState n = new CaseInsensitiveLetterState( this, 'N', c, null );
56 newLineState = new WhitespaceOrCaseInsensitiveLetterState( this, 'I', n );
57 }
58
59 /**
60 * Called by FilenameState at completion of file name production.
61 *
62 * @param include include file name
63 */
64 public void addFilename( final String include )
65 {
66 includes.addElement( include );
67 }
68
69 /**
70 * Gets collection of include file names encountered in parse.
71 *
72 * @return include file names
73 */
74 public String[] getIncludes()
75 {
76 String[] retval = new String[includes.size()];
77 includes.copyInto( retval );
78 return retval;
79 }
80
81 /**
82 * Get the state for the beginning of a new line.
83 *
84 * @return start of line state
85 */
86 public AbstractParserState getNewLineState()
87 {
88 return newLineState;
89 }
90
91 /**
92 * Collects all included files from the content of the reader.
93 *
94 * @param reader character reader containing a FORTRAN source module
95 * @throws IOException throw if I/O error during parse
96 */
97 public void parse( final Reader reader )
98 throws IOException
99 {
100 includes.setSize( 0 );
101 super.parse( reader );
102 }
103 }