6
$\begingroup$

I’ve recently written a compiler and I wouldn’t mind turning it into a language server. I’m also sitting on a couple of interpreters that I would like to turn into language servers. And I’m planning to start a new compiler that could potentially be designed as a language server from the get-go.

But there’s a problem: I can’t find guidance. All I’ve found is, “Study the Language Server Protocol.” But that’s not enough. Here are some unanswered questions:

  • What LSP features should I implement first? Which should I delay until later or not at all?
  • What parts of my compiler or interpreter should I expect to reuse as is? What parts should I plan on reusing with minor changes? What parts (besides code generation) should I plan not to reuse?
$\endgroup$

1 Answer 1

4
$\begingroup$

Let me assume your compiler has a type checking process which outputs compilable ASTs. Then, I expect you to reuse everything in and before this phase. A type checking process will require names to be resolved, and this information will be useful.

What to implement first?

  1. Report error messages in the source code. This should be the easiest.
  2. Go to definition. The easiest yet important feature to implement, which also builds the foundation for code completion. This will make use of the name resolving information.
  3. Find usages. This is a simple extension to 'go to definition'.
  4. Rename. Also a simple extension.
  5. Various 'source code queries' such as lookup function signature, show documentation of a function, etc.

In order to implement some useful features, you may need the following:

  1. Parse incomplete code. Also prepares for code completion.
  2. Syntax tree needs to contain source information and should be carried through in the type checking process.
  3. Build string from a syntax tree. Suppose you want to implement refactoring, then you'll need to generate code from transformed ASTs. It would be better if you can build a patch of code from a diff of two syntax trees.

After these preparations, you may work on the following:

  1. Code completion. Hard, but most useful.
  2. Refactoring. Use your imagination to do as much as you can!
  3. Debugging. This is a separate topic.
  4. Formatting. This is another separate topic.
  5. And much more!
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .