1
0
Fork 0
mirror of https://github.com/pnx/tree-sitter-dotenv synced 2026-06-16 01:54:56 +02:00
tree-sitter-dotenv/grammar.js

73 lines
1.3 KiB
JavaScript

/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: "dotenv",
extras: _ => [
/\s/
],
externals: $ => [
$._empty_value,
],
rules: {
document: $ => repeat(choice(
$.comment,
$.assignment,
)),
assignment: $ => seq(
field("key", $.identifier),
"=",
field("value", $._value),
),
comment: _ => seq('#', /.*/),
identifier: _ => token(/[A-Za-z_][A-Za-z0-9_]*/),
_value: $ => choice(
$.string,
$.string_interpolation,
$.number,
$.boolean,
$.value,
alias($._empty_value, $.value),
),
string: $ => seq(
"'",
$.string_content,
"'",
),
string_interpolation: $ => seq(
'"',
alias($.string_interpolation_content, $.string_content),
'"',
),
// Strings
string_content: _ => token(/[^']*/),
string_interpolation_content: _ => token(/[^"]*/),
// Numbers
number: $ => choice(
$.integer,
$.float,
$.hexadecimal,
),
integer: _ => token(/(\-)?\d+/),
float: _ => seq(/(\-)?\d+/, '.', /\d+/),
hexadecimal: _ => seq('0x', /[0-9a-fA-F]+/),
boolean: _ => token(choice('true', 'false')),
value: _ => token(prec(-1, /[^\#\s]+/)),
},
});