/tools/dspec/parseunit.d

http://github.com/wilkie/djehuty · D · 116 lines · 79 code · 31 blank · 6 comment · 13 complexity · 2512ea555b8f2846c6eee32e16669a8d MD5 · raw file

  1. module parseunit;
  2. import feeder;
  3. import ast;
  4. import core.string;
  5. import core.unicode;
  6. import core.definitions;
  7. import io.console;
  8. class ParseUnit {
  9. final void attachFeeder(Feeder feed) {
  10. feeder = feed;
  11. }
  12. final AST parse() {
  13. // get class name
  14. ClassInfo ci = this.classinfo;
  15. string className = ci.name.dup;
  16. //Console.putln("CLASS: ", className.array);
  17. original = new AST(null,null);
  18. parseTree = original;
  19. string[] foo = className.split('.');
  20. className = foo[$-1];
  21. //Console.putln("CLASS: ", className.array);
  22. parseTree.name = className;
  23. for(;;) {
  24. if (tokens is null) {
  25. tokens = feeder.feed();
  26. idx = 0;
  27. if (tokens is null) {
  28. break;
  29. }
  30. }
  31. for( ; idx < tokens.length ; idx++) {
  32. currentToken = tokens[idx];
  33. if (currentToken in parseFunctions) {
  34. parseFunctions[currentToken]();
  35. }
  36. else {
  37. parseDefault();
  38. }
  39. // Finished?
  40. if (iAmDone) { idx++; return original; }
  41. }
  42. // get more
  43. tokens = feeder.feed();
  44. idx=0;
  45. }
  46. return original;
  47. }
  48. void progressTree(AST right) {
  49. if (parseTree.right !is null && parseTree.left is null) {
  50. parseTree.left = new AST(null, null);
  51. parseTree = parseTree.left;
  52. }
  53. parseTree.right = right;
  54. if (parseTree !is original && right !is null && right.valueType == AST.ValueType.Name) {
  55. string val;
  56. right.getValue(val);
  57. parseTree.hint = val;
  58. }
  59. }
  60. protected:
  61. // feed state
  62. static string[] tokens;
  63. static uint idx;
  64. static Feeder feeder;
  65. alias void delegate() ParseFunction;
  66. ParseFunction[string] parseFunctions;
  67. ParseUnit parseUnit;
  68. AST parseTree;
  69. AST original;
  70. bool iAmDone;
  71. string currentToken;
  72. AST newParseUnit(ParseUnit newUnit) {
  73. parseUnit = newUnit;
  74. return parseUnit.parse();
  75. }
  76. void done() {
  77. iAmDone = true;
  78. }
  79. void registerToken(string token, ParseFunction func) {
  80. parseFunctions[token] = func;
  81. }
  82. void parseDefault() {
  83. return;
  84. }
  85. }