aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java
blob: 2c93f35fc0bca0bf65660ccc46bff8ebf4d3f5ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2011 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.channel;

import java.io.IOException;
import java.io.Reader;
import java.util.regex.Matcher;

/**
 * The CodeReader class provides all the basic features to lex a source code. Those features are :
 * <ul>
 * <li>Read and consume next characters until a regular expression is matched</li>
 * </ul>
 */
public class CodeReader extends CodeBuffer {

  private Cursor previousCursor;

  /*
   * Constructor needed to be backward compatible (before using CodeReaderFilter)
   */
  public CodeReader(Reader code) {
    super(code, new CodeReaderConfiguration());
  }

  /*
   * Constructor needed to be backward compatible (before using CodeReaderFilter)
   */
  public CodeReader(String code) {
    super(code, new CodeReaderConfiguration());
  }

  /**
   * Creates a code reader with specific configuration parameters.
   * 
   * @param code
   *          the Reader to read code from
   * @param configuration
   *          the configuration parameters
   */
  public CodeReader(Reader code, CodeReaderConfiguration configuration) {
    super(code, configuration);
  }

  /**
   * Creates a code reader with specific configuration parameters.
   * 
   * @param code
   *          the code itself
   * @param configuration
   *          the configuration parameters
   */
  public CodeReader(String code, CodeReaderConfiguration configuration) {
    super(code, configuration);
  }

  /**
   * Read and consume the next character
   * 
   * @param appendable
   *          the read character is appended to appendable
   */
  public final void pop(Appendable appendable) {
    try {
      appendable.append((char) pop());
    } catch (IOException e) {
      throw new ChannelException(e.getMessage());
    }
  }

  /**
   * Read without consuming the next characters
   * 
   * @param length
   *          number of character to read
   * @return array of characters
   */
  public final char[] peek(int length) {
    char[] result = new char[length];
    int index = 0;
    int nextChar = intAt(index);
    while (nextChar != -1 && index < length) {
      result[index] = (char) nextChar;
      nextChar = intAt(++index);
    }
    return result;
  }

  /**
   * Read without consuming the next characters until a condition is reached (EndMatcher)
   * 
   * @param matcher
   *          the EndMatcher used to stop the reading
   * @param appendable
   *          the read characters is appended to appendable
   */
  public final void peekTo(EndMatcher matcher, Appendable appendable) {
    int index = 0;
    char nextChar = charAt(index);
    try {
      while ( !matcher.match(nextChar) && nextChar != -1) {
        appendable.append(nextChar);
        nextChar = charAt(++index);
      }
    } catch (IOException e) {
      throw new ChannelException(e.getMessage(), e);
    }
  }

  /**
   * @see peekTo(EndMatcher matcher, Appendable appendable)
   */
  @Deprecated
  public final String peekTo(EndMatcher matcher) {
    StringBuilder sb = new StringBuilder();
    peekTo(matcher, sb);
    return sb.toString();
  }

  /**
   * @see popTo(Matcher matcher, Appendable appendable)
   */
  @Deprecated
  public final void popTo(EndMatcher matcher, Appendable appendable) {
    previousCursor = getCursor().clone();
    try {
      do {
        appendable.append((char) pop());
      } while ( !matcher.match(peek()) && peek() != -1);
    } catch (IOException e) {
      throw new ChannelException(e.getMessage(), e);
    }
  }

  /**
   * Read and consume the next characters according to a given regular expression
   * 
   * @param matcher
   *          the regular expression matcher
   * @param appendable
   *          the consumed characters are appended to this appendable
   * @return number of consumed characters or -1 if the next input sequence doesn't match this matcher's pattern
   */
  public final int popTo(Matcher matcher, Appendable appendable) {
    return popTo(matcher, null, appendable);
  }

  /**
   * Read and consume the next characters according to a given regular expression. Moreover the character sequence immediately following the
   * desired characters must also match a given regular expression.
   * 
   * @param matcher
   *          the Matcher used to try consuming next characters
   * @param afterMatcher
   *          the Matcher used to check character sequence immediately following the consumed characters
   * @param appendable
   *          the consumed characters are appended to this appendable
   * @return number of consumed characters or -1 if one of the two Matchers doesn't match
   */
  public final int popTo(Matcher matcher, Matcher afterMatcher, Appendable appendable) {
    try {
      matcher.reset(this);
      if (matcher.lookingAt()) {
        if (afterMatcher != null) {
          afterMatcher.reset(this);
          afterMatcher.region(matcher.end(), length());
          if ( !afterMatcher.lookingAt()) {
            return -1;
          }
        }
        previousCursor = getCursor().clone();
        for (int i = 0; i < matcher.end(); i++) {
          appendable.append((char) pop());
        }
        return matcher.end();
      }
    } catch (IndexOutOfBoundsException e) {
      return -1;
    } catch (IOException e) {
      throw new ChannelException(e.getMessage(), e);
    }
    return -1;
  }

  public final Cursor getPreviousCursor() {
    return previousCursor;
  }
}