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