-
Notifications
You must be signed in to change notification settings - Fork 345
/
ParseImportsFast.lean
225 lines (193 loc) · 7.39 KB
/
ParseImportsFast.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Lean.Parser.Module
namespace Lean
namespace ParseImports
structure State where
imports : Array Import := #[]
pos : String.Pos := 0
error? : Option String := none
deriving Inhabited
def Parser := String → State → State
instance : Inhabited Parser where
default := fun _ s => s
@[inline] def State.setPos (s : State) (pos : String.Pos) : State :=
{ s with pos := pos }
@[inline] def State.mkError (s : State) (msg : String) : State :=
{ s with error? := some msg }
def State.mkEOIError (s : State) : State :=
s.mkError "unexpected end of input"
@[inline] def State.next (s : State) (input : String) (pos : String.Pos) : State :=
{ s with pos := input.next pos }
@[inline] def State.next' (s : State) (input : String) (pos : String.Pos) (h : ¬ input.atEnd pos): State :=
{ s with pos := input.next' pos h }
partial def finishCommentBlock (nesting : Nat) : Parser := fun input s =>
let input := input
let i := s.pos
if h : input.atEnd i then eoi s
else
let curr := input.get' i h
let i := input.next' i h
if curr == '-' then
if h : input.atEnd i then eoi s
else
let curr := input.get' i h
if curr == '/' then -- "-/" end of comment
if nesting == 1 then s.next input i
else finishCommentBlock (nesting-1) input (s.next' input i h)
else
finishCommentBlock nesting input (s.next' input i h)
else if curr == '/' then
if h : input.atEnd i then eoi s
else
let curr := input.get' i h
if curr == '-' then finishCommentBlock (nesting+1) input (s.next' input i h)
else finishCommentBlock nesting input (s.setPos i)
else finishCommentBlock nesting input (s.setPos i)
where
eoi s := s.mkError "unterminated comment"
@[specialize] partial def takeUntil (p : Char → Bool) : Parser := fun input s =>
let i := s.pos
if h : input.atEnd i then s
else if p (input.get' i h) then s
else takeUntil p input (s.next' input i h)
@[inline] def takeWhile (p : Char → Bool) : Parser :=
takeUntil (fun c => !p c)
@[inline] def andthen (p q : Parser) : Parser := fun input s =>
let s := p input s
if s.error? matches some .. then s else q input s
instance : AndThen Parser where
andThen p q := andthen p (q ())
partial def whitespace : Parser := fun input s =>
let i := s.pos
if h : input.atEnd i then s
else
let curr := input.get' i h
if curr == '\t' then
s.mkError "tabs are not allowed; please configure your editor to expand them"
else if curr.isWhitespace then whitespace input (s.next input i)
else if curr == '-' then
let i := input.next' i h
let curr := input.get i
if curr == '-' then andthen (takeUntil (fun c => c = '\n')) whitespace input (s.next input i)
else s
else if curr == '/' then
let i := input.next' i h
let curr := input.get i
if curr == '-' then
let i := input.next i
let curr := input.get i
if curr == '-' || curr == '!' then s -- "/--" and "/-!" doc comment are actual tokens
else andthen (finishCommentBlock 1) whitespace input (s.next input i)
else s
else s
@[inline] partial def keywordCore (k : String) (failure : Parser) (success : Parser) : Parser := fun input s =>
let rec @[specialize] go (i j : String.Pos) : State :=
if h₁ : k.atEnd i then
success input <| whitespace input (s.setPos j)
else if h₂ : input.atEnd j then
failure input s
else
let curr₁ := k.get' i h₁
let curr₂ := input.get' j h₂
if curr₁ != curr₂ then
failure input s
else
go (k.next' i h₁) (input.next' j h₂)
go 0 s.pos
@[inline] partial def keyword (k : String) : Parser :=
keywordCore k (fun _ s => s.mkError s!"`{k}` expected") (fun _ s => s)
@[inline] def isIdCont : String → State → Bool := fun input s =>
let i := s.pos
let curr := input.get i
if curr == '.' then
let i := input.next i
if h : input.atEnd i then
false
else
let curr := input.get' i h
isIdFirst curr || isIdBeginEscape curr
else
false
def State.pushModule (module : Name) (runtimeOnly : Bool) (s : State) : State :=
{ s with imports := s.imports.push { module, runtimeOnly } }
@[inline] def isIdRestCold (c : Char) : Bool :=
c = '_' || c = '\'' || c == '!' || c == '?' || isLetterLike c || isSubScriptAlnum c
@[inline] def isIdRestFast (c : Char) : Bool :=
c.isAlphanum || (c != '.' && c != '\n' && c != ' ' && isIdRestCold c)
partial def moduleIdent (runtimeOnly : Bool) : Parser := fun input s =>
let rec parse (module : Name) (s : State) :=
let i := s.pos
if h : input.atEnd i then
s.mkEOIError
else
let curr := input.get' i h
if isIdBeginEscape curr then
let startPart := input.next' i h
let s := takeUntil isIdEndEscape input (s.setPos startPart)
if h : input.atEnd s.pos then
s.mkError "unterminated identifier escape"
else
let stopPart := s.pos
let s := s.next' input s.pos h
let module := .str module (input.extract startPart stopPart)
if isIdCont input s then
let s := s.next input s.pos
parse module s
else
whitespace input (s.pushModule module runtimeOnly)
else if isIdFirst curr then
let startPart := i
let s := takeWhile isIdRestFast input (s.next' input i h)
let stopPart := s.pos
let module := .str module (input.extract startPart stopPart)
if isIdCont input s then
let s := s.next input s.pos
parse module s
else
whitespace input (s.pushModule module runtimeOnly)
else
s.mkError "expected identifier"
parse .anonymous s
@[specialize] partial def many (p : Parser) : Parser := fun input s =>
let pos := s.pos
let size := s.imports.size
let s := p input s
match s.error? with
| none => many p input s
| some _ => { pos, error? := none, imports := s.imports.shrink size }
@[inline] partial def preludeOpt (k : String) : Parser :=
keywordCore k (fun _ s => s.pushModule `Init false) (fun _ s => s)
def main : Parser :=
preludeOpt "prelude" >>
many (keyword "import" >> keywordCore "runtime" (moduleIdent false) (moduleIdent true))
end ParseImports
/--
Simpler and faster version of `parseImports`. We use it to implement Lake.
-/
def parseImports' (input : String) (fileName : String) : IO (Array Lean.Import) := do
let s := ParseImports.main input (ParseImports.whitespace input {})
match s.error? with
| none => return s.imports
| some err => throw <| IO.userError s!"{fileName}: {err}"
deriving instance ToJson for Import
structure PrintImportResult where
imports? : Option (Array Import) := none
errors : Array String := #[]
deriving ToJson
structure PrintImportsResult where
imports : Array PrintImportResult
deriving ToJson
@[export lean_print_imports_json]
def printImportsJson (fileNames : Array String) : IO Unit := do
let rs ← fileNames.mapM fun fn => do
try
let deps ← parseImports' (← IO.FS.readFile ⟨fn⟩) fn
return { imports? := some deps }
catch e => return { errors := #[e.toString] }
IO.println (toJson { imports := rs : PrintImportsResult } |>.compress)
end Lean