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  /**
23   * This parser state checks consumed characters against a specific character or
24   * whitespace.
25   *
26   * @author Curt Arnold
27   */
28  public final class WhitespaceOrLetterState extends AbstractParserState {
29    /**
30     * Next state if the character is found.
31     */
32    private final AbstractParserState nextState;
33  
34    /**
35     * Character to match.
36     */
37    private final char thisLetter;
38  
39    /**
40     * Constructor.
41     *
42     * @param parser
43     *          parser
44     * @param matchLetter
45     *          letter to match
46     * @param nextStateArg
47     *          next state if a match on the letter
48     */
49    public WhitespaceOrLetterState(final AbstractParser parser, final char matchLetter,
50        final AbstractParserState nextStateArg) {
51      super(parser);
52      this.thisLetter = matchLetter;
53      this.nextState = nextStateArg;
54    }
55  
56    /**
57     * Consumes a character and returns the next state for the parser.
58     *
59     * @param ch
60     *          next character @returns the configured nextState if ch is the
61     *          expected character or the configure noMatchState otherwise.
62     * @return next state
63     */
64    @Override
65    public AbstractParserState consume(final char ch) {
66      if (ch == this.thisLetter) {
67        return this.nextState;
68      }
69      if (ch == ' ' || ch == '\t') {
70        return this;
71      }
72      if (ch == '\n') {
73        getParser().getNewLineState();
74      }
75      return null;
76    }
77  }