summaryrefslogtreecommitdiff
path: root/Common/Parsing.lean
diff options
context:
space:
mode:
authorAndreas Grois <andi@grois.info>2024-09-18 19:42:46 +0200
committerAndreas Grois <andi@grois.info>2024-09-18 19:42:46 +0200
commit2c1f2362c69ab20b10b61bdbebf8e5121eca38b1 (patch)
treeb31f7e8fff82630d33319b231cf2868d5922cb30 /Common/Parsing.lean
parent6554499717d70d5656a3b38a20dc60850674a873 (diff)
Continue Day11/Parsing
Diffstat (limited to 'Common/Parsing.lean')
-rw-r--r--Common/Parsing.lean32
1 files changed, 32 insertions, 0 deletions
diff --git a/Common/Parsing.lean b/Common/Parsing.lean
index a89d785..4967385 100644
--- a/Common/Parsing.lean
+++ b/Common/Parsing.lean
@@ -1,5 +1,7 @@
/- This file contains various parsing helpers. Started _after_ Day10. -/
+import Common.List
+
namespace Parsing
structure RectangularGrid (Element : Type) where
@@ -44,3 +46,33 @@ def RectangularGrid.Get {grid : RectangularGrid Element} (coordinate : grid.Coor
instance : GetElem (RectangularGrid Element) (RectangularGrid.Coordinate grid) Element (λg _ ↦ g = grid) where
getElem := λ g c h ↦ g.Get (h▸c)
+
+inductive RectangularGrid.ParseError (ParseCharError : Type)
+| NoInput : RectangularGrid.ParseError ParseCharError
+| NotRectangular : RectangularGrid.ParseError ParseCharError
+| CharacterParsingError : ParseCharError → RectangularGrid.ParseError ParseCharError
+
+instance [ToString ParseCharError] : ToString (RectangularGrid.ParseError ParseCharError) where
+ toString := λ
+ | .NoInput => "No input provided."
+ | .NotRectangular => "Input is not rectangular."
+ | .CharacterParsingError e => ToString.toString e
+
+def RectangularGrid.parseSingleLine (parseCharacter : Char → Except ParseCharError Element) (alreadyParsed : Array Element) (remainingLine : Substring) : Except (RectangularGrid.ParseError ParseCharError) (Array Element × Nat) :=
+ sorry
+
+def RectangularGrid.parseLines (parseCharacter : Char → Except ParseCharError Element) (alreadyParsed : Array Element) (remainingLines : List Substring) : Except (RectangularGrid.ParseError ParseCharError) (Array Element × Nat × Nat) :=
+ sorry
+
+def RectangularGrid.ofSubstring (parseCharacter : Char → Except ParseCharError Element) (input : Substring) : Except (RectangularGrid.ParseError ParseCharError) (RectangularGrid Element) :=
+ let input := input.trim
+ let lines := input.splitOn "\n"
+ if h₁ : lines.isEmpty then
+ throw RectangularGrid.ParseError.NoInput
+ else
+ --let height := lines.length
+ --let width := lines[0]'(List.not_empty_iff_size_gt_zero.mp $ (Bool.not_eq_true _).mp h₁)
+ sorry
+
+def RectangularGrid.ofString (parseCharacter : Char → Except ParseCharError Element) (input : String) : Except (RectangularGrid.ParseError ParseCharError) (RectangularGrid Element) :=
+ ofSubstring parseCharacter input.toSubstring