Skip to content

Commit

Permalink
Painless: Types Section Clean Up (#30283)
Browse files Browse the repository at this point in the history
Clean up of types section, casting section, and a large number of examples.
  • Loading branch information
jdconrad committed May 23, 2018
1 parent 07e830e commit b905ef1
Show file tree
Hide file tree
Showing 9 changed files with 1,032 additions and 459 deletions.
550 changes: 417 additions & 133 deletions docs/painless/painless-casting.asciidoc

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions docs/painless/painless-comments.asciidoc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[[painless-comments]]
=== Comments

Use the `//` token anywhere on a line to specify a single-line comment. All
characters from the `//` token to the end of the line are ignored. Use an
opening `/*` token and a closing `*/` token to specify a multi-line comment.
Multi-line comments can start anywhere on a line, and all characters in between
the `/*` token and `*/` token are ignored. Comments can be included anywhere
within a script.
Use a comment to annotate or explain code within a script. Use the `//` token
anywhere on a line to specify a single-line comment. All characters from the
`//` token to the end of the line are ignored. Use an opening `/*` token and a
closing `*/` token to specify a multi-line comment. Multi-line comments can
start anywhere on a line, and all characters in between the `/*` token and `*/`
token are ignored. Comments can be included anywhere within a script.

*Grammar*
[source,ANTLR4]
Expand Down
8 changes: 4 additions & 4 deletions docs/painless/painless-identifiers.asciidoc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[[painless-identifiers]]
=== Identifiers

Specify identifiers to <<declaration, declare>>, <<assignment, assign>>, and
<<painless-operators, use>> variables, <<dot-operator, access fields>>, and
<<dot-operator, call methods>>. <<painless-keywords, Keywords>> and
<<painless-types, types>> cannot be used as identifiers.
Use an identifier as a named token to specify a
<<painless-variables, variable>>, <<painless-types, type>>,
<<dot-operator, field>>, <<dot-operator, method>>, or function.
<<painless-keywords, Keywords>> cannot be used as identifiers.

*Grammar*
[source,ANTLR4]
Expand Down
6 changes: 3 additions & 3 deletions docs/painless/painless-keywords.asciidoc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[[painless-keywords]]
=== Keywords

The keywords in the table below are reserved for built-in language
features. These keywords cannot be used as
<<painless-identifiers, identifiers>> or <<painless-types, types>>.
Keywords are reserved tokens for built-in language features and cannot be used
as <<painless-identifiers, identifiers>> within a script. The following are
keywords:

[cols="^1,^1,^1,^1,^1"]
|====
Expand Down
2 changes: 1 addition & 1 deletion docs/painless/painless-lang-spec.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Painless syntax is similar to Java syntax along with some additional
features such as dynamic typing, Map and List accessor shortcuts, and array
initializers. As a direct comparison to Java, there are some important
differences, especially related to the casting model. For more detailed
conceptual information about the basic constructs that Java and Painless share,
conceptual information about the basic constructs that Painless and Java share,
refer to the corresponding topics in the
https://docs.oracle.com/javase/specs/jls/se8/html/index.html[Java Language
Specification].
Expand Down
52 changes: 16 additions & 36 deletions docs/painless/painless-literals.asciidoc
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
[[painless-literals]]
=== Literals

Use literals to specify different types of values directly in a script.
Use a literal to specify a value directly in an
<<painless-operators, operation>>.

[[integers]]
==== Integers

Use integer literals to specify an integer value in decimal, octal, or hex
notation of the <<primitive-types, primitive types>> `int`, `long`, `float`,
Use an integer literal to specify an integer type value in decimal, octal, or
hex notation of a <<primitive-types, primitive type>> `int`, `long`, `float`,
or `double`. Use the following single letter designations to specify the
<<primitive-types, primitive type>>: `l` or `L` for `long`, `f` or `F` for
`float`, and `d` or `D` for `double`. If not specified, the type defaults to
`int`. Use `0` as a prefix to specify an integer literal as octal, and use
`0x` or `0X` as a prefix to specify an integer literal as hex.
primitive type: `l` or `L` for `long`, `f` or `F` for `float`, and `d` or `D`
for `double`. If not specified, the type defaults to `int`. Use `0` as a prefix
to specify an integer literal as octal, and use `0x` or `0X` as a prefix to
specify an integer literal as hex.

*Grammar*
[source,ANTLR4]
Expand Down Expand Up @@ -46,11 +47,10 @@ HEX: '-'? '0' [xX] [0-9a-fA-F]+ [lL]?;
[[floats]]
==== Floats

Use floating point literals to specify a floating point value of the
<<primitive-types, primitive types>> `float` or `double`. Use the following
single letter designations to specify the <<primitive-types, primitive type>>:
`f` or `F` for `float` and `d` or `D` for `double`. If not specified, the type defaults
to `double`.
Use a floating point literal to specify a floating point type value of a
<<primitive-types, primitive type>> `float` or `double`. Use the following
single letter designations to specify the primitive type: `f` or `F` for `float`
and `d` or `D` for `double`. If not specified, the type defaults to `double`.

*Grammar*
[source,ANTLR4]
Expand Down Expand Up @@ -81,7 +81,7 @@ EXPONENT: ( [eE] [+\-]? [0-9]+ );
[[strings]]
==== Strings

Use string literals to specify <<string-type, String>> values with
Use a string literal to specify a <<string-type, `String` type>> value with
either single-quotes or double-quotes. Use a `\"` token to include a
double-quote as part of a double-quoted string literal. Use a `\'` token to
include a single-quote as part of a single-quoted string literal. Use a `\\`
Expand Down Expand Up @@ -117,26 +117,6 @@ STRING: ( '"' ( '\\"' | '\\\\' | ~[\\"] )*? '"' )
[[characters]]
==== Characters

Use the <<painless-casting, casting operator>> to convert string literals or
<<string-type, String>> values into <<primitive-types, char>> values.
<<string-type, String>> values converted into
<<primitive-types, char>> values must be exactly one character in length
or an error will occur.

*Examples*

* Casting string literals into <<primitive-types, char>> values.
+
[source,Painless]
----
(char)"C"
(char)'c'
----
+
* Casting a <<string-type, String>> value into a <<primitive-types, char>> value.
+
[source,Painless]
----
String s = "s";
char c = (char)s;
----
A character literal cannot be specified directly. Instead, use the
<<string-character-casting, cast operator>> to convert a `String` type value
into a `char` type value.
47 changes: 45 additions & 2 deletions docs/painless/painless-operators.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ operator. See Function Calls [MARK] for more information.
The brackets operator `[]` is used to create and access arrays, lists, and maps.
The braces operator `{}` is used to intialize arrays.

[[array-initialization]]
===== Creating and Initializing Arrays

You create and initialize arrays using the brackets `[]` and braces `{}`
Expand All @@ -248,9 +249,49 @@ initialize each dimension with are specified as a comma-separated list enclosed
in braces. For example, `new int[] {1, 2, 3}` creates a one dimensional `int`
array with a size of 3 and the values 1, 2, and 3.

For more information about allocating and initializing arrays, see <<array-type,
Array Type>>.
To allocate an array, you use the `new` keyword followed by the type and a
set of brackets for each dimension. You can explicitly define the size of each dimension by specifying an expression within the brackets, or initialize each
dimension with the desired number of values. The allocated size of each
dimension is its permanent size.

To initialize an array, specify the values you want to initialize
each dimension with as a comma-separated list of expressions enclosed in braces.
For example, `new int[] {1, 2, 3}` creates a one-dimensional `int` array with a
size of 3 and the values 1, 2, and 3.

When you initialize an array, the order of the expressions is maintained. Each expression used as part of the initialization is converted to the
array's type. An error occurs if the types do not match.

*Grammar:*
[source,ANTLR4]
----
declare_array: TYPE ('[' ']')+;
array_initialization: 'new' TYPE '[' ']' '{' expression (',' expression) '}'
| 'new' TYPE '[' ']' '{' '}';
----

*Examples:*
[source,Java]
----
int[] x = new int[5]; // Declare int array x and assign it a newly
// allocated int array with a size of 5
def[][] y = new def[5][5]; // Declare the 2-dimensional def array y and
// assign it a newly allocated 2-dimensional
// array where both dimensions have a size of 5
int[] x = new int[] {1, 2, 3}; // Declare int array x and set it to an int
// array with values 1, 2, 3 and a size of 3
int i = 1;
long l = 2L;
float f = 3.0F;
double d = 4.0;
String s = "5";
def[] da = new def[] {i, l, f*d, s}; // Declare def array da and set it to
// a def array with a size of 4 and the
// values i, l, f*d, and s
----

[[array-access]]
===== Accessing Array Elements

Elements in an array are stored and accessed using the brackets `[]` operator.
Expand Down Expand Up @@ -298,6 +339,7 @@ return d[z]; // Access the 1st element of array d using the
NOTE: The use of the `def` type in the second example means that the types
cannot be resolved until runtime.

[[array-length]]
===== Array Length

Arrays contain a special member known as 'length' that is a read-only value that contains the size of the array. This member can be accessed from an array using the dot operator.
Expand Down Expand Up @@ -727,6 +769,7 @@ def e; // declares the def variable e
e = new HashMap(m); // sets e to a newly allocated HashMap using the constructor with a single argument m
----

[[new-array]]
==== New Array

An array type instance can be allocated using the new operator. The format starts with the new operator followed by the type followed by a series of opening and closing braces each containing an expression for the size of the dimension.
Expand Down
Loading

0 comments on commit b905ef1

Please sign in to comment.