PageRenderTime 18ms CodeModel.GetById 9ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/parsing/parseunit.d

http://github.com/wilkie/djehuty
D | 139 lines | 103 code | 29 blank | 7 comment | 9 complexity | eb5e21bf1f547dbebbe9921306c50989 MD5 | raw file
  1module parsing.parseunit;
  2
  3import parsing.ast;
  4import parsing.lexer;
  5import parsing.token;
  6
  7import djehuty;
  8
  9import io.console;
 10
 11class ParseUnit {
 12	final AbstractSyntaxTree parse() {
 13		// get class name
 14		ClassInfo ci = this.classinfo;
 15		string className = ci.name.dup;
 16
 17		// Do not have a lexer installed...
 18		if (_lexer is null) {
 19			return _tree;
 20		}
 21
 22		// Go through every token...
 23
 24		// Starting with the first
 25		current = _lexer.pop();
 26
 27		if (current.type == 0) {
 28			return _root;
 29		}
 30
 31		// get position in lexer
 32		_firstLine = current.line;
 33		_firstColumn = current.column;
 34
 35		do {
 36//			Console.putln("T: ", current.type, " ", current.value);
 37			if (!tokenFound(current)) {
 38				break;
 39			}
 40			if (_error) {
 41				break;
 42			}
 43
 44			_lastLine = current.lineEnd;
 45			_lastColumn = current.columnEnd;
 46		} while((current = _lexer.pop()).type != 0);
 47
 48		// Return resulting parse tree...
 49		return _root;
 50	}
 51
 52	template expand(T) {
 53		AbstractSyntaxTree expand() {
 54			auto machine = new T();
 55			machine.lexer = _lexer;
 56			return machine.parse();
 57		}
 58	}
 59
 60	Lexer lexer() {
 61		return _lexer;
 62	}
 63
 64	void lexer(Lexer val) {
 65		_lexer = val;
 66	}
 67
 68protected:
 69
 70	uint state() {
 71		return _state;
 72	}
 73
 74	void state(uint value) {
 75		_state = value;
 76	}
 77
 78	void root(AbstractSyntaxTree ast) {
 79		_root = ast;
 80	}
 81
 82	AbstractSyntaxTree root() {
 83		return _root;
 84	}
 85
 86	final void errorAtStart(string msg, string desc = null, string[] usages = null) {
 87		_printerror(msg, desc, usages, _firstLine, _firstColumn);
 88	}
 89
 90	final void errorAtPrevious(string msg, string desc = null, string[] usages = null) {
 91		_printerror(msg, desc, usages, _lastLine, _lastColumn);
 92	}
 93
 94	final void error(string msg, string desc = null, string[] usages = null) {
 95		_printerror(msg, desc, usages, current.line, current.column);
 96	}
 97
 98	bool tokenFound(Token token) {
 99		return true;
100	}
101
102private:
103	uint _firstLine;
104	uint _firstColumn;
105
106	uint _lastLine;
107	uint _lastColumn;
108
109	uint _state;
110
111	Lexer _lexer;
112	AbstractSyntaxTree _tree;
113	AbstractSyntaxTree _root;
114	static bool _error;
115	Token current;
116
117	void _printerror(string msg, string desc, string[] usages, uint line, uint column) {
118		Console.putln("Syntax Error: file.d");
119		Console.putln("   Line: ", line, ": ", _lexer.line(line));
120		uint position = column;
121		position = position + toStr(line).length + 10;
122		for (uint i; i < position; i++) {
123			Console.put(" ");
124		}
125		Console.putln("^");
126		Console.forecolor = Color.Gray;
127		Console.putln(" Reason: ", msg);
128		if (desc !is null) {
129			Console.putln("   Hint: ", desc);
130		}
131		if (usages !is null) {
132			Console.putln("  Usage: ", usages[0]);
133			foreach(usage; usages[1..$]) {
134				Console.putln("         ", usage);
135			}
136		}
137		_error = true;
138	}
139}