home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / euphor10.zip / SYNCOLOR.E < prev    next >
Text File  |  1993-06-08  |  5KB  |  181 lines

  1. --         Syntax Color
  2. -- These routines are used to display Euphoria program lines
  3. -- in multiple colors. The editor (ed.ex) and the pretty printer (eprint.ex)
  4. -- both include this file.
  5.  
  6. -- this file assumes that the following symbols have already
  7. -- been defined:
  8. --     SCREEN       - file/device number to write output to
  9. --    BLANK_LINE   - extra blanks to print at end of a line
  10. --      NORMAL_COLOR - colors of various syntax classes
  11. --    COMMENT_COLOR
  12. --    KEYWORD_COLOR
  13. --     BUILTIN_COLOR
  14. --      STRING_COLOR
  15.  
  16. constant keywords = {
  17.     "if", "end", "then", "procedure", "else", "for", "return",
  18.     "do", "elsif", "while", "type", "constant", "to", "and", "or",
  19.     "exit", "function", "global", "by", "not", "include",
  20.     "with", "without", "profile"}
  21.  
  22. constant builtins = {
  23.     "length", "puts", "integer", "sequence", "position", "object",
  24.     "append", "prepend", "print", "printf",
  25.     "clear_screen", "floor", "getc", "gets", "get_key",
  26.     "rand", "repeat", "atom", "compare", "find", "match",
  27.     "time", "command_line", "open", "close", "trace",
  28.     "sqrt", "sin", "cos", "tan", "log", "system", "date", "remainder",
  29.     "power", "machine_func", "machine_proc"}
  30.  
  31. -- character classes
  32. constant DIGIT = 1,
  33.      OTHER = 2,
  34.      LETTER  = 3,
  35.      BRACKET = 4,
  36.      QUOTE   = 5,
  37.      DASH = 6,
  38.      WHITE_SPACE = 7
  39.  
  40. sequence char_class
  41.  
  42. global procedure init_class()
  43. -- set up character classes for easier line scanning
  44. -- (assume no 0 char)
  45.     char_class = repeat(OTHER, 255)
  46.  
  47.     char_class['a'..'z'] = LETTER
  48.     char_class['A'..'Z'] = LETTER
  49.     char_class['_'] = LETTER
  50.     char_class['0'..'9'] = DIGIT
  51.     char_class['['] = BRACKET
  52.     char_class[']'] = BRACKET
  53.     char_class['('] = BRACKET
  54.     char_class[')'] = BRACKET
  55.     char_class['{'] = BRACKET
  56.     char_class['}'] = BRACKET
  57.     char_class['\''] = QUOTE
  58.     char_class['"'] = QUOTE
  59.     char_class[' '] = WHITE_SPACE
  60.     char_class['\t'] = WHITE_SPACE
  61.     char_class['-'] = DASH
  62. end procedure
  63.  
  64. sequence line  -- the line being processed
  65. integer seg_start, seg_end -- start and end of current segment of line
  66. integer color  -- the current color
  67.  
  68. procedure flush(integer new_color)
  69. -- if the color is changing, write out the current segment
  70.     if new_color != color then
  71.     if color != -1 then
  72.         text_color(color)
  73.         puts(SCREEN, line[seg_start..seg_end])
  74.         seg_start = seg_end + 1
  75.     end if
  76.     color = new_color
  77.     end if
  78. end procedure
  79.  
  80. global procedure DisplayColorLine(sequence pline, integer all_clear)
  81. -- Display a line with colors identifying the various
  82. -- parts of the Euphoria language.
  83. -- Each screen write has a lot of overhead, so we try to minimize
  84. -- the number of them by collecting consecutive characters of the
  85. -- same color into a 'segment' seg_start..seg_end.
  86.  
  87.     integer class, last, i, c, bracket_level
  88.     sequence word
  89.  
  90.     line = pline
  91.     color = -1 -- initially undefined
  92.     bracket_level = 0
  93.     seg_start = 1
  94.     seg_end = 0
  95.     while seg_end < length(line) do
  96.     c = line[seg_end+1]
  97.     class = char_class[c]
  98.  
  99.     if class = WHITE_SPACE then
  100.         seg_end = seg_end + 1 -- continue with same color
  101.  
  102.     elsif class = LETTER then
  103.         last = length(line)
  104.         for j = seg_end + 2 to last do
  105.         c = line[j]
  106.         class = char_class[c]
  107.         if class != LETTER then
  108.             if class != DIGIT then
  109.             last = j - 1
  110.             exit
  111.             end if
  112.         end if
  113.         end for
  114.         word = line[seg_end+1..last]
  115.         if find(word, keywords) then
  116.         flush(KEYWORD_COLOR)
  117.         elsif find(word, builtins) then
  118.         flush(BUILTIN_COLOR)
  119.         else
  120.         flush(NORMAL_COLOR)
  121.         end if
  122.         seg_end = last
  123.  
  124.     elsif class <= OTHER then -- DIGIT too
  125.         flush(NORMAL_COLOR)
  126.         seg_end = seg_end + 1
  127.  
  128.     elsif class = BRACKET then
  129.         if find(c, "([{") then
  130.         bracket_level = bracket_level + 1
  131.         end if
  132.         if bracket_level >= 1 and
  133.            bracket_level <= length(bracket_color) then
  134.         flush(bracket_color[bracket_level])
  135.         else
  136.         flush(NORMAL_COLOR)
  137.         end if
  138.         if find(c, ")]}") then
  139.         bracket_level = bracket_level - 1
  140.         end if
  141.         seg_end = seg_end + 1
  142.  
  143.     elsif class = DASH then
  144.         if seg_end + 2 <= length(line) then
  145.         if line[seg_end+2] = '-' then
  146.             flush(COMMENT_COLOR)
  147.             seg_end = length(line)
  148.             exit
  149.         end if
  150.         end if
  151.         flush(NORMAL_COLOR)
  152.         seg_end = seg_end + 1
  153.  
  154.     else  -- QUOTE
  155.         i = seg_end + 2
  156.         while i <= length(line) do
  157.         if line[i] = c then
  158.             if line[i-1] != '\\' then
  159.             i = i + 1
  160.             exit
  161.             end if
  162.         end if
  163.         i = i + 1
  164.         end while
  165.         flush(STRING_COLOR)
  166.         seg_end = i - 1
  167.     end if
  168.     end while
  169.  
  170.     if color != -1 then
  171.     text_color(color)
  172.     end if
  173.     if all_clear then
  174.     puts(SCREEN, line[seg_start..seg_end])
  175.     else
  176.     puts(SCREEN, line[seg_start..seg_end] & BLANK_LINE)
  177.     end if
  178. end procedure
  179.  
  180.  
  181.