home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 876 / hugs.sis / Literate.lhs < prev    next >
Encoding:
Text File  |  2000-09-21  |  3.7 KB  |  100 lines

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