org.sonar.flex.checks.SyntacticEquivalence Maven / Gradle / Ivy
/*
* SonarQube Flex Plugin
* Copyright (C) 2010-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* 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 02110-1301, USA.
*/
package org.sonar.flex.checks;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Token;
import java.util.List;
public class SyntacticEquivalence {
private SyntacticEquivalence() {
}
public static boolean areEquivalent(AstNode node1, AstNode node2) {
if (!node1.getType().equals(node2.getType()) || node1.getNumberOfChildren() != node2.getNumberOfChildren()) {
return false;
}
if (node1.getNumberOfChildren() == 0) {
Token token1 = node1.getToken();
Token token2 = node2.getToken();
return (token1 == null && token2 == null) ||
(token1 != null && token2 != null && token1.getValue().equals(token2.getValue()));
}
List children1 = node1.getChildren();
List children2 = node2.getChildren();
for (int i = 0; i < children1.size(); i++) {
if (!areEquivalent(children1.get(i), children2.get(i))) {
return false;
}
}
return true;
}
public static boolean areEquivalent(List list1, List list2) {
if (list1.size() != list2.size()) {
return false;
}
for (int i = 0; i < list1.size(); i++) {
if (!areEquivalent(list1.get(i), list2.get(i))) {
return false;
}
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy