FoxDocumentationTree.Mod 9.6 KB

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