|
| 1 | +const startTag = ($, tag) => seq( |
| 2 | + '<', |
| 3 | + alias(tag, $.tag_name), |
| 4 | + repeat($.attribute), |
| 5 | + '>' |
| 6 | +) |
| 7 | + |
| 8 | +module.exports = grammar({ |
| 9 | + name: 'html', |
| 10 | + |
| 11 | + externals: $ => [ |
| 12 | + $.tag_name, |
| 13 | + |
| 14 | + ], |
| 15 | + |
| 16 | + rules: { |
| 17 | + fragment: $ => repeat($._node), |
| 18 | + |
| 19 | + _node: $ => choice( |
| 20 | + $.text, |
| 21 | + $.element, |
| 22 | + $.void_element |
| 23 | + ), |
| 24 | + |
| 25 | + element: $ => seq( |
| 26 | + $.start_tag, |
| 27 | + repeat($._node), |
| 28 | + $.end_tag |
| 29 | + ), |
| 30 | + |
| 31 | + void_element: $ => choice( |
| 32 | + seq($.void_start_tag, optional($.end_tag)), |
| 33 | + $.self_closing_tag |
| 34 | + ), |
| 35 | + |
| 36 | + start_tag: $ => startTag($, $.tag_name), |
| 37 | + |
| 38 | + void_start_tag: $ => startTag($, $.void_tag_name), |
| 39 | + |
| 40 | + self_closing_tag: $ => seq( |
| 41 | + '<', |
| 42 | + choice($.tag_name, $.void_tag_name), |
| 43 | + repeat($.attribute), |
| 44 | + '/', |
| 45 | + '>' |
| 46 | + ), |
| 47 | + |
| 48 | + attribute: $ => seq( |
| 49 | + alias($._attribute_part, $.attribute_name), |
| 50 | + optional(seq( |
| 51 | + '=', |
| 52 | + choice( |
| 53 | + alias($._attribute_part, $.attribute_value), |
| 54 | + $.quoted_attribute_value |
| 55 | + ) |
| 56 | + )) |
| 57 | + ), |
| 58 | + |
| 59 | + _attribute_part: $ => /[^<>"'/=\s]+/, |
| 60 | + |
| 61 | + quoted_attribute_value: $ => choice( |
| 62 | + seq("'", optional(alias(/[^']+/, $.attribute_value)), "'"), |
| 63 | + seq('"', optional(alias(/[^"]+/, $.attribute_value)), '"') |
| 64 | + ), |
| 65 | + |
| 66 | + end_tag: $ => seq( |
| 67 | + '</', |
| 68 | + choice($.tag_name, $.void_tag_name), |
| 69 | + '>' |
| 70 | + ), |
| 71 | + |
| 72 | + tag_name: $ => /[a-zA-Z\-]+/, |
| 73 | + |
| 74 | + void_tag_name: $ => token(prec(1, choice( |
| 75 | + 'area', |
| 76 | + 'base', |
| 77 | + 'basefont', |
| 78 | + 'bgsound', |
| 79 | + 'br', |
| 80 | + 'col', |
| 81 | + 'command', |
| 82 | + 'embed', |
| 83 | + 'frame', |
| 84 | + 'hr', |
| 85 | + 'image', |
| 86 | + 'img', |
| 87 | + 'input', |
| 88 | + 'isindex', |
| 89 | + 'keygen', |
| 90 | + 'link', |
| 91 | + 'menuitem', |
| 92 | + 'meta', |
| 93 | + 'nextid', |
| 94 | + 'param', |
| 95 | + 'source', |
| 96 | + 'track', |
| 97 | + 'wbr' |
| 98 | + ))), |
| 99 | + |
| 100 | + text: $ => /[^<>]+/ |
| 101 | + } |
| 102 | +}); |
0 commit comments