PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/tools/dspec/feeder.d

http://github.com/wilkie/djehuty
D | 89 lines | 52 code | 21 blank | 16 comment | 6 complexity | a117f4287e70bd95e5f2f165e34a2e67 MD5 | raw file
 1module feeder;
 2
 3import core.string;
 4import core.unicode;
 5import core.definitions;
 6
 7import io.file;
 8import io.console;
 9
10char[] delims = " \t.{}()[];,-+=/\\*&^%!|?:<>`\"'";
11
12class Feeder {
13	this(string filename) {
14		fp = File.open(filename);
15		//fp = fopen(std.string.toStringz(filename), "rb");
16
17		// reinit
18		lineNumber = 0;
19	}
20
21	~this() {
22		// *** delete
23		//fclose(fp);
24	}
25
26	string[] feed() {
27		char[] line;
28
29		if(fp.readLine(line)) {
30			// Minimal Logic:
31			// - know not to parse comments
32			// - know that first describe dictates control
33			// - know how to expand should and shouldNot
34
35			// *** delete
36			//char[] line = readln(fp);
37
38			// Increment line counter
39			lineNumber++;
40
41			// sanitize line
42			// *** delete (I don't add newline)
43			//line = chomp(line);
44
45			// return tokens
46			line = line.trim();
47			return splitAll(line, delims);
48		}
49
50		return null;
51	}
52
53	uint getLineNumber() {
54		return lineNumber;
55	}
56	
57	string fileName() {
58		return fp.name;
59	}
60
61protected:
62
63	File fp;
64	//_iobuf* fp;
65	uint lineNumber = 0;
66
67	string[] splitAll(char[] s, char[] delim, bool keepDelim = true) {
68		string[] ret;
69
70		uint lastpos = 0;
71		foreach(i, c; s) {
72			foreach(cmp; delim) {
73				if (c == cmp) {
74					if (lastpos != i) {
75						ret ~= s[lastpos..i].dup;
76					}
77					if (keepDelim) {
78						ret ~= s[i..i+1].dup;
79					}
80					lastpos = i+1;
81				}
82			}
83		}
84
85		ret ~= ["\n"];
86
87		return ret;
88	}
89}