/**
 * Grammar definition for HTML/Markup syntax highlighting.
 * Handles tags, attributes, comments, doctypes, CDATA, entities, and embedded content.
 */
export const markupGrammar = {
  comment: {
    pattern: /<!--(?:(?!<!--)[\s\S])*?-->/,
    greedy: true,
  },
  prolog: {
    pattern: /<\?[\s\S]+?\?>/,
    greedy: true,
  },
  doctype: {
    pattern: /<!DOCTYPE(?:[^>"']|"[^"]*"|'[^']*')+(?:>|$)/i,
    greedy: true,
    inside: {
      'internal-subset': {
        pattern: /(^[^[]*\[)[\s\S]+(?=\]>$)/,
        lookbehind: true,
        greedy: true,
      },
      'punctuation': { pattern: /^<!|>$|[[\]]/ },
      'doctype-tag': { pattern: /^DOCTYPE/i },
      'name': { pattern: /[^\s<>'"/]+/ },
      'string': {
        pattern: /("|')[\s\S]*?\1/,
        greedy: true,
      },
    },
  },
  cdata: {
    pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
    greedy: true,
  },
  tag: {
    pattern: /<\/?(?!\d)[^\s>/=$<%]+(?:\s(?:\s*[^\s>/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/,
    greedy: true,
    inside: {
      'tag': {
        pattern: /^<\/?[^\s>/]+/,
        inside: {
          punctuation: { pattern: /^<\/?/ },
          namespace: { pattern: /^[^\s>/:]+:/ },
        },
      },
      'special-attr': [],
      'attr-value': {
        pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/,
        inside: {
          punctuation: [{ pattern: /^=/ }, { pattern: /^(\s*)["']|["']$/, lookbehind: true }],
        },
      },
      'punctuation': { pattern: /\/?>/ },
      'attr-name': {
        pattern: /[^\s>/]+/,
        inside: {
          namespace: { pattern: /^[^\s>/:]+:/ },
        },
      },
    },
  },
  entity: [
    {
      pattern: /&[\da-z]{1,8};/i,
    },
    {
      pattern: /&#x?[\da-f]{1,8};/i,
    },
  ],
};
