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
Henrik Hautakoski d0b54d61aa Update grammar to correctly produce value token
Problem was before that for example the input [0x000KKK] would produce a
hexadecimal and a value token (for the respective parts)

However, the more correct way is to have the whole input identified as a
value token. basicly if the whole input between "=" and a special
"end-of-assignment" token can't be identified as a string,bool,integer
whatever. it should be identified as a value token.
2024-12-12 10:55:11 +01:00

69 lines
1.2 KiB
JavaScript

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