home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / itags.icn < prev    next >
Text File  |  2000-08-03  |  4KB  |  129 lines

  1. ############################################################################
  2. #
  3. #    File:     itags.icn
  4. #
  5. #    Subject:  Program to create tags file for Icon programs
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     August 3, 2000
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  Program to create a tags file for an Icon program.  It has the
  18. #  options described in the Sun 3.5 man entry for ctags (except -u --
  19. #  update tags file):
  20. #
  21. #  Usage: itags [-aBFtvwx] [-f tagsfile] file...
  22. #
  23. #    -a   append output to an existing tags file.
  24. #
  25. #    -B   use backward searching patterns (?...?).
  26. #
  27. #    -F   use forward searching patterns (/.../) (default).
  28. #
  29. #    -x   produce a list of object names,  the  line  number  and
  30. #         file name on which each is defined, as well as the text
  31. #         of that line and prints this on  the  standard  output.
  32. #         This  is  a simple index which can be printed out as an
  33. #         off-line readable function index.
  34. #
  35. #    -t   create tags for records. 
  36. #
  37. #    -v   produce on the standard output an  index  of  the  form
  38. #         expected  by  vgrind(1).   This  listing  contains  the
  39. #         function name, file name, and page number (assuming  64
  40. #         line pages).  Since the output will be sorted into lex-
  41. #         icographic order, it may be desired to run  the  output
  42. #         through sort -f.  Sample use:
  43. #              itags -v files | sort -f > index
  44. #              vgrind -x index
  45. #
  46. #    -w   suppress warning diagnostics.
  47. #
  48. ############################################################################
  49. #
  50. #  Links:  sort, io, options
  51. #
  52. ############################################################################
  53.  
  54. link sort, io, options
  55.  
  56. global patChar
  57.  
  58. record Tag(fn,line,linenbr,shortline)
  59.  
  60. procedure main(arg)
  61.    local Write,f,fn,idChar,line,linenbr,noWarnings,opt,space,tag,tags,
  62.        tf,tfn,typedef,x
  63.    #
  64.    #  Handle command line options and initialization.
  65.    #
  66.    opt := options(arg,"aBFxtvwuf:")
  67.    if *arg = 0 then
  68.      stop("usage: itags [-aBFtvwx] [-f tagsfile] file...")
  69.    if \opt["u"] then stop("update option (-u) not supported -- rebuild file")
  70.    patChar := if \opt["B"] & /opt["F"] then "?" else "/"
  71.    Write := (if \opt["v"] then VGrind
  72.       else if \opt["x"] then Index
  73.       else {
  74.      tfn := \opt["f"] | "tags"
  75.      tf := open(tfn,if \opt["a"] then "a" else "w") |
  76.            stop("Can't open tags file \"",tfn,"\"")
  77.      Tags
  78.      })
  79.    typedef := opt["t"]
  80.    noWarnings := opt["w"]
  81.    idChar := &letters ++ &digits ++ "_"
  82.    space := ' \t\v\f\r'
  83.    tags := table()
  84.    #
  85.    #  Loop to read files.
  86.    #
  87.    every fn := !arg do {
  88.       if not find(".",fn) then fn ||:= ".icn"
  89.       f := open(fn) | write(&errout,"Couldn't open \"",fn,"\"")
  90.       linenbr := 0
  91.       while line := read(f) do line ? {
  92.      linenbr +:= 1
  93.      if (tab(many(space)) | &null) & =("procedure" | (\typedef,"record")) &
  94.            tab(many(space)) then {
  95.         tag := tab(many(idChar))
  96.         if x := \tags[tag] then {
  97.            if /noWarnings then
  98.              write(&errout,"Duplicate entry in file ",fn,", line ",linenbr,
  99.              ": ",tag,"\nSecond entry ignored")
  100.            }
  101.         else
  102.           tags[tag] := Tag(fn,line,linenbr,line[1:&pos + 1])
  103.         }
  104.      }
  105.       close(f)
  106.       }
  107.    #
  108.    #  Do requested output.
  109.    #
  110.    every Write(!sort(tags),tf)
  111. end
  112.  
  113.  
  114. #
  115. #  Output procedures.
  116. #
  117. procedure Tags(x,f)
  118.    return write(f,x[1],"\t",x[2].fn,"\t",patChar,"^",x[2].shortline,patChar)
  119. end
  120.  
  121. procedure Index(x)
  122.    return write(left(x[1],*x[1] < 16) | x[1],right(x[2].linenbr,4)," ",
  123.      left(x[2].fn,17),x[2].line)
  124. end
  125.  
  126. procedure VGrind(x)
  127.    return write(x[1]," ",x[2].fn," ",(x[2].linenbr - 1) / 64 + 1)
  128. end
  129.