fix: XML parsing bugs (#2601)

* fix: make XML parser handle trailing whitespace in opening tags

* fix: make XML parser handle comments correctly

---------

Co-authored-by: György Kurucz <me@kuruczgy.com>
This commit is contained in:
kuruczgy 2023-10-04 02:51:22 +02:00 committed by GitHub
parent 44bc68bdc6
commit 83c7c29075
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View file

@ -119,7 +119,7 @@ def XMLdecl : Parsec Unit := do
def Comment : Parsec String :=
let notDash := Char.toString <$> satisfy (λ c => c ≠ '-')
skipString "<!--" *>
Array.foldl String.append "" <$> many (notDash <|> (do
Array.foldl String.append "" <$> many (attempt <| notDash <|> (do
let d ← pchar '-'
let c ← notDash
pure $ d.toString ++ c))
@ -408,7 +408,7 @@ def Attribute : Parsec (String × String) := do
protected def elementPrefix : Parsec (Array Content → Element) := do
skipChar '<'
let name ← Name
let attributes ← many (S *> Attribute)
let attributes ← many (attempt <| S *> Attribute)
optional S *> pure ()
return Element.Element name (RBMap.fromList attributes.toList compare)

View file

@ -0,0 +1,10 @@
import Lean.Data.Xml
open Lean.Xml
/-! Test XML parsing. -/
/-! Test whether trailing whitespace in opening tags is handled correctly. -/
#eval parse "<a ><b a=\"v\" ></b></a>"
/-! Test whether comments are parsed correctly. -/
#eval parse "<a><!-- comment --></a>"

View file

@ -0,0 +1,2 @@
ok: <a><b a="v"></b></a>
ok: <a><!-- comment --></a>