%YAML 1.2
---
name: Storybook
file_extensions:
  - sb
  - storybook
scope: source.storybook

contexts:
  main:
    # Comments
    - match: '//!.*$'
      scope: comment.line.documentation.storybook
    - match: '//.*$'
      scope: comment.line.double-slash.storybook

    # Prose blocks
    - match: '^(\s*)(---\w*)$'
      captures:
        2: keyword.control.prose.storybook
      push: prose_block

    # Declaration keywords
    - match: '\b(species|character|behavior|template|use|from)\b'
      scope: keyword.declaration.storybook

    # Control flow keywords
    - match: '\b(for|where|if|else|match|with)\b'
      scope: keyword.control.storybook

    # Boolean literals
    - match: '\b(true|false)\b'
      scope: constant.language.boolean.storybook

    # Numeric literals
    - match: '\b\d+(\.\d+)?\b'
      scope: constant.numeric.storybook

    # Time/Duration literals
    - match: '\b\d+[hms]\b'
      scope: constant.numeric.time.storybook

    # String literals
    - match: '"'
      scope: punctuation.definition.string.begin.storybook
      push: double_quoted_string

    # Field names (before colon)
    - match: '^\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*(:)'
      captures:
        1: variable.other.member.storybook
        2: punctuation.separator.storybook

    # Range operator
    - match: '\.\.'
      scope: keyword.operator.range.storybook

    # Operators
    - match: '[+\-*/%<>=!&|]'
      scope: keyword.operator.storybook

    # Punctuation
    - match: '[{}()\[\],;]'
      scope: punctuation.storybook

    # Type names and identifiers (capitalized)
    - match: '\b[A-Z][a-zA-Z0-9_]*\b'
      scope: entity.name.type.storybook

    # Regular identifiers
    - match: '\b[a-z_][a-zA-Z0-9_]*\b'
      scope: variable.other.storybook

  prose_block:
    - meta_scope: string.unquoted.prose.storybook
    - match: '^(\s*)(---)$'
      captures:
        2: keyword.control.prose.storybook
      pop: true
    - match: '.*$'
      scope: string.unquoted.prose.storybook

  double_quoted_string:
    - meta_scope: string.quoted.double.storybook
    - match: '\\.'
      scope: constant.character.escape.storybook
    - match: '"'
      scope: punctuation.definition.string.end.storybook
      pop: true
