/*
* Copyright 2011-2014 Gregory Shrago
*
* 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.jetbrains.dokka.Markdown;
import com.intellij.lang.*;
import com.intellij.lang.impl.PsiBuilderAdapter;
import com.intellij.lang.impl.PsiBuilderImpl;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringHash;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.TokenType;
import com.intellij.psi.impl.source.resolve.FileContextUtil;
import com.intellij.psi.impl.source.tree.CompositePsiElement;
import com.intellij.psi.tree.ICompositeElementType;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.util.Function;
import com.intellij.util.PairProcessor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.LimitedPool;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
/**
* @author gregsh
*/
public class GeneratedParserUtilBase {
private static final Logger LOG = Logger.getInstance("org.intellij.grammar.parser.GeneratedParserUtilBase");
private static final int MAX_RECURSION_LEVEL = 1000;
private static final int MAX_VARIANTS_SIZE = 10000;
private static final int MAX_VARIANTS_TO_DISPLAY = 50;
private static final int INITIAL_VARIANTS_SIZE = 1000;
private static final int VARIANTS_POOL_SIZE = 10000;
private static final int FRAMES_POOL_SIZE = 500;
public static final IElementType DUMMY_BLOCK = new DummyBlockElementType();
public interface Parser {
boolean parse(PsiBuilder builder, int level);
}
public static final Parser TOKEN_ADVANCER = new Parser() {
@Override
public boolean parse(PsiBuilder builder, int level) {
if (builder.eof()) return false;
builder.advanceLexer();
return true;
}
};
public static final Parser TRUE_CONDITION = new Parser() {
@Override
public boolean parse(PsiBuilder builder, int level) {
return true;
}
};
public static boolean eof(PsiBuilder builder_, int level_) {
return builder_.eof();
}
public static int current_position_(PsiBuilder builder_) {
return builder_.rawTokenIndex();
}
public static boolean recursion_guard_(PsiBuilder builder_, int level_, String funcName_) {
if (level_ > MAX_RECURSION_LEVEL) {
builder_.error("Maximum recursion level (" + MAX_RECURSION_LEVEL + ") reached in '" + funcName_ + "'");
return false;
}
return true;
}
public static boolean empty_element_parsed_guard_(PsiBuilder builder_, String funcName_, int prev_position_) {
if (prev_position_ == current_position_(builder_)) {
builder_.error("Empty element parsed in '" + funcName_ + "' at offset " + builder_.getCurrentOffset());
return false;
}
return true;
}
public static boolean invalid_left_marker_guard_(PsiBuilder builder_, PsiBuilder.Marker marker_, String funcName_) {
//builder_.error("Invalid left marker encountered in " + funcName_ +" at offset " + builder_.getCurrentOffset());
boolean goodMarker = marker_ != null; // && ((LighterASTNode)marker_).getTokenType() != TokenType.ERROR_ELEMENT;
if (!goodMarker) return false;
ErrorState state = ErrorState.get(builder_);
return !state.frameStack.isEmpty();
}
public static TokenSet create_token_set_(IElementType... tokenTypes_) {
return TokenSet.create(tokenTypes_);
}
private static boolean consumeTokens(PsiBuilder builder_, boolean smart, int pin, IElementType... tokens) {
ErrorState state =