Introduction to groovy grammar

        In the process of learning gradle, I found that although the script of this thing can be understood, but I don't know why, many grammars are inexplicable. I want to come, I want to learn this gradle only with java, so I remembered that gradle is a groovy As a construction tool based on java applications, it is obvious that many things in gradle come from groovy. Well, it seems that we can't escape. Gradle is put on hold for the time being. Let's learn about groovy first, and then study gradle. .

        First of all, Baidu popularize the knowledge of groovy:

         Groovy is an agile development language based on JVM ( Java Virtual Machine ), which combines many powerful features of Python , Ruby and Smalltalk . Groovy code can be well combined with Java code and can also be used to extend existing code. Due to its nature of running on the JVM, Groovy can use libraries written in other Java languages.

         Groovy is an agile dynamic language for the Java Virtual Machine , a full-fledged object -oriented programming language that can be used both for object-oriented programming and as a pure scripting language . Using this language does not have to write too much code, but also has closures and other features in dynamic languages.

         Groovy is an alternative language to the JVM (alternative means that you can use Groovy for Java programming on the Java platform), and it is used in basically the same way as using Java code. The language is particularly suitable for use with Spring 's dynamic language support. With Java integration in mind, this makes it easy for Groovy to interoperate with Java code. (Note: Not referring to Groovy replacing java, but referring to the good combination of Groovy and java programming.

        It seems that groovy is still very powerful, ok, now start learning groovy grammar.


Syntax

This chapter covers the syntax of the Groovy programming language.The grammar of the language derives from the Java grammar,but enhances it with specific constructs for Groovy, and allows certain simplifications.

This article describes the syntax of the groovy programming language. The syntax of this language is derived from the Java syntax, but enhances the concrete build of groovy and allows a certain degree of simplification.


1. Comments

1.1. Single line comment

Single line comments start with // and can be found at any position in the line.The characters following //, till the end of the line, are considered part of the comment.

Single-line comments begin with // and can be placed anywhere on the line. The characters after // until the end of the line are part of the comment.

// a standalone single line comment
println "hello" // a comment till the end of the line

1.2. Multiline comment

A multiline comment starts with /* and can be found at any position in the line.The characters following /* will be considered part of the comment, including new line characters,up to the first */ closing the comment.Multiline comments can thus be put at the end of a statement, or even inside a statement.

Multi-line comments start with /* and can be placed anywhere on a line. Characters after /*/ above will be considered part of the comment, including new line characters (as in java, in between). Therefore, multi-line comments can be placed after a statement, or even inside a statement

/* a standalone multiline comment
   spanning two lines */
println "hello" /* a multiline comment starting
                   at the end of a statement */
println 1 /* one */ + 2 /* two */

1.3. GroovyDoc comment

Similarly to multiline comments, GroovyDoc comments are multiline, but start with /** and end with */.Lines following the first GroovyDoc comment line can optionally start with a star *.Those comments are associated with:

  • type definitions (classes, interfaces, enums, annotations),

  • fields and properties definitions

  • methods definitions

Although the compiler will not complain about GroovyDoc comments not being associated with the above language elements,you should prepend those constructs with the comment right before it.

Similar to multiline comments, groovy documentation comments are multiline, but do start with /** and end with */. After the first groovy documentation comment line, optionally start with a *. Those annotations are used to:

  • Type definitions (classes, interfaces, enums, annotations),

  • Variable and property definitions

  • method is set at

Although the compiler won't care that the language above is not linked to the comment, you should preemptively add those language constructs to the right of the comment before that.


/**
 * A Class description
 */
class Person {
    /** the name of the person */
    String name

    /**
     * Creates a greeting method for a certain person.
     *
     * @param otherPerson the person to greet
     * @return a greeting message
     */
    String greet(String otherPerson) {
       "Hello ${otherPerson}"
    }
}

GroovyDoc follows the same conventions as Java’s own JavaDoc.So you’ll be able to use the same tags as with JavaDoc.

Groovy documentation follows the same constraints as java, so you can use the same markup as java.


1.4. Shebang line

Beside the single line comment, there is a special line comment, often called the shebang line understood by UNIX systems which allows scripts to be run directly from the command-line, provided you have installed the Groovy distributionand the groovy command is available on the PATH.

In contrast to single-line comments, there is a special line comment, commonly called a shebang on UNIX systems, that allows scripts to be run directly from the command line. The premise is that you have installed groovy and the command path of groovy is available.

#!/usr/bin/env groovy
println "Hello from the shebang line"
  The # character must be the first character of the file. Any indentation would yield a compilation error.
The # character must be the first character of the file, any indentation will result in a compilation error.


2. Keywords

The following list represents all the keywords of the Groovy language:

Here are all the groovy keywords listed:

Table 1. Keywords

as

assert

break

case

catch

class

const

continue

def

default

do

else

enum

extends

false

finally

for

goto

if

implements

import

in

instanceof

interface

new

null

package

return

super

switch

this

throw

throws

trait

true

try

while

   


3. Identifiers (identifiers, including method names and variable names and constant names, etc. )

3.1. Normal identifiers

Identifiers start with a letter, a dollar or an underscore.They cannot start with a number.

Identifiers can start with a letter, $ (dollar sign), or underscore. They cannot start with a number

A letter can be in the following ranges:

Letters can be in the following ranges:

  • 'a' to 'z' (lowercase ascii letter)

  • 'A' to 'Z' (uppercase ascii letter)

  • '\u00C0' to '\u00D6'

  • '\u00D8' to '\u00F6'

  • '\u00F8' to '\u00FF'

  • '\u0100' to '\uFFFE'

Then following characters can contain letters and numbers.

In addition to the first letter, subsequent characters can contain letters and numbers.

Here are a few examples of valid identifiers (here, variable names):

Here are some examples of legal identifiers (legal variable names):

def name
def item3
def with_underscore
def $dollarStart

But the following ones are invalid identifiers:

But some of the following are invalid identifiers:

def 3tier
def a+b
def a#b
All keywords are also valid identifiers when following a dot:

All keywords are also valid identifiers when they appear after a dot:

foo.as
foo.assert
foo.break
foo.case
foo.catch

3.2. Quoted identifiers

Quoted identifiers appear after the dot of a dotted expression.For instance, the name part of the person.name expression can be quoted with person."name" or person.'name'.This is particularly interesting when certain identifiers contain illegal characters that are forbidden by the Java Language Specification,but which are allowed by Groovy when quoted. For example, characters like a dash, a space, an exclamation mark, etc.

Quoted identifiers appear after the dot in a dotted expression, eg, the name part of a person.name expression, can be quoted to become person."name" or person.'name'. It is important to note that some characters that are explicitly specified in the Java language are illegal, but are allowed in groovy by adding quotation marks. For example, things like dash, space, exclamation mark, etc.


def map = [:]

map."an identifier with a space and double quotes" = "ALLOWED"
map.'with-dash-signs-and-single-quotes' = "ALLOWED"

assert map."an identifier with a space and double quotes" == "ALLOWED"
assert map.'with-dash-signs-and-single-quotes' == "ALLOWED"

As we shall see in the following section on strings, Groovy provides different string literals.All kind of strings are actually allowed after the dot:

We will see strings in the following chapters, groovy provides different strings. All strings are allowed after a dot:

map.'single quote'
map."double quote"
map.'''triple single quote'''
map."""triple double quote"""
map./slashy string/
map.$/dollar slashy string/$
There's a difference between plain character strings and Groovy's GStrings (interpolated strings), as in that the latter case, the interpolated values ​​are inserted in the final string for evaluating the whole identifier:
this is a plain character string and Groovy's GString (interpolated strings ) string) strings, as in the latter case inserting a changed value into the unchanged string to determine the value of the entire identifier:

def firstname = "Homer"
map."Simson-${firstname}" = "Homer Simson"

assert map.'Simson-Homer' == "Homer Simson"
To be continued...




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325490680&siteId=291194637