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