Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add support for Zephyr's Kconfig extensions #17

Merged
merged 4 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ jobs:
test-go: true
test-swift: true
- name: Set up examples
if: matrix.os != 'windows-latest'
run: |-
git clone https://github.com/torvalds/linux examples/linux --single-branch --depth=1 --filter=blob:none
git clone https://github.com/torvalds/linux examples/linux --single-branch --depth=1 --filter=blob:none
git clone https://github.com/zephyrproject-rtos/zephyr examples/zephyr --single-branch --depth=1 --filter=blob:none
# TODO: Fix parsing on windows, case-insensitive fs leads to folders named `kconfig` being parsed
- name: Parse examples
uses: tree-sitter/parse-action@v4
if: matrix.os != 'windows-latest'
with:
files: |
examples/**/Kconfig*
Expand Down
90 changes: 65 additions & 25 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const PREC = {
module.exports = grammar({
name: 'kconfig',

conflicts: $ => [
[$.expression, $.name],
],

extras: $ => [
/\s/,
/\\\r?\n/,
Expand All @@ -36,15 +40,12 @@ module.exports = grammar({
word: $ => $.symbol,

rules: {
configuration: $ => seq(
optional($.mainmenu),
repeat($._entry),
),

mainmenu: $ => seq('mainmenu', field('name', $.prompt)),
configuration: $ => repeat($._entry),

_entry: $ => choice(
$.mainmenu,
$.config,
$.configdefault,
$.menuconfig,
$.choice,
$.comment_entry,
Expand All @@ -54,35 +55,43 @@ module.exports = grammar({
$.variable,
),

mainmenu: $ => seq('mainmenu', field('name', $.string)),

config: $ => seq(
'config',
field('name', $.symbol),
field('name', $.name),
repeat1($._config_option),
),

configdefault: $ => seq(
'configdefault',
field('name', $.name),
repeat1($.default_value),
),

menuconfig: $ => seq(
'menuconfig',
field('name', $.symbol),
field('name', $.name),
repeat1($._config_option),
),

choice: $ => seq(
'choice',
optional(field('name', $.symbol)),
optional(field('name', $.name)),
repeat1($._config_option),
repeat1($._entry),
repeat($._entry),
'endchoice',
),

comment_entry: $ => seq(
'comment',
field('name', $.prompt),
field('name', $.string),
repeat($._config_option),
),

menu: $ => seq(
'menu',
field('name', $.prompt),
field('name', $.string),
repeat($._config_option),
repeat($._entry),
'endmenu',
Expand All @@ -95,12 +104,18 @@ module.exports = grammar({
'endif',
),

source: $ => seq('source', $.prompt),
source: $ => seq(
choice('source', 'rsource', 'osource', 'orsource'),
$.string,
),

variable: $ => seq(
field('left', $.symbol),
choice('=', ':=', '+=', '?='),
repeat(choice(',', field('right', $.expression))),
choice(
repeat(choice(',', field('right', $.expression))),
$.text,
),
/\r?\n/,
),

Expand All @@ -121,14 +136,14 @@ module.exports = grammar({

type_definition: $ => seq(
choice('bool', 'tristate', 'int', 'hex', 'string'),
optional(choice($.prompt, $.input_prompt)),
optional(choice($.string, $.input_prompt)),
optional($.conditional_clause),
/\n/,
),

input_prompt: $ => seq(
'prompt',
$.prompt,
$.string,
optional($.conditional_clause),
/\n/,
),
Expand All @@ -141,7 +156,7 @@ module.exports = grammar({
),

type_definition_default: $ => seq(
choice('def_bool', 'def_tristate'),
choice('def_bool', 'def_tristate', 'def_int', 'def_hex', 'def_string'),
$.expression,
optional($.conditional_clause),
/\n/,
Expand All @@ -155,7 +170,7 @@ module.exports = grammar({

reverse_dependencies: $ => seq(
'select',
$.symbol,
$.name,
optional($.conditional_clause),
/\n/,
),
Expand Down Expand Up @@ -193,7 +208,8 @@ module.exports = grammar({

expression: $ => choice(
$.symbol,
$.prompt,
$.name,
$.string,
$.macro_variable,
$.unary_expression,
$.binary_expression,
Expand Down Expand Up @@ -229,18 +245,42 @@ module.exports = grammar({
repeat(choice(
$.macro_variable,
$.macro_content,
alias($.prompt, $.text),
alias($.string, $.text),
)),
')',
),
macro_content: _ => /([^\$'"\)]|(\([^\)]*\))|(\\\$)|\$[^(])+/,

prompt: _ => token(choice(
seq('"', repeat(choice(/[^"\\]/, /\\./)), '"'),
seq('\'', repeat(choice(/[^'\\]/, /\\./)), '\''),
)),
string: $ => choice(
seq(
'"',
repeat(choice(
alias(/([^"\\]|\$[^\(])+/, $.string_content),
/\\(.|\n)/,
$.macro_variable,
)),
'"',
),
seq(
'\'',
repeat(choice(
alias(/([^'\\]|\$[^\(])+/, $.string_content),
/\\(.|\n)/,
$.macro_variable,
)),
'\'',
),
),

symbol: _ => /-?[a-zA-Z0-9_]+/,
symbol: _ => /-?[a-zA-Z0-9_-]+/,

text: _ => /[^\s].*/,

name: $ => prec.right(prec.dynamic(-1, repeat1(choice(
$.symbol,
$.macro_variable,
$.string,
)))),

comment: _ => token(seq('#', /.*/)),
},
Expand Down
17 changes: 9 additions & 8 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,24 @@
(symbol) @variable

[
(prompt)
(string)
(macro_content)
(text)
] @string

(config name: (symbol) @constant)
(menuconfig name: (symbol) @constant)
(choice name: (symbol) @constant)
(config name: (name (symbol) @constant))
(configdefault name: (name (symbol) @constant))
(menuconfig name: (name (symbol) @constant))
(choice name: (name (symbol) @constant))

((symbol) @constant
(#lua-match? @constant "[A-Z0-9]+"))

(mainmenu name: (prompt) @text.title)
(comment_entry name: (prompt) @text.title)
(menu name: (prompt) @text.title)
(mainmenu name: (string) @text.title)
(comment_entry name: (string) @text.title)
(menu name: (string) @text.title)

(source (prompt) @text.uri @string.special)
(source (string) @text.uri @string.special)

(comment) @comment

Expand Down
8 changes: 4 additions & 4 deletions queries/locals.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
(symbol)
(prompt)
(string)
] @reference

[
Expand All @@ -12,6 +12,6 @@
(if)
] @scope

(type_definition (prompt) @definition.var)
(type_definition (input_prompt (prompt) @definition.var))
(type_definition_default (expression (prompt) @definition.var))
(type_definition (string) @definition.var)
(type_definition (input_prompt (string) @definition.var))
(type_definition_default (expression (string) @definition.var))
Loading
Loading