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