eu.cqse.check.framework.shallowparser.languages.groovy.GroovyFieldRecognizer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-check-api Show documentation
Show all versions of teamscale-check-api Show documentation
The Teamscale Custom Check API allows users to extend Teamscale by writing custom analyses that create findings.
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.cqse.check.framework.shallowparser.languages.groovy;
import static eu.cqse.check.framework.scanner.ETokenType.COMMA;
import static eu.cqse.check.framework.scanner.ETokenType.EQ;
import static eu.cqse.check.framework.scanner.ETokenType.SEMICOLON;
import static eu.cqse.check.framework.scanner.ETokenType.SWITCH;
import java.util.EnumSet;
import java.util.List;
import eu.cqse.check.framework.scanner.ETokenType;
import eu.cqse.check.framework.scanner.IToken;
import eu.cqse.check.framework.shallowparser.framework.ParserState;
import eu.cqse.check.framework.shallowparser.framework.RecognizerBase;
/**
* Recognizer that helps to match fields in Groovy. An extra recognizer is necessary because we have
* to check for a new line to begin.
*/
/* package */ class GroovyFieldRecognizer extends RecognizerBase {
/** The allowed token types after a field declaration. */
private static final EnumSet ALLOWED_TYPES = EnumSet.of(EQ, COMMA, SEMICOLON, SWITCH);
/** {@inheritDoc} */
@Override
protected int matchesLocally(ParserState parserState, List tokens,
int startOffset) {
if (startOffset < 1 || startOffset >= tokens.size()) {
return NO_MATCH;
}
IToken token = tokens.get(startOffset);
IToken lastToken = tokens.get(startOffset - 1);
if (ALLOWED_TYPES.contains(token.getType()) || lastToken.getLineNumber() < token.getLineNumber()) {
return startOffset + 1;
}
return NO_MATCH;
}
}