#######################

contexts:

  prototype:
    - meta_prepend: true
    - include: php-embedded

  main:
    - meta_include_prototype: false
    - match: ''
      push: [php, shebang]

  php:
    # This is the main php context.
    # It includes `php-tags` which are allowed to be closed by php tags at any
    # level of code-block on stack resuming with the next top-level php tag.
    - meta_include_prototype: false
    - meta_scope: text.html.php
    - include: php-tags
    - include: html

###[ SHEBANG ]################################################################

  shebang:
    - meta_include_prototype: false
    - match: ^\#!
      scope: punctuation.definition.comment.php
      set: shebang-body
    - match: ^|(?=\S)
      pop: 1

  shebang-body:
    - meta_include_prototype: false
    - meta_scope: comment.line.shebang.php
    - match: \bphp(?:[-_]?\d+(?:\.\d+){0,2})?\b
      scope: constant.language.shebang.php
    - match: \n
      pop: 1

###[ HTML CONTENT ]###########################################################

  cdata-content:
    - meta_prepend: true
    - meta_include_prototype: false
    - include: php-interpolations

  script-javascript-content:
    - meta_include_prototype: false
    - match: \s*((<!\[)(CDATA)(\[))
      captures:
        1: meta.tag.sgml.cdata.html
        2: punctuation.definition.tag.begin.html
        3: keyword.declaration.cdata.html
        4: punctuation.definition.tag.begin.html
      pop: 1  # make sure to match only once
      embed: scope:source.js.php
      embed_scope: meta.tag.sgml.cdata.html source.js.embedded.html
      escape: \]\]>
      escape_captures:
        0: meta.tag.sgml.cdata.html punctuation.definition.tag.end.html
    - match: '{{script_content_begin}}'
      captures:
        1: comment.block.html punctuation.definition.comment.begin.html
      pop: 1  # make sure to match only once
      embed: scope:source.js.php
      embed_scope: source.js.embedded.html
      escape: '{{script_content_end}}'
      escape_captures:
        1: source.js.embedded.html
        2: comment.block.html punctuation.definition.comment.end.html
        3: source.js.embedded.html
        4: comment.block.html punctuation.definition.comment.end.html

  style-css-content:
    - meta_include_prototype: false
    - match: \s*((<!\[)(CDATA)(\[))
      captures:
        1: meta.tag.sgml.cdata.html
        2: punctuation.definition.tag.begin.html
        3: keyword.declaration.cdata.html
        4: punctuation.definition.tag.begin.html
      pop: 1  # make sure to match only once
      embed: scope:source.css.php
      embed_scope: meta.tag.sgml.cdata.html source.css.embedded.html
      escape: \]\]>
      escape_captures:
        0: meta.tag.sgml.cdata.html punctuation.definition.tag.end.html
    - match: '{{style_content_begin}}'
      captures:
        1: comment.block.html punctuation.definition.comment.begin.html
      pop: 1  # make sure to match only once
      embed: scope:source.css.php
      embed_scope: source.css.embedded.html
      escape: '{{style_content_end}}'
      escape_captures:
        1: source.css.embedded.html
        2: comment.block.html punctuation.definition.comment.end.html
        3: source.css.embedded.html
        4: comment.block.html punctuation.definition.comment.end.html

  tag-event-attribute-value:
    - match: \"
      scope: meta.string.html string.quoted.double.html punctuation.definition.string.begin.html
      embed: scope:source.js.php
      embed_scope: meta.string.html meta.interpolation.html source.js.embedded.html
      escape: \"
      escape_captures:
        0: meta.string.html string.quoted.double.html punctuation.definition.string.end.html
    - match: \'
      scope: meta.string.html string.quoted.single.html punctuation.definition.string.begin.html
      embed: scope:source.js.php
      embed_scope: meta.string.html meta.interpolation.html source.js.embedded.html
      escape: \'
      escape_captures:
        0: meta.string.html string.quoted.single.html punctuation.definition.string.end.html
    - include: else-pop

  tag-style-attribute-value:
    - match: \"
      scope: meta.string.html string.quoted.double.html punctuation.definition.string.begin.html
      embed: scope:source.css.php#rule-list-body
      embed_scope: meta.string.html meta.interpolation.html source.css.embedded.html
      escape: \"
      escape_captures:
        0: meta.string.html string.quoted.double.html punctuation.definition.string.end.html
    - match: \'
      scope: meta.string.html string.quoted.single.html punctuation.definition.string.begin.html
      embed: scope:source.css.php#rule-list-body
      embed_scope: meta.string.html meta.interpolation.html source.css.embedded.html
      escape: \'
      escape_captures:
        0: meta.string.html string.quoted.single.html punctuation.definition.string.end.html
    - include: else-pop

  tag-attribute-value-content:
    - meta_prepend: true
    - include: php-interpolations

  strings-common-content:
    - meta_prepend: true
    - include: php-interpolations

###[ PHP TAGS ]###############################################################

  php-tags:
    - include: php-tag-incomplete
    - match: <\?(?i:php\b|=)?
      scope: punctuation.section.embedded.begin.php
      push: php-tag-content

  php-tag-content:
    - meta_include_prototype: false
    - meta_scope: meta.embedded.php
    - meta_content_scope: source.php.embedded.html
    - match: (\?>)(\s*\n)?
      captures:
        1: punctuation.section.embedded.end.php
        2: meta.html-newline-after-php.php  # used by indentation rules
      pop: 1
    - include: Packages/PHP/Embeddings/PHP Source (for PHP).sublime-syntax
      apply_prototype: true

  php-tag-incomplete:
    - match: <\?(?i:(?!php)ph?)
      scope: meta.embedded.php punctuation.section.embedded.begin.php

  php-interpolations:
    - meta_include_prototype: false
    - match: (?=<\?)
      push: php-interpolation-body

  php-interpolation-body:
    - clear_scopes: 1
    - meta_include_prototype: false
    - include: php-embedded
    - include: immediately-pop

  php-embedded:
    - meta_include_prototype: false
    - include: php-tag-incomplete
    - match: <\?(?i:php\b|=)?
      scope: meta.embedded.php punctuation.section.embedded.begin.php
      embed: Packages/PHP/PHP Source.sublime-syntax
      embed_scope: meta.embedded.php source.php.embedded.html
      escape: (\?>)(\s*\n)?
      escape_captures:
        0: meta.embedded.php
        1: punctuation.section.embedded.end.php
        2: meta.html-newline-after-php.php
PK