/* * Copyright (c) 2013, the Dart project authors. * * Licensed under the Eclipse Public License v1.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.eclipse.org/legal/epl-v10.html * * 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 com.google.dart.engine.services.internal.util; import com.google.common.collect.Lists; import com.google.dart.engine.scanner.CharSequenceReader; import com.google.dart.engine.scanner.Keyword; import com.google.dart.engine.scanner.KeywordToken; import com.google.dart.engine.scanner.Scanner; import com.google.dart.engine.scanner.Token; import com.google.dart.engine.scanner.TokenType; import java.util.List; /** * Utilities to work with {@link Token}s. */ public class TokenUtils { /** * @return the first {@link KeywordToken} with given {@link Keyword}, may be <code>null</code> if * not found. */ public static KeywordToken findKeywordToken(List<Token> tokens, Keyword keyword) { for (Token token : tokens) { if (token instanceof KeywordToken) { KeywordToken keywordToken = (KeywordToken) token; if (keywordToken.getKeyword() == keyword) { return keywordToken; } } } return null; } /** * @return the first {@link Token} with given {@link TokenType}, may be <code>null</code> if not * found. */ public static Token findToken(List<Token> tokens, TokenType type) { for (Token token : tokens) { if (token.getType() == type) { return token; } } return null; } /** * @return {@link Token}s of the given Dart source, not <code>null</code>, may be empty if no * tokens or some exception happens. */ public static List<Token> getTokens(final String s) { try { List<Token> tokens = Lists.newArrayList(); Scanner scanner = new Scanner(null, new CharSequenceReader(s), null); Token token = scanner.tokenize(); while (token.getType() != TokenType.EOF) { tokens.add(token); token = token.getNext(); } return tokens; } catch (Throwable e) { return Lists.newArrayList(); } } /** * @return <code>true</code> if given {@link Token}s contain only single {@link Token} with given * {@link TokenType}. */ public static boolean hasOnly(List<Token> tokens, TokenType type) { return tokens.size() == 1 && tokens.get(0).getType() == type; } }