home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d07xx / d0770.lha / Uedit / Jenkins2.LZH / MatchingBraces < prev    next >
Text File  |  1991-04-08  |  6KB  |  192 lines

  1. ========== MatchingBraces ===================================================
  2.  
  3. COPYRIGHT 1990
  4. Robert A. Jenkins, Ph.D.
  5. 22901 Shagbark Lane
  6. Miller Woods, IL 60411
  7. (708) 758-0344
  8. (708) 759-7063 answering machine
  9. All Rights Reserved
  10. Rick Stiles may distribute it as he pleases.
  11. Thanks for writing and continually improving Uedit!
  12. =============================================================================
  13. I hate to think of how many hours I have wasted looking for unmatched braces
  14. in my Uedit commands.  Eventually I wrote this command so that the computer
  15. would do the looking for me.
  16.  
  17. If you place the cursor on the < and hit shftCtl-[ repeatedly, it will walk
  18. through the command, inverting the regions between matching braces.  It keeps
  19. a count of left minus right starting with < so it can warn you of extra
  20. rights at the end of the command, but if you do not start at < the count may
  21. be off.  If you place the cursor on a left { it will show you each brace and
  22. the current count of the level of nesting.
  23.  
  24. Finds first left { after cursor.
  25. Scans, skipping literal and comments, counting right-left until
  26. right-left = 0, so we have found a matching right }.
  27. Displays the matching {} region in invert.
  28.  
  29. Uses virtual-e and virtual-f
  30. n50 = char
  31. n51 = # of unmatched left
  32. n52 = flag (no, trace  msg)
  33. n53 = # right minus left = unmatched left
  34. =============================================================================
  35. Find matching braces { }
  36. <shftCtl-[:
  37.    equateNum(n52,0)                              ..0 = no trace messages
  38.    putMsg("Searching, please wait...")
  39.    copyChar(curFile,n50)
  40.    if (eqNum(n50,"{")) equateNum(n52,1)          ..display trace msgs
  41.    .. find first {
  42.    while (not eqNum(n50,">")) {                  
  43.       runKey(virtual-e)                          ..skip literal and comment
  44.       if (not runKey(virtual-f)) {                             .. check eFile
  45.          putMsg("NO LEFT { FOUND IN FILE")
  46.          return }
  47.       if (eqNum(n50,"<")) equateNum(n53,0)       ..reset n53 since <
  48.       if (eqNum(n50,"}")) {                      ..found right
  49.          incNum(n53)
  50.          refreshDisplay }
  51.       if (eqNum(n50,"{")) {                      ..found left
  52.          equateNum(n51,1)                        ..# unmatched left
  53.          decNum(n53)
  54.          refreshDisplay
  55.          goto label(1) }
  56.       if (eqNum(n50,"}")) if (gtNum(n53,0)) {
  57.          putMsg(" ")
  58.          incLoc(curFile,atCursor)
  59.          alertUser("UNMATCHED RIGHT }")
  60.          equateNum(n53,0)
  61.          return }
  62.       incLoc(curFile,atCursor)
  63.       copyChar(curFile,n50) }
  64.    incLoc(curFile,atCursor)
  65.    putMsg ("NO (MORE) LEFT { FOUND IN <command>")
  66.    refreshDisplay
  67.    return
  68.    
  69. label(1) ..found first left, scan tallying right-left
  70.    if (eqNum(n52,1)) alertUser("found first left {")
  71.    equateLoc(curFile,sInvert,atCursor)           .. mark first left
  72.    incLoc(curFile,atCursor)
  73.    equateLoc(curFile,eInvert,atCursor)
  74.    decNum(n53)
  75.    while (not eqNum(n50,">")) {                  ..scan, tally { and }
  76.       runKey(virtual-e)
  77.       if (not runKey(virtual-f)) {
  78.          alertUser("MATCHING RIGHT } NOT FOUND") 
  79.          return }         
  80.       if (eqNum(n50,"{")) {                      ..found nested left
  81.          incNum(n51)
  82.          decNum(n53)
  83.          refreshDisplay
  84.          if (eqNum(n52,1)) {
  85.             putMsg(n51)
  86.             alertUser("found nested left {") } }            
  87.       if (eqNum(n50,"}")) {                      ..nested or final right
  88.          decNum(n51)
  89.          incNum(n53)
  90.          refreshDisplay
  91.          if (eqNum(n52,1)) {
  92.             putMsg(n51)
  93.             alertUser("found right }") }
  94.          if (eqNum(n51,0)) goto label(2) }       ..final right
  95.       incLoc(curFile,atCursor)
  96.       copyChar(curFile,n50) }
  97.    moveCursor(curFile,eInvert)                   ..go back to first left
  98.    freeBuf(buf50)
  99.    toWord(buf50,n51)
  100.    insertRgn(buf50,eFile," MATCHING RIGHT } NOT FOUND",all)
  101.    alertUser(buf50)
  102.    return
  103.    
  104. label(2) ..found matching right }
  105.    if (eqNum(n52,1)) alertUser("found matching right")
  106.    equateLoc(curFile,eInvert,atCursor)
  107.    incLoc(curFile,eInvert)
  108.    moveCursor(curFile,sInvert)
  109.    incLoc(curFile,atCursor)
  110.    putMsg("{} region displayed in invert")
  111.    refreshDisplay
  112. >
  113.  
  114. Skip over literal and comment
  115. <virtual-e:
  116. copyChar(curFile,n50)
  117. if (eqNum(n50,"\"")) {
  118.    ..alertUser("start of comment")
  119.    incLoc(curFile,atCursor)
  120.    copyChar(curFile,n50)
  121.    while (not eqNum(n50,"\"")) {
  122.       moveCursor(curFile,eChar)
  123.       copyChar(curFile,n50)    }
  124.    incLoc(curFile,atCursor)
  125.    copyChar(curFile,n50)
  126.    ..alertUser("after comment")
  127.    }
  128. if (eqNum(n50,".")) {
  129.    incLoc(curFile,atCursor)
  130.    copyChar(curFile,n50)
  131.    if (eqNum(n50,".")) {
  132.       moveCursor(curFile,sLine)
  133.       moveCursor(curFile,downLine)
  134.       copyChar(curFile,n50)
  135.       if (eqNum(n50,">")) {
  136.          decLoc(curFile,atCursor)
  137.          copyChar(curFile,n50) } } }
  138. >Saturday 23-Feb-91 17:54:53
  139.  
  140. Check eFile
  141. <virtual-f:
  142.    if (eqLoc(curFile,atCursor,eFile)) {
  143.       equateLoc(curFile,atCursor,eInvert)
  144.       returnFalse }
  145.    returnTrue
  146. >
  147.  
  148. Test suite:
  149. < {  }>
  150. <{        
  151. }>
  152. < {               }>
  153. < {  {            }  }>
  154. < {  {  {         }  }  }> 
  155.  
  156. < {  {  {  {      }  }  }  }>
  157.  
  158. < {"("} >
  159.  
  160. < {  "literal"  "}"  }>
  161.  
  162. <.. comment {  }
  163.  
  164.  {    { "literal" }  }>
  165.  
  166. < {}
  167. .. comment
  168. >
  169.  
  170. < {} {
  171. ..comment
  172. }
  173. >
  174.  
  175. < {   { { } }   { }  }>
  176. < {   { { } }   { }  { { } }   }>
  177.  
  178. < { {   { { } }   { }  } }>
  179.  
  180. One matching right } not found
  181. < { {   { { } }   { }  } >
  182.  
  183. Two matching right } not found
  184. < { { {   { { } }   { }  } >
  185.  
  186. One unmatched right { in file
  187. < { {   { { } }   { }  } } }>
  188.  
  189. Two unmatched right } in file
  190. < { {   { { } }   { }  } } } }>
  191.  
  192.