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