All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.claas.parser.visitors.ConcatenateTerminals Maven / Gradle / Ivy

package de.claas.parser.visitors;

import java.util.HashSet;
import java.util.Set;

import de.claas.parser.Node;
import de.claas.parser.NodeVisitor;
import de.claas.parser.exceptions.CyclicNodeException;
import de.claas.parser.results.IntermediateNode;
import de.claas.parser.results.NonTerminalNode;
import de.claas.parser.results.TerminalNode;

/**
 * The class {@link ConcatenateTerminals}. It is an implementation of the
 * interface {@link NodeVisitor}. It is intended to collect and concatenate
 * terminal symbols of the {@link Node} tree, that are being visited.
 * 

* This visitor is meant for one-time use, only. As such, every tree needs to be * concatenated with a separate instance of this visitor. An instance of this * visitor should not be used to concatenate multiple trees. * * @author Claas Ahlrichs */ public class ConcatenateTerminals implements NodeVisitor { private final Set visitedPath = new HashSet<>(); private final StringBuilder data = new StringBuilder(); @Override public void visitTerminalNode(TerminalNode node) { this.data.append(node.getTerminal()); } @Override public void visitIntermediateNode(IntermediateNode node) { if (this.visitedPath.add(node)) { for (Node child : node) { child.visit(this); } this.visitedPath.remove(node); } else { throw new CyclicNodeException(node); } } @Override public void visitNonTerminaNode(NonTerminalNode node) { if (this.visitedPath.add(node)) { for (Node child : node) { child.visit(this); } this.visitedPath.remove(node); } else { throw new CyclicNodeException(node); } } @Override public String toString() { return this.data.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy