home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / thesrc15.zip / match.the < prev    next >
Text File  |  1993-10-30  |  3KB  |  87 lines

  1. /*
  2. $Header: C:\THE\RCS\match.the 1.4 1993/09/01 16:27:20 MH Interim MH $
  3. */
  4. /***********************************************************************/
  5. /* Description: REXX macro to match pairs of keywords                  */
  6. /* Syntax:      match                                                  */
  7. /* Notes:       This macro will mark a line block enclosing all lines  */
  8. /*              that are enclosed by a matching pair of keywords.      */
  9. /*              Keywords can actually be more than 1 word, like        */
  10. /*              'end if' but are specified as 1 word.                  */
  11. /*              Supported keyword pairs are:                           */
  12. /*               #if      - #endif                                     */
  13. /*               begin    - end;                                       */
  14. /*               do       - end                                        */
  15. /*               if       - endif                                      */
  16. /*               loop     - endloop                                    */
  17. /* Bugs:        Only keywords that START a line are considered for     */
  18. /*              matching and each keyword must be on seperate lines.   */
  19. /***********************************************************************/
  20. Trace o
  21. string.1 =  '#IF #ENDIF >'
  22. string.2 =  '#ENDIF #IF <'
  23. string.3 =  'BEGIN END; >'
  24. string.4 =  'END; BEGIN <'
  25. string.5 =  'DO END >'
  26. string.6 =  'END DO <'
  27. string.7 =  'IF ENDIF >'
  28. string.8 =  'ENDIF IF <'
  29. string.9 =  'LOOP ENDLOOP >'
  30. string.10 = 'ENDLOOP LOOP <'
  31. num_string = 10
  32.  
  33. 'EXTRACT /LINE/CURLINE/'/* get the focus line contents and line number */
  34. save_current_line = line.1
  35. newline = ''
  36. Do i = 1 To Words(curline.3)      /* get rid of all blanks in the line */
  37.    newline = newline||Strip(Word(curline.3,i))
  38. End
  39. newline = Translate(newline)            /* uppercase to match keywords */
  40. stridx = 0
  41. Do i = 1 To num_string                             /* find a keyword...*/
  42.    source = Word(string.i,1)
  43.    length = Length(source)
  44.    If Substr(newline,1,length) = source Then
  45.       Do
  46.         stridx = i
  47.         Leave
  48.       End
  49. End
  50. If stridx = 0 Then                       /* if no keyword found, error */
  51.    Do
  52.      'EMSG Unknown match string'
  53.      Exit
  54.    End
  55. num_source = 1
  56. num_target = 0
  57. source = Word(string.stridx,1)
  58. target = Word(string.stridx,2)
  59. direction = Word(string.stridx,3)
  60. Do Forever                                  /* find matching keyword...*/
  61.    If direction = '>' Then 'N'
  62.    Else 'U'
  63.    If rc \= 0 Then Leave
  64.    'EXTRACT /CURLINE/'
  65.    newline = ''
  66.    Do i = 1 To Words(curline.3)
  67.       newline = newline||Strip(Word(curline.3,i))
  68.    End
  69.    newline = Translate(newline)
  70.    Select
  71.      When Substr(newline,1,Length(source)) = source Then num_source = num_source + 1
  72.      When Substr(newline,1,Length(target)) = target Then num_target = num_target + 1
  73.      Otherwise
  74.    End
  75.    If num_source = num_target Then                 /* if match found...*/
  76.       Do
  77.         'reset block'                           
  78.         'mark line'                             /*...mark a line block */
  79.         ':'||save_current_line
  80.         'mark line'
  81.         Return
  82.       End
  83. End
  84. 'EMSG No matching target' target  'found for' source
  85. ':'||save_current_line
  86. Return
  87.