home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / syncolor.e < prev    next >
Text File  |  1993-11-18  |  4KB  |  170 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. include keywords.e
  17.  
  18. -- character classes
  19. constant DIGIT = 1,
  20.      OTHER = 2,
  21.      LETTER  = 3,
  22.      BRACKET = 4,
  23.      QUOTE   = 5,
  24.      DASH = 6,
  25.      WHITE_SPACE = 7
  26.  
  27. sequence char_class
  28.  
  29. global procedure init_class()
  30. -- set up character classes for easier line scanning
  31. -- (assume no 0 char)
  32.     char_class = repeat(OTHER, 255)
  33.  
  34.     char_class['a'..'z'] = LETTER
  35.     char_class['A'..'Z'] = LETTER
  36.     char_class['_'] = LETTER
  37.     char_class['0'..'9'] = DIGIT
  38.     char_class['['] = BRACKET
  39.     char_class[']'] = BRACKET
  40.     char_class['('] = BRACKET
  41.     char_class[')'] = BRACKET
  42.     char_class['{'] = BRACKET
  43.     char_class['}'] = BRACKET
  44.     char_class['\''] = QUOTE
  45.     char_class['"'] = QUOTE
  46.     char_class[' '] = WHITE_SPACE
  47.     char_class['\t'] = WHITE_SPACE
  48.     char_class['-'] = DASH
  49. end procedure
  50.  
  51. sequence line  -- the line being processed
  52. integer seg_start, seg_end -- start and end of current segment of line
  53. integer color  -- the current color
  54.  
  55. procedure flush(integer new_color)
  56. -- if the color is changing, write out the current segment
  57.     if new_color != color then
  58.     if color != -1 then
  59.         text_color(color)
  60.         puts(SCREEN, line[seg_start..seg_end])
  61.         seg_start = seg_end + 1
  62.     end if
  63.     color = new_color
  64.     end if
  65. end procedure
  66.  
  67. global procedure DisplayColorLine(sequence pline, integer all_clear)
  68. -- Display a line with colors identifying the various
  69. -- parts of the Euphoria language.
  70. -- Each screen write has a lot of overhead, so we try to minimize
  71. -- the number of them by collecting consecutive characters of the
  72. -- same color into a 'segment' seg_start..seg_end.
  73.  
  74.     integer class, last, i, c, bracket_level
  75.     sequence word
  76.  
  77.     line = pline
  78.     color = -1 -- initially undefined
  79.     bracket_level = 0
  80.     seg_start = 1
  81.     seg_end = 0
  82.     while seg_end < length(line) do
  83.     c = line[seg_end+1]
  84.     class = char_class[c]
  85.  
  86.     if class = WHITE_SPACE then
  87.         seg_end = seg_end + 1 -- continue with same color
  88.  
  89.     elsif class = LETTER then
  90.         last = length(line)
  91.         for j = seg_end + 2 to last do
  92.         c = line[j]
  93.         class = char_class[c]
  94.         if class != LETTER then
  95.             if class != DIGIT then
  96.             last = j - 1
  97.             exit
  98.             end if
  99.         end if
  100.         end for
  101.         word = line[seg_end+1..last]
  102.         if find(word, keywords) then
  103.         flush(KEYWORD_COLOR)
  104.         elsif find(word, builtins) then
  105.         flush(BUILTIN_COLOR)
  106.         else
  107.         flush(NORMAL_COLOR)
  108.         end if
  109.         seg_end = last
  110.  
  111.     elsif class <= OTHER then -- DIGIT too
  112.         flush(NORMAL_COLOR)
  113.         seg_end = seg_end + 1
  114.  
  115.     elsif class = BRACKET then
  116.         if find(c, "([{") then
  117.         bracket_level = bracket_level + 1
  118.         end if
  119.         if bracket_level >= 1 and
  120.            bracket_level <= length(bracket_color) then
  121.         flush(bracket_color[bracket_level])
  122.         else
  123.         flush(NORMAL_COLOR)
  124.         end if
  125.         if find(c, ")]}") then
  126.         bracket_level = bracket_level - 1
  127.         end if
  128.         seg_end = seg_end + 1
  129.  
  130.     elsif class = DASH then
  131.         if seg_end + 2 <= length(line) then
  132.         if line[seg_end+2] = '-' then
  133.             flush(COMMENT_COLOR)
  134.             seg_end = length(line)
  135.             exit
  136.         end if
  137.         end if
  138.         flush(NORMAL_COLOR)
  139.         seg_end = seg_end + 1
  140.  
  141.     else  -- QUOTE
  142.         i = seg_end + 2
  143.         while i <= length(line) do
  144.         if line[i] = c then
  145.             i = i + 1
  146.             exit
  147.         elsif line[i] = '\\' then
  148.             if i < length(line) then
  149.             i = i + 1 -- ignore escaped char
  150.             end if
  151.         end if
  152.         i = i + 1
  153.         end while
  154.         flush(STRING_COLOR)
  155.         seg_end = i - 1
  156.     end if
  157.     end while
  158.  
  159.     if color != -1 then
  160.     text_color(color)
  161.     end if
  162.     if all_clear then
  163.     puts(SCREEN, line[seg_start..seg_end])
  164.     else
  165.     puts(SCREEN, line[seg_start..seg_end] & BLANK_LINE)
  166.     end if
  167. end procedure
  168.  
  169.  
  170.