home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gofer230.zip / Progs / Gofer / Demos / literate.lgs < prev    next >
Text File  |  1994-06-23  |  4KB  |  102 lines

  1. Literate comments
  2. -----------------
  3.  
  4. [This file contains an executable version of the program for
  5. processing literate scripts that appears in Appendix C of the Haskell
  6. report, version 1.2.]
  7.  
  8. Many Haskell implementations support the ``literate comment''
  9. convention, first developed by Richard Bird and Philip Wadler for
  10. Orwell, and inspired in turn by Donald Knuth's ``literate programming''.
  11. The convention is not part of the Haskell language, but it is
  12. supported by the implementations known to us (Chalmers, Glasgow,
  13. and Yale).
  14.  
  15. The literate style encourages comments by making them the default.
  16. A line in which ">" is the first character is treated as part of
  17. the program; all other lines are comment.  Within the program part,
  18. the usual "--" and "{- -}" comment conventions may still be used.
  19. To capture some cases where one omits an ">" by mistake, it is an
  20. error for a program line to appear adjacent to a non-blank comment
  21. line, where a line is taken as blank if it consists only of
  22. whitespace.
  23.  
  24. By convention, the style of comment is indicated by the file
  25. extension, with ".hs" indicating a usual Haskell file, and ".lhs"
  26. indicating a literate Haskell file.
  27.  
  28. To make this precise, we present a literate Haskell program to
  29. convert literate programs.  The program expects a single name "file"
  30. on the command line, reads "file.lhs", and either writes the
  31. corresponding program to "file.hs" or prints error messages to
  32. "stderr".
  33.  
  34. Each of the lines in a literate script is a program line, a blank
  35. line, or a comment line.  In the first case, the text is kept with
  36. the line.
  37.  
  38. > data Classified  =  Program String | Blank | Comment
  39.  
  40. In a literate program, program lines begins with a `>' character,
  41. blank lines contain only whitespace, and all other lines are comment
  42. lines.
  43.  
  44. > classify                           ::  String -> Classified
  45. > classify ('>':s)            =   Program s
  46. > classify s  |  all isSpace s        =   Blank
  47. > classify s  |  otherwise        =   Comment
  48.  
  49. In the corresponding program, program lines have the leading `>'
  50. replaced by a leading space, to preserve tab alignments.
  51.  
  52. > unclassify                        ::  Classified -> String
  53. > unclassify (Program s)        =   " " ++ s
  54. > unclassify Blank            =   ""
  55. > unclassify Comment            =   ""
  56.  
  57. Process a literate program into error messages (if any) and the
  58. corresponding non-literate program.
  59.  
  60. > process       ::  String -> (String, String)
  61. > process lhs    =   (es, hs)
  62. >        where    cs  =  map classify (lines lhs)
  63. >            es  =  unlines (errors cs)
  64. >            hs  =  unlines (map unclassify cs)
  65.  
  66. Check that each program line is not adjacent to a comment line.
  67.  
  68. > errors    ::  [Classified] -> [String]
  69. > errors cs    =   concat (zipWith3 adjacent [1..] cs (tail cs))
  70.  
  71. Given a line number and a pair of adjacent lines, generate a list
  72. of error messages, which will contain either one entry or none.
  73.  
  74. > adjacent    ::  Int -> Classified -> Classified -> [String]
  75. > adjacent n (Program _) Comment  =  [message n "program" "comment"]
  76. > adjacent n Comment (Program _)  =  [message n "comment" "program"]
  77. > adjacent n this           next  =  []
  78.  
  79. > message n p c = "Line "++show n++": "++p++" line before "++c++" line."
  80.  
  81. Get one argument from the command line; complain if too many or
  82. too few.
  83.  
  84. > getArg  ::  FailCont -> StrCont -> Dialogue
  85. > getArg fail succ
  86. >      =   getArgs                       fail (\strs ->
  87. >          case strs of
  88. >            [str] -> succ str
  89. >            _     -> fail (OtherError "Too many or too few args"))
  90.  
  91. The main program gets name "file", reads "file.lhs", and either
  92. writes the corresponding program to "file.hs" or appends error
  93. messages to "stderr".
  94.  
  95. > main      ::  Dialogue
  96. > main      =   getArg                      exit (\file ->
  97. >          readFile (file ++ ".lhs")              exit (\lhs ->
  98. >          case (process lhs) of
  99. >            ([],hs) -> writeFile (file ++ ".hs") hs      exit done
  100. >            (es,_)  -> appendChan stderr es          exit done))
  101.  
  102.