/*
 * Copyright 2013-2014 must-be.org
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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 org.mustbe.consulo.csharp.lang.parser;

import org.mustbe.consulo.csharp.lang.parser.exp.ExpressionParsing;
import com.intellij.lang.PsiBuilder;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import lombok.val;

/**
 * @author VISTALL
 * @since 28.11.13.
 */
public class UsingStatementParsing extends SharedParsingHelpers
{
	public static void parseUsingList(CSharpBuilderWrapper builder, PsiBuilder.Marker marker)
	{
		boolean empty = true;
		while(builder.getTokenType() == USING_KEYWORD)
		{
			parseUsing(builder);

			empty = false;
		}

		if(empty)
		{
			marker.drop();
		}
		else
		{
			marker.done(USING_LIST);
		}
	}

	public static void parseUsing(CSharpBuilderWrapper builder)
	{
		val marker = builder.mark();

		builder.advanceLexer();

		IElementType to = null;
		if(builder.getTokenType() == IDENTIFIER && builder.lookAhead(1) == EQ)
		{
			builder.advanceLexer();
			builder.advanceLexer();

			if(parseType(builder, STUB_SUPPORT) == null)
			{
				builder.error("Type expected");
			}
			to = TYPE_DEF_STATEMENT;
		}
		else if(builder.getTokenType() == STATIC_KEYWORD)
		{
			builder.advanceLexer();

			if(parseType(builder, STUB_SUPPORT) == null)
			{
				builder.error("Type expected");
			}
			to = USING_TYPE_STATEMENT;
		}
		else
		{
			ExpressionParsing.parseQualifiedReference(builder, null);
			to = USING_NAMESPACE_STATEMENT;
		}

		reportErrorUntil(builder, "';' expected", ourSemicolonSet, TokenSet.EMPTY);

		expect(builder, SEMICOLON, "';' expected");

		marker.done(to);
	}
}

@@ -706,16 +706,131 @@
 arsing;%0A
+import org.mustbe.consulo.csharp.lang.psi.CSharpTokenSets;%0Aimport org.mustbe.consulo.csharp.lang.psi.CSharpTokens;%0A
 import c
@@ -1073,16 +1073,214 @@
 lpers%0A%7B%0A
+%09public static final TokenSet ourStoppers = TokenSet.orSet(ourSemicolonSet, CSharpTokenSets.MODIFIERS, CSharpTokenSets.TYPE_DECLARATION_START,%0A%09%09%09TokenSet.create(DELEGATE_KEYWORD, CONST_KEYWORD));%0A%0A
 %09public 
@@ -1423,16 +1423,29 @@
 pe() == 
+CSharpTokens.
 USING_KE
@@ -1768,16 +1768,29 @@
 pe() == 
+CSharpTokens.
 IDENTIFI
@@ -1819,16 +1819,29 @@
 d(1) == 
+CSharpTokens.
 EQ)%0A%09%09%7B%0A
@@ -2057,16 +2057,29 @@
 pe() == 
+CSharpTokens.
 STATIC_K
@@ -2257,16 +2257,52 @@
 lse%0A%09%09%7B%0A
+%09%09%09to = USING_NAMESPACE_STATEMENT;%0A%0A
 %09%09%09Expre
@@ -2358,43 +2358,8 @@
 l);%0A
-%09%09%09to = USING_NAMESPACE_STATEMENT;%0A
 %09%09%7D%0A
@@ -2407,27 +2407,23 @@
 d%22, ourS
-emicolonSet
+toppers
 , TokenS
@@ -2452,16 +2452,29 @@
 uilder, 
+CSharpTokens.
 SEMICOLO

move smarter using failback parsing