org.btrplace.btrpsl.tree.BtrPlaceTree Maven / Gradle / Ivy
/*
* Copyright 2020 The BtrPlace Authors. All rights reserved.
* Use of this source code is governed by a LGPL-style
* license that can be found in the LICENSE.txt file.
*/
package org.btrplace.btrpsl.tree;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.CommonTree;
import org.btrplace.btrpsl.ErrorReporter;
import org.btrplace.btrpsl.element.BtrpOperand;
import org.btrplace.btrpsl.element.IgnorableOperand;
/**
* An abstract parser for a tree.
*
* @author Fabien Hermenier
*/
public class BtrPlaceTree extends CommonTree {
/**
* All the errors to report.
*/
protected final ErrorReporter errors;
/**
* Make a new tree.
*
* @param t the token to handle. The root of this tree
* @param errs the errors to report
*/
public BtrPlaceTree(Token t, ErrorReporter errs) {
super(t);
errors = errs;
}
/**
* Parse the root of the tree.
*
* @param parent the parent of the root
* @return a content
*/
public BtrpOperand go(BtrPlaceTree parent) {
append(token, "Unhandled token " + token.getText() + " (type=" + token.getType() + ")");
return IgnorableOperand.getInstance();
}
/**
* Report an error for the current token and generate a content to ignore.
*
* @param msg the error message
* @return an empty content
*/
public IgnorableOperand ignoreError(String msg) {
append(token, msg);
return IgnorableOperand.getInstance();
}
/**
* Report an error from an exception for the current token
*
* @param e the exception
* @return an empty operand
*/
public IgnorableOperand ignoreError(Exception e) {
append(token, e.getMessage());
return IgnorableOperand.getInstance();
}
/**
* Add all the error messages included in a reporter.
*
* @param err the error reporter
* @return {@link IgnorableOperand} to indicate to skip the operand
*/
public IgnorableOperand ignoreErrors(ErrorReporter err) {
errors.getErrors().addAll(err.getErrors());
return IgnorableOperand.getInstance();
}
/**
* Add an error message to a given error
*
* @param t the token that points to the error
* @param msg the error message
* @return {@link IgnorableOperand} to indicate to skip the operand
*/
public IgnorableOperand ignoreError(Token t, String msg) {
append(t, msg);
return IgnorableOperand.getInstance();
}
/**
* Add an error message related to a given token
*
* @param t the token that points to the error
* @param msg the error message
*/
public void append(Token t, String msg) {
errors.append(t.getLine(), t.getCharPositionInLine(), msg);
}
@Override
public BtrPlaceTree getChild(int i) {
return (BtrPlaceTree) super.getChild(i);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy