1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-16 07:33:09 -08:00
Abacus/src/org/nwapw/abacus/lexing/pattern/PatternChain.java

76 lines
1.8 KiB
Java
Raw Normal View History

package org.nwapw.abacus.lexing.pattern;
2017-07-25 22:47:48 -07:00
/**
* A chain of nodes that can be treated as a single unit.
* Used during pattern compilation.
* @param <T> the type used to identify which pattern has been matched.
*/
public class PatternChain<T> {
2017-07-25 22:47:48 -07:00
/**
* The head node of the chain.
*/
public PatternNode<T> head;
2017-07-25 22:47:48 -07:00
/**
* The tail node of the chain.
*/
public PatternNode<T> tail;
2017-07-25 22:47:48 -07:00
/**
* Creates a new chain with the given start and end.
* @param head the start of the chain.
* @param tail the end of the chain.
*/
public PatternChain(PatternNode<T> head, PatternNode<T> tail){
this.head = head;
this.tail = tail;
}
2017-07-25 22:47:48 -07:00
/**
* Creates a chain that starts and ends with the same node.
* @param node the node to use.
*/
public PatternChain(PatternNode<T> node){
this(node, node);
}
2017-07-25 22:47:48 -07:00
/**
* Creates an empty chain.
*/
public PatternChain(){
this(null);
}
2017-07-25 22:47:48 -07:00
/**
* Appends the other chain to this one. This modifies
* the nodes, as well.
* If this chain is empty, it is set to the other.
* @param other the other chain to append.
*/
public void append(PatternChain<T> other){
if(other.head == null || tail == null) {
this.head = other.head;
this.tail = other.tail;
} else {
tail.outputStates.add(other.head);
tail = other.tail;
}
}
2017-07-25 22:47:48 -07:00
/**
* Appends a single node to this chain. This modifies
* the nodes, as well.
* If this chain is empty, it is set to the node.
* @param node the node to append to this chain.
*/
public void append(PatternNode<T> node){
if(tail == null){
head = tail = node;
} else {
tail.outputStates.add(node);
tail = node;
}
}
}