summary refs log tree commit diff stats
path: root/tests/compiles/trecursive_generic_in_compiles.nim
blob: 77bf0bb02772ce078a682cacfd6c899fbdcdd5ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# bug #3313
import unittest, future

type
  ListNodeKind = enum
    lnkNil, lnkCons
  List*[T] = ref object
    ## List ADT
    case kind: ListNodeKind
    of lnkNil:
      discard
    of lnkCons:
      value: T
      next: List[T] not nil

proc Cons*[T](head: T, tail: List[T]): List[T] =
  ## Constructs non empty list
  List[T](kind: lnkCons, value: head, next: tail)

proc Nil*[T](): List[T] =
  ## Constructs empty list
  List[T](kind: lnkNil)

proc head*[T](xs: List[T]): T =
  ## Returns list's head
  xs.value

# TODO
# proc headOption*[T](xs: List[T]): Option[T] = ???

proc tail*[T](xs: List[T]): List[T] =
  ## Returns list's tail
  case xs.kind
  of lnkCons: xs.next
  else: xs

proc isEmpty*(xs: List): bool =
  ## Checks  if list is empty
  xs.kind == lnkNil

proc `==`*[T](xs, ys: List[T]): bool =
  ## Compares two lists
  if (xs.isEmpty, ys.isEmpty) == (true, true): true
  elif (xs.isEmpty, ys.isEmpty) == (false, false): xs.head == ys.head and xs.tail == ys.tail
  else: false

proc asList*[T](xs: varargs[T]): List[T] =
  ## Creates list from varargs
  proc initListImpl(i: int, xs: openarray[T]): List[T] =
    if i > high(xs):
      Nil[T]()
    else:
      Cons(xs[i], initListImpl(i+1, xs))
  initListImpl(0, xs)

proc foldRight*[T,U](xs: List[T], z: U, f: (T, U) -> U): U =
  case xs.isEmpty
  of true: z
  else: f(xs.head, xs.tail.foldRight(z, f))

proc dup*[T](xs: List[T]): List[T] =
  ## Duplicates the list
  xs.foldRight(Nil[T](), (x: T, xs: List[T]) => Cons(x, xs))

type
  ListFormat = enum
    lfADT, lfSTD

proc asString[T](xs: List[T], f = lfSTD): string =
  proc asAdt(xs: List[T]): string =
    case xs.isEmpty
    of true: "Nil"
    else: "Cons(" & $xs.head & ", " & xs.tail.asAdt & ")"

  proc asStd(xs: List[T]): string =
    "List(" & xs.foldLeft("", (s: string, v: T) =>
      (if s == "": $v else: s & ", " & $v)) & ")"

  case f
  of lfADT: xs.asAdt
  else: xs.asStd

proc `$`*[T](xs: List[T]): string =
  ## Converts list to string
  result = xs.asString

proc foldLeft*[T,U](xs: List[T], z: U, f: (U, T) -> U): U =
  case xs.isEmpty
  of true: z
  else: foldLeft(xs.tail, f(z, xs.head), f)

suite "unittest compilation error":

  test "issue 3313":
    let lst = lc[$x | (x <- 'a'..'z'), string].asList

    let lstCopy = lst.dup
    check: lstCopy == lst
ref='#n997'>997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
%YAML 1.2
---
# http://www.sublimetext.com/docs/3/syntax.html
name: java-new
file_extensions:
  - java
  - bsh
scope: source.java

variables:
  primitives: (?:boolean|byte|char|short|int|float|long|double|var)
  storage_modifiers: (?:public|private|protected|static|final|native|synchronized|strictfp|abstract|transient|default|volatile)

  id: (?:[\p{L}_$][\p{L}\p{N}_$]*)
  classcase_id: (?:\p{Lu}[\p{L}\p{N}_$]*)
  lowercase_id: (?:[_$]*\p{Ll}[\p{Ll}\p{N}_$]*\b)
  uppercase_id: (?:[_$]*\p{Lu}[\p{Lu}\p{N}_$]*\b)

  # One dot is mandatory to not compete with other regexes that match an id.
  before_fqn: (?={{lowercase_id}}\s*\.)

  # utility lookaround
  lambda_lookahead: (?:\(.*\)|{{id}})\s*->

  # digits
  ddigits0: '\d[\d_]*?(_*)'
  ddigits: (?:(_*){{ddigits0}})
  hdigits: (?:(_*)\h[\h_]*?(_*))
  exponent: '[-+]?{{ddigits}}'
  eexponent: (?:[eE]{{exponent}})
  pexponent: (?:[pP]{{exponent}})

contexts:
  prototype:
    - match: (?=%>)
      pop: true
    - include: comments
    - include: illegal-keywords

  any_POP:
    - match: (?=\S)
      pop: true

  immediate_POP:
    - match: ''
      pop: true

  main:
    - include: prototype
    - include: package-statement
    - include: import-statement
    - include: module
    - include: class
    - include: annotations
    # Get modifiers defined on a different line than the class
    - include: storage-modifiers
    - include: stray-braces
    - include: code

  punctuation-accessor-dot:
    - match: \.
      scope: punctuation.accessor.dot.java

  punctuation-separator-comma:
    - match: \,
      scope: punctuation.separator.comma.java

  punctuation-terminator-semicolon:
    - match: ;
      scope: punctuation.terminator.java

  dot-separated-identifier:
    - match: '{{id}}'
    - include: punctuation-accessor-dot
    - include: immediate_POP

  package-statement:
    - match: \bpackage\b
      scope: keyword.other.package.java
      push:
        - - meta_scope: meta.package-declaration.java
          - include: immediate_POP
        - - match: '{{id}}'
            set:
              - meta_scope: meta.path.java entity.name.namespace.java
              - include: dot-separated-identifier
          - include: any_POP

  import-statement:
    - match: \bimport\b
      scope: keyword.control.import.java
      push:
        - - meta_scope: meta.import.java
          - include: immediate_POP
        - import-statement-body

  import-statement-body:
    - match: \bstatic\b
      scope: keyword.control.import.static.java
      set: static-import-statement-body
    - include: before-next-import
    - match: '{{lowercase_id}}'
      scope: meta.path.java support.type.package.java
      set:
        - meta_content_scope: meta.path.java
        - include: before-next-import
        - include: package
        - match: \*
          scope: meta.path.java keyword.operator.wildcard.asterisk.java
          pop: true
        - match: '{{classcase_id}}'
          scope: support.class.import.java
          set:
            - include: before-next-import
            - include: punctuation-accessor-dot
            - include: import-class
            - include: import-wildcard
            - include: any_POP
        - include: any_POP
    - include: any_POP

  static-import-statement-body:
    - include: before-next-import
    - match: '{{lowercase_id}}'
      scope: meta.path.java support.type.package.java
      set:
        - meta_content_scope: meta.path.java
        - include: before-next-import
        - include: package
        - match: '{{classcase_id}}'
          scope: support.class.import.java
          set:
            - include: before-next-import
            - include: punctuation-accessor-dot
            - include: import-constant
            - include: import-class
            - include: import-function
            - include: import-wildcard
            - include: any_POP
        - include: any_POP
    - include: any_POP

  before-next-import:
    # Prevent next import statement to be consumed when a current statement isn't terminated with ';'.
    - match: (?=\bimport\b)
      pop: true
    # For a case of a statement immediately before a class definition.
    - match: (?=\b(?:{{storage_modifiers}}|class|interface|enum)\b)
      pop: true

  package:
    - match: '{{lowercase_id}}'
      scope: support.type.package.java
    - include: punctuation-accessor-dot

  all-types:
    - include: primitive-types
    - include: object-types

  import-constant:
    - match: '{{uppercase_id}}'
      scope: constant.other.import.java

  import-class:
    - match: '{{classcase_id}}'
      scope: support.class.import.java

  import-function:
    - match: '{{id}}'
      scope: support.function.import.java

  import-wildcard:
    - match: \*
      scope: keyword.operator.wildcard.asterisk.java

  annotations:
    - match: \@
      scope: punctuation.definition.annotation.java
      push:
        - - meta_scope: meta.annotation.java
          - include: immediate_POP
        - annotation-parameters
        - - meta_content_scope: meta.annotation.identifier.java
          - include: immediate_POP
        - annotation-type-reference

  annotation-type-reference:
    - match: '{{before_fqn}}'
      set:
        - meta_scope: meta.path.java
        - match: '{{lowercase_id}}'
          scope: variable.annotation.package.java
        - include: punctuation-accessor-dot
        - include: annotation-type-no-fqn
    - include: annotation-type-no-fqn

  annotation-type-no-fqn:
    - match: '{{classcase_id}}'
      scope: variable.annotation.java
      set: after-annotation-type-reference
    - include: any_POP

  after-annotation-type-reference:
    - match: \.
      scope: punctuation.accessor.dot.java
      set: annotation-type-no-fqn
    - include: any_POP

  annotation-parameters:
    - match: \(
      scope: punctuation.section.parens.begin.java
      set:
        - meta_scope: meta.annotation.parameters.java
        - match: \)
          scope: punctuation.section.parens.end.java
          pop: true
        - match: ({{id}})\s*(=)
          captures:
            1: variable.parameter.java
            2: keyword.operator.assignment.java
          push:
            - match: (?=[,})])
              pop: true
            - include: annotations
            - include: code
        - include: annotation-array-initialization
        - include: annotations
        - include: code
    - include: any_POP

  annotation-array-initialization:
    - match: \{
      scope: punctuation.section.braces.begin.java
      push:
        - meta_scope: meta.braces.annotation-array-initialization.java
        - include: array-initialization-common
        - include: annotations

  anonymous-classes-and-new:
    - match: \bnew\b
      scope: keyword.other.storage.new.java
      push:
        - - meta_scope: meta.instantiation.java
          - include: immediate_POP
        - instantiation

  instantiation:
    - match: \b{{primitives}}\b
      scope: storage.type.primitive.java
      set: array-definition
    - match: '{{before_fqn}}'
      set: [after-object-type-in-instantiation, object-type-fqn]
    - include: object-type-instantiation-no-fqn

  object-type-instantiation-no-fqn:
    - match: '{{classcase_id}}'
      scope: support.class.java
      set: after-object-type-in-instantiation
    - include: any_POP

  after-object-type-in-instantiation:
    - match: (?=\[)
      set: array-definition
    - match: (?=\()
      set: object-construction
    - match: <>
      scope: punctuation.definition.generic.diamond.java
      set: object-construction
    - match: (?=<)
      set: [after-generic-in-instantiation, generic-type-invocation]
    - match: \.
      scope: punctuation.accessor.dot.java
      set: object-type-instantiation-no-fqn
    - include: any_POP

  after-generic-in-instantiation:
    - match: (?=\[)
      set: array-definition
    - include: object-construction

  object-construction:
    - match: \(
      scope: punctuation.section.parens.begin.java
      set:
        - meta_scope: meta.parens.constructor-arguments.java
        - match: \)
          scope: punctuation.section.parens.end.java
          set:
            - match: \{
              scope: punctuation.section.braces.begin.java
              set:
                - meta_scope: meta.class.body.anonymous.java
                - match: \}
                  scope: punctuation.section.braces.end.java
                  pop: true
                - include: class-body
            - include: any_POP
        - include: illegal-parens-terminators
        - include: code
    - include: any_POP

  array-definition:
    - match: \[
      scope: punctuation.section.brackets.begin.java
      set:
        - meta_scope: meta.brackets.array-initialization.java
        - match: \]
          scope: punctuation.section.brackets.end.java
          set:
            - match: (?=\[)
              set: array-definition
            - match: \{
              scope: punctuation.section.braces.begin.java
              set: array-initialization
            - include: any_POP
        - include: code
    - include: any_POP

  array-initialization:
    - meta_scope: meta.braces.array-initialization.java
    - include: array-initialization-common

  array-initialization-common:
    - match: \}
      scope: punctuation.section.braces.end.java
      pop: true
    - match: \{
      scope: punctuation.section.braces.begin.java
      push: array-initialization
    - include: code

  class:
    - match: (?=({{storage_modifiers}}\s+)*(?:class|(?:@)?interface|enum)\b)
      push: [class-meta, class-type]

  class-meta:
    - meta_scope: meta.class.java
    - include: immediate_POP

  class-type:
    - include: storage-modifiers
    - match: (?:class|(\@?)interface)\b
      scope: storage.type.java
      captures:
        1: punctuation.definition.type.java
      set:
        - class-block
        - class-extends
        - generic-type-declaration
        - class-name
    - match: enum\b
      scope: storage.type.java
      set:
        - enum-block
        - class-extends
        - generic-type-declaration
        - class-name
    - include: any_POP

  class-name:
    - meta_scope: meta.class.identifier.java
    - match: (?!extends|implements){{id}}\b
      scope: entity.name.class.java
      pop: true
    - include: any_POP

  class-extends:
    - match: extends\b
      scope: keyword.declaration.extends.java
      push:
        - - meta_scope: meta.class.extends.java
          - match: \,
            scope: punctuation.separator.comma.java
            push: inherited-object-type-reference
          - include: any_POP
        - inherited-object-type-reference
    - match: implements\b
      scope: keyword.declaration.implements.java
      push:
        - - meta_scope: meta.class.implements.java
          - match: \,
            scope: punctuation.separator.comma.java
            push: inherited-object-type-reference
          - include: any_POP
        - inherited-object-type-reference
    - include: any_POP

  class-block:
    - match: \{
      scope: punctuation.section.block.begin.java
      set:
        - meta_scope: meta.class.body.java meta.block.java
        - match: \}
          scope: punctuation.section.block.end.java
          pop: true
        - include: class-body
    - include: any_POP

  class-body:
    - include: class
    - include: annotations
    - include: fields-and-methods
    - include: constants-and-special-vars
    - include: storage-modifiers
    - include: all-types
    - include: static-code-block
    - include: punctuation-separator-comma
    - include: punctuation-terminator-semicolon
    - match: (?=<)
      push: generic-type-declaration

  enum-block:
    - match: \{
      scope: punctuation.section.block.begin.java
      set:
        - meta_scope: meta.class.body.java meta.block.java
        - match: \}
          scope: punctuation.section.block.end.java
          pop: true
        - include: enum-body
    - include: any_POP

  enum-body:
    - match: ^(?=\s*([[:upper:]_][[:upper:][:digit:]_]*|(?!{{primitives}}|{{storage_modifiers}})[[:lower:]_][[:alnum:]_]*)\s*[,;{(])
      push:
        - match: (?=[;}])
          pop: true
        - match: \w+
          scope: constant.other.enum.java
          push:
            - meta_scope: meta.enum.java
            - match: \{
              scope: punctuation.section.block.begin.java
              push:
                - meta_scope: meta.enum.body.java meta.block.java
                - match: \}
                  scope: punctuation.section.block.end.java
                  pop: true
                - include: enum-body
            - include: parens
            - include: any_POP
        - include: punctuation-separator-comma
    - include: class-body

  code:
    - include: constants-and-special-vars
    - include: assignment
    - include: lambdas
    - include: strings
    - include: anonymous-classes-and-new
    - include: keywords-control
    - include: method-invocations
    - include: uppercase-identifiers
    - include: all-types
    - include: keywords
    - include: code-block-include
    - include: parens
  code-block-include:
    - match: \{
      scope: punctuation.section.block.begin.java
      push:
        - meta_scope: meta.block.java
        - match: \}
          scope: punctuation.section.block.end.java
          pop: true
        - include: code-block
  code-block:
    - include: storage-modifiers
    - include: var-type
    - include: code
    - include: annotations
    - include: code-block-include
    - include: stray-parens
  comments:
    - match: /\*\*/
      scope: comment.block.empty.java punctuation.definition.comment.java
    - include: scope:text.html.javadoc
    - include: comments-inline
  comments-inline:
    - match: /\*
      scope: punctuation.definition.comment.java
      push:
        - meta_scope: comment.block.java
        - match: \*/
          scope: punctuation.definition.comment.java
          pop: true
    - match: //
      scope: punctuation.definition.comment.java
      push:
        - meta_scope: comment.line.double-slash.java
        - match: \n
          pop: true
        - match: (?=%>)
          pop: true

  constants-and-special-vars:
    - match: \b(true|false|null)\b
      scope: constant.language.java
    - match: \b(this|super)\b
      scope: variable.language.java
    # hexadecimal floats
    - match: |-
        \b(0[xX])(?x:
          # 0x1., 0x1.1, 0x1.1p1, 0x1.1p-1, 0x1.p1, 0x1.p-1 | 0x1p1
          {{hdigits}} (?: (\.) (?: {{hdigits}}? {{pexponent}}? \b )? | {{pexponent}} \b )
          # 0x.1, 0x.1p1, 0x.1p-1
          | (\.) {{hdigits}} {{pexponent}}? \b
        )
      scope: constant.numeric.float.hexadecimal.java
      captures:
        1: punctuation.definition.numeric.hexadecimal.java
        2: invalid.illegal.numeric.java
        3: invalid.illegal.numeric.java
        4: punctuation.separator.decimal.java
        5: invalid.illegal.numeric.java
        6: invalid.illegal.numeric.java
        7: invalid.illegal.numeric.java
        8: invalid.illegal.numeric.java
        9: invalid.illegal.numeric.java
        10: invalid.illegal.numeric.java
        11: punctuation.separator.decimal.java
        12: invalid.illegal.numeric.java
        13: invalid.illegal.numeric.java
        14: invalid.illegal.numeric.java
        15: invalid.illegal.numeric.java
    # decimal floats
    - match: |-
        (?x:
          \b{{ddigits0}}
          (?:
            # 1., 1.1, 1.1e1, 1.1e-1, 1.e1, 1.e-1, 1.d, 1.1d, 1.1e1d, 1.1e-1d, 1.e1d, 1.e-1d
            (\.) (?: {{ddigits}}? {{eexponent}}? ([dDfF])? \b )?
            # 1e1 1e1d
            | {{eexponent}} ([dDfF])? \b
            # 1d
            | ([dDfF]) \b
          )
          # .1, .1e1, .1e-1
          | (\.) {{ddigits}} {{eexponent}}? ([dDfF])? \b
        )
      scope: constant.numeric.float.decimal.java
      captures:
        1: invalid.illegal.numeric.java
        2: punctuation.separator.decimal.java
        3: invalid.illegal.numeric.java
        4: invalid.illegal.numeric.java
        5: invalid.illegal.numeric.java
        6: invalid.illegal.numeric.java
        7: storage.type.numeric.java
        8: invalid.illegal.numeric.java
        9: invalid.illegal.numeric.java
        10: storage.type.numeric.java
        11: storage.type.numeric.java
        12: punctuation.separator.decimal.java
        13: invalid.illegal.numeric.java
        14: invalid.illegal.numeric.java
        15: invalid.illegal.numeric.java
        16: invalid.illegal.numeric.java
        17: storage.type.numeric.java
    # binary integers
    - match: \b(0[bB])(_*)[01][01_]*?(_*)([lL])?\b
      scope: constant.numeric.integer.binary.java
      captures:
        1: punctuation.definition.numeric.binary.java
        2: invalid.illegal.numeric.java
        3: invalid.illegal.numeric.java
        4: storage.type.numeric.java
    # hexadecimal integers
    - match: \b(0[xX]){{hdigits}}([lL])?\b
      scope: constant.numeric.integer.hexadecimal.java
      captures:
        1: punctuation.definition.numeric.hexadecimal.java
        2: invalid.illegal.numeric.java
        3: invalid.illegal.numeric.java
        4: storage.type.numeric.java
    # octal integers
    - match: \b(0)(?:(_+)|[0-7_]+?(_*)|([\d_]+))([lL])?\b
      scope: constant.numeric.integer.octal.java
      captures:
        1: punctuation.definition.numeric.octal.java
        2: invalid.illegal.numeric.java
        3: invalid.illegal.numeric.java
        4: invalid.illegal.numeric.java
        5: storage.type.numeric.java
    # decimal integers
    - match: \b{{ddigits0}}([lL])?\b
      scope: constant.numeric.integer.decimal.java
      captures:
        1: invalid.illegal.numeric.java
        2: storage.type.numeric.java

  keywords:
    - match: '::'
      scope: punctuation.accessor.double-colon.java
      push:
        - match: '{{id}}'
          scope: variable.function.reference.java
          pop: true
        - include: any_POP
    - match: '\?|:'
      scope: keyword.operator.ternary.java
    - match: \binstanceof\b
      scope: keyword.operator.word.instanceof.java
    - match: (<<|>>>?)
      scope: keyword.operator.bitshift.java
    - match: (==|!=|<=|>=|<>|<|>)
      scope: keyword.operator.comparison.java
    - match: (\-\-|\+\+)
      scope: keyword.operator.increment-decrement.java
    - match: (\-|\+|\*|\/|%)
      scope: keyword.operator.arithmetic.java
    - match: (!|&&|\|\|)
      scope: keyword.operator.logical.java
    - match: (~|\^|&|\|)
      scope: keyword.operator.bitwise.java
    - match: (\.)(class\b)?
      captures:
        1: punctuation.accessor.dot.java
        2: variable.language.java
    - include: punctuation-separator-comma
    - include: punctuation-terminator-semicolon

  keywords-control:
    # exceptions
    - match: \bcatch\b
      scope: keyword.control.exception.catch.java
      push:
        - meta_scope: meta.catch.java
        - match: (?=\()
          set:
            - match: \(
              scope: punctuation.section.parens.begin.java
              set:
                - meta_scope: meta.catch.parameters.java meta.parens.java
                - match: \)
                  scope: punctuation.section.parens.end.java
                  pop: true
                - match: \|
                  scope: punctuation.separator.bar.java
                - include: parameters
        - include: any_POP
    - match: \bfinally\b
      scope: keyword.control.exception.finally.java
    - match: \btry\b
      scope: keyword.control.exception.try.java
      push: declaration-statement-parens
    # flow
    - match: \bassert\b
      scope: keyword.control.flow.assert.java
      push:
        - meta_scope: meta.assertion.java
        - match: (?=;)
          pop: true
        - match: ':'
          scope: punctuation.separator.expressions.java
        - include: code
    - match: \bbreak\b
      scope: keyword.control.flow.break.java
    - match: \bcontinue\b
      scope: keyword.control.flow.continue.java
    - match: \breturn\b
      scope: keyword.control.flow.return.java
    - match: \bthrow\b
      scope: keyword.control.flow.throw.java
    # conditional
    - match: \bif\b
      scope: keyword.control.conditional.if.java
    - match: \belse\b
      scope: keyword.control.conditional.else.java
    - match: \bswitch\b
      scope: keyword.control.conditional.switch.java
    - match: \bcase\b
      scope: keyword.control.conditional.case.java
    - match: \bdefault\b
      scope: keyword.control.conditional.default.java
    # loop
    - match: \bdo\b
      scope: keyword.control.loop.do-while.java
    - match: \bfor\b
      scope: keyword.control.loop.for.java
      push: declaration-statement-parens
    - match: \bwhile\b
      scope: keyword.control.loop.while.java

  illegal-keywords:
    - match: \b(goto|const)\b
      scope: invalid.illegal.keyword.java

  illegal-open-block:
    - match: \s?(?={)
      scope: invalid.illegal.stray-terminator-end
      pop: true

  illegal-semicolon:
    - match: ;
      scope: invalid.illegal.stray-terminator-end
      pop: true

  illegal-parens-terminators:
    # Pops the stack if anything matches
    - include: illegal-semicolon
    - include: illegal-open-block

  method-invocations:
    - match: (\.)\s*(?=<)
      captures:
        1: punctuation.accessor.dot.java
      push: generic-type-invocation
    - match: ({{id}})\s*(\()
      captures:
        1: variable.function.java
        2: punctuation.section.parens.begin.java
      push:
        - meta_scope: meta.function-call.java
        - match: \)
          scope: punctuation.section.parens.end.java
          pop: true
        - include: illegal-parens-terminators
        - include: code

  fields-and-methods:
    - match: \bvoid\b
      scope: storage.type.void.java
      push: method
    - match: (?={{id}}\s*\()
      push: method
    - match: '{{before_fqn}}'
      push: [field-or-method, after-object-and-array-types, object-type-fqn]
    - match: \b{{classcase_id}}
      scope: support.class.java
      push: [field-or-method, after-object-and-array-types]
    - match: \b{{primitives}}\b
      scope: storage.type.primitive.java
      push: [field-or-method, array-brackets]

  field-or-method:
    - match: (?={{id}}\s*\()
      set: method
    - match: (?=\S)
      set:
      - include: before-next-field
      - match: (?:({{uppercase_id}})|({{id}}))
        captures:
          1: entity.name.constant.java
          2: meta.field.java
        push: [static-assignment, array-brackets]
      - include: punctuation-separator-comma
      - include: any_POP

  before-next-field:
    # Prevent style from being removed from whole file when making a new expression
    - match: (?=\b(?:{{storage_modifiers}}|{{primitives}}|void)\b)
      pop: true

  method:
    - meta_scope: meta.method.java
    - match: ({{classcase_id}})\s*(?=\()
      captures:
        1: meta.method.identifier.java entity.name.function.constructor.java
    - match: ({{id}})\s*(?=\()
      captures:
        1: meta.method.identifier.java entity.name.function.java
    - match: \(
      scope: punctuation.section.parens.begin.java
      push:
        - meta_scope: meta.method.parameters.java meta.parens.java
        - match: \)
          scope: punctuation.section.parens.end.java
          pop: true
        - include: parameters
        - match: \S
          scope: invalid.illegal.missing-parameter-end
          pop: true
    - include: throws
    - include: annotation-default
    - match: \{
      scope: punctuation.section.block.begin.java
      set:
        - meta_scope: meta.method.java meta.method.body.java
        - match: \}
          scope: punctuation.section.block.end.java
          pop: true
        - include: code-block
    - include: any_POP

  throws:
    - match: \bthrows\b
      scope: keyword.declaration.throws.java
      push:
      - - meta_scope: meta.method.throws.java
        - match: \,
          scope: punctuation.separator.comma.java
          push: object-type-reference
        - include: any_POP
      - object-type-reference

  # Stand-along uppercase id, either type or constant.
  # Should be used only inside code blocks.
  uppercase-identifiers:
    # Popular JDK classes
    - match: \b(?:UUID|UR[LI])\b
      scope: support.class.java
      push: after-object-type
    # Generic type variable
    - match: \b\p{Lu}\b
      scope: support.class.java
      push: after-object-type
    # Uppercase constants
    - match: \b{{uppercase_id}}
      scope: constant.other.java

  # Stand-alone type, maybe type of the variable or class object reference.
  # Should be used only inside code blocks.
  object-types:
    # Here the match is more complex than 'before_fqn'.
    # In code block we can't simply distinguish package from variable.
    - match: (?=\b(?:{{lowercase_id}}\.)+\p{Lu})
      push: [after-object-type, object-type-fqn]
    - match: \b{{classcase_id}}\b
      scope: support.class.java
      push: after-object-type

  object-type-fqn:
    - meta_scope: meta.path.java
    - include: package
    - match: '{{classcase_id}}'
      scope: support.class.java
      pop: true
    - include: any_POP

  after-object-type:
    - match: (?=<)
      set: [array-brackets, generic-type-invocation]
    - match: \.(?!\.)
      scope: punctuation.accessor.dot.java
      set:
        - match: (?=<)
          set: generic-type-invocation
        - match: (?:(class)\b|({{uppercase_id}}))
          captures:
            1: variable.language.java
            2: constant.other.java
          pop: true
        - match: '{{classcase_id}}'
          scope: support.class.java
          set: after-object-type
        - include: any_POP
    - include: array-brackets

  # Used in 'throws' and generic bounds
  object-type-reference:
    - match: '{{before_fqn}}'
      set:
        - meta_scope: meta.path.java
        - include: package
        - include: object-type-reference-no-fqn
    - include: object-type-reference-no-fqn

  object-type-reference-no-fqn:
    - match: '{{classcase_id}}'
      scope: support.class.java
      set: after-object-type-reference
    - include: any_POP

  after-object-type-reference:
    - match: (?=<)
      set: generic-type-invocation
    - match: \.
      scope: punctuation.accessor.dot.java
      set: object-type-reference-no-fqn
    - include: any_POP

  # Used in method's and generic's parameters
  object-and-array-types:
    - match: '{{before_fqn}}'
      push:
        - meta_scope: meta.path.java
        - include: package
        - include: object-and-array-types-no-fqn
    - match: \b({{primitives}})(?=\s*\[)
      scope: storage.type.primitive.java
      push: array-brackets
    - match: \b{{classcase_id}}
      scope: support.class.java
      push: after-object-and-array-types

  object-and-array-types-no-fqn:
    - match: '{{classcase_id}}'
      scope: support.class.java
      set: after-object-and-array-types
    - include: any_POP

  after-object-and-array-types:
    - match: (?=<)
      set: [array-brackets, generic-type-invocation]
    - match: \.(?!\.)
      scope: punctuation.accessor.dot.java
      set: object-and-array-types-no-fqn
    - include: array-brackets

  # Used in class-level 'extends' and 'implements'
  inherited-object-type-reference:
    - match: '{{before_fqn}}'
      set:
        - meta_scope: meta.path.java
        - match: '{{lowercase_id}}'
          scope: entity.other.inherited-class.package.java
        - include: punctuation-accessor-dot
        - include: inherited-object-type-reference-no-fqn
    - include: inherited-object-type-reference-no-fqn

  inherited-object-type-reference-no-fqn:
    - match: (?!class|extends|implements|interface){{id}}
      scope: entity.other.inherited-class.java
      set: after-inherited-object-type-reference
    - include: any_POP

  after-inherited-object-type-reference:
    - match: (?=<)
      set: generic-type-invocation
    - match: \.
      scope: punctuation.accessor.dot.java
      set: inherited-object-type-reference-no-fqn
    - include: any_POP

  generic-type-declaration:
    - match: <
      scope: punctuation.definition.generic.begin.java
      push: generic-type-parameter
    - include: any_POP

  generic-type-terminator:
    - include: illegal-semicolon
    # These characters can't appear in a generic. If we've matched
    # them then someone forgot to close it.
    - match: (?=[{}()])
      pop: true
    - match: '>'
      scope: punctuation.definition.generic.end.java
      pop: true

  generic-type-parameter:
    - meta_scope: meta.generic.declaration.java
    - match: \b{{id}}\b
      scope: variable.parameter.type.java
      push: generic-type-bounds
    - include: generic-type-terminator

  generic-type-bounds:
    - match: (,)|(?=>)
      captures:
        1: punctuation.separator.comma.java
      pop: true
    - match: \bextends\b
      scope: keyword.declaration.extends.java
      push: [generic-type-extends-multiple-bounds, object-type-reference]
    - match: \bsuper\b
      scope: keyword.declaration.super.java
      push: object-type-reference

  generic-type-extends-multiple-bounds:
    - match: '&'
      scope: keyword.operator.multiple-bounds.java
      set: [generic-type-extends-multiple-bounds, object-type-reference]
    - include: any_POP

  generic-type-invocation:
    - match: <
      scope: punctuation.definition.generic.begin.java
      set: generic-type-argument
    - include: any_POP

  generic-type-argument:
    - meta_scope: meta.generic.java
    - match: \?
      scope: keyword.operator.wildcard.java
      push: generic-type-bounds
    - include: generic-type-terminator
    - include: object-and-array-types
    - include: punctuation-separator-comma

  annotation-default:
    - match: \bdefault\b
      scope: keyword.declaration.default.java
      push:
        - meta_scope: meta.annotation.default.java
        - match: (?=;)
          pop: true
        - include: code

  parameters:
    - match: \bfinal\b
      scope: storage.modifier.java
    - include: annotations
    - include: primitive-types
    - include: object-and-array-types
    - match: \.\.\.
      scope: keyword.operator.variadic.java
    - match: '{{id}}'
      scope: variable.parameter.java
      push: array-brackets
    - include: punctuation-separator-comma

  lambdas:
    - match: (?={{lambda_lookahead}})
      push: lambda-params

  lambda-params:
    - meta_scope: meta.function.anonymous.parameters.java
    - match: \(
      scope: punctuation.section.parens.begin.java
      set:
        - meta_scope: meta.function.anonymous.parameters.java
        - match: \)
          scope: punctuation.section.parens.end.java
          set: lambda-arrow
        - include: parameters
    - match: '{{id}}'
      scope: variable.parameter.java
      set: lambda-arrow

  lambda-arrow:
    - match: ->
      scope: storage.type.function.anonymous.java
      set:
        - meta_scope: meta.function.anonymous.body.java
        - match: (?=[)};])
          pop: true
        - include: code

  parens:
    - match: \(
      scope: punctuation.section.parens.begin.java
      push:
        - meta_scope: meta.parens.java
        - match: \)
          scope: punctuation.section.parens.end.java
          pop: true
        - include: illegal-parens-terminators
        - include: code

  declaration-statement-parens:
    - match: \(
      scope: punctuation.section.parens.begin.java
      set:
        - meta_scope: meta.parens.java
        - match: \)
          scope: punctuation.section.parens.end.java
          pop: true
        - include: illegal-open-block
        - include: code-block
    - include: any_POP

  primitive-types:
    - match: \b{{primitives}}\b
      scope: storage.type.primitive.java
      push: array-brackets

  var-type:
    - match: \bvar\b
      scope: storage.type.var.java

  array-brackets:
    - match: \[\s*\]
      scope: storage.modifier.array.java
    - include: any_POP

  static-assignment:
    - match: \=
      scope: keyword.operator.assignment.java
      set:
        - meta_scope: meta.assignment.rhs.java
        - match: (?=[,;])
          pop: true
        - include: before-next-field
        - include: code
        - include: stray-parens
    - include: any_POP

  assignment:
    - match: ([|&^*/+-]\=|\=(?!=))
      scope: keyword.operator.assignment.java
      push:
        - meta_scope: meta.assignment.rhs.java
        - match: (?=;|\)|\}|,)
          pop: true
        - include: code
  static-code-block:
    - match: \{
      scope: punctuation.section.block.begin.java
      push:
        - meta_scope: meta.static.body.java
        - match: \}
          scope: punctuation.section.block.end.java
          pop: true
        - include: code-block
  storage-modifiers:
    - match: \b{{storage_modifiers}}\b
      scope: storage.modifier.java
  stray-braces:
    - match: \}
      scope: invalid.illegal.stray-brace-end
  stray-parens:
    - match: \)
      scope: invalid.illegal.stray-parens-end

  strings:
    - match: \"
      scope: punctuation.definition.string.begin.java
      push:
        - meta_include_prototype: false
        - meta_scope: string.quoted.double.java
        - match: \"
          scope: punctuation.definition.string.end.java
          pop: true
        - include: strings-common
    - match: \'
      scope: punctuation.definition.string.begin.java
      push:
        - meta_include_prototype: false
        - meta_scope: string.quoted.single.java
        - match: \'
          scope: punctuation.definition.string.end.java
          pop: true
        - include: strings-common

  strings-common:
    - match: \n
      scope: invalid.illegal.newline.java
      pop: true
    - match: \\.
      scope: constant.character.escape.java

  module:
    - match: (?=\b(?:open\s+)?module\b)
      push:
      - - meta_scope: meta.module.java
        - include: immediate_POP
      - - match: \bopen\b
          scope: storage.modifier.java
        - match: \bmodule\b
          scope: storage.type.java
          set: [module-body, module-identifier-scope, module-identifier]

  module-identifier-scope:
    - meta_scope: meta.module.identifier.java
    - include: immediate_POP

  module-identifier:
    - match: '{{id}}'
      set:
        - - meta_scope: entity.name.module.java
          - include: immediate_POP
        - dot-separated-identifier
    - include: any_POP

  module-body:
    - match: \{
      scope: punctuation.section.braces.begin.java
      set:
        - meta_scope: meta.module.body.java
        - include: module-body-content
        - match: \}
          scope: punctuation.section.braces.end.java
          pop: true
    - include: any_POP

  module-body-content:
    - match: \bexports\b
      scope: keyword.other.module.exports.java
      push: [exports-statement-scope, exports-or-opens-statement]
    - match: \bopens\b
      scope: keyword.other.module.opens.java
      push: [opens-statement-scope, exports-or-opens-statement]
    - match: \brequires\b
      scope: keyword.other.module.requires.java
      push: requires-statement
    - match: \buses\b
      scope: keyword.other.module.uses.java
      push: [uses-statement-scope, object-type-reference]
    - match: \bprovides\b
      scope: keyword.other.module.provides.java
      push: [provides-statement-scope, provides-with-statement, object-type-reference]
    - include: punctuation-terminator-semicolon

  # Should always come before module/package patterns
  module-statement-terminator:
    - match: (?=[;\}])
      pop: true
    - match: (?=\b(?:requires|exports|uses|provides|opens)\b)
      pop: true

  support-type-module:
    - match: '{{id}}'
      push:
        - - meta_scope: support.type.module.java
          - include: immediate_POP
        - dot-separated-identifier

  exports-statement-scope:
    - meta_scope: meta.exports.java
    - include: immediate_POP

  opens-statement-scope:
    - meta_scope: meta.opens.java
    - include: immediate_POP

  exports-or-opens-statement:
    - match: \bto\b
      scope: keyword.other.module.to.java
      set:
        - include: module-statement-terminator
        - include: support-type-module
        - include: punctuation-separator-comma
    - include: module-statement-terminator
    - match: '{{id}}'
      push:
        - - meta_scope: support.type.package.java
          - include: immediate_POP
        - dot-separated-identifier

  requires-statement:
    - meta_scope: meta.requires.java
    - match: \btransitive\b
      scope: keyword.other.module.transitive.java
    - include: module-statement-terminator
    - include: support-type-module

  uses-statement-scope:
    - meta_scope: meta.uses.java
    - include: immediate_POP

  provides-statement-scope:
    - meta_scope: meta.provides.java
    - include: immediate_POP

  provides-with-statement:
    - match: \bwith\b
      scope: keyword.other.module.with.java
      set:
        - - match: \,
            scope: punctuation.separator.comma.java
            push: object-type-reference
          - include: any_POP
        - object-type-reference
    - include: any_POP