mirror of
https://github.com/soconnor0919/tree-sitter-xml.git
synced 2026-02-05 00:06:41 -05:00
Initial commit: Tree-sitter grammar for XML language
This commit is contained in:
62
grammar.js
Normal file
62
grammar.js
Normal file
@@ -0,0 +1,62 @@
|
||||
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
|
||||
]
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user