update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / source / parsing / GTTokens.java
blob7793e0f020b10b2528fd81672bd5fdf8cd6eb4fa
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.lang.ASTFactory;
19 import com.intellij.lexer.Lexer;
20 import com.intellij.lexer.LexerPosition;
21 import com.intellij.openapi.diagnostic.Logger;
22 import com.intellij.psi.JavaTokenType;
23 import com.intellij.psi.impl.source.tree.TreeElement;
24 import com.intellij.psi.tree.IElementType;
25 import com.intellij.util.CharTable;
27 /**
28 * @author dsl
30 class GTTokens implements JavaTokenType {
31 private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.parsing.GTTokens");
32 static IElementType getTokenType(Lexer lexer) {
33 if (lexer.getTokenType() != GT) return lexer.getTokenType();
34 final LexerPosition originalPosition = lexer.getCurrentPosition();
35 final int prevTokenEnd = lexer.getTokenEnd();
36 lexer.advance();
37 if (lexer.getTokenStart() != prevTokenEnd) {
38 lexer.restore(originalPosition);
39 return GT;
41 final IElementType resultType;
42 IElementType i1 = lexer.getTokenType();
43 if (i1 == EQ) {
44 resultType = GE;
46 else if (i1 == GT) {
48 final int prevTokenEnd2 = lexer.getTokenEnd();
49 lexer.advance();
50 if (lexer.getTokenStart() != prevTokenEnd2) {
51 resultType = GTGT;
53 else {
54 IElementType i = lexer.getTokenType();
55 if (i == GT) {
56 final int prevTokenEnd3 = lexer.getTokenEnd();
57 lexer.advance();
58 if (lexer.getTokenStart() != prevTokenEnd3 || lexer.getTokenType() != EQ) {
59 resultType = GTGTGT;
61 else {
62 resultType = GTGTGTEQ;
65 else if (i == EQ) {
66 resultType = GTGTEQ;
68 else {
69 resultType = GTGT;
74 else {
75 resultType = GT;
77 lexer.restore(originalPosition);
78 return resultType;
81 static TreeElement createTokenElementAndAdvance(IElementType tokenType, Lexer lexer, CharTable table) {
82 final TreeElement result;
83 if (tokenType == GTGT || tokenType == GE) {
84 result = mergeTokens(1, lexer, tokenType, table);
86 else if (tokenType == GTGTEQ || tokenType == GTGTGT) {
87 result = mergeTokens(2, lexer, tokenType, table);
89 else if (tokenType == GTGTGTEQ) {
90 result = mergeTokens(3, lexer, tokenType, table);
92 else {
93 LOG.assertTrue(tokenType == lexer.getTokenType());
94 result = ParseUtil.createTokenElement(lexer, table);
96 lexer.advance();
97 return result;
100 static void advance(IElementType tokenType, Lexer lexer) {
101 if(lexer.getTokenType() != GT){
102 lexer.advance();
104 else {
105 if (tokenType == GTGTGTEQ) {
106 lexer.advance();
107 lexer.advance();
108 lexer.advance();
110 else if (tokenType == GTGTEQ || tokenType == GTGTGT) {
111 lexer.advance();
112 lexer.advance();
114 else if (tokenType == GTGT || tokenType == GE) {
115 lexer.advance();
117 lexer.advance();
121 private static TreeElement mergeTokens(int nTokens, Lexer lexer, IElementType tokenType, CharTable table) {
122 final int tokenStart = lexer.getTokenStart();
123 for (int i = 0; i < nTokens; i++) {
124 lexer.advance();
126 final int tokenEnd = lexer.getTokenEnd();
127 return ASTFactory.leaf(tokenType, table.intern(lexer.getBufferSequence(), tokenStart, tokenEnd));