Example

HTML Fragment

Nested HTML-like markup.

Parse a small nested markup fragment.

What it shows

  • Attribute exposes its name and value.
  • Node has comment, text, and element forms.
  • Node::Element contains Node[] children for nested markup.
  • #{=tagName} requires the closing tag to match the opening tag.

Try adding a nested tag or another attribute.

Specs

spec.yml
$flags: 'FLEXIBLE_WHITESPACE'

MarkupName: '[A-Za-z][A-Za-z0-9-]*'

Attribute:
  $types:
    AttributeValue: '.*?'
  $fields:
    name: MarkupName
    value: AttributeValue
  $patterns: '#{name}=\"#{value}\"'

Node:
  Comment: '<!--.*?-->'
  Text: '(?<=>)${!Node}[^>\s][^>]*(?=<)'
  Element:
    $fields:
      tagName: MarkupName
      attributes: Attribute[]
      children: Node[]
    $patterns:
      - '<#{tagName}( #{attributes})*>(\s*#{children}\s*)*</#{=tagName}>'
      - '<#{tagName}( #{attributes})*/>'
Try it liveOpen HTML Fragment in StudioTweak the spec, paste your own input, and see the parse tree update in real time.

Or browse other examples →