painless-syntax.asciidoc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. [[modules-scripting-painless-syntax]]
  2. === Painless Syntax
  3. experimental[The Painless scripting language is new and is still marked as experimental. The syntax or API may be changed in the future in non-backwards compatible ways if required.]
  4. [float]
  5. [[painless-types]]
  6. === Variable types
  7. Painless supports all of https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html[Java's types],
  8. including array types, but adds some additional built-in types.
  9. [float]
  10. [[painless-def]]
  11. ==== Def
  12. The dynamic type `def` serves as a placeholder for any other type. It adopts the behavior
  13. of whatever runtime type it represents.
  14. [float]
  15. [[painless-strings]]
  16. ==== String
  17. String constants can be declared with single quotes, to avoid escaping horrors with JSON:
  18. [source,painless]
  19. ---------------------------------------------------------
  20. def mystring = 'foo';
  21. ---------------------------------------------------------
  22. [float]
  23. [[painless-arrays]]
  24. ==== Arrays
  25. Arrays can be subscripted starting from `0` for traditional array access or with
  26. negative numbers to starting from the back of the array. So the following
  27. returns `2`.
  28. [source,painless]
  29. ---------------------------------------------------------
  30. int[] x = new int[5];
  31. x[0]++;
  32. x[-5]++;
  33. return x[0];
  34. ---------------------------------------------------------
  35. [float]
  36. [[painless-lists]]
  37. ==== List
  38. Lists can be created explicitly (e.g. `new ArrayList()`) or initialized similar to Groovy:
  39. [source,painless]
  40. ---------------------------------------------------------
  41. def list = [1,2,3];
  42. ---------------------------------------------------------
  43. Lists can also be accessed similar to arrays. They support `.length` and
  44. subscripts, including negative subscripts to read from the back of the list:
  45. [source,painless]
  46. ---------------------------------------------------------
  47. def list = [1,2,3];
  48. list[-1] = 5
  49. return list[0]
  50. ---------------------------------------------------------
  51. [float]
  52. [[painless-maps]]
  53. ==== Map
  54. Maps can be created explicitly (e.g. `new HashMap()`) or initialized similar to Groovy:
  55. [source,painless]
  56. ---------------------------------------------------------
  57. def person = ['name': 'Joe', 'age': 63];
  58. ---------------------------------------------------------
  59. Map keys can also be accessed as properties.
  60. [source,painless]
  61. ---------------------------------------------------------
  62. def person = ['name': 'Joe', 'age': 63];
  63. person.retired = true;
  64. return person.name
  65. ---------------------------------------------------------
  66. Map keys can also be accessed via subscript (for keys containing special characters):
  67. [source,painless]
  68. ---------------------------------------------------------
  69. return map['something-absurd!']
  70. ---------------------------------------------------------
  71. [float]
  72. [[painless-pattern]]
  73. ==== Pattern
  74. Regular expression constants are directly supported:
  75. [source,painless]
  76. ---------------------------------------------------------
  77. Pattern p = /[aeiou]/
  78. ---------------------------------------------------------
  79. Patterns can only be created via this mechanism. This ensures fast performance, regular expressions
  80. are always constants and compiled efficiently a single time.
  81. [float]
  82. [[modules-scripting-painless-regex-flags]]
  83. ==== Pattern flags
  84. You can define flags on patterns in Painless by adding characters after the
  85. trailing `/` like `/foo/i` or `/foo \w #comment/iUx`. Painless exposes all the
  86. flags from
  87. https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html[Java's Pattern class]
  88. using these characters:
  89. [cols="<,<,<",options="header",]
  90. |=======================================================================
  91. | Character | Java Constant | Example
  92. |`c` | CANON_EQ | `'å' ==~ /å/c` (open in hex editor to see)
  93. |`i` | CASE_INSENSITIVE | `'A' ==~ /a/i`
  94. |`l` | LITERAL | `'[a]' ==~ /[a]/l`
  95. |`m` | MULTILINE | `'a\nb\nc' =~ /^b$/m`
  96. |`s` | DOTALL (aka single line) | `'a\nb\nc' =~ /.b./s`
  97. |`U` | UNICODE_CHARACTER_CLASS | `'Ɛ' ==~ /\\w/U`
  98. |`u` | UNICODE_CASE | `'Ɛ' ==~ /ɛ/iu`
  99. |`x` | COMMENTS (aka extended) | `'a' ==~ /a #comment/x`
  100. |=======================================================================
  101. [float]
  102. [[painless-operators]]
  103. === Operators
  104. All of Java's https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html[operators] are
  105. supported with the same precedence, promotion, and semantics.
  106. There are only a few minor differences and add-ons:
  107. * `==` behaves as Java's for numeric types, but for non-numeric types acts as https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-[`Object.equals()`]
  108. * `===` and `!==` support exact reference comparison (e.g. `x === y`)
  109. * `=~` true if a portion of the text matches a pattern (e.g. `x =~ /b/`)
  110. * `==~` true if the entire text matches a pattern (e.g. `x ==~ /[Bb]ob/`)
  111. [float]
  112. [[painless-control-flow]]
  113. === Control flow
  114. Java's https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html[control flow statements] are supported, with the exception
  115. of the `switch` statement.
  116. In addition to Java's `enhanced for` loop, the `for in` syntax from groovy can also be used:
  117. [source,painless]
  118. ---------------------------------------------------------
  119. for (item : list) {
  120. ...
  121. }
  122. ---------------------------------------------------------
  123. [float]
  124. [[painless-functions]]
  125. === Functions
  126. Functions can be declared at the beginning of the script, for example:
  127. [source,painless]
  128. ---------------------------------------------------------
  129. boolean isNegative(def x) { x < 0 }
  130. ...
  131. if (isNegative(someVar)) {
  132. ...
  133. }
  134. ---------------------------------------------------------
  135. [float]
  136. [[painless-lambda-expressions]]
  137. === Lambda expressions
  138. Lambda expressions and method references work the same as https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html[Java's].
  139. [source,painless]
  140. ---------------------------------------------------------
  141. list.removeIf(item -> item == 2);
  142. list.removeIf((int item) -> item == 2);
  143. list.removeIf((int item) -> { item == 2 });
  144. list.sort((x, y) -> x - y);
  145. list.sort(Integer::compare);
  146. ---------------------------------------------------------
  147. Method references to functions within the script can be accomplished using `this`, e.g. `list.sort(this::mycompare)`.