2018-06-11 20:18:00 +00:00
|
|
|
module.exports = grammar({
|
|
|
|
name: 'html',
|
|
|
|
|
2018-06-11 22:36:18 +00:00
|
|
|
extras: $ => [
|
|
|
|
$.comment,
|
|
|
|
/\s+/,
|
|
|
|
],
|
|
|
|
|
2018-06-11 20:18:00 +00:00
|
|
|
externals: $ => [
|
2018-06-12 19:20:13 +00:00
|
|
|
$._start_tag_name,
|
|
|
|
$._start_raw_tag_name,
|
|
|
|
$._end_tag_name,
|
|
|
|
$.erroneous_end_tag_name,
|
|
|
|
'/>',
|
2018-06-11 22:12:01 +00:00
|
|
|
$._implicit_end_tag,
|
2018-06-12 19:20:13 +00:00
|
|
|
$.raw_text,
|
2018-06-11 22:36:18 +00:00
|
|
|
$.comment,
|
2018-06-11 20:18:00 +00:00
|
|
|
],
|
|
|
|
|
|
|
|
rules: {
|
|
|
|
fragment: $ => repeat($._node),
|
|
|
|
|
2018-06-11 23:56:33 +00:00
|
|
|
doctype: $ => seq(
|
|
|
|
'<!',
|
|
|
|
/[Dd][Oo][Cc][Tt][Yy][Pp][Ee]/,
|
|
|
|
/[^>]+/,
|
|
|
|
'>'
|
|
|
|
),
|
|
|
|
|
2018-06-11 20:18:00 +00:00
|
|
|
_node: $ => choice(
|
2018-06-11 23:56:33 +00:00
|
|
|
$.doctype,
|
2018-06-11 20:18:00 +00:00
|
|
|
$.text,
|
2018-06-11 23:56:33 +00:00
|
|
|
$.element,
|
2018-06-12 19:20:13 +00:00
|
|
|
$.erroneous_end_tag,
|
2018-06-11 23:56:33 +00:00
|
|
|
$.raw_element
|
2018-06-11 20:18:00 +00:00
|
|
|
),
|
|
|
|
|
2018-06-11 22:12:01 +00:00
|
|
|
element: $ => choice(
|
|
|
|
seq(
|
|
|
|
$.start_tag,
|
|
|
|
repeat($._node),
|
|
|
|
choice($.end_tag, $._implicit_end_tag)
|
|
|
|
),
|
2018-06-11 20:18:00 +00:00
|
|
|
$.self_closing_tag
|
|
|
|
),
|
|
|
|
|
2018-06-11 23:56:33 +00:00
|
|
|
raw_element: $ => seq(
|
|
|
|
alias($._raw_start_tag, $.start_tag),
|
2018-06-12 19:20:13 +00:00
|
|
|
optional($.raw_text),
|
2018-06-11 23:56:33 +00:00
|
|
|
$.end_tag
|
|
|
|
),
|
|
|
|
|
2018-06-11 22:12:01 +00:00
|
|
|
start_tag: $ => seq(
|
2018-06-12 19:20:13 +00:00
|
|
|
'<',
|
|
|
|
alias($._start_tag_name, $.tag_name),
|
2018-06-11 22:12:01 +00:00
|
|
|
repeat($.attribute),
|
2018-06-12 19:20:13 +00:00
|
|
|
'>'
|
2018-06-11 22:12:01 +00:00
|
|
|
),
|
2018-06-11 20:18:00 +00:00
|
|
|
|
2018-06-11 23:56:33 +00:00
|
|
|
_raw_start_tag: $ => seq(
|
2018-06-12 19:20:13 +00:00
|
|
|
'<',
|
|
|
|
alias($._start_raw_tag_name, $.tag_name),
|
2018-06-11 23:56:33 +00:00
|
|
|
repeat($.attribute),
|
2018-06-12 19:20:13 +00:00
|
|
|
'>'
|
2018-06-11 23:56:33 +00:00
|
|
|
),
|
|
|
|
|
2018-06-11 20:18:00 +00:00
|
|
|
self_closing_tag: $ => seq(
|
2018-06-12 19:20:13 +00:00
|
|
|
'<',
|
|
|
|
alias($._start_tag_name, $.tag_name),
|
2018-06-11 20:18:00 +00:00
|
|
|
repeat($.attribute),
|
2018-06-12 19:20:13 +00:00
|
|
|
'/>'
|
|
|
|
),
|
|
|
|
|
|
|
|
end_tag: $ => seq(
|
|
|
|
'</',
|
|
|
|
alias($._end_tag_name, $.tag_name),
|
|
|
|
'>'
|
|
|
|
),
|
|
|
|
|
|
|
|
erroneous_end_tag: $ => seq(
|
|
|
|
'</',
|
|
|
|
$.erroneous_end_tag_name,
|
|
|
|
'>'
|
2018-06-11 20:18:00 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
attribute: $ => seq(
|
2018-06-11 22:36:18 +00:00
|
|
|
$.attribute_name,
|
2018-06-11 20:18:00 +00:00
|
|
|
optional(seq(
|
|
|
|
'=',
|
|
|
|
choice(
|
2018-06-11 22:36:18 +00:00
|
|
|
$.attribute_value,
|
2018-06-11 20:18:00 +00:00
|
|
|
$.quoted_attribute_value
|
|
|
|
)
|
|
|
|
))
|
|
|
|
),
|
|
|
|
|
2018-06-11 22:36:18 +00:00
|
|
|
attribute_name: $ => /[^<>"'/=\s]+/,
|
|
|
|
|
|
|
|
attribute_value: $ => /[^<>"'=\s]+/,
|
2018-06-11 20:18:00 +00:00
|
|
|
|
|
|
|
quoted_attribute_value: $ => choice(
|
|
|
|
seq("'", optional(alias(/[^']+/, $.attribute_value)), "'"),
|
|
|
|
seq('"', optional(alias(/[^"]+/, $.attribute_value)), '"')
|
|
|
|
),
|
|
|
|
|
|
|
|
text: $ => /[^<>]+/
|
|
|
|
}
|
|
|
|
});
|