Skip to content

Commit

Permalink
feat: add include_str
Browse files Browse the repository at this point in the history
  • Loading branch information
Vtec234 committed Aug 3, 2022
1 parent 4bdab83 commit 98513fa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Lean/Elab.lean
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ import Lean.Elab.BuiltinCommand
import Lean.Elab.RecAppSyntax
import Lean.Elab.Eval
import Lean.Elab.Calc
import Lean.Elab.IncludeStr
30 changes: 30 additions & 0 deletions src/Lean/Elab/IncludeStr.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/-
Copyright (c) 2021 Henrik Böving. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Henrik Böving, Xubai Wang, Wojciech Nawrocki
-/

import Lean.Elab.Term
import Lean.Elab.Eval

namespace Lean.Elab.Term

private unsafe def evalFilePathUnsafe (stx : Syntax) : TermElabM System.FilePath :=
evalTerm System.FilePath (Lean.mkConst ``System.FilePath) stx

@[implementedBy evalFilePathUnsafe]
private opaque evalFilePath (stx : Syntax) : TermElabM System.FilePath

/-- When `parent_dir` contains the current Lean file, `include_str "path" / "to" / "file"` becomes
a string literal with the contents of the file at `"parent_dir" / "path" / "to" / "file"`. If this
file cannot be read, elaboration fails. -/
elab (name := includeStr) "include_str " path:term : term => do
let path ← evalFilePath path
let ctx ← readThe Lean.Core.Context
let srcPath := System.FilePath.mk ctx.fileName
let some srcDir := srcPath.parent
| throwError "cannot compute parent directory of '{srcPath}'"
let path := srcDir / path
Lean.mkStrLit <$> IO.FS.readFile path

end Lean.Elab.Term

0 comments on commit 98513fa

Please sign in to comment.