| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  | package org.nwapw.abacus.lexing.pattern;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import java.util.ArrayList;
 | 
					
						
							| 
									
										
										
										
											2017-07-24 20:45:56 -07:00
										 |  |  | import java.util.Collection;
 | 
					
						
							| 
									
										
										
										
											2017-07-24 19:46:22 -07:00
										 |  |  | import java.util.HashSet;
 | 
					
						
							| 
									
										
										
										
											2017-07-27 18:19:12 -07:00
										 |  |  | import java.util.Set;
 | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 22:47:48 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * A base class for a pattern node. Provides all functions
 | 
					
						
							|  |  |  |  * necessary for matching, and is constructed by a Pattern instance
 | 
					
						
							|  |  |  |  * from a string.
 | 
					
						
							|  |  |  |  * @param <T> the type that's used to tell which pattern this node belongs to.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  | public class PatternNode<T> {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 22:47:48 -07:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * The set of states to which the lexer should continue
 | 
					
						
							|  |  |  |      * should this node be correctly matched.
 | 
					
						
							|  |  |  |      */
 | 
					
						
							| 
									
										
										
										
											2017-07-27 18:19:12 -07:00
										 |  |  |     protected Set<PatternNode<T>> outputStates;
 | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 22:47:48 -07:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Creates a new pattern node.
 | 
					
						
							|  |  |  |      */
 | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  |     public PatternNode(){
 | 
					
						
							| 
									
										
										
										
											2017-07-24 19:46:22 -07:00
										 |  |  |         outputStates = new HashSet<>();
 | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 22:47:48 -07:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Determines whether the current input character can
 | 
					
						
							|  |  |  |      * be matched by this node.
 | 
					
						
							|  |  |  |      * @param other the character being matched.
 | 
					
						
							|  |  |  |      * @return true if the character can be matched, false otherwise.
 | 
					
						
							|  |  |  |      */
 | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  |     public boolean matches(char other){
 | 
					
						
							|  |  |  |         return false;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 22:47:48 -07:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * If this node can be used as part of a range, returns that value.
 | 
					
						
							|  |  |  |      * @return a NULL terminator if this character cannot be converted
 | 
					
						
							|  |  |  |      * into a range bound, or the appropriate range bound if it can.
 | 
					
						
							|  |  |  |      */
 | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  |     public char range(){
 | 
					
						
							|  |  |  |         return '\0';
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 22:47:48 -07:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Adds this node in a collection of other nodes.
 | 
					
						
							|  |  |  |      * @param into the collection to add into.
 | 
					
						
							|  |  |  |      */
 | 
					
						
							| 
									
										
										
										
											2017-07-24 20:45:56 -07:00
										 |  |  |     public void addInto(Collection<PatternNode<T>> into){
 | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  |         into.add(this);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 22:47:48 -07:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Adds the node's children into a collection of other nodes.
 | 
					
						
							|  |  |  |      * @param into the collection to add into.
 | 
					
						
							|  |  |  |      */
 | 
					
						
							| 
									
										
										
										
											2017-07-24 20:45:56 -07:00
										 |  |  |     public void addOutputsInto(Collection<PatternNode<T>> into){
 | 
					
						
							|  |  |  |         outputStates.forEach(e -> e.addInto(into));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 -07:00
										 |  |  | }
 |