Micheline: fix loop in resilient parser

This commit is contained in:
Benjamin Canou 2018-01-02 11:54:46 +01:00
parent a7617f527d
commit 0ce7c79367

View File

@ -466,7 +466,7 @@ let rec parse ?(check = true) errors tokens stack =
parse ~check errors rem (* skip *) stack parse ~check errors rem (* skip *) stack
| (Sequence _ | Toplevel _) :: _ , | (Sequence _ | Toplevel _) :: _ ,
({ token = Semi } as valid) :: ({ token = Semi } as token) :: rem -> ({ token = Semi } as valid) :: ({ token = Semi } as token) :: rem ->
let errors = Unexpected token :: errors in let errors = Extra token :: errors in
parse ~check errors (valid (* skip *) :: rem) stack parse ~check errors (valid (* skip *) :: rem) stack
| (Wrapped _ | Unwrapped _) :: _ , | (Wrapped _ | Unwrapped _) :: _ ,
{ token = Open_paren } { token = Open_paren }
@ -479,27 +479,29 @@ let rec parse ?(check = true) errors tokens stack =
({ token = Close_paren } as token) :: rem ({ token = Close_paren } as token) :: rem
| Toplevel _ :: [], | Toplevel _ :: [],
({ token = Close_paren } as token) :: rem ({ token = Close_paren } as token) :: rem
| Toplevel _ :: [],
({ token = Open_paren } as token) :: rem
| Toplevel _ :: [], | Toplevel _ :: [],
({ token = Close_brace } as token) :: rem ({ token = Close_brace } as token) :: rem
| Sequence _ :: _,
({ token = Open_paren } as token) :: rem
| Sequence _ :: _,
({ token = Close_paren } as token :: rem)
| (Wrapped _ | Unwrapped _) :: _,
({ token = Open_paren } as token) :: ({ token = Close_brace | Semi } :: _ | [] as rem)
| _, | _,
({ token = Annot _ } as token) :: rem -> ({ token = Annot _ } as token) :: rem ->
let errors = Unexpected token :: errors in let errors = Unexpected token :: errors in
parse ~check errors rem (* skip *) stack parse ~check errors rem (* skip *) stack
| Wrapped (token, _, _, _) :: _, | Wrapped (token, _, _, _) :: _, ([] | { token = Close_brace | Semi } :: _) ->
({ token = Close_brace | Semi } :: _ | [])
| (Sequence _ | Toplevel _) :: _,
({ token = Open_paren } as token) :: _
| (Wrapped _ | Unwrapped _) :: _,
({ token = Open_paren } as token) :: ({ token = Close_brace | Semi } :: _ | [])
| (Sequence (token, _, _) :: _ | Unwrapped _ :: Sequence (token, _, _) :: _),
({ token = Close_paren } :: _ | [])->
let errors = Unclosed token :: errors in let errors = Unclosed token :: errors in
let fake = let fake = { token with token = Close_paren } in
{ token with token = match token.token with let tokens = (* insert *) fake :: tokens in
| Open_paren -> Close_paren parse ~check errors tokens stack
| Open_brace -> Close_brace | (Sequence (token, _, _) :: _ | Unwrapped _ :: Sequence (token, _, _) :: _), [] ->
| _ -> assert false } in let errors = Unclosed token :: errors in
let tokens = token :: (* insert *) fake :: List.tl tokens in let fake = { token with token = Close_brace } in
let tokens = (* insert *) fake :: tokens in
parse ~check errors tokens stack parse ~check errors tokens stack
(* Valid states *) (* Valid states *)
| (Toplevel _ | Sequence (_, _, _)) :: _ , | (Toplevel _ | Sequence (_, _, _)) :: _ ,