1 module zua.pattern;
2 import std.bitmanip;
3 
4 /** Denotes an error occurring at any point during the execution of a pattern */
5 final class PatternError : Exception {
6 	/** Create a new PatternError */
7 	this(string msg) {
8 		super(msg);
9 	}
10 }
11 
12 /** A pattern */
13 final class Pattern {
14 	/** Whether or not to anchor this pattern to the start of the subject string */
15 	bool startAnchor;
16 
17 	/** Whether or not to anchor this pattern to the end of the subject string */
18 	bool endAnchor;
19 
20 	/** The items that comprise this pattern */
21 	PatternItem[] items;
22 }
23 
24 /** An abstract pattern item */
25 abstract class PatternItem {}
26 
27 /** A capture */
28 final class Capture : PatternItem {
29 	/** The items to match in this capture */
30 	PatternItem[] items;
31 }
32 
33 /** The type of sequence to match*/
34 enum SequenceType {
35 	Greedy0, /// *
36 	Greedy1, /// +
37 	NonGreedy1, /// -
38 	Maybe, /// ?
39 }
40 
41 /** Matches a sequence */
42 final class SequenceMatch : PatternItem {
43 	/** The type of sequence to match */
44 	SequenceType type;
45 
46 	/** The character class to match */
47 	CharClass charClass;
48 }
49 
50 /** Matches a pattern that has been previously captured */
51 final class CaptureMatch : PatternItem {
52 	/** The 0-based index of the capture to match */
53 	int index;
54 }
55 
56 /** Matches a balanced string */
57 final class BalancedMatch : PatternItem {
58 	/** The left character */
59 	char left;
60 
61 	/** The right character */
62 	char right;
63 }
64 
65 /** An abstract character class */
66 abstract class CharClass : PatternItem {}
67 
68 /** A literal character class */
69 final class LiteralChar : CharClass {
70 	/** The character value to match */
71 	char value;
72 }
73 
74 /** Represents a character set */
75 final class SetClass : CharClass {
76 	/** Holds the contents of this set class */
77 	BitArray set;
78 
79 	/** Create a new SetClass */
80 	this() {
81 		set.length = 256;
82 	}
83 
84 	/** Create a new SetClass, including the given characters in the set */
85 	this(string str) {
86 		this();
87 		foreach (c; str) {
88 			set[cast(ubyte)c] = true;
89 		}
90 	}
91 }