home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / extrnl / adaform.src < prev    next >
Encoding:
Text File  |  1988-05-03  |  16.3 KB  |  471 lines

  1. ::::::::::
  2. ADAFORM.PRO
  3. ::::::::::
  4. -------- SIMTEL20 Ada Software Repository Prologue ------------
  5. --                                                           -*
  6. -- Unit name    : text Adaform  (UNIX Lex input file)
  7. -- Version      : 1.3
  8. -- Author       : David Emery
  9. --              : Siemens Research
  10. --              : 105 College Rd
  11. --              : Princeton, NJ   08540
  12. -- DDN Address  : "siemens!emery"@topaz
  13. -- Date created : 2 MAY 85
  14. -- Release date : 2 MAY 85
  15. -- Last update  : 2 MAY 85
  16. -- Machine/System Compiled/Run on : UNIX using LEX & CC
  17. --                                                           -*
  18. ---------------------------------------------------------------
  19. --                                                           -*
  20. -- Keywords     :  Formatting, Scribe, UNIX, prettyprinting,
  21. --                 typesetting
  22. ----------------:
  23. --
  24. -- Abstract     :  NOTE:  This is a UNIX lex input file.  You 
  25. --           need a lex processor (and C compiler) for this
  26. --           tool!!  This tool is NOT in Ada.
  27. --
  28. --                 Adaform takes a legal Ada program as input
  29. --           and adds Scribe commands.  The output is
  30. --           then run through Scribe.  The form of the
  31. --           output follows the typesetting conventions
  32. --           in the Ada LRM, with reserved words in 
  33. --           lower-case/boldface.  Additionally, comments
  34. --           are in italics.  Adaform uses the Scribe
  35. --           indexing capability to produce an index of
  36. --           types, packages, procedures, etc, indexed by
  37. --           both name and 'class'.  For instance, package
  38. --            Text_IO would be entered as "Text_IO, package" and
  39. --           "Package, Text_IO".  Adaform is based on the
  40. --           lex input from H. Fisher's Ada grammar.
  41. --
  42. --           This submission consists of 3 parts, text describing
  43. --           the tool, the Lex input file, and a driver, written
  44. --            in C.  
  45. ----------------:  
  46. --                                                           -*
  47. ------------------ Revision history ---------------------------
  48. --                                                           -*
  49. -- DATE         VERSION    AUTHOR                  HISTORY
  50. --                                                           -*
  51. ------------------ Distribution and Copyright -----------------
  52. --                                                           -*
  53. -- This prologue must be included in all copies of this software.
  54. --
  55. -- This software is released to the Ada community.
  56. --
  57. -- Restrictions on use or distribution:  NONE
  58. --                                                           -*
  59. ------------------ Disclaimer ---------------------------------
  60. --                                                           -*
  61. -- This software and its documentation are provided "AS IS" and
  62. -- without any expressed or implied warranties whatsoever.
  63. -- No warranties as to performance, merchantability, or fitness
  64. -- for a particular purpose exist.
  65. --
  66. -- Because of the diversity of conditions and hardware under
  67. -- which this software may be used, no warranty of fitness for
  68. -- a particular purpose is offered.  The user is advised to
  69. -- test the software thoroughly before relying on it.  The user
  70. -- must assume the entire risk and liability of using this
  71. -- software.
  72. --
  73. -- In no event shall any person or organization of people be
  74. -- held responsible for any direct, indirect, consequential
  75. -- or inconsequential damages or lost profits.
  76. --                                                           -*
  77. -------------------END-PROLOGUE--------------------------------
  78.  
  79. ::::::::::
  80. ADAFORM.DOC
  81. ::::::::::
  82.  
  83. We use Scribe as our standard word processing system.  We needed a way
  84. to present Ada code within some text.  The Ada Language Reference
  85. Manual has defined a presentation style for Ada code, with reserved
  86. words in lower-case/boldface.  We wanted to use this style, and add
  87. other changes to make the code more readable.  In particular, we wanted
  88. to set comments off from Ada code.  Finally, we wanted to do this 
  89. without having to modify our Ada code by hand.  
  90.  
  91. To meet these requirements, we developed a tool called "adaform".
  92. "adaform" is a UNIX filter that accepts (well formed) Ada programs from stdin,
  93. and adds Scribe commands.  The output is then suitable for 'Scribing'.
  94. See the implementation section for a definition of the Scribe environmnents
  95. used by adaform.  Invocation is "adaform < <your input> > <your output>".
  96. You must direct input from standard input; adaform does not recognize the
  97. convention that a filename may be passed as a parameter for standard input.
  98.  
  99. The following is straight text, of 3 parts:  The first is a description of
  100. the tool, followed by the LEX file, followed by our C driver program.
  101.  
  102. Capabilities:
  103.  
  104. adaform does the following things:
  105.  
  106. 1. Ada reserved words are printed in boldface.
  107.  
  108. 2. Ada comments are printed in italics.
  109.  
  110. 3. Comment reserved words (such as pre-conditions, history, etc.) are 
  111. printed in bold italics.
  112.  
  113. 4.  Pragma page is recognized; a new page is started after this pragma.
  114. Additionally, every library unit is started on a new page (triggered by an
  115. Ada 'with' clause.)
  116.  
  117. 5.  Package, procedure, function, type, subtype and task names are 
  118. indexed.  The index entry is the identifier, followed by the Ada thing
  119. identified.  Additionally, if you use the LibraryFile MultiLevelIndex, 
  120. then there is a secondary index by the class of item.  for instance:
  121.     Foo, package            2
  122.     Fred, type            1
  123.     Frog, type            3
  124.     Fubar, task            3
  125.     package
  126.       Foo                       2
  127.     task
  128.       Fubar                3
  129.       Fred                1
  130.       Frog                3
  131.  
  132. 6.  A 'hinge' is placed on each blank line.  This means that a group of
  133. non-blank lines will be kept on one page.  An example of this is a
  134. function declaration and its comments.
  135.  
  136. Limitations:  
  137.  
  138. adaform expects legal Ada code.  It has no error handling, and its actions
  139. on an ill-formed program are undefined, and will probably either give you
  140. a lex error, or garbage.
  141.  
  142. The logic used to detect a new compilation unit is very primitive.  If
  143. adaform finds a with clause (that is not in a generic declaration), it
  144. first inserts a page feed (@newpage).  Lines preceeding the with clause
  145. will be placed on the preceeding page.  If there is more than one 
  146. compilation unit in a package, and they do not have with clauses, then
  147. they will not start on a new page.  The rule of thumb here is first,
  148. use only 1 compilation unit per text file, and second, don't use 
  149. preceeding comments.
  150.  
  151. If a set of non-blank lines exceeds a single page, Scribe will probably
  152. issue an error message.  Use "pragma page" to establish page breaks.
  153.  
  154. The only 'prettyprinting' performed by adaform is that reserved words are
  155. translated to lower case, except for EXIT, RETURN, RAISE and GOTO. Adaform
  156. performs no formatting.  However, tabs are converted to the scribe tab
  157. character "@\".  
  158.  
  159. Because the output from Scribe is in proportionally spaced fonts, column
  160. alignment is not preserved.  This is most noticable when you have a comment
  161. like:
  162.  
  163. ----------------------------------
  164. --  Here is my comment          --
  165. ----------------------------------
  166. This usually comes out looking something like:
  167.  
  168. -----------------------
  169. --  Here is my comment         --
  170. -----------------------
  171.  
  172. due to the different character widths of the text and the '-' character.
  173. To insure indentation alignment, use tabs instead of spaces.
  174.  
  175. Implementation:
  176.  
  177. adaform was created using Lex and Herm Fischer's Lex input for his Ada 
  178. grammar.  Because it is 'lexed', and I'm not a Lexpert, it runs a little
  179. slowly.  A driver is also requred for the Lex output, this driver writes 
  180. out the environment standards, and then calls yylex().
  181.  
  182. There are 4 Scribe definitions needed to support adaform.  These are
  183. definitions of the various Scribe environments used by adaform:
  184.  
  185. @define(AdaResWord=B)        makes reserved words all boldface
  186. @define(AdaComment=I)           comments are in italics
  187. @define(AdaCommentWord=P)       special comment reserved words in bold italics
  188.  
  189. @define(AdaCode=example, FaceCode R, LeftMargin 0, RightMargin 0, 
  190.         font SmallBodyFont, BlankLines=HingeKeep)
  191.  
  192.                     AdaCode is an example environment, with 
  193.                 no indentation, using roman (standard) type,
  194.                 in the SmallBodyFont (10 point), and simulates
  195.                 a hinge command at each blank line.
  196.  
  197. These definitions are usually written out by the driver.
  198.  
  199. Future Features:
  200.  
  201. I hope to add scope information to the index entries, so the following 
  202. will be the result:
  203.  
  204.     Foo, type, defined in Ralph.Bar        3
  205.     Fred, function, defined in Ralph    2
  206.  
  207. It would be nice to make adaform more intelligent about when to start a new
  208. page.
  209.  
  210. Finally, in conjunction with a conventional prettyprinter, I hope to solve
  211. the alignment problem when using porportional spaced fonts.
  212.  
  213. Send bugs (hopefully with fixes) to:
  214.  
  215.                             Dave Emery
  216.         uucp:    ...princeton!siemens!emery 
  217.         arpa:    "siemens!emery"@topaz
  218.         us mail: Siemens Research
  219.              105 College Rd East
  220.              Princeton, NJ 08540
  221.         ma bell: (609) 734-6568
  222.  
  223.  
  224. trademarks:  Ada is a trademark of U.S. Government, AJPO.  Scribe is a
  225. trademark of Unilogic, Inc.  UNIX is a trademark of ATT Bell Labs.
  226.  
  227. ::::::::::
  228. ADAFORM.LEX
  229. ::::::::::
  230.  
  231. LEX DEFINITION
  232. %{
  233. /**************************************************************************/
  234. /*                 Ada for SCRIBE PRINTER                                 */
  235. /**************************************************************************/
  236. /*------------------------------------------------------------------------*/
  237. /* Lexical input for LEX for LALR(1) Grammar for ANSI Ada                 */
  238. /*                                                                        */
  239. /*              Herman Fischer                                            */
  240. /*            Litton Data Systems                                         */
  241. /*              March 26, 1984                                            */
  242. /*                                                                        */
  243. /*------------------------------------------------------------------------*/
  244.     int    my_start   = 0; /* indicates that it's startup time       */
  245.     int    ignore_next_subprog = 0;  /* when in a generic parameter  */
  246.                       /* declaration, indicates that  */
  247.                       /* the next subprogram is a     */
  248.                       /* formal parameter, and not    */
  249.                       /* the actual generic thing.    */
  250.     int    funct_defn = 0;  /* indicates that this return is part    */
  251.                  /* of a function declaration, and should */
  252.                  /* not be capitalized              */ 
  253.     char    class[20];
  254. #include "stdio.h"
  255. #include "ctype.h"
  256. %}
  257.  
  258. %e      3500  /* tree nodes array size    */
  259. %p      9500  /* positions                */
  260. %a      10000  /* transitions array size   */
  261. %k      500   /* packed character classes */
  262. %o      10000  /* output array size        */
  263. %n    2000   /* states ?!          */
  264.  
  265. %START IDENT Z COMMENT OTHERCMT PRAGMA KEEPNAME GENERICSPEC
  266.  
  267. %%
  268.      {BEGIN Z;}
  269. <Z,IDENT>(ABORT|abort)|(ABS|abs)|(ACCEPT|accept)|(ACCESS|access)    |
  270. <Z,IDENT>(ALL|all)|(AND|and)|(ARRAY|array)|(AT|at)            |
  271. <Z,IDENT>(BEGIN|begin)|(BODY|body)|(CASE|case)|(CONSTANT|constant)    |
  272. <Z,IDENT>(DECLARE|declare)|(DELAY|delay)|(DELTA|delta)|(DIGITS|digits)    |
  273. <Z,IDENT>(DO|do)|(ELSE|else)|(ELSIF|elsif)|(END|end)|(ENTRY|entry)    |
  274. <Z,IDENT>(EXCEPTION|exception)|(FOR|for)                |
  275. <Z,IDENT>(IF|if)|(IN|in)|(IS|is)|(LIMITED|limited)|(LOOP|loop)        |
  276. <Z,IDENT>(MOD|mod)|(NEW|new)|(NOT|not)|(NULL|null)|(OF|of)|(OR|or)    |
  277. <Z,IDENT>(OTHERS|others)|(OUT|out)|(PRIVATE|private)            |
  278. <Z,IDENT>(RANGE|range)|(RECORD|record)|(REM|rem)            |
  279. <Z,IDENT>(RENAMES|renames)|(REVERSE|reverse)                |
  280. <Z,IDENT>(SELECT|select)|(SEPARATE|separate)                |
  281. <Z,IDENT>(TERMINATE|terminate)|(THEN|then)|(USE|use)            |
  282. <Z,IDENT>(WHEN|when)|(WHILE|while)|(XOR|xor)                {
  283.             MAKELOWER(yytext); hilite(yytext); BEGIN Z;    }
  284.  
  285. <Z,IDENT>(WITH|with)                            {
  286.             printf("\n@newpage\n");
  287.             MAKELOWER(yytext); hilite(yytext);
  288.             BEGIN Z;                    }
  289.  
  290.  
  291. <Z,IDENT>(GOTO|goto)|(EXIT|exit)|(RAISE|raise)                {
  292.             MAKEUPPER(yytext); hilite(yytext); BEGIN Z;    }
  293.  
  294. <Z,IDENT>(RETURN|return)                        {
  295.             if (funct_defn) 
  296.                 funct_defn = 0;
  297.              else   {MAKEUPPER(yytext);
  298.                  funct_defn = 0;};
  299.             hilite(yytext); BEGIN Z;            }
  300.  
  301. <Z,IDENT>("=>")|("..")|("**")|(":=")|("/=")|(">=")|("<=")|("<<")    |
  302. <Z,IDENT>(">>")|("<>")|("&")|("(")|("*")|("+")|(",")|("-")|(".")    |
  303. <Z,IDENT>("/")|(":")|(";")|("<")|("=")|(">")|("|")            {
  304.                         ECHO; BEGIN Z;        }
  305.  
  306. <Z,IDENT>")"             {ECHO; BEGIN IDENT;}
  307.  
  308. "\t"                {unput('\\'); unput('@');}
  309.  
  310. <Z,IDENT>(PRAGMA|pragma)    {
  311.                 MAKELOWER(yytext);
  312.                 hilite(yytext);
  313.                 BEGIN PRAGMA;
  314.                 }
  315.  
  316. <PRAGMA>[" "(@\\)]+(PAGE|page)[ (@\\)]*;    {
  317.                     ECHO;
  318.                     printf("\n@newpage\n");
  319.                     BEGIN Z;
  320.                     }
  321.  
  322. <PRAGMA>.            {
  323.                 unput(yytext[0]);
  324.                 BEGIN Z;
  325.                 }
  326.  
  327. <Z,IDENT>TYPE|type        |
  328. <Z,IDENT>SUBTYPE|subtype    |
  329. <Z,IDENT>TASK|task        |
  330. <Z,IDENT>(PROCEDURE|procedure)    |
  331. <Z,IDENT>(PACKAGE|package)     {
  332.                 MAKELOWER(yytext);
  333.                 hilite(yytext); strcpy(class, yytext);
  334.                 BEGIN KEEPNAME;}
  335.  
  336.  
  337. <Z,IDENT>(FUNCTION|function)    {
  338.                 MAKELOWER(yytext);
  339.                 hilite(yytext); strcpy(class, yytext);
  340.                 funct_defn = 1;
  341.                 BEGIN KEEPNAME;}
  342.  
  343.  
  344. <KEEPNAME>[a-zA-Z][a-z_A-Z0-9]*       {
  345.                 ECHO; printf("@index(%s, %s)", yytext, class);
  346.            printf("@indexsecondary(primary=%s, secondary=%s)", class, yytext);
  347.                 BEGIN Z;}
  348.  
  349. <Z,IDENT>(GENERIC|generic)    {
  350.                 MAKELOWER(yytext); hilite(yytext); 
  351.                 BEGIN GENERICSPEC;}
  352.  
  353. <GENERICSPEC>(WITH|with)                        {
  354.                 MAKELOWER(yytext); hilite(yytext);
  355.                 ignore_next_subprog = 1;}
  356.  
  357. <GENERICSPEC>(PROCEDURE|procedure)|(FUNCTION|function)            {
  358.                 MAKELOWER(yytext); hilite(yytext);
  359.                 {if (ignore_next_subprog)
  360.                     ignore_next_subprog = 0;
  361.                  else {
  362.                         {if (strcmp(yytext,"procedure"))
  363.                         strcpy(class,"generic procedure");
  364.                      else    
  365.                         strcpy(class,"generic function");
  366.                         funct_defn = 1;
  367.                     };
  368.                     ignore_next_subprog = 0;
  369.                     BEGIN KEEPNAME;
  370.                        };
  371.                 };
  372.                                     }
  373.  
  374. <GENERICSPEC>(PACKAGE|package)                        {
  375.                 MAKELOWER(yytext); hilite(yytext);
  376.                 strcpy(class,"generic package");
  377.                 BEGIN KEEPNAME;}
  378.  
  379.  
  380. <GENERICSPEC>(ACCESS|access)|(ARRAY|array)|(CONSTANT|constant)        |
  381. <GENERICSPEC>(DELTA|delta)|(DIGITS|digits)|(IN|in)|(IS|is)        |
  382. <GENERICSPEC>(LIMITED|limited)|(OF|of)|(OTHERS|others)|(OUT|out)    |
  383. <GENERICSPEC>(PRIVATE|private)|(RANGE|range)|(RETURN|return)        |
  384. <GENERICSPEC>(SUBTYPE|subtype)|(TYPE|type)                {
  385.                 MAKELOWER(yytext); hilite(yytext);    }
  386.  
  387. <GENERICSPEC>[a-zA-Z][a-z_A-Z0-9]*    {ECHO;}
  388.  
  389. <GENERICSPEC>.                {ECHO;}
  390.  
  391.  
  392. <IDENT>\'  {ECHO; BEGIN Z;}   /* type mark only */
  393.  
  394. <Z,IDENT>[a-zA-Z][a-z_A-Z0-9]*       {ECHO; BEGIN IDENT; }
  395.  
  396. <Z,IDENT>[0-9][0-9_]*([.][0-9_]+)?([Ee][-+]?[0-9_]+)?  {
  397.                                  ECHO; BEGIN Z; }
  398.  
  399. <Z,IDENT>[0-9][0-9_]*#[0-9a-fA-F_]+([.][0-9a-fA-F_]+)?#([Ee][-+]?[0-9_]+)? {
  400.                   ECHO; BEGIN Z; }
  401.  
  402. <Z,IDENT>\"([^\"]*(\"\")*)*\" {ECHO; BEGIN Z; }
  403.  
  404. <Z>\'([^\']|\'\')\'    {ECHO; BEGIN Z; }
  405.  
  406. <Z,IDENT>[" "(@\\)]   {ECHO;}          /* ignore spaces and tabs */
  407.  
  408. <Z,IDENT>.       {ECHO; printf("?? lexical error [%s] ??\n(@\\)", yytext);}
  409.  
  410. <Z,IDENT>"--"([" "(@\\)]*)    {ECHO; BEGIN COMMENT;}
  411.  
  412. <COMMENT,OTHERCMT>"-"+    {ECHO;}
  413. <COMMENT,OTHERCMT>[\n]    {ECHO; BEGIN Z;}
  414.  
  415. <COMMENT>(("Function"[" "(@\\)]*":")|("Reference"[" "(@\\)]*":"))        |
  416. <COMMENT>(("History"[" "(@\\)]*":")|("Exceptions"[" "(@\\)]*":"))        |
  417. <COMMENT>(("Authorization"[" "(@\\)]*":")|("Pre-conditions"[" "(@\\)]*":"))    |
  418. <COMMENT>(("Post-conditions"[" "(@\\)]*":")|("EXCEPTIONS"[" "(@\\)]*":"))    {
  419.                 printf("@AdaCommentWord(%s)", yytext);
  420.                 BEGIN OTHERCMT;
  421.                                     }
  422.  
  423. <COMMENT>.    {
  424.          unput(yytext[0]); 
  425.          BEGIN OTHERCMT;
  426.         }
  427.  
  428. <OTHERCMT>.*    {
  429.          printf("@begin(AdaComment)%s@end(AdaComment)", yytext);
  430.          BEGIN Z;
  431.         }
  432.  
  433. %%
  434. hilite(it)    
  435.     char     *it;
  436. {printf("@AdaResWord(%s)", it);}
  437.  
  438. MAKEUPPER(s)
  439.     char    *s;
  440. {
  441.     for (; *s != NULL; ++s)
  442.         *s = (islower(*s) ? toupper(*s) : *s);
  443. }
  444. MAKELOWER(s)
  445.     char    *s;
  446. {
  447.     for (; *s != NULL; ++s)
  448.         *s = (isupper(*s) ? tolower(*s) : *s);
  449. }
  450.  
  451. end of LEX DEFINITION
  452. OUR DRIVER:
  453.  
  454. main()
  455.  
  456. {
  457.     printf("@comment(formatted with adaform v 1.3)\n");
  458.     printf("@define(AdaResWord=B)\n");
  459.     printf("@define(AdaComment=I)\n");
  460.     printf("@define(AdaCommentWord=P)\n");
  461. printf("@define(AdaCode=example, FaceCode R, LeftMargin 0, RightMargin 0,\n");
  462.     printf("\t font SmallBodyFont, blanklines=hingekeep)\n");
  463.     printf("@begin(AdaCode)\n");
  464.     printf("@TabClear\n@TabDivide(10)\n");
  465.     yylex();
  466.     printf("@end(AdaCode)\n");
  467. }
  468.  
  469. end of OUR DRIVER
  470.  
  471.