On this page:
paren-tree%
new
add-token
match-forward
match-backward
split-tree
merge-tree
truncate
is-open-pos?
is-close-pos?
get-invisible-count
8.15.0.8

1 Parenthesis Matching🔗ℹ

 (require syntax-color/paren-tree)
  package: syntax-color-lib

Parenthesis matching code built on top of token-tree%.

class

paren-tree% : class?

  superclass: object%

constructor

(new paren-tree% 
    [matches matches] 
    ...superclass-args...) 
  (is-a?/c paren-tree%)
  matches : (listof (list/c symbol? symbol?))
Creates a paren-tree% object that treats (map car matches) as open parens and (map cadr matches) as close parens, where each element of matches is a matching pair of parens.

Each paren tree tracks a sequence of tokens (added with add-token) and can respond to queries about the location of a paren that matches a paren at some specific location via match-forward and match-backward. The paren-tree% also supports a notion of invisible parentheses that take up no space, where the opens exist only at the start of a token and the closes exist only at the end of a token.

Changed in version 1.6 of package syntax-color-lib: Added support for invisible parens.

method

(send a-paren-tree add-token 
  type 
  length 
  [#:invisible-opens invisible-opens 
  #:invisible-closes invisible-closes]) 
  void?
  type : (or/c #f symbol?)
  length : natural?
  invisible-opens : natural? = 0
  invisible-closes : natural? = 0
Adds one token to the end of the current tree. If type is a symbol, it is expected to be one of the symbols in matches. If it is #f, then there are no visible parenthese in this token. The invisible-opens and invsible-closes indicate how many of each there are on this token (note that the invisible opens all exist at the start of the token and the invisible closes all exist at the end of the token).

Changed in version 1.6 of package syntax-color-lib: Added #:invisible-opens and #:invisible-closes arguments.

method

(send a-paren-tree match-forward pos 
  [#:invisible invisible]) 
  
(or/c natural? #f)
(or/c natural? #f)
(or/c natural? #f)
  pos : natural?
  invisible : (or/c #f natural? 'all) = #f
Determines if there is a match for the paren at pos.

If invisible is #f, then the invisible parens are ignored and the match considers only the parentheses that were explicit in the token argument to add-token.

If invisible is a natural number, then the matching starts outside of that many invisible parens. For example, if there are two invisible open parenthes on the token at pos, then passing 1 as invisible will find the match to only the inner invisible paren. If it is 2, it will find the match to the outer invisible paren. If invisible is 'all, then it is the same as passing the total number of invisible parens that are on the token at pos.

The first return is the starting position of the open paren The second return is the position of the matching close paren. If the third return is #f, then the first two returns represent a real match. If the third return is a number, it is the maximum position in the tree that was searched. If the third result indicates an error, the first two results give the starting and stopping positions for error highlighting. If all three are #f, then there was no tree to search, or the position did not immediately precede an open.

Changed in version 1.6 of package syntax-color-lib: Added #:invisible argument.

method

(send a-paren-tree match-backward pos 
  [#:invisible invisible]) 
  
(or/c natural? #f)
(or/c natural? #f)
(or/c natural? #f)
  pos : natural?
  invisible : (or/c #f natural? 'all) = #f
Like paren-tree% match-forward, except matches backwards from the given paren.

The matching goes backwards from pos to the matching paren; accordingly, the count of invisibles starts from the close parens and goes through the opens (unlike paren-tree% match-forward which starts with the opens and goes through the closes).

The results are, however, identical to paren-tree% match-forward. So, if the match is successful, the first result is the location of the open paren and the second is the close.

Changed in version 1.6 of package syntax-color-lib: Added #:invisible argument.

method

(send a-paren-tree split-tree pos)  void?

  pos : natural?
Splits the tree at pos, which must not be in the middle of a token. Everything following pos is marked as invalid.

method

(send a-paren-tree merge-tree num-to-keep)  void?

  num-to-keep : natural?
Makes the num-to-keep last positions that have been marked invalid valid again.

method

(send a-paren-tree truncate pos)  void?

  pos : natural?
Removes the tokens after pos.

method

(send a-paren-tree is-open-pos? pos)  (or/c #f symbol?)

  pos : natural?
Returns #f if the position does not have a visible paren. Returns the corresponding close if it does have an open.

method

(send a-paren-tree is-close-pos? pos)  (or/c #f symbol?)

  pos : natural?
Returns #f if the position does not have a visible paren. Returns the corresponding open if it does have a close.

method

(send a-paren-tree get-invisible-count pos)  
natural? natural?
  pos : natural?
Returns the number of invisible opens and invisible closes at pos.

Added in version 1.6 of package syntax-color-lib.