Skip to content

Commit

Permalink
Added javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Apr 23, 2024
1 parent 98b4c67 commit f89a931
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

import org.antlr.v4.runtime.ParserRuleContext;

/**
* Delegates visiting a {@link ParserRuleContext} to a different {@link ContextVisitor} derived by the given mapper
* function
* @param <T> The original antlr type visited
* @param <V> The target {@link ParserRuleContext} to visit instead
*/
public class ContextDelegateVisitor<T, V extends ParserRuleContext> extends DelegateVisitor<T, V> {
private final ContextVisitor<V> contextVisitor;

/**
* @param delegate The visitor to delegate to
* @param mapper The mapper function used to derive the target antlr context
*/
public ContextDelegateVisitor(ContextVisitor<V> delegate, Function<T, V> mapper) {
super(delegate, mapper);
this.contextVisitor = delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,59 @@

import java.util.function.Function;

/**
* Delegates visiting of a given antlr entity to a visitor for a different antlr entity.
* @param <T> The original antlr type visited
* @param <V> The target antlr type
*/
public class DelegateVisitor<T, V> {
private final AbstractVisitor<V> delegate;
protected final Function<T, V> mapper;
private boolean mapOnExit;

/**
* @param delegate The target visitor to use
* @param mapper The mapper function used to derive the target entity
*/
public DelegateVisitor(AbstractVisitor<V> delegate, Function<T, V> mapper) {
this.delegate = delegate;
this.mapper = mapper;
this.mapOnExit = false;
}

/**
* Delegates entering the original context
* @param parentData The data of the original visitor
*/
public void delegateEnter(HandlerData<T> parentData) {
if (!this.mapOnExit) {
this.delegate.enter(parentData.derive(this.mapper));
}
}

/**
* Makes this visitor map exit events to enter events. Used mostly for mapping exit events to terminal nodes, which only
* provide enter events
*/
public void mapOnExit() {
this.mapOnExit = true;
}

/**
* Delegates exiting the original context
* @param parentData The data of the original visitor
*/
public void delegateExit(HandlerData<T> parentData) {
if (this.mapOnExit) {
this.delegate.enter(parentData.derive(this.mapper));
}
}

/**
* Checks if the target entity is present in the given antlr entity
* @param entity The original antlr entity
* @return is present
*/
public boolean isPresent(T entity) {
try {
return this.mapper.apply(entity) != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@
import de.jplag.golang.grammar.GoParser.TypeSwitchStmtContext;
import de.jplag.golang.grammar.GoParser.VarDeclContext;

/**
* Provides token extraction rules for {@link GoLanguage} Based on an older implementation of the language module; see
* JPlagGoListener.java in the history.
*/
public class GoListener extends AbstractAntlrListener {
public GoListener() {
metaDeclarations();
Expand Down

0 comments on commit f89a931

Please sign in to comment.