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 Adam Murdoch
30   * @author Curt Arnold
31   */
32  public final class CParser extends AbstractParser implements Parser {
33    private final Vector<String> includes = new Vector<>();
34    private final AbstractParserState newLineState;
35  
36    /**
37       *
38       *
39       */
40    public CParser() {
41      final AbstractParserState quote = new FilenameState(this, new char[] {
42        '"'
43      });
44      final AbstractParserState bracket = new FilenameState(this, new char[] {
45        '>'
46      });
47      final AbstractParserState postE = new PostE(this, bracket, quote);
48      //
49      // nclude
50      //
51      final AbstractParserState e = new LetterState(this, 'e', postE, null);
52      final AbstractParserState d = new LetterState(this, 'd', e, null);
53      final AbstractParserState u = new LetterState(this, 'u', d, null);
54      final AbstractParserState l = new LetterState(this, 'l', u, null);
55      final AbstractParserState c = new LetterState(this, 'c', l, null);
56      final AbstractParserState n = new LetterState(this, 'n', c, null);
57      //
58      // mport is equivalent to nclude
59      //
60      final AbstractParserState t = new LetterState(this, 't', postE, null);
61      final AbstractParserState r = new LetterState(this, 'r', t, null);
62      final AbstractParserState o = new LetterState(this, 'o', r, null);
63      final AbstractParserState p = new LetterState(this, 'p', o, null);
64      final AbstractParserState m = new LetterState(this, 'm', p, null);
65      //
66      // switch between
67      //
68      final AbstractParserState n_m = new BranchState(this, new char[] {
69          'n', 'm'
70      }, new AbstractParserState[] {
71          n, m
72      }, null);
73      final AbstractParserState i = new WhitespaceOrLetterState(this, 'i', n_m);
74      this.newLineState = new WhitespaceOrLetterState(this, '#', i);
75    }
76  
77    @Override
78    public void addFilename(final String include) {
79      this.includes.addElement(include);
80    }
81  
82    @Override
83    public String[] getIncludes() {
84      final String[] retval = new String[this.includes.size()];
85      this.includes.copyInto(retval);
86      return retval;
87    }
88  
89    @Override
90    public AbstractParserState getNewLineState() {
91      return this.newLineState;
92    }
93  
94    @Override
95    public void parse(final Reader reader) throws IOException {
96      this.includes.setSize(0);
97      super.parse(reader);
98    }
99  }