فهرست منبع

Better errors for unmatching parentheses

Taddeus Kroes 11 سال پیش
والد
کامیت
3e754a515d
1فایلهای تغییر یافته به همراه10 افزوده شده و 3 حذف شده
  1. 10 3
      parse.ml

+ 10 - 3
parse.ml

@@ -19,9 +19,9 @@ let tokenize next_char emit =
   in
 
   let emit_buf () =
-    match Buffer.length buf with
-    | 0 -> ()
-    | _ ->
+    if Buffer.length buf = 0 then
+      ()
+    else
       emit (ID (Buffer.contents buf));
       Buffer.clear buf
   in
@@ -74,6 +74,9 @@ let parse next_char =
     | MINUS  -> expect := E_ntest
     | LPAREN -> stack := ref [] :: !stack
     | RPAREN ->
+      if List.length !stack < 2 then begin
+        raise (ParseError "too many closing parentheses")
+      end;
       let body = List.rev !(List.hd !stack) in
       stack := List.tl !stack;
       append (Repeat (program_of_list body))
@@ -89,6 +92,10 @@ let parse next_char =
       expect := E_basic
   in
   tokenize next_char handler;
+
+  if List.length !stack > 1 then
+    raise (ParseError "missing closing parenthesis");
+
   Concat (List.rev !(List.hd !stack))
 
 let parse_string s =