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