Skip to content

Commit

Permalink
Add support for "import ... as"
Browse files Browse the repository at this point in the history
  • Loading branch information
irh committed Jan 18, 2024
1 parent bf64ac5 commit 12733e7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
13 changes: 7 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,12 @@ module.exports = grammar({

ellipsis: _ => '...',

import: $ => seq(
import: $ => prec.right(PREC.import, seq(
optional(seq('from', $.import_module)),
'import',
$.import_items,
),
$.import_item,
repeat(seq(',', $.import_item)),
)),

import_module: $ => choice(
seq(
Expand All @@ -607,10 +608,10 @@ module.exports = grammar({
$.string,
),

import_items: $ => prec.right(PREC.import, seq(
import_item: $ => seq(
choice($.string, $.identifier),
repeat(seq(",", choice($.string, $.identifier))),
)),
optional(seq("as", $.identifier)),
),

try: $ => prec.right(PREC.try, seq(
'try',
Expand Down
3 changes: 2 additions & 1 deletion queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@
"export"
"from"
"import"
"as"
] @include

(identifier) @variable

(import_module (identifier) @namespace)
(import_items (identifier) @namespace)
(import_item (identifier) @namespace)
(export (identifier) @namespace)

(chain lookup: (identifier) @field)
Expand Down
4 changes: 2 additions & 2 deletions src/grammar.json
Git LFS file not shown
4 changes: 2 additions & 2 deletions src/node-types.json
Git LFS file not shown
4 changes: 2 additions & 2 deletions src/parser.c
Git LFS file not shown
19 changes: 15 additions & 4 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,27 +247,38 @@ import
===========================================================================================

import foo
import foo as bar
from foo import 'bar'
from foo.bar import x, 'y'
from 'hello' import foo
from foo import bar as x, baz as y

---

(module
(import
(import_items (identifier))
(import_item (identifier))
)
(import
(import_item (identifier) (identifier))
)
(import
(import_module (identifier))
(import_items (string))
(import_item (string))
)
(import
(import_module (identifier) (identifier))
(import_items (identifier) (string))
(import_item (identifier))
(import_item (string))
)
(import
(import_module (string))
(import_items (identifier))
(import_item (identifier))
)
(import
(import_module (identifier))
(import_item (identifier) (identifier))
(import_item (identifier) (identifier))
)
)

Expand Down

0 comments on commit 12733e7

Please sign in to comment.