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: $ => /[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 ] });