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