Skip to content

Commit 4a59ea4

Browse files
authored
feat: ParseableType ModuleName supporting paths or module identifiers (leanprover#15)
1 parent 5a858c3 commit 4a59ea4

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Cli/Basic.lean

+31
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,37 @@ section Configuration
232232
| "" => none
233233
| s => s.toInt?
234234

235+
open Lean
236+
237+
/-- We provide a type synonym here for `Name`, which will carry a `ParseableType ModuleName`
238+
instance, supporting parsing either a module name (e.g. `Mathlib.Topology.Basic`)
239+
or a relative path to a Lean file (e.g. `Mathlib/Topology/Basic.lean`). -/
240+
def ModuleName := Name
241+
242+
/-- Check if a `ModuleName` has no `.num` components, and is not `.anonymous`. -/
243+
def ModuleName.isValid : ModuleName → Bool
244+
| .anonymous => false
245+
| m => loop m
246+
where
247+
loop : ModuleName → Bool
248+
| .anonymous => true
249+
| .str pre _ => loop pre
250+
| .num .. => false
251+
252+
open System in
253+
/-- A custom command-line argument parser that allows either relative paths to Lean files,
254+
(e.g. `Mathlib/Topology/Basic.lean`) or the module name (e.g. `Mathlib.Topology.Basic`). -/
255+
instance : ParseableType ModuleName where
256+
name := "ModuleName"
257+
parse? s := do
258+
if s.endsWith ".lean" then
259+
let pathComponents := (s : FilePath).withExtension "" |>.components
260+
return pathComponents.foldl Name.mkStr Name.anonymous
261+
else
262+
let name := String.toName s
263+
guard <| ModuleName.isValid name
264+
return name
265+
235266
instance [inst : ParseableType α] : ParseableType (Array α) where
236267
name :=
237268
if inst.name.contains ' ' then

lean-toolchain

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
leanprover/lean4:nightly-2022-11-20
1+
leanprover/lean4:nightly-2023-08-15

0 commit comments

Comments
 (0)