Checking the Balance of Parentheses
 
 
 

AutoLISP uses parentheses more frequently than most other computer languages. One of the most frequent syntax errors in AutoLISP is an unequal number of open and close parentheses. VLISP includes a number of tools to help you detect unbalanced or unmatched parentheses.

As noted in To format text in an active editor window, the VLISP code formatter searches for unbalanced parentheses when it formats your code. If you allow it to, the formatter will add parentheses where it thinks they are missing. Typically, though, the VLISP formatter adds parentheses at the end of a program, not to where you really need them. If you let VLISP add the parentheses, you will probably have to remove them later.

NoteIf you do not allow the formatter to add the balancing parentheses, it won't format your code either.

In any event, you must check the structure of your program to determine where the parentheses are really missing. You can use these parentheses matching items from the Edit menu to help you find unbalanced parentheses:

Match Forward (CTRL +])

Moves the insertion point (marked by the cursor) just past the close parenthesis that matches an open parenthesis.

If the current cursor position is just before an open parenthesis, VLISP matches that parenthesis with its closing parenthesis. If the cursor position is in the middle of an expression, VLISP matches the current expression's open parenthesis with its closing parenthesis.

Match Backward (CTRL +[)

Moves the insertion point to just before the open parenthesis that matches a close parenthesis.

If the current cursor position is just after a close parenthesis, VLISP matches that parenthesis with its opening parenthesis. If the cursor position is in the middle of an expression, VLISP matches the current expression's close parenthesis with its open parenthesis.

Select Forward (CTRL + SHIFT +]

Moves the insertion point as the Match Forward command does, but also selects all text between the start and end positions.

With the cursor positioned right before an open parenthesis, double-clicking also selects all text up to the matching close parenthesis, but does not move the insertion point.

Select Backward (CTRL + SHIFT +[)

Moves the insertion point as the Match Backward command does, but also selects all text between the start and end positions.

With the cursor positioned right after a close parenthesis, double-clicking also selects all text up to the matching open parenthesis, but does not move the insertion point.

For example, look at the following code:

1 (defun yinyang (/ origin radius i-radius half-r origin-x origin-y)
2 (setq half-r (/ radius 2))
3 (setq origin-x (car origin))
4 (setq origin-y (cadr origin))
5 (command "_.CIRCLE" 
6		origin 
7		radius
8		(command "_.ARC"
9				"_C"
10				(list origin-x (+ origin-y half-r)) 
11				(list origin-x (+ origin-y radius)) 
12  			 origin							 
13		 )
14		 (command "_.ARC"
15				"_C"
16			 (list origin-x (- origin-y half-r)) 
17				(list origin-x (- origin-y radius)) 
18				origin							 
19		 )
20 )

(The line numbers are not part of the text; they are used to help explain the example.)

Here is what happens if you load this code in VLISP and continually issue the Match Forward command, starting with the insertion point at the beginning of line 1.

In other words, the close parenthesis that matches the open parenthesis on line 5 is the last parenthesis in the program. You know this is an error because the last close parenthesis in an AutoLISP program should match the open parenthesis of the program's defun. Notice also that all the statements after line 5 are indented in a manner unlike in the preceding program code. These two clues indicate something is amiss at this point in the program. In fact, the close parenthesis to the command that begins on line 5 is missing.