1 /**
2  * Authors: Szabo Bogdan <szabobogdan@yahoo.com>
3  * Date: 3 9, 2015
4  * License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
5  * Copyright: Public Domain
6  */
7 module vibedav.parser;
8 
9 struct Parser {
10 	ParseToken[] tokens;
11 
12 	void start(string data) {
13 		foreach(i; 0..data.length) {
14 			bool isInside;
15 
16 			foreach(token; tokens)
17 				if(token.isInside)
18 					isInside = true;
19 
20 			if(!isInside) {
21 				foreach(token; tokens) {
22 					if(token.checkStart(data, i)) {
23 						token.isInside = true;
24 						token.start = i;
25 					}
26 				}
27 			} else {
28 				foreach(token; tokens) {
29 					if(token.isInside && token.checkEnd(data, i)) {
30 						token.isInside = false;
31 
32 						if(token.callback !is null)
33 							token.callback(data[token.start..i+1]);
34 					}
35 				}
36 			}
37 		}
38 	}
39 }
40 
41 abstract class ParseToken {
42 	alias ParseCallback = void delegate(string data);
43 
44 	bool isInside;
45 	ulong start;
46 
47 	bool checkStart(string data, ulong pos);
48 	bool checkEnd(string data, ulong pos);
49 
50 	ParseCallback callback;
51 }
52 
53 
54 class ParseInsideToken : ParseToken {
55 	char startChar;
56 	char endChar;
57 
58 	override bool checkStart(string data, ulong pos) {
59 		return data[pos] == startChar;
60 	}
61 	override bool checkEnd(string data, ulong pos) {
62 		return data[pos] == endChar;
63 	}
64 }
65 
66 class ParseStaticToken : ParseToken {
67 	alias ParseCallback = void delegate(string data);
68 
69 	bool isInside;
70 	ulong start;
71 	string value;
72 
73 	override bool checkStart(string data, ulong pos) {
74 		if(data.length - pos < value.length)
75 			return false;
76 
77 		return data[pos..pos+value.length] == value;
78 	}
79 	override bool checkEnd(string data, ulong pos) {
80 		if(pos < value.length)
81 			return false;
82 
83 		return data[pos-value.length..pos] == value;
84 	}
85 }