001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 the ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package eu.cqse.check.framework.shallowparser.languages.base; 018 019import static eu.cqse.check.framework.scanner.ETokenType.IDENTIFIER; 020import static eu.cqse.check.framework.scanner.ETokenType.LPAREN; 021import static eu.cqse.check.framework.scanner.ETokenType.RPAREN; 022import static eu.cqse.check.framework.shallowparser.framework.RecognizerBase.NO_MATCH; 023import static eu.cqse.check.framework.shallowparser.languages.base.EGenericParserStates.IN_EXPRESSION; 024 025import java.util.List; 026 027import eu.cqse.check.framework.scanner.ETokenType; 028import eu.cqse.check.framework.scanner.IToken; 029import eu.cqse.check.framework.shallowparser.TokenStreamUtils; 030import eu.cqse.check.framework.shallowparser.framework.ParserState; 031import eu.cqse.check.framework.shallowparser.framework.RecognizerBase; 032 033/** 034 * Utilities for matching token sequences in recognizers. 035 */ 036public class MatcherUtils { 037 038 /** 039 * Tries to match a lambda with arrow and returns the index after the match into 040 * the token stream. If it could not be matched {@link RecognizerBase#NO_MATCH} 041 * is returned. 042 */ 043 public static int matchesLambdaWithArrow(ParserState<EGenericParserStates> parserState, List<IToken> tokens, 044 int startOffset, ETokenType arrowType) { 045 boolean hasSimpleLambda = TokenStreamUtils.hasTokenTypeSequence(tokens, startOffset, IDENTIFIER, arrowType); 046 if (hasSimpleLambda) { 047 return parserState.parse(IN_EXPRESSION, tokens, startOffset); 048 } 049 050 if (TokenStreamUtils.hasTokenTypeSequence(tokens, startOffset, LPAREN)) { 051 int closingPosition = TokenStreamUtils.findMatchingClosingToken(tokens, startOffset + 1, LPAREN, RPAREN); 052 if (closingPosition == TokenStreamUtils.NOT_FOUND) { 053 return NO_MATCH; 054 } 055 056 if (TokenStreamUtils.hasTokenTypeSequence(tokens, closingPosition + 1, arrowType)) { 057 return parserState.parse(IN_EXPRESSION, tokens, startOffset); 058 } 059 } 060 061 return NO_MATCH; 062 } 063}