Skip to content

Instantly share code, notes, and snippets.

@bryanl
Last active August 21, 2018 13:07
Show Gist options
  • Save bryanl/b8230634d452a9655137840f338dc9c6 to your computer and use it in GitHub Desktop.
Save bryanl/b8230634d452a9655137840f338dc9c6 to your computer and use it in GitHub Desktop.

Parsing incomplete jsonnet

#Heptio/ksonnet/vscode

To create completion items for a jsonnet source file, you might have to work with an incomplete or malformed source file. JSL should be able to handle this and still offer completion items.

Use Cases

First use case

local a = "a";

If the cursor to is at (2,1), what can be shown?

  • Standard library (std)
  • a
  • expression items...

How do we know this?

The scope for this location would include {std, a}. Because we know we are expecting an expression, we could generate a list of expression keywords.

How is the scope determined in this case?

  • std is always in scope because the standard library is automatically imported
  • a comes from the bind defined in the local on line 1.

To make this work, the completer will need to know where the cursor is. In this case, the cursor is in the body of a local. due to local bind; expr.

The document produces the following tokens:

  1. tmp/file11.jsonnet:1:1-6: local = local
  2. tmp/file11.jsonnet:1:7-8: IDENTIFIER = a
  3. tmp/file11.jsonnet:1:9-10: OPERATOR = =
  4. tmp/file11.jsonnet:1:11-14: STRING_DOUBLE = a
  5. tmp/file11.jsonnet:1:14-15: ";" = ;
  6. tmp/file11.jsonnet:2:1: end of file =

To parse this,

  1. Parse token 0. It is a local
  2. Using the rules for local, the next token is a bind. A bind is a id = expr or id([params]) = expr. In this case tokens 1-3 identify the bind.
  3. Token 4 is a ;, so the following item should be an expression. Since it is blank, we should mark a syntax error at token 4.

The cursor is on line 2, column 1. We'll need to know what the parser expects at this point. Given the rules for a local, the parser is expecting an expression. Given our current scope, what expressions could we generate?

  • a : from scope
  • std: from scope
  • assert: expression keyword
  • error: expression keyword
  • if: expression keyword
  • function: expression keyword
  • local: expression keyword
  • import: expression keyword
  • importstr: expression keyword
  • super: expression keyword

These are organized in the following order:

  1. scope
  2. other

Completion research

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment