%YAML 1.2
---
name: Starlark
file_extensions: [bzl, star, bazel]
scope: source.starlark
contexts:
  main:
    - include: comments
    - include: strings
    - include: keywords
    - include: functions
    - include: numbers
    - include: operators
    - include: punctuation
  comments:
    - match: '#.*$'
      scope: comment.line.number-sign.starlark
  strings:
    - match: '"""'
      push: [{meta_scope: string.quoted.triple.double.starlark}, {match: '"""', pop: true}]
    - match: "'''"
      push: [{meta_scope: string.quoted.triple.single.starlark}, {match: "'''", pop: true}]
    - match: '"'
      push:
        - meta_scope: string.quoted.double.starlark
        - match: '\\.'
          scope: constant.character.escape.starlark
        - match: '"'
          pop: true
    - match: "'"
      push:
        - meta_scope: string.quoted.single.starlark
        - match: '\\.'
          scope: constant.character.escape.starlark
        - match: "'"
          pop: true
  keywords:
    - match: '\b(def|return|if|elif|else|for|in|not|and|or|break|continue|pass|lambda|load)\b'
      scope: keyword.control.starlark
    - match: '\b(True|False|None)\b'
      scope: constant.language.starlark
    - match: '\b(struct|provider|rule|repository_rule|module_extension|tag_class|aspect)\b'
      scope: keyword.declaration.starlark
    - match: '\b(load|select|glob|package|exports_files|licenses|visibility|filegroup|bazel_dep|module|use_extension|use_repo|register_toolchains|register_execution_platforms)\b'
      scope: support.function.starlark
    - match: '\b(len|range|str|repr|bool|int|dict|list|tuple|type|enumerate|zip|all|any|sorted|reversed|depset|fail|print)\b'
      scope: support.function.builtin.starlark
  functions:
    - match: '\b[A-Za-z_][A-Za-z0-9_]*(?=\s*\()'
      scope: entity.name.function.starlark
    - match: '\b[A-Za-z_][A-Za-z0-9_]*(?=\s*=)'
      scope: variable.parameter.starlark
  numbers:
    - match: '\b0[bB][01]+\b'
      scope: constant.numeric.binary.starlark
    - match: '\b0[xX][0-9a-fA-F]+\b'
      scope: constant.numeric.hex.starlark
    - match: '\b0[oO][0-7]+\b'
      scope: constant.numeric.octal.starlark
    - match: '\b\d+\b'
      scope: constant.numeric.starlark
  operators:
    - match: '==|!=|<=|>=|//|\*\*|<<|>>|\+=|-=|\*=|/=|%=|&=|\|=|\^=|[+\-*/%@|&^~<>:=]'
      scope: keyword.operator.starlark
  punctuation:
    - match: '[()\[\]{}]'
      scope: punctuation.section.brackets.starlark
    - match: '[,.]'
      scope: punctuation.separator.starlark
