home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / word2x0a.zip / source / INTERNALS < prev    next >
Text File  |  1997-04-10  |  7KB  |  172 lines

  1. $Id: INTERNALS,v 1.3 1997/04/10 19:53:54 dps Exp $
  2.  
  3. Here is how the program works.
  4.  
  5.  
  6. reader.cc (1.10)
  7.  
  8. read_character reads characters from a word document suitably
  9. translated, including dsitingishing between multiple and single ^Gs,
  10. etc.
  11.  
  12. The output is fetched by chunk_reader::read_chunk_raw that assembles
  13. it into bits ignoring inclusions. chunk_Reader::read_chunk gets these
  14. chunks are parcels them out with inclusion seperated out.
  15.  
  16. tok_seq::rd_token adds start and end tags for rows, fields, paragraphs
  17. and all the rest storing the tokens in a table on a seperate queue
  18. before transfering them all onto the main queue. tok_seq::rd_token also
  19. keeps track of the size and detects the probable end of the table.
  20.  
  21. tok_seq::feed_token takes a token off the queue and requests a refill at the
  22. appropiate time. At the end of the document it tests a flag and if the flag
  23. is not set then adds a document end entry (and then feeds it to the caller).
  24.  
  25. OK, so far? Now the fun begins!
  26.  
  27. If you look at the outptut now you see horrofic stuff like
  28. <PARAGRAPH>550 *<SPEC>eq \F(foom bar)</SPEC><PARGRAPH>= 42</PARAGRAPH>
  29. so the input is further processed by tok_seq::math_collect().
  30. math_collect() uses saved_tok as a one byte push back mechamism and
  31. will use this token before asking feed_token() for one. Non-paragraphs and non-equations go straight thorugh.
  32.  
  33. When math_collect sees a paragaph is pears at the next item. If this
  34. is not an equation it just forwards the token and stashes the item it
  35. got in saved_token (saved_token is definately free: either it was used
  36. or feed_token supplied something). If it sees an euqation it calls
  37. math_reverse_scan to work out whether there is any equation in the
  38. string (guesswork but works quite nicely). If math_reverse_scan
  39. decides it is all real text the token is just forwarded (with the
  40. extra token still stashed in saved_tok).
  41.  
  42. Assuming math_reverse_scan found something to move that material is
  43. moved into the equation and ntok and the current token
  44. modified. saved_token still pointds to ntok so we use the same
  45. structure but new strings. The reduced paragraoh token is returned.
  46.  
  47. -----
  48.  
  49. When the code sees an equation special (quite possibly saved_tok from
  50. the paragraph process above) it ask feed_token() for the next two
  51. tokens. The next token is the end token for the special and the one
  52. after that interesting, and will be called T (the token itself is
  53. *ntok in the code).
  54.  
  55. If T is an equation the end spec token is junked and the two equations
  56. joined.  One of the equations is then junked. The end special is
  57. pushed onto the start of the outpiut for feed_token to find there;
  58. saved_tok is pointed to the expanded equations. The code then returns
  59. to the original read a token state so further aggregation can take
  60. place.
  61.  
  62. If T is a paragraph then the code uses math_forward_scan to see how
  63. much of that is consumed as part of the equation. If none then the end
  64. special and paragraph tokens are pushed onto the front of the output
  65. queue and saved_tok invalided. The code is then returns the current
  66. (equation special) token. The end special passes straight through and
  67. then the accumulaion can begin again.
  68.  
  69. If T (a paragraph) is partial consumed the current equation and it is
  70. adjusted and the same processing as if the paragraph had no formula contents.
  71.  
  72. If T (a paragraph) entirely consumed its contents are added onto to
  73. the text, the paragraph junked, the end spec pushed pack. saved_tok is
  74. pointed to the current, expanded equations.  The code then returns
  75. to the original read a token state so further aggregation can take
  76. place.
  77.  
  78. The output now contians nice stuff like
  79.  
  80. <PARAGRAPH><SPEC> 550 * \F(foo,bar) = 29</SPEC></PARAGRAPH> and even
  81. horrors that word veiwer renders as  displayed equations like
  82. <PARAGRAPH><SPEC> 550 * \F(foo,bar) = 29</SPEC><PARAGRAPH>.</PARAGRAPH>
  83.  
  84.  
  85.  
  86.  
  87. This output is requested by tok_seq::eqn_rd_token() which is an
  88. internal method. It is not devoid of tricks however. Anything other
  89. than the start of a paragragh passes straight through.
  90.  
  91.  
  92. When it sees a paragraph it pushes it onto a seperate queue and
  93. acculumates totals of characters and specials in it sees. The loop
  94. exits when any of the following applies:
  95.  
  96.     The paragaraph character total exceeds then (small, currently 3)
  97.     treshold.
  98.  
  99.     The end of the paragraph is spotted.
  100.  
  101.     A non-special, non-pargraph, non-other character is seen (if this
  102.     happens we add the treshold to the count to be sure of being >= to it.
  103.  
  104.  
  105. On exit from the loop if the total is less than the critical value the
  106. queue is reveresed and inserted at the front of the output queue minus
  107. the paragraph items. Since the tokens are inserted as the first
  108. character of the ouput they appear in reverse order of insertion (hence
  109. the reverse makes the elements appear it the original order on the output
  110. queue). This deletes that extraneous and wrong full stop, for example.
  111.  
  112. Otherwise the queue is the elements are transfered to the front of the
  113. output queue in the existing order (this actually just sets a couple
  114. of pointers).
  115.  
  116. Either way the temporary queue is now empty and is deleted. The first
  117. item dequeued is returned. (This is what rtest2 shows you).
  118.  
  119.  
  120. -----
  121.  
  122. The output of eqn_rd_token is fed to the listhandling guesswork. A
  123. list is started by A pargraph starting with a number 1 (enumerate) or
  124. a bullet character (itemize). If a paragraph does not fit then it is
  125. checked for a list start---if so it is assumed to be a sublist. The
  126. end of the list is signalled by MAX_ITEM_SEP_PARS which can not be
  127. part of a list (default value is 5). Only the last paragraph with the
  128. appropiate lead in text is included. Since this involves delaying
  129. tokens and looking ahead the easiest place to do this is in the
  130. reader.
  131.  
  132. Only the bottom level list is actually ended by enough non-list
  133. paragraphs; the list is closed and the output is fed back in, giving
  134. them the chance to kill the list the next level up.
  135.  
  136.  
  137. The lists queues are completely seperate from anything eqn_rd_token
  138. uses: each list builds the tokens in items. Note that the first item
  139. includes the list start to make it easy to recover the original text
  140. if required. When a level of list is popped these tokens are pointed
  141. to by recycled, which is initially NULL.
  142.  
  143. If the list only has one item the list is transformed back into its
  144. (listless) tokens, as recieved from eqn_rd_token. The first token is
  145. appended to the list bellow the one popped or the output queue if there
  146. is not such list.
  147.  
  148. The main loop of read_token grabs values from recycled if it is not
  149. null, setting it to NULL if it becomes empty. If recycled is NULL then
  150. the code asks eqn_rd_token for a token. As with eqn_rd_token the loop
  151. waits for its own output queue (outqueue) to have something in it and
  152. returns the first item in this queue.
  153.  
  154.  
  155. The overall effect of the layered intelligence described above is that
  156. the code in reader.cc is too complicated for comfort. The overall
  157. performance is nice though...
  158.  
  159.  
  160. ----------------------------------------------------------------------
  161.  
  162. OH, yes and the *TeX output format also uses context cues. There is a
  163. minimal amount of context cue usage in the ascii format. Overall this
  164. program tends towards my idea of a complex AI program using context
  165. cues to do the right stuff with what word throws at it!!
  166.  
  167. I hope this is now 100% clear.
  168.  
  169.  
  170.  
  171.  
  172.