(in-package :cl-ppcre)
(defvar *open-parens* 0)
(defvar *close-parens* 0)
(defparameter *tree*
`(:SEQUENCE "function" (:GREEDY-REPETITION 0 NIL :WHITESPACE-CHAR-CLASS) #\(
(:GREEDY-REPETITION
0 NIL
(:filter ,#'(lambda (pos)
(let ((char (char *string* pos)))
(cond
((char= char #\( ) (incf *open-parens*) (+ 1 pos))
((char= char #\) )
(incf *close-parens*)
(if (<= *close-parens* *open-parens*)
(+ 1 pos)
(progn
(setf *open-parens* 0 *close-parens* 0)
nil)))
(T (+ 1 pos))))))
) #\)))
(cl-ppcre:scan-to-strings *tree* "some times I like to \"function (calling all coppers (), another param (), test)\" just to see what happens")
> "function (calling all coppers (), another param (), test)"