PageRenderTime 42ms CodeModel.GetById 15ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

/tools/dspec/parseunit.d

http://github.com/wilkie/djehuty
D | 116 lines | 79 code | 31 blank | 6 comment | 13 complexity | 2512ea555b8f2846c6eee32e16669a8d MD5 | raw file
  1module parseunit;
  2
  3import feeder;
  4import ast;
  5
  6import core.string;
  7import core.unicode;
  8import core.definitions;
  9
 10import io.console;
 11
 12class ParseUnit {
 13	final void attachFeeder(Feeder feed) {
 14		feeder = feed;
 15	}
 16
 17	final AST parse() {
 18		// get class name
 19		ClassInfo ci = this.classinfo;
 20		string className = ci.name.dup;
 21
 22		//Console.putln("CLASS: ", className.array);
 23
 24		original = new AST(null,null);
 25		parseTree = original;
 26
 27		string[] foo = className.split('.');
 28		className = foo[$-1];
 29		//Console.putln("CLASS: ", className.array);
 30
 31		parseTree.name = className;
 32
 33		for(;;) {
 34			if (tokens is null) {
 35				tokens = feeder.feed();
 36				idx = 0;
 37
 38				if (tokens is null) {
 39					break;
 40				}
 41			}
 42
 43			for( ; idx < tokens.length ; idx++) {
 44				currentToken = tokens[idx];
 45				if (currentToken in parseFunctions) {
 46					parseFunctions[currentToken]();
 47				}
 48				else {
 49					parseDefault();
 50				}
 51
 52				// Finished?
 53				if (iAmDone) { idx++; return original; }
 54			}
 55
 56			// get more
 57			tokens = feeder.feed();
 58			idx=0;
 59		}
 60
 61		return original;
 62	}
 63	
 64	void progressTree(AST right) {
 65		if (parseTree.right !is null && parseTree.left is null) {
 66			parseTree.left = new AST(null, null);
 67			parseTree = parseTree.left;
 68		}
 69				
 70		parseTree.right = right;
 71
 72		if (parseTree !is original && right !is null && right.valueType == AST.ValueType.Name) {
 73			string val;
 74			right.getValue(val);
 75			parseTree.hint = val;
 76		}
 77	}
 78	
 79protected:
 80
 81	// feed state
 82	static string[] tokens;
 83	static uint idx;
 84
 85	static Feeder feeder;
 86
 87	alias void delegate() ParseFunction;
 88
 89	ParseFunction[string] parseFunctions;
 90
 91	ParseUnit parseUnit;
 92	
 93	AST parseTree;
 94	AST original;
 95
 96	bool iAmDone;
 97	
 98	string currentToken;
 99	
100	AST newParseUnit(ParseUnit newUnit) {
101		parseUnit = newUnit;
102		return parseUnit.parse();
103	}
104	
105	void done() {
106		iAmDone = true;
107	}
108	
109	void registerToken(string token, ParseFunction func) {
110		parseFunctions[token] = func;
111	}
112	
113	void parseDefault() {
114		return;
115	}
116}