org.sonar.javascript.parser.sslr.SyntaxTreeCreator Maven / Gradle / Ivy
The newest version!
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011 SonarSource and Eriks Nukis
* [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.javascript.parser.sslr;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.GenericTokenType;
import com.sonar.sslr.api.Token;
import com.sonar.sslr.api.TokenType;
import com.sonar.sslr.api.Trivia;
import com.sonar.sslr.api.Trivia.TriviaKind;
import org.sonar.sslr.grammar.GrammarRuleKey;
import org.sonar.sslr.internal.grammar.MutableParsingRule;
import org.sonar.sslr.internal.matchers.ParseNode;
import org.sonar.sslr.internal.vm.TokenExpression;
import org.sonar.sslr.internal.vm.TriviaExpression;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
public class SyntaxTreeCreator {
private static final TokenType UNDEFINED_TOKEN_TYPE = new TokenType() {
@Override
public String getName() {
return "TOKEN";
}
@Override
public String getValue() {
return getName();
}
@Override
public boolean hasToBeSkippedFromAst(AstNode node) {
return false;
}
@Override
public String toString() {
return SyntaxTreeCreator.class.getSimpleName();
}
};
private final Object treeFactory;
private final ActionParser2.GrammarBuilderInterceptor mapping;
private final Token.Builder tokenBuilder = Token.builder();
private final List trivias = Lists.newArrayList();
private Input input;
public SyntaxTreeCreator(Object treeFactory, ActionParser2.GrammarBuilderInterceptor mapping) {
this.treeFactory = treeFactory;
this.mapping = mapping;
}
public T create(ParseNode node, Input input) {
this.input = input;
this.trivias.clear();
T result = (T) visit(node);
if (result instanceof AstNode) {
((AstNode) result).hasToBeSkippedFromAst();
}
return result;
}
private Object visit(ParseNode node) {
if (node.getMatcher() instanceof MutableParsingRule) {
return visitNonTerminal(node);
} else {
return visitTerminal(node);
}
}
private Object visitNonTerminal(ParseNode node) {
MutableParsingRule rule = (MutableParsingRule) node.getMatcher();
GrammarRuleKey ruleKey = rule.getRuleKey();
if (mapping.hasMethodForRuleKey(ruleKey)) {
// TODO Drop useless intermediate nodes
Preconditions.checkState(node.getChildren().size() == 1);
return visit(node.getChildren().get(0));
}
if (mapping.isOptionalRule(ruleKey)) {
Preconditions.checkState(node.getChildren().size() <= 1);
if (node.getChildren().isEmpty()) {
return Optional.absent();
} else {
Object child = visit(node.getChildren().get(0));
if (child instanceof AstNode) {
((AstNode) child).hasToBeSkippedFromAst();
}
return Optional.of(child);
}
}
List children = node.getChildren();
List