PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/parsing/d/importdeclunit.d

http://github.com/wilkie/djehuty
D | 94 lines | 61 code | 18 blank | 15 comment | 9 complexity | dc306940f6b89d6d4ccab0acd799ce67 MD5 | raw file
 1/*
 2 * importdeclunit.d
 3 *
 4 * This module parses out the package and module name foo
 5 * out of an import declaration.
 6 *
 7 */
 8
 9module parsing.d.importdeclunit;
10
11import parsing.parseunit;
12import parsing.token;
13
14import parsing.d.tokens;
15import parsing.d.nodes;
16
17import parsing.d.trees;
18
19import io.console;
20
21import djehuty;
22
23class ImportDeclUnit : ParseUnit {
24	override bool tokenFound(Token current) {
25		switch (current.type) {
26			case DToken.Dot:
27				if (cur_string.length > 0 && cur_string[$-1] == '.') {
28
29					// Error: We found two dots, probably left behind after an edit.
30					error(_common_error_msg,
31							"There are a few too many dots in a row. Did you mean to have only one?",
32							_common_error_usages);
33
34				}
35				else {
36					cur_string ~= ".";
37				}
38				break;
39
40			case DToken.Semicolon:
41				// End of declaration
42				this.root = new Import(cur_string);
43				return false;
44
45			case DToken.Identifier:
46				if (cur_string.length > 0 && cur_string[$-1] != '.') {
47
48					// Error: Found an identifier and then another identifier. Probably
49					// due to an editing mistake.
50					error(_common_error_msg,
51							"Did you mean to place a '.' between the two names?",
52							_common_error_usages);
53
54				}
55				else {
56					// Add the package or module name to the overall value.
57					cur_string ~= toStr(current.value);
58				}
59
60				break;
61			case DToken.Slice:
62				// Error: Found .. when we expected just one dot.
63				error(_common_error_msg,
64					"You placed two dots, did you mean to only have one?",
65					_common_error_usages);
66				break;
67
68			case DToken.Variadic:
69				// Error: Found ... when we expected just one dot.
70				error(_common_error_msg,
71					"You placed three dots, did you mean to only have one?",
72					_common_error_usages);
73				break;
74			default:
75
76				// Error: Found some illegal token. Probably due to lack of semicolon.
77				errorAtPrevious(_common_error_msg,
78					"You probably forgot a semicolon.",
79					_common_error_usages);
80				break;
81		}
82		return true;
83	}
84
85protected:
86	string cur_string = "";
87
88	static const string _common_error_msg = "The import declaration is not formed correctly.";
89	static const string[] _common_error_usages = [
90		"import package.file;",
91		"import MyAlias = package.file;",
92		"import MyFoo = package.file : Foo;"
93	];
94}