%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
name: Grammalecte rules
file_extensions:
  - grx
scope: source.grammalecte
contexts:
  main:
    # Strings begin and end with quotes, and use backslashes as an escape
    # character
    - match: '"[^"]*"'
      scope: string.quoted.double
      #push: double_quoted_string

    # Comments begin with a '#' and finish at the end of the line
    - match: '#'
      scope: punctuation.definition.comment
      push: line_comment

    # Numbers
    - match: '\b(-)?[0-9.]+\b'
      scope: constant.numeric

    # Bookmarks
    - match: '^!!.*|^\[\+\+\].*'
      scope: bookmark

    # Bookmarks
    - match: '^GRAPH_NAME:.*'
      scope: bookmark

      # Bookmarks
    - match: '^@@@@GRAPH: *(\w+)@@@@ *'
      scope: bookmark
      captures:
        1: string.graphname

    # Keywords are if, else.
    # Note that blackslashes don't need to be escaped within single quoted
    # strings in YAML. When using single quoted strings, only single quotes
    # need to be escaped: this is done by using two single quotes next to each
    # other.
    - match: '\b(?:if|else|and|or|not|in)\b'
      scope: keyword.python

    - match: '\b(?:True|False|None)\b'
      scope: constant.language
    
    - match: '\b(?:spell|morph|morphex|g_morph|stem|textarea0?\w*|before0?\w*|after0?\w*|word|option|define|select|exclude|g_define|g_select|g_exclude|analysex?|apposition|is[A-Z]\w+|rewriteSubject|checkD\w+|getD\w+|has[A-Z]\w+|sugg[A-Z]\w+|switch[A-Z]\w+|ceOrCet|formatN\w+|mbUnit)\b'
      scope: entity.name.function

    - match: '\b(?:replace|endswith|startswith|search|upper|lower|capitalize|strip|rstrip|is(?:upper|lower|digit|title))\b'
      scope: support.function

    - match: '\becho\b'
      scope: support.function.debug

    - match: '\bre\b'
      scope: support.class

    # Regex rule option
    - match: '^__[\[<]([isu])[\]>](/\w+|)(\(\w+\)|)(![0-9]|)__|</?js>'
      scope: rule.options
      captures:
        1: rule.casing
        2: rule.optionname
        3: rule.rulename
        4: rule.priority

    # Graph rules option
    - match: '^__(\w+)(![0-9]|)__'
      scope: rule.options
      captures:
        1: rule.rulename2
        2: rule.priority

    - match: '/(\w+)/'
      scope: rule.options
      captures:
        1: rule.optionname

    # Definitions and options
    - match: '^OPT(?:GROUP|LANG|PRIORITY)/|^OPTSOFTWARE:'
      scope: options.command

    - match: '^OPT(?:LABEL|)/'
      scope: options.parameter

    - match: '^DEF: '
      scope: definition

    # Tests & TODO
    - match: '^TEST: (?:__[a-zA-Z0-9]+__ |)'
      scope: test

    - match: '^TODO: '
      scope: todo

    # JS groups positioning codes
    - match: ' @@[0-9w,$*>:]+'
      scope: comment.line.percentage

    # rule delimiters
    - match: '<<-|>>>'
      scope: keyword.action
    - match: '__also__'
      scope: keyword.condition.green
    - match: '__else__'
      scope: keyword.condition.red
    - match: '-(\d*(?::\d+|))>>'
      scope: keyword.error
      captures:
        1: keyword.error.group
    - match: '~(\d*(?::\d+|))>>'
      scope: keyword.textprocessor
      captures:
        1: keyword.textprocessor.group
    - match: '=>>'
      scope: keyword.disambiguator

    # Tokens
    - match: '(>)\w+'
      scope: string.lemma
      captures:
        1: keyword.valid

    - match: '(~)(?!(?:\d+(?::\d+|)|)>>)[^\s]+'
      scope: string.regex
      captures:
        1: keyword.valid

    - match: '(@)([^@][^\s¬]+)'
      scope: string.morph
      captures:
        1: keyword.valid
        2: string.morph.pattern

    - match: '(¬)(\S+)'
      scope: string.morph
      captures:
        1: keyword.invalid
        2: string.morph.antipattern  

    # Escaped chars
    - match: '\\(?:\d+|w|d|b|n|s|t)'
      scope: constant.character.escape

    # URL
    - match: '\| ?https?://[\w./%?&=#+-]+'
      scope: string.other

    # Example errors
    - match: '{{.+?}}'
      scope: message.error

    # special chars
    - match: '[@=*^?!:+<>~]'
      scope: keyword.other

    - match: '\(\?(?:[:=!]|<!)|[(|)]'
      scope: keyword.parenthesis

    - match: '\[|[]{}]'
      scope: keyword.brackets

#  double_quoted_string:
#    - meta_scope: string.quoted.double
#    - match: '\\.'
#      scope: constant.character.escape
#    - match: '(?:"|$)'
#      scope: punctuation.definition.string.end
#      pop: true

  line_comment:
    - meta_scope: comment.line
    - match: '\\[0-9]+'
      scope: constant.character.escape
    - match: '\| ?https?://[\w./%?&=#+-]+'
      scope: string.other
    - match: $
      pop: true
