From 2c1f2362c69ab20b10b61bdbebf8e5121eca38b1 Mon Sep 17 00:00:00 2001
From: Andreas Grois <andi@grois.info>
Date: Wed, 18 Sep 2024 19:42:46 +0200
Subject: Continue Day11/Parsing

---
 Common/Parsing.lean | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

(limited to 'Common/Parsing.lean')

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
-- 
cgit v1.2.3

