mirror of
https://github.com/soconnor0919/tree-sitter-xml.git
synced 2026-02-04 15:56:37 -05:00
63 lines
1.0 KiB
JavaScript
63 lines
1.0 KiB
JavaScript
module.exports = grammar({
|
|
name: 'xml',
|
|
|
|
rules: {
|
|
source_file: $ => repeat($._item),
|
|
|
|
_item: $ => choice(
|
|
$.element,
|
|
$.comment,
|
|
$.text,
|
|
$._whitespace
|
|
),
|
|
|
|
// XML element
|
|
element: $ => seq(
|
|
'<',
|
|
$.tag_name,
|
|
repeat($.attribute),
|
|
choice(
|
|
seq('>', repeat($._item), '</', $.tag_name, '>'),
|
|
'/>'
|
|
)
|
|
),
|
|
|
|
// Tag name
|
|
tag_name: $ => /[A-Za-z][A-Za-z0-9_-]*/,
|
|
|
|
// Attribute
|
|
attribute: $ => seq(
|
|
$.attribute_name,
|
|
'=',
|
|
$.attribute_value
|
|
),
|
|
|
|
// Attribute name
|
|
attribute_name: $ => /[A-Za-z][A-Za-z0-9_-]*/,
|
|
|
|
// Attribute value
|
|
attribute_value: $ => choice(
|
|
seq('"', $.quoted_value, '"'),
|
|
seq("'", $.quoted_value, "'")
|
|
),
|
|
|
|
// Quoted value content
|
|
quoted_value: $ => /[^"']*/,
|
|
|
|
// XML comment
|
|
comment: $ => token(seq('<!--', /[^-]*(?:-[^-]+)*/, '-->')),
|
|
|
|
// Text content
|
|
text: $ => /[^<]+/,
|
|
|
|
// Whitespace
|
|
_whitespace: $ => /\s+/
|
|
},
|
|
|
|
extras: $ => [
|
|
/\s/,
|
|
$.comment
|
|
]
|
|
});
|
|
|