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.util; 018 019import static eu.cqse.check.framework.scanner.ETokenType.EQ; 020import static eu.cqse.check.framework.scanner.ETokenType.LBRACK; 021import static eu.cqse.check.framework.scanner.ETokenType.RBRACK; 022 023import java.util.ArrayList; 024import java.util.List; 025 026import org.conqat.lib.commons.collections.CollectionUtils; 027 028import eu.cqse.check.framework.scanner.ELanguage; 029import eu.cqse.check.framework.scanner.IToken; 030import eu.cqse.check.framework.shallowparser.TokenStreamUtils; 031import eu.cqse.check.framework.shallowparser.framework.ShallowEntity; 032 033/** 034 * Language feature parser for Matlab. 035 */ 036public class MatlabLanguageFeatureParser implements ILanguageFeatureParser { 037 038 /** 039 * Extracts a list of output parameter tokens from the given method entity. 040 */ 041 public List<IToken> extractOutputParameterTokens(ShallowEntity method) { 042 List<IToken> outputParameterTokens = getOutputParameterTokens(method); 043 List<IToken> result = new ArrayList<>(); 044 for (int i = 0; i < outputParameterTokens.size(); i += 2) { 045 result.add(outputParameterTokens.get(i)); 046 } 047 return result; 048 } 049 050 /** 051 * Returns the the tokens that specify output parameters from the given 052 * method (excluding "[" and "]") 053 */ 054 public List<IToken> getOutputParameterTokens(ShallowEntity method) { 055 List<IToken> tokens = method.ownStartTokens(); 056 057 int equalIndex = TokenStreamUtils.firstTokenOfType(tokens, EQ); 058 if (equalIndex == -1) { 059 return CollectionUtils.emptyList(); 060 } 061 062 int startIndex = 1; 063 int endIndex = equalIndex; 064 if (tokens.get(1).getType() == LBRACK && tokens.get(equalIndex - 1).getType() == RBRACK) { 065 startIndex++; 066 endIndex--; 067 } 068 069 return tokens.subList(startIndex, endIndex); 070 } 071 072 /** {@inheritDoc} */ 073 @Override 074 public ELanguage getLanguage() { 075 return ELanguage.MATLAB; 076 } 077}