home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ssrtf.ycc < prev    next >
Text File  |  1995-06-14  |  7KB  |  195 lines

  1. //------------------------------------------------------------------
  2. //
  3. //  Note : Set tabs to 6 to view this file.
  4. //
  5. //  Here is our first crack at an RTF parser. It is very 'sparse' in
  6. //  that it just breaks the input up into tokens and doesn't look
  7. //  for many control words (you can add these easily, see below), 
  8. //  but it should parse all RTF files.
  9. //
  10. //  Like HTML, RTF is problematic, at least from a 'grammar' point 
  11. //  of view. There is a very detailed RTF spec, but most RTF writers
  12. //  don't follow it exactly. These deviations can cause parsing errors.
  13. //  It's probably best to just break out the control words you are 
  14. //  looking for, and don't worry to much about 'forming' the tokens 
  15. //  with a grammar. All your doing is transferring the work to 
  16. //  the code. You would like to have the grammar handle it, but alas...
  17. //
  18. //  There is a commented out section which handles the 'fonttbl' 
  19. //  portion. We tested it here, and it seems to work. We did run
  20. //  into 1 problem while developing this. Word sometimes sticks 
  21. //  '\n' characters in between some of the font descriptions, so
  22. //  we had to account for that, which is a nuisance. If the
  23. //  '\n' characters can show up randomly, another solution will
  24. //  be needed, our solution only accounts for them between 
  25. //  individual font descriptions. This is typical of the seemingly
  26. //  small deviations that can cause problems in a grammar. There 
  27. //  are ways to do this with %expression lists, but I'm not sure 
  28. //  its worth the effort. You're probably better off having the 
  29. //  parser do things like recognizing specific tokens, and handling 
  30. //  the logic in the code, see above.
  31. //
  32. //  It is very easy to add control words to this rule file. Just
  33. //  add them after the 'CtrlWParm' regular expression. We use two
  34. //  different tokens for the control words, one with the optional
  35. //  space, and one with the numeric parameter. You can do the same 
  36. //  thing when adding the control words.
  37. //
  38. //  In the grammar, just add 'statement -> '\myNewCtrlWord';' for 
  39. //  each control word. This way, you won't have to look for the
  40. //  control words in your code, they will each have their own
  41. //  token. 
  42. //
  43. //  If you run into problems, let us know by call 800-988-9023, or
  44. //  send an e-mail to 'willd.accessnv.com' on the internet, or
  45. //  71332,2577 on CompuServe.
  46. //-------------------------------------------------------------------
  47.  
  48. %macro
  49. {n}                    '\-?[0-9]+';
  50. {hex}                    '[a-f0-9]';
  51.  
  52. %expression Main
  53.  
  54. '[^\\{}; \n]+'            Text,       'text';
  55. '[ \n]+'                White,    'white';
  56. '\{'                    OCurly,     '{';
  57. '\}'                    CCurly,    '}';
  58. '\\\*'                Dest,     '\*';
  59. '\\[a-z]+[ ]?'                Ctrl,       '\ctrl';
  60. '\\[a-z]+\-?[0-9]*'           CtrlWParm,  '\ctrlWParm';
  61.  
  62. '\\rtf[0-9]+'            Rtf,        '\rtf';
  63. '\\ansi[ ]?'            Ansi,        '\ansi';
  64. '\\mac[ ]?'                Mac,        '\mac';
  65. '\\pc[ ]?'                Pc,        '\pc';
  66. '\\pca[ ]?'                Pca,        '\pca';
  67.  
  68. //'\\fonttbl[ ]?'            Fonttbl,    '\fonttbl';
  69. //'\\f[0-9]+'            Fnum,        '\f';
  70. //'\\fnil[ ]?'            Fnil,        '\fnil';
  71. //'\\froman[ ]?'            Froman    '\froman';
  72. //'\\fswiss[ ]?'            Fswiss    '\fswiss';
  73. //'\\fmodern[ ]?'            Fmodern    '\fmodern';
  74. //'\\fscript[ ]?'            Fscript    '\fscript';
  75. //'\\fdecor[ ]?'            Fdecor    '\fdecor';
  76. //'\\ftech[ ]?'            Ftech        '\ftech';
  77. //'\\fbidi[ ]?'            Fbidi        '\fbidi';
  78. //'\\fcharset[0-9]+'        Fcharset    '\fcharset';
  79. //'\\fprq[0-9]+'            Fprq        '\fprq';
  80. //'\\fontemb[ ]?'            Fontemb    '\fontemb';
  81. //'\\ftnil[ ]?'            Ftnil        '\ftnil';
  82. //'\\fttruetype[ ]?'        Fttruetype    '\fttruetype';
  83. //'\\falt[ ]?'            Falt        '\falt';
  84. //'\\cpg[0-9]+'            Cpg        '\cpg';
  85.  
  86. '\\[^a-z]'                    Spec,       '\spec';
  87. '\\`{hex}{hex}'            SpecHex,     '\`';
  88. '\\\-'                SpecHy,     '\-';
  89. '\\_'                    SpecNonbHy, '\_';
  90. '\\\{'                SpecOCurly, '\{';
  91. '\\\|'                SpecFmula,  '\|';
  92. '\\\}'                SpecCCurly, '\}';
  93. '\\~'                    SpecNonbSp, '\~';
  94.  
  95. ';'                    Semi,     ';';
  96.  
  97. %production start
  98.  
  99. Start                start     -> junkOpt group junkOpt;
  100.  
  101. GroupNested            group        -> '{' statements '}';
  102. StatementList        statements    -> statements statement;
  103. StatementOne        statements    -> statement;
  104.  
  105. StatementCtrl        statement    -> '\ctrl';
  106. StatementCtrlWParm    statement     -> '\ctrlWParm';
  107. StatementSpecAny        statement    -> '\spec';
  108. StatementSpecHex         statement    -> '\`';
  109. StatementSpecDest        statement    -> '\*';
  110. StatementSpecHyphen    statement    -> '\-';
  111. StatementSpecNonbHyph   statement    -> '\_';
  112. StatementSpecOCurly    statement    -> '\{';
  113. StatementSpecFormula    statement    -> '\|';
  114. StatementSpecCCurly    statement    -> '\}';
  115. StatementSpecNonbSpace    statement    -> '\~';
  116. StatementSemi        statement    -> ';';
  117. StatementText        statement    -> 'text';
  118. StatementWhite        statement    -> 'white';
  119. StatementGroup        statement    -> 'group';
  120. //StatementFontref    statement    -> '\f';
  121.  
  122. StatementRtf        statement    -> rtf;
  123. StatementCharset        statement    -> charset;
  124. //StatementFonttbl    statement    -> fonttbl;
  125. //StatementFiletbl    statement    -> filetbl;
  126. //StatementColortbl    statement    -> colortbl;
  127.  
  128. Rtf                rtf        -> '\rtf';
  129.  
  130. CharsetAnsi            charset    -> '\ansi';
  131. CharsetMac            charset    -> '\mac';
  132. CharsetPc            charset    -> '\pc';
  133. CharsetPca            charset    -> '\pca';
  134.  
  135. //Fonttbl            fonttbl    -> '{' '\fonttbl' fonts '}';
  136. //FontList            fonts        -> fonts whiteOpt font;
  137. //FontListOne        fonts        -> font;
  138. //FontBracketed        font        -> '{' fontinfo '}';
  139. //FontPlain            font        -> fontinfo;
  140. //FontinfoStatement    fontinfo    -> fnum ffamily fcharsetOpt fprqOpt 
  141. //                            fembOpt fcodepageOpt fdatalist 
  142. //                            faltnameOpt ';';
  143. //FontNum            fnum        -> '\f';
  144. //FontFamilyNil        ffamily    -> '\fnil';
  145. //FontFamilyRoman        ffamily    -> '\froman';
  146. //FontFamilySwiss        ffamily    -> '\fswiss';
  147. //FontFamilyModern    ffamily    -> '\fmodern';
  148. //FontFamilyScript        ffamily    -> '\fscript';
  149. //FontFamilyDecor        ffamily    -> '\fdecor';
  150. //FontFamilyTech        ffamily    -> '\ftech';
  151. //FontFamilyBidi        ffamily    -> '\fbidi';
  152. //FontCharsetNull        fcharsetOpt    -> ;
  153. //FontCharset        fcharsetOpt    -> '\fcharset';
  154. //FontPrqNull        fprqOpt    -> ;
  155. //FontPrq            fprqOpt    -> '\fprq';
  156. //FontEmbNull        fembOpt    -> ;
  157. //FontEmb            fembOpt    -> '{' '\*' '\fontemb' fonttype 
  158. //                            fontdataOpt '}';
  159. //FontTypeNil        fonttype    -> '\ftnil';
  160. //FontTypeTrue        fonttype    -> '\fttruetype';
  161. //FontDataNull        fontdataOpt    -> ;
  162. //FontDataList        fontdataOpt    -> fdatalist;
  163. //FDataList            fdatalist    -> fdatalist fdata;
  164. //FDataListOne        fdatalist    -> fdata;
  165. //FDataText            fdata        -> 'text';
  166. //FDataWhite        fdata        -> 'white';
  167. //FontAltNameOpt        faltnameOpt    -> ;
  168. //FontAltName        faltnameOpt    -> '{' '\*' '\falt' 'text' '}';
  169. //FontCodePageOptNull    fcodepageOpt -> ;
  170. //FontCodepageOpt        fcodepageOpt -> '\cpg';
  171.  
  172. //WhiteOptNull        whiteOpt    -> ;
  173. //WhiteOpt            whiteOpt    -> 'white';
  174.  
  175. Junk                junkOpt      -> junkStatement;
  176. JunkNull            junkOpt      -> ;
  177. JunkStatementCtrl        junkStatement -> '\ctrl';
  178. JunkStatementCtrlWParm    junkStatement -> '\ctrlWParm';
  179. JunkStatementSpecAny    junkStatement -> '\spec';
  180. JunkStatementSpecHex     junkStatement -> '\`';
  181. JunkStatementSpecDest    junkStatement -> '\*';
  182. JunkStatementSpecHyphen    junkStatement -> '\-';
  183. JunkStatementSpecNonbHy junkStatement -> '\_';
  184. JunkStatementSpecOCurly    junkStatement -> '\{';
  185. JunkStatementSpecFmula    junkStatement -> '\|';
  186. JunkStatementSpecCCurly    junkStatement -> '\}';
  187. JunkStatementSpecNonbSp    junkStatement -> '\~';
  188. JunkStatementWhite    junkStatement -> 'white';
  189. JunkStatementText        junkStatement -> 'text';
  190.  
  191.  
  192.                                                   
  193.  
  194.  
  195.