PageRenderTime 32ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/iteration-zero/src/Sofu/List.d

http://iteration-zero.googlecode.com/
D | 201 lines | 150 code | 50 blank | 1 comment | 11 complexity | e44251d6941d266d318d8db7ae1ad750 MD5 | raw file
Possible License(s): LGPL-2.1, CC-BY-3.0, GPL-2.0, LGPL-2.0
  1. module Sofu.List;
  2. private import Sofu.Object;
  3. private import Sofu.Exception;
  4. private import Sofu.Lex;
  5. private import Sofu.Parse;
  6. private import Sofu.Value;
  7. private import Sofu.Map;
  8. private import std.string;
  9. class List : SofuObject
  10. {
  11. public:
  12. this()
  13. {
  14. super();
  15. }
  16. this(int lineNumber, int colNumber, char[] fileName)
  17. {
  18. super(lineNumber, colNumber, fileName);
  19. }
  20. this(LexToken token)
  21. {
  22. if(token.type() != LexTokenType.LISTSTART) {
  23. throw new ListExpectedException(token);
  24. }
  25. with(token) this(lineNumber(), colNumber(), fileName());
  26. }
  27. List asList() {
  28. return this;
  29. }
  30. bool isList() {
  31. return true;
  32. }
  33. /+ Functions for accessing elements +/
  34. SofuObject object(int index)
  35. {
  36. if(index >= 0 && index < _elements.length) {
  37. return _elements[index];
  38. }
  39. else {
  40. throw new ListIndexOutOfRangeException(this, index);
  41. }
  42. }
  43. Value value(int index)
  44. {
  45. return object(index).asValue();
  46. }
  47. List list(int index)
  48. {
  49. return object(index).asList();
  50. }
  51. Map map(int index)
  52. {
  53. return object(index).asMap();
  54. }
  55. bool hasElement(int index)
  56. {
  57. return index < _elements.length;
  58. }
  59. bool hasValue(int index)
  60. {
  61. return hasElement(index) && _elements[index].isValue();
  62. }
  63. bool hasList(int index)
  64. {
  65. return hasElement(index) && _elements[index].isList();
  66. }
  67. bool hasMap(int index)
  68. {
  69. return hasElement(index) && _elements[index].isMap();
  70. }
  71. int opApply(int delegate(inout SofuObject elem) dg)
  72. {
  73. int result = 0;
  74. foreach(SofuObject elem; _elements) {
  75. result = dg(elem);
  76. if(result)
  77. break;
  78. }
  79. return result;
  80. }
  81. char[] typeString() {
  82. return "List";
  83. }
  84. void appendElement(SofuObject object)
  85. {
  86. _elements ~= object;
  87. }
  88. char[] outputString(int indent = 0)
  89. {
  90. char[] result;
  91. foreach(SofuObject object; _elements) {
  92. for(int i = 0; i < indent; ++i) {
  93. result ~= " ";
  94. }
  95. result ~= object.startSeq();
  96. result ~= object.outputString(indent + 4);
  97. result ~= object.endSeq(indent);
  98. result ~= "\n";
  99. }
  100. return result;
  101. }
  102. char[] startSeq() {
  103. return "(\n";
  104. }
  105. char[] endSeq(int indent = 0) {
  106. char[] result;
  107. for(int i = 0; i < indent; ++i) {
  108. result ~= " ";
  109. }
  110. result ~= ")";
  111. return result;
  112. }
  113. private:
  114. SofuObject[] _elements;
  115. }
  116. class ListElementException : SofuException
  117. {
  118. List list;
  119. int index;
  120. this(char[] msg, List ilist, int iindex)
  121. {
  122. super(msg);
  123. list = ilist;
  124. index = iindex;
  125. }
  126. }
  127. class ListIndexOutOfRangeException : ListElementException
  128. {
  129. this(List list, int index)
  130. {
  131. char[] msg = format("Sofu: List index %d is out of range for "
  132. "list at %s, line %d column %d",
  133. index, list.fileName(), list.lineNumber(), list.colNumber());
  134. super(msg, list, index);
  135. }
  136. }
  137. class ListElementTypeException : ListElementException
  138. {
  139. char[] assumedType;
  140. this(List list, int index, char[] iassumedType)
  141. {
  142. assumedType = iassumedType;
  143. char[] msg = format("Sofu: Assumed list element number %d to be of type %s."
  144. "Actual type is %s in "
  145. "list at %s, line %d column %d",
  146. index, assumedType, list.object(index).typeString(),
  147. list.fileName(), list.lineNumber(),
  148. list.colNumber());
  149. super(msg, list, index);
  150. }
  151. }