Commit graph

49 commits

Author SHA1 Message Date
David Thrane Christiansen
74e7886ce7
feat: custom error recovery in parser (#3413)
Adds a simple error-recovery mechanism to Lean's parser, similar to
those used in other combinator parsing libraries.

Lean itself isn't very amenable to error recovery with this mechanism,
as it requires global knowledge of the grammar in question to write
recovery rules that don't break backtracking or `<|>`. I only found a
few opportunities.

But for DSLs, this is really important. In particular, Verso parse
errors interacted very badly with Lean parse errors in a way that
required frequent "restart file" commands, but this mechanism allows me
to both recover from Verso parse errors and to have Lean skip the rest
of the file rather than repeatedly trying to parse it as Lean commands.
2024-02-21 14:29:54 +00:00
Henrik Böving
23e49eb519 perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Marc Huisinga
681fca1f8f
feat: import auto-completion (#2904)
This PR adds basic auto-completion support for imports. Since it still
lacks Lake support for accurate completion suggestions (cc @tydeu - we
already know what needs to be done), it falls back to traversing the
`LEAN_SRC_PATH` for available imports.

Three kinds of import completion requests are supported:

- Completion of the full `import` command. Triggered when requesting
completions in an empty space within the header.
- Known issue: It is possible to trigger this completion within a
comment in the header. Fixing this would require architecture for
parsing some kind of sub-syntax between individual commands.
- Completion of the full module name after an incomplete `import`
command.
- Completion of a partial module name with a trailing dot.

Since the set of imports is potentially expensive to compute, they are
cached for 10 seconds after the last import auto-completion request.

Closes #2655.

### Changes

This PR also makes the following changes:
- To support completions on the trailing dot, the `import` syntax was
adjusted to provide partial syntax when a trailing dot is used.
- `FileWorker.lean` was refactored lightly with some larger definitions
being broken apart.
- The `WorkerState` gained two new fields:
- `currHeaderStx` tracks the current header syntax, as opposed to
tracking only the initial header syntax in `initHeaderStx`. When the
header syntax changes, a task is launched that restarts the file worker
after a certain delay to avoid constant restarts while editing the
header. During this time period, we may still want to serve import
auto-completion requests, so we need to know the up-to-date header
syntax.
- `importCachingTask?` contains a task that computes the set of
available imports.
- `determineLakePath` has moved to a new file `Lean/Util/LakePath.lean`
as it is now needed both in `ImportCompletion.lean` and
`FileWorker.lean`.
- `forEachModuleIn` from `Lake/Config/Blob.lean` has moved to
`Lean/Util/Path.lean` as it is a generally useful utility function that
was useful for traversing the `LEAN_SRC_PATH` as well.

### Tests

Unfortunately, this PR lacks tests since the set of imports available in
`tests/lean/interactive` will not be stable. In the future, I will add
support for testing LSP requests in full project setups, which is when
tests for import auto-completion will be added as well.
2023-11-24 07:46:19 +00:00
Sebastian Ullrich
e9d60e143a perf: avoid allocation in mkUnexpectedTokenErrors 2023-09-12 11:42:24 +02:00
Sebastian Ullrich
241430aa03 perf: avoid calculating position, revert building unexpected message in mkUnexpectedTokenErrors 2023-09-12 11:42:24 +02:00
Sebastian Ullrich
c67686132a feat: include unexpected token in error message 2023-09-12 11:42:24 +02:00
Sebastian Ullrich
e580c903e6 feat: adjust message range on unexpected token error 2023-09-12 11:42:24 +02:00
Sebastian Ullrich
f24608c4d1 fix: make eoi an actual command with info tree 2023-01-26 13:05:57 +01:00
Leonardo de Moura
6fea2946c2 fix: fixes #2006 2023-01-04 08:19:22 -08:00
Sebastian Ullrich
17782fba1a fix: replace broken ptrEq cache sanity checks with private ParserContext constructor
The context is now manipulated using `adaptCacheableContext` and `adaptUncacheableContext`
and created using `ParserFn.run`.
2022-11-11 09:13:02 +01:00
Mario Carneiro
583e023314 chore: snake-case attributes (part 2) 2022-10-19 09:28:08 -07:00
Mario Carneiro
c06cffa54f refactor: rename isExitCommand -> isTerminalCommand 2022-10-12 11:11:31 -07:00
Mario Carneiro
8dfae9eb38 feat: import command stub 2022-10-12 11:11:31 -07:00
Sebastian Ullrich
2f67295c7d feat: strengthen pp* signatures 2022-07-03 19:14:49 +02:00
Sebastian Ullrich
86cd656fc6 refactor: adapt raw syntax manipulations to TSyntax
Sometimes there's just no structure to work on
2022-06-27 22:37:02 +02:00
Sebastian Ullrich
829c81d677 fix: skip antiquotations during parser recovery 2022-04-27 10:41:27 +02:00
Sebastian Ullrich
83f331b5e2 parseCommand: use do 2022-04-27 10:41:27 +02:00
Leonardo de Moura
3862e7867b refactor: make String.Pos opaque
TODO: this refactoring exposed bugs in `FuzzyMatching` and `Lake`

closes #410
2022-03-20 10:47:13 -07:00
Gabriel Ebner
bfc74decde feat: add info field to Syntax.node 2021-10-26 20:19:27 +02:00
Daniel Selsam
6940166db4 chore: rebase and rm rawPos 2021-08-03 09:13:18 +02:00
Daniel Selsam
89364b802b feat: top-down heuristic delaboration 2021-08-03 09:13:18 +02:00
Sebastian Ullrich
619873c842 feat: make System.FilePath opaque 2021-05-28 14:19:59 +02:00
Leonardo de Moura
3b7bcdc449 feat: add endPos field to SourceInfo.original
We need an update stage0 before we use it.
2021-05-17 14:32:58 -07:00
Sebastian Ullrich
fd42b111fb chore: always push missing on a parser error 2021-04-17 23:44:43 +02:00
Leonardo de Moura
6c0f3c277f fix: ignore syntactically incorrect commands that do not contain any symbol
This kind of broken command does not have a position information, and
was producing a panic message in the server because it makes the
reasonable assumption that every command returned by `parserCommand`
has a position.
2021-04-07 14:46:13 -07:00
Leonardo de Moura
fb38955be2 feat: improve command parser error recovery
After a parser error in a command, we are "swallowing" all command
parsing errors until a command was succesfully parsed.
This was producing counterintuitive behavior in the IDEs.
2021-04-05 12:31:33 -07:00
Sebastian Ullrich
d0996fb945 chore: improve EOI error message 2021-04-03 11:56:26 +02:00
Leonardo de Moura
de024274fe feat: elaborate syntactically incorrect commands 2021-03-31 17:05:34 -07:00
Leonardo de Moura
03e3a1cc6b chore: remove hack
It produces weird error messages in some examples, and it will be
obsolete after the new precedence feature.
2021-03-19 11:09:18 -07:00
Leonardo de Moura
164577d94e chore: remove parser! and tparser!
The new macros are called "leading_parser` and `trailing_parser`.

cc @Kha
2021-03-11 09:36:58 -08:00
Sebastian Ullrich
79107a2316 feat: copy & store whole ref range in SourceInfo 2021-01-20 16:48:50 +01:00
Sebastian Ullrich
4380d4a9da feat: parser: store options & pass to evalConst 2020-12-16 23:15:58 +01:00
Leonardo de Moura
3b6d65c3c3 chore: use deriving Inhabited 2020-12-13 10:09:20 -08:00
Sebastian Ullrich
21f4257da5 feat: name resolution during parsing 2020-12-03 17:46:13 +01:00
Leonardo de Moura
0869f38de4 chore: update structure, class, inductive 2020-11-27 15:09:30 -08:00
Leonardo de Moura
13c2a8ff51 chore: remove #lang lean4 header 2020-10-25 09:54:07 -07:00
Leonardo de Moura
7966856b32 chore: move to new frontend 2020-10-21 09:13:55 -07:00
Sebastian Ullrich
e8cf086641 fix: synthesize pretty printers early 2020-10-20 09:50:54 +02:00
Leonardo de Moura
ef01053d58 fix: set mainModuleName in the new frontend 2020-10-15 15:30:03 -07:00
Leonardo de Moura
5d3e08d43a chore: documented modification needed to enable the elaboration of commands containing syntax errors
cc @Kha
2020-10-14 13:20:01 -07:00
Sebastian Ullrich
bdff53fdf5 feat: use ppLine 2020-10-07 09:43:05 +02:00
Leonardo de Moura
dc9626ceab feat: try to improve weird error message
@Kha Before this commit, we were producing the error "expected command" at the `let` token
```lean
check id let x := 1; x
```
The new error is "expected command, but found term; this error may be
due to parsing precedence levels, consider parenthesizing the term".

The example above looks artificial, but it will happen all the time as
users start to define their own notation.
2020-09-21 18:29:01 -07:00
Sebastian Ullrich
f693e910b1 fix: skip minimum amount of tokens during parser recovery
Fixes #186
2020-09-21 11:37:50 +02:00
Leonardo de Moura
0abca5475f refactor: move ppExpr to IO
@Kha I am also tracking `currNamespace` and `openDecls`.

BTW, I also tried an experiment where I added `currNamespace` and
`openDecls` to `Meta.Context`, but it looked weird. This information
is only needed in the elaborator and pretty printer.
The `PPContext` object should contain everything you need. You
can put `currNamespace` and `openDecls` in the `Delaborator.Context`.
2020-09-15 18:48:21 -07:00
Sebastian Ullrich
75b2dc1baf refactor: simplify ppModule using new module parser 2020-08-31 15:44:58 +02:00
Sebastian Ullrich
2418d851fc feat: declarative module parser
/cc @leodemoura
2020-08-31 14:45:21 +02:00
Sebastian Ullrich
e5de32c2dd feat: auto-generate header formatter 2020-08-21 16:43:55 +02:00
Leonardo de Moura
249bda16c0 chore: remove prelude commands from Lean package 2020-06-25 11:21:17 -07:00
Leonardo de Moura
4ccc3fef52 chore: move Init.Lean files to Lean package 2020-05-26 15:04:35 -07:00
Renamed from src/Init/Lean/Parser/Module.lean (Browse further)