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

ftl.ast.BaseNode Maven / Gradle / Ivy

Go to download

A simple, small but powerful template engine based loosely on the FreeMarker syntax. FreshMarker is implemented in Java 21 and supports the `java.time` API and Records.

There is a newer version: 1.6.9
Show newest version
/* Generated by: CongoCC Parser Generator. Do not edit. BaseNode.java  */
package ftl.ast;

import ftl.*;
import static ftl.Token.TokenType.*;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Collection;
import java.util.Collections;


/**
* The base concrete class for non-terminal Nodes
*/
public class BaseNode implements Node {
    private FreshMarkerLexer tokenSource;

    public FreshMarkerLexer getTokenSource() {
        if (tokenSource == null) {
            for (Node child : children()) {
                if (child.getTokenSource() instanceof FreshMarkerLexer) tokenSource = (FreshMarkerLexer) child.getTokenSource();
                if (tokenSource != null) break;
            }
        }
        return tokenSource;
    }

    public void setTokenSource(TokenSource tokenSource) {
        this.tokenSource = (FreshMarkerLexer) tokenSource;
    }

    private static Class> listClass;

    /**
    * Sets the List class that is used to store child nodes. By default,
    * this is java.util.ArrayList. There is probably very little reason
    * to ever use anything else, though you could use this method
    * to replace this with LinkedList or your own java.util.List implementation even.
    * @param listClass the #java.util.List implementation to use internally
    * for the child nodes. By default #java.util.ArrayList is used.
    */
    public static void setListClass(Class> listClass) {
        BaseNode.listClass = listClass;
    }

    private List newList() {
        if (listClass == null) {
            return new ArrayList<>();
        }
        try {
            return listClass.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
    * the parent node
    */
    private Node parent;
    /**
    * the child nodes
    */
    private final List children = newList();
    private int beginOffset, endOffset;
    private boolean unparsed;

    public boolean isUnparsed() {
        return this.unparsed;
    }

    public void setUnparsed(boolean unparsed) {
        this.unparsed = unparsed;
    }

    public void setParent(Node n) {
        parent = n;
    }

    public Node getParent() {
        return parent;
    }

    public boolean add(Node n) {
        n.setParent(this);
        return children.add(n);
    }

    public void add(int i, Node n) {
        children.add(i, n);
        n.setParent(this);
    }

    public Node get(int i) {
        return children.get(i);
    }

    public Node set(int i, Node n) {
        children.get(i).setParent(null);
        n.setParent(this);
        return children.set(i, n);
    }

    public Node remove(int i) {
        Node n = children.remove(i);
        n.setParent(null);
        return n;
    }

    public boolean remove(Object obj) {
        Node n = (Node) obj;
        n.setParent(null);
        return children.remove(n);
    }

    public void clear() {
        for (Node child : children) {
            child.setParent(null);
        }
        children.clear();
    }

    public int size() {
        return children.size();
    }

    public List children() {
        return Collections.unmodifiableList(children);
    }

    public int getBeginOffset() {
        return beginOffset;
    }

    public void setBeginOffset(int beginOffset) {
        this.beginOffset = beginOffset;
    }

    public int getEndOffset() {
        return endOffset;
    }

    public void setEndOffset(int endOffset) {
        this.endOffset = endOffset;
    }

    public List subList(int from, int to) {
        return children.subList(from, to);
    }

    public List getRealTokens() {
        return descendants(Token.class, t -> !t.isUnparsed());
    }

    public boolean addAll(Collection nodes) {
        for (Node n : nodes)n.setParent(this);
        return children.addAll(nodes);
    }

    public boolean addAll(int i, Collection nodes) {
        for (Node n : nodes)n.setParent(this);
        return children.addAll(i, nodes);
    }

    public boolean containsAll(Collection nodes) {
        return children.containsAll(nodes);
    }

    public boolean retainAll(Collection nodes) {
        return children.containsAll(nodes);
    }

    public boolean removeAll(Collection nodes) {
        return children.removeAll(nodes);
    }

    public String toString() {
        return getSource();
    }

    private Map namedChildMap;
    private Map> namedChildListMap;

    public Node getNamedChild(String name) {
        if (namedChildMap == null) {
            return null;
        }
        return namedChildMap.get(name);
    }

    public void setNamedChild(String name, Node node) {
        if (namedChildMap == null) {
            namedChildMap = new HashMap<>();
        }
        if (namedChildMap.containsKey(name)) {
            // Can't have duplicates
            String msg = String.format("Duplicate named child not allowed: {0}", name);
            throw new RuntimeException(msg);
        }
        namedChildMap.put(name, node);
    }

    public List getNamedChildList(String name) {
        if (namedChildListMap == null) {
            return null;
        }
        return namedChildListMap.get(name);
    }

    public void addToNamedChildList(String name, Node node) {
        if (namedChildListMap == null) {
            namedChildListMap = new HashMap<>();
        }
        List nodeList = namedChildListMap.computeIfAbsent(name, k -> new ArrayList<>());
        nodeList.add(node);
    }

}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy