update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / source / parsing / WhiteSpaceAndCommentsProcessor.java
blob7c81de4f301fb5817fe4b1e8c457bf27983052b4
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.psi.impl.source.parsing;
18 import com.intellij.psi.tree.TokenSet;
19 import com.intellij.psi.tree.IElementType;
20 import com.intellij.psi.impl.source.tree.StdTokenSets;
21 import com.intellij.psi.impl.source.tree.TreeElement;
22 import com.intellij.psi.impl.source.ParsingContext;
23 import com.intellij.lexer.Lexer;
24 import com.intellij.openapi.diagnostic.Logger;
26 public class WhiteSpaceAndCommentsProcessor implements TokenProcessor {
27 private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.parsing.WhiteSpaceAndCommentsProcessor");
29 public static final TokenProcessor INSTANCE = new WhiteSpaceAndCommentsProcessor();
30 private final TokenSet myWhitespaceSet;
32 public WhiteSpaceAndCommentsProcessor() {
33 this(StdTokenSets.WHITE_SPACE_OR_COMMENT_BIT_SET);
36 public WhiteSpaceAndCommentsProcessor(TokenSet whitespaceSet) {
37 myWhitespaceSet = whitespaceSet;
40 public TreeElement process(Lexer lexer, ParsingContext context) {
41 TreeElement first = null;
42 TreeElement last = null;
43 while (isTokenValid(lexer.getTokenType())) {
44 TreeElement tokenElement = createToken(lexer, context);
45 IElementType type = lexer.getTokenType();
46 if (!myWhitespaceSet.contains(type)) {
47 LOG.error("Missed token should be white space or comment:" + tokenElement);
48 throw new RuntimeException();
50 if (last != null) {
51 last.setTreeNext(tokenElement);
52 tokenElement.setTreePrev(last);
53 last = tokenElement;
55 else {
56 first = last = tokenElement;
58 lexer.advance();
60 return first;
63 protected TreeElement createToken(final Lexer lexer, final ParsingContext context) {
64 return ParseUtil.createTokenElement(lexer, context.getCharTable());
67 public boolean isTokenValid(IElementType tokenType) {
68 return tokenType != null && myWhitespaceSet.contains(tokenType);