View Javadoc

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