FoxDocumentationTree.Mod 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. (**
  2. Documentation tree to represent a module documentation. Independent of source language.
  3. The documentation tree represents the [[docuEBNF|EBNF]] as implemented by [[FoxDocumentationParser]]
  4. #author# Felix Friedrich
  5. *)
  6. MODULE FoxDocumentationTree;
  7. (**
  8. @concept
  9. = FoxDocumentationTree
  10. All information that has been parsed by the documentation parser is stored in a documentation tree. The documentation tree is implemented in module \
  11. [[FoxDocumentationTree]].
  12. The root of such a documentation is a [[FoxDocumenationTree.Document|Document]], it conists of a description and a number of sections. Each [[Section|section]] consist of a title, cotents and \
  13. can have a label. Sections that actually have a label are merged by the DocumentationBackend. A rough picture is also provided by the [[docuEBNF|EBNF]].
  14. ==docuEBNF EBNF of the documentation in a paragraph
  15. @@docuEBNF <<docuEBNF>> EBNF of the documentation
  16. The following EBNF of the documentation cannot be taken literally because some additional rules are implemented in scanner and parser. However it provides a picture \
  17. of the documentation rules and how they are parsed.
  18. **)
  19. IMPORT Basic := FoxBasic, Scanner := FoxDocumentationScanner, Strings;
  20. TYPE
  21. String*= Scanner.String;
  22. ElementType* = ENUM
  23. Whitespace*, Default*, Italic*, Bold*, Underline*, Link*, WeakLink*, Label*, Code*, LineBreak*, DataCell*, HeaderCell*, Row*
  24. END;
  25. (**
  26. @docuEBNF
  27. {{{TextElement = String |BeginItalic Text EndItalic | BeginBold Text EndBold | "[[" string | Text "]]" | "<<" String ">>" | "{{{" any text "}}}" | "\\" | "|" Text | "|=" Text | }}}
  28. **)
  29. TextElement*= OBJECT
  30. VAR
  31. type-: ElementType;
  32. string-: String;
  33. text-: Text;
  34. PROCEDURE &InitTextElement*(type: ElementType);
  35. BEGIN
  36. NEW(text,4);
  37. SELF.type := type;
  38. string := NIL;
  39. END InitTextElement;
  40. PROCEDURE SetString*(string: String);
  41. BEGIN SELF.string := string
  42. END SetString;
  43. PROCEDURE SetType*(t: ElementType);
  44. BEGIN
  45. type := t
  46. END SetType;
  47. END TextElement;
  48. (**
  49. Comment of Text
  50. @docuEBNF EBNF
  51. {{{ ElementList = {Element} }}}
  52. *)
  53. Text*=OBJECT (Basic.List)
  54. PROCEDURE AppendNew*(type: ElementType): TextElement;
  55. VAR textElement: TextElement;
  56. BEGIN
  57. NEW(textElement,type); AddElement(textElement); RETURN textElement
  58. END AppendNew;
  59. PROCEDURE AddElement*(x: TextElement);
  60. BEGIN Add(x)
  61. END AddElement;
  62. PROCEDURE GetElement*(index: LONGINT): TextElement;
  63. VAR x: ANY;
  64. BEGIN x := Get(index); IF x = NIL THEN RETURN NIL ELSE RETURN x(TextElement) END;
  65. END GetElement;
  66. PROCEDURE FindByString*(CONST string: ARRAY OF CHAR): TextElement;
  67. VAR i: LONGINT; element: TextElement;
  68. BEGIN
  69. FOR i := 0 TO Length()-1 DO
  70. element := GetElement(i);
  71. IF (element.string#NIL) & (element.string^ = string) THEN RETURN element END;
  72. END;
  73. RETURN NIL
  74. END FindByString;
  75. PROCEDURE WriteString*(CONST s: ARRAY OF CHAR);
  76. VAR e: TextElement;
  77. BEGIN
  78. e := AppendNew(ElementType.Default);
  79. e.string := Strings.NewString(s);
  80. END WriteString;
  81. PROCEDURE WriteWhitespace*();
  82. VAR e: TextElement;
  83. BEGIN
  84. e := AppendNew(ElementType.Whitespace);
  85. END WriteWhitespace;
  86. PROCEDURE WriteLink*(CONST name, label: ARRAY OF CHAR);
  87. VAR e: TextElement;
  88. BEGIN
  89. e := AppendNew(ElementType.Link);
  90. e.string := Strings.NewString(name);
  91. e.text.WriteString(label);
  92. END WriteLink;
  93. PROCEDURE WriteWeakLink*(CONST name, label: ARRAY OF CHAR);
  94. VAR e: TextElement;
  95. BEGIN
  96. e := AppendNew(ElementType.WeakLink);
  97. e.string := Strings.NewString(name);
  98. e.text.WriteString(label);
  99. END WriteWeakLink;
  100. PROCEDURE WriteText*(t: Text);
  101. VAR i: LONGINT;
  102. BEGIN
  103. FOR i := 0 TO t.Length()-1 DO
  104. AddElement(t.GetElement(i));
  105. END;
  106. END WriteText;
  107. PROCEDURE WriteLabel*(CONST name: ARRAY OF CHAR);
  108. VAR e: TextElement;
  109. BEGIN
  110. e := AppendNew(ElementType.Label);
  111. e.string := Strings.NewString(name);
  112. END WriteLabel;
  113. PROCEDURE Last*(): TextElement;
  114. BEGIN RETURN GetElement(Length()-1)
  115. END Last;
  116. PROCEDURE ForAllElementsDo*(this: PROCEDURE {DELEGATE} (x: TextElement));
  117. VAR i: LONGINT;
  118. BEGIN
  119. FOR i := 0 TO Length()-1 DO
  120. this(GetElement(i))
  121. END;
  122. END ForAllElementsDo;
  123. END Text;
  124. Word*=ARRAY 32 OF CHAR;
  125. ParagraphType* = ENUM
  126. TextBlock*, Heading*, Bullet*, Number*, Description*, Code*, Line*, Table*
  127. END;
  128. (**
  129. @@docuEBNF EBNF
  130. {{{ paragraph = Text | Heading Text | InsertLabel | Bullet Text | Number Text | Description Text | CodeBegin Text CodeEnd | Line | Table Text. }}}
  131. *)
  132. Paragraph*=OBJECT
  133. VAR
  134. type-: ParagraphType;
  135. level-: LONGINT;
  136. text*: Text;
  137. description-: Text;
  138. label*: Strings.String;
  139. PROCEDURE SetLabel*(label: String);
  140. BEGIN SELF.label := label
  141. END SetLabel;
  142. PROCEDURE & InitParagraph*(type: ParagraphType);
  143. BEGIN SELF.type := type; SELF.level := level; NEW(text,4); NEW(description, 4);
  144. END InitParagraph;
  145. PROCEDURE SetLevel*(level: LONGINT);
  146. BEGIN SELF.level := level
  147. END SetLevel;
  148. END Paragraph;
  149. (**
  150. @docuEBNF
  151. {{{ Paragraphs = {Paragraph}. }}}
  152. *)
  153. Paragraphs*=OBJECT (Basic.List)
  154. PROCEDURE AppendNew*(type: ParagraphType): Paragraph;
  155. VAR paragraph: Paragraph;
  156. BEGIN
  157. NEW(paragraph,type); AddParagraph(paragraph); RETURN paragraph
  158. END AppendNew;
  159. PROCEDURE AddParagraph*(x: Paragraph);
  160. BEGIN Add(x)
  161. END AddParagraph;
  162. PROCEDURE GetParagraph*(index: LONGINT): Paragraph;
  163. VAR x: ANY;
  164. BEGIN x := Get(index); IF x = NIL THEN RETURN NIL ELSE RETURN x(Paragraph) END;
  165. END GetParagraph;
  166. PROCEDURE Last*(): Paragraph;
  167. BEGIN RETURN GetParagraph(Length()-1)
  168. END Last;
  169. PROCEDURE ForAllParagraphsDo*(this: PROCEDURE {DELEGATE} (x: Paragraph));
  170. VAR i: LONGINT;
  171. BEGIN
  172. FOR i := 0 TO Length()-1 DO
  173. this(GetParagraph(i))
  174. END;
  175. END ForAllParagraphsDo;
  176. END Paragraphs;
  177. (**
  178. @docuEBNF
  179. {{{ Section = "@"{"@"} Title Contents. }}}
  180. {{{ Contents = {Paragraph}. }}}
  181. *)
  182. Section*=OBJECT
  183. VAR
  184. level-: LONGINT;
  185. title-: Text;
  186. contents-: Paragraphs;
  187. label-: String;
  188. PROCEDURE &InitSection*(level: LONGINT);
  189. BEGIN NEW(contents,4); SELF.level := level; NEW(title,4); label := NIL;
  190. END InitSection;
  191. PROCEDURE SetLabel*(label: String);
  192. BEGIN SELF.label := label
  193. END SetLabel;
  194. PROCEDURE SetContents*(paragraphs: Paragraphs);
  195. BEGIN contents := paragraphs
  196. END SetContents;
  197. PROCEDURE WriteLabel*(CONST l: ARRAY OF CHAR);
  198. BEGIN
  199. label := Strings.NewString(l);
  200. END WriteLabel;
  201. PROCEDURE WriteHeading*(level: LONGINT; CONST string: ARRAY OF CHAR);
  202. VAR paragraph: Paragraph;
  203. BEGIN
  204. paragraph := contents.AppendNew(ParagraphType.Heading);
  205. paragraph.SetLevel(level);
  206. paragraph.text.WriteString(string);
  207. END WriteHeading;
  208. END Section;
  209. (**
  210. @docuEBNF
  211. {{{ SectionList = {Section} }}}
  212. *)
  213. Sections*=OBJECT (Basic.List)
  214. PROCEDURE AppendNew*(level: LONGINT): Section;
  215. VAR section: Section;
  216. BEGIN
  217. NEW(section, level); AddSection(section); RETURN section;
  218. END AppendNew;
  219. PROCEDURE AddSection*(x: Section);
  220. BEGIN Add(x)
  221. END AddSection;
  222. PROCEDURE GetSection*(index: LONGINT): Section;
  223. VAR x: ANY;
  224. BEGIN x := Get(index); IF x = NIL THEN RETURN NIL ELSE RETURN x(Section) END;
  225. END GetSection;
  226. PROCEDURE Last*(): Section;
  227. BEGIN RETURN GetSection(Length()-1)
  228. END Last;
  229. PROCEDURE ForAllSectionsDo*(this: PROCEDURE {DELEGATE} (x: Section));
  230. VAR i: LONGINT;
  231. BEGIN
  232. FOR i := 0 TO Length()-1 DO
  233. this(GetSection(i))
  234. END;
  235. END ForAllSectionsDo;
  236. END Sections;
  237. (** a document provides the documentation of a module in a structured way *)
  238. (**
  239. @docuEBNF
  240. {{{ Document = Description {Section}. }}}
  241. {{{ Description = {Paragraph}. }}}
  242. *)
  243. Document*=OBJECT
  244. VAR
  245. description-: Paragraphs;
  246. sections-: Sections;
  247. PROCEDURE &InitDocument*;
  248. BEGIN NEW(sections,4); NEW(description,4);
  249. END InitDocument;
  250. END Document;
  251. (**
  252. @docuEBNF
  253. {{{Documents = {Document}. }}}
  254. *)
  255. Documents*=OBJECT (Basic.List)
  256. PROCEDURE AddDocument*(x: Document);
  257. BEGIN Add(x)
  258. END AddDocument;
  259. PROCEDURE GetDocument*(index: LONGINT): Document;
  260. VAR x: ANY;
  261. BEGIN x := Get(index); IF x = NIL THEN RETURN NIL ELSE RETURN x(Document) END;
  262. END GetDocument;
  263. PROCEDURE ForAllDocumentsDo*(this: PROCEDURE {DELEGATE} (x: Document));
  264. VAR i: LONGINT;
  265. BEGIN
  266. FOR i := 0 TO Length()-1 DO
  267. this(GetDocument(i))
  268. END;
  269. END ForAllDocumentsDo;
  270. END Documents;
  271. (** a documentation provides the documentation of several modules *)
  272. (**
  273. @docuEBNF
  274. {{{ Documentation = {Document}. }}}
  275. *)
  276. Documentation*=OBJECT
  277. VAR
  278. documents-: Documents
  279. PROCEDURE &InitDocumentation*;
  280. BEGIN NEW(documents,4)
  281. END InitDocumentation;
  282. END Documentation;
  283. PROCEDURE NewText*(): Text;
  284. VAR text: Text;
  285. BEGIN NEW(text,4); RETURN text
  286. END NewText;
  287. PROCEDURE NewTextElement*(type: ElementType): TextElement;
  288. VAR textElement: TextElement;
  289. BEGIN NEW(textElement,type); RETURN textElement
  290. END NewTextElement;
  291. PROCEDURE NewParagraph*(type: ParagraphType): Paragraph;
  292. VAR paragraph: Paragraph;
  293. BEGIN NEW(paragraph,type); RETURN paragraph
  294. END NewParagraph;
  295. PROCEDURE NewParagraphs*(): Paragraphs;
  296. VAR paragraphs: Paragraphs;
  297. BEGIN NEW(paragraphs,4); RETURN paragraphs
  298. END NewParagraphs;
  299. PROCEDURE NewSection*(level: LONGINT): Section;
  300. VAR section: Section;
  301. BEGIN NEW(section,level); RETURN section
  302. END NewSection;
  303. PROCEDURE NewSections*(level: LONGINT): Sections;
  304. VAR sections: Sections;
  305. BEGIN NEW(sections,4); RETURN sections
  306. END NewSections;
  307. PROCEDURE NewDocument*(): Document;
  308. VAR document: Document;
  309. BEGIN NEW(document); RETURN document
  310. END NewDocument;
  311. PROCEDURE Test;
  312. VAR e: TextElement;
  313. BEGIN
  314. e := NewTextElement(ElementType.Default);
  315. END Test;
  316. END FoxDocumentationTree.