Skip to content

Commit 0645746

Browse files
committed
Apply suggestions from review
Signed-off-by: Ben Sherman <[email protected]>
1 parent 505c98e commit 0645746

File tree

1 file changed

+59
-46
lines changed

1 file changed

+59
-46
lines changed

docs/reference/syntax.md

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ This page provides a comprehensive description of the Nextflow language.
66

77
## Comments
88

9-
Nextflow uses Java-style comments: `//` for a line comment, and `/* ... */` for a block comment:
9+
A line comment starts with `//` and includes the rest of the line.
1010

1111
```groovy
1212
println 'Hello world!' // line comment
13+
```
14+
15+
A block comment starts with `/*` and includes all subsequent characters up to the first `*/`.
1316

17+
```groovy
1418
/*
1519
* block comment
1620
*/
@@ -31,11 +35,9 @@ A Nextflow script may contain the following top-level declarations:
3135
- Enum types
3236
- Output block
3337

34-
These declarations are in turn composed of statements and expressions.
38+
Script declarations are in turn composed of statements and expressions.
3539

36-
Alternatively, a script may contain one or more [statements](#statements), as long as there are no top-level declarations. In this case, the entire script will be treated as an entry workflow.
37-
38-
For example, the following script:
40+
A script may contain one or more [statements](#statements), if there are no top-level declarations. In this case, the entire script will be treated as an entry workflow. For example:
3941

4042
```groovy
4143
println 'Hello world!'
@@ -50,7 +52,7 @@ workflow {
5052
```
5153

5254
:::{warning}
53-
Top-level declarations and statements can not be mixed at the same level. If your script has top-level declarations, all statements must be contained within top-level declarations such as the entry workflow.
55+
Statements and top-level declarations can not be mixed at the same level. If your script has top-level declarations, all statements must be contained within top-level declarations such as the entry workflow.
5456
:::
5557

5658
### Shebang
@@ -74,23 +76,27 @@ nextflow.preview.topic = true
7476
An include declaration consists of an *include source* and one or more *include clauses*:
7577

7678
```groovy
77-
include { foo ; bar as baz } from './some/module'
79+
include { foo as bar } from './some/module'
7880
```
7981

80-
The include source should be a string literal and should refer to either a local path (e.g. `./module.nf`) or a plugin (e.g. `plugin/nf-hello`).
82+
The include source should be a string literal and should refer to either a local path (e.g. `./module.nf`) or a plugin (e.g. `plugin/nf-hello`). Each include clause should specify a name, and may also specify an *alias*. In the example above, `foo` is included under the alias `bar`.
8183

82-
Each include clause should specify a name, and may also specify an *alias*. In the example above, `bar` is included under the alias `baz`.
83-
84-
Include clauses can be separated by newlines or semi-colons, or they can be specified as separate includes:
84+
Include clauses can be separated by semi-colons or newlines:
8585

8686
```groovy
87+
// semi-colons
88+
include { foo ; bar as baz } from './some/module'
89+
8790
// newlines
8891
include {
8992
foo
9093
bar as baz
9194
} from './some/module'
95+
```
96+
97+
Include clauses can also be specified as separate includes:
9298

93-
// separate includes
99+
```groovy
94100
include { foo } from './some/module'
95101
include { bar as baz } from './some/module'
96102
```
@@ -115,7 +121,9 @@ Parameters supplied via command line options, params files, and config files tak
115121

116122
### Workflow
117123

118-
A workflow consists of a name and a body. The workflow body consists of a *main* section, with additional sections for *takes*, *emits*, and *publishers* (shown later):
124+
A workflow can be a *named workflow* or an *entry workflow*.
125+
126+
A *named workflow* consists of a name and a body, and may consist of a *take*, *main*, *emit*, and *publish* section:
119127

120128
```groovy
121129
workflow greet {
@@ -130,15 +138,18 @@ workflow greet {
130138
}
131139
```
132140

133-
- The take, emit, and publish sections are optional. If they are not specified, the `main:` section label can be omitted.
141+
- The take, emit, and publish sections are optional. The `main:` section label can be omitted if they are not specified.
134142

135143
- The take section consists of one or more parameters.
136144

137145
- The main section consists of one or more [statements](#statements).
138146

139147
- The emit section consists of one or more *emit statements*. An emit statement can be a [variable name](#variable), an [assignment](#assignment), or an [expression statement](#expression-statement). If an emit statement is an expression statement, it must be the only emit.
140148

141-
An alternative workflow form, known as an *entry workflow*, has no name and may only define a main and publish section:
149+
- The publish section can be specified but is intended to be used in the entry workflow (see below).
150+
151+
152+
An *entry workflow* has no name and may consist of a *main* and *publish* section:
142153

143154
```groovy
144155
workflow {
@@ -158,8 +169,6 @@ workflow {
158169

159170
- The publish section consists of one or more *publish statements*. A publish statement is a [right-shift expression](#binary-expressions), where the left-hand side is an expression that refers to a value in the workflow body, and the right-hand side is an expression that returns a string.
160171

161-
- The publish section can also be specified in named workflows as a convenience, but is intended mainly to be used in the entry workflow.
162-
163172
In order for a script to be executable, it must either define an entry workflow or use the implicit workflow syntax described [above](#top-level-declarations).
164173

165174
Entry workflow definitions are ignored when a script is included as a module. This way, the same script can be included as a module or executed as a pipeline.
@@ -205,13 +214,13 @@ process greet {
205214
}
206215
```
207216

208-
- Each of these additional sections are optional. Directives do not have an explicit section label, but are simply defined first.
217+
- A process must define a script, shell, or exec section (see below). All other sections are optional. Directives do not have an explicit section label, but must be defined first.
209218

210219
- The `script:` section label can be omitted only when there are no other sections in the body.
211220

212-
- Sections must be defined in the order shown above, with the exception of the output section, which can alternatively be specified after the script and stub.
221+
- Sections must be defined in the order shown above, with the exception of the output section, which can also be specified after the script and stub.
213222

214-
Each section may contain one or more statements. For directives, inputs, and outputs, these statements must be [function calls](#function-call). Refer to {ref}`process-reference` for the set of available input qualifiers, output qualifiers, and directives.
223+
Each section may contain one or more statements. For directives, inputs, and outputs, these statements must be [function calls](#function-call). See {ref}`process-reference` for the set of available input qualifiers, output qualifiers, and directives.
215224

216225
The script section can be substituted with a shell or exec section:
217226

@@ -241,7 +250,7 @@ process greetExec {
241250

242251
The script, shell, and stub sections must return a string in the same manner as a [function](#function).
243252

244-
Refer to {ref}`process-page` for more information on the semantics of each process section.
253+
See {ref}`process-page` for more information on the semantics of each process section.
245254

246255
(syntax-function)=
247256

@@ -277,7 +286,7 @@ def fib(x) {
277286

278287
### Enum type
279288

280-
An enum type declaration consists of a name and a body, which consists of a comma-separated list of identifiers:
289+
An enum type declaration consists of a name and a body. The body consists of a comma-separated list of identifiers:
281290

282291
```groovy
283292
enum Day {
@@ -291,7 +300,7 @@ enum Day {
291300
}
292301
```
293302

294-
Enum values can be accessed as `Day.MONDAY`, `Day.TUESDAY`, and so on.
303+
Enum values in the above example can be accessed as `Day.MONDAY`, `Day.TUESDAY`, and so on.
295304

296305
:::{note}
297306
Enum types cannot be included across modules at this time.
@@ -312,11 +321,11 @@ output {
312321
}
313322
```
314323

315-
Only one output block may be defined in a script. Refer to {ref}`workflow-output-def` for the set of available target directives.
324+
Only one output block may be defined in a script. See {ref}`workflow-output-def` for the set of available target directives.
316325

317326
## Statements
318327

319-
Statements should be separated by a newline or semi-colon:
328+
Statements can be separated by either a newline or a semi-colon:
320329

321330
```groovy
322331
// newline
@@ -335,19 +344,19 @@ Variables can be declared with the `def` keyword:
335344
def x = 42
336345
```
337346

338-
Multiple variables can be declared in a single statement as long as the initializer is a [list literal](#list) with as many elements as declared variables:
347+
Multiple variables can be declared in a single statement if the initializer is a [list literal](#list) with the same number of elements and declared variables:
339348

340349
```groovy
341350
def (x, y) = [ 1, 2 ]
342351
```
343352

344-
Every variable has a *scope*, which determines the region of code in which the variable is defined.
353+
Each variable has a *scope*, which is the region of code in which the variable can be used.
345354

346355
Variables declared in a function, as well as the parameters of that function, exist for the duration of that function call. The same applies to closures.
347356

348357
Workflow inputs exist for the entire workflow body. Variables declared in the main section exist for the main, emit, and publish sections. Named outputs are not considered variable declarations and therefore do not have any scope.
349358

350-
Process input variables exist for the entire process body. Variables declared in the process script, shell, exec, and stub sections exist only in their respective section, with one exception -- in these sections, a variable can be declared with the `def` keyword, in which case it will also exist in the output section.
359+
Process input variables exist for the entire process body. Variables declared in the process script, shell, exec, and stub sections exist only in their respective section, with one exception -- variables declared without the `def` keyword also exist in the output section.
351360

352361
Variables declared in an if or else branch exist only within that branch:
353362

@@ -363,7 +372,7 @@ if( true )
363372
println x
364373
```
365374

366-
A variable cannot be declared with the same name as another variable in the same scope or any enclosing scope:
375+
A variable cannot be declared with the same name as another variable in the same scope or an enclosing scope:
367376

368377
```groovy
369378
def clash(x) {
@@ -385,15 +394,15 @@ map.key = 'value'
385394

386395
The target expression must be a [variable](#variable), [index](#binary-expressions), or [property](#binary-expressions) expression. The source expression can be any expression.
387396

388-
Multiple variables can be assigned in a single statement as long as the source expression is a [list literal](#list) with as many elements as assigned variables:
397+
Multiple variables can be assigned in a single statement as long as the source expression is a [list literal](#list) with the same number of elements and assigned variables:
389398

390399
```groovy
391400
(x, y) = [ 1, 2 ]
392401
```
393402

394403
### Expression statement
395404

396-
Any [expression](#expressions) can also be a statement.
405+
Any [expression](#expressions) can be a statement.
397406

398407
In general, the only expressions that can have any effect as expression statements are function calls that have side effects (e.g. `println`) or an implicit return statement (e.g. in a function or closure).
399408

@@ -407,9 +416,9 @@ assert 2 + 2 == 4 : 'The math broke!'
407416

408417
If the condition is false, an error will be raised with the given error message.
409418

410-
### if / else
419+
### if/else
411420

412-
An if/else statement consists of an *if branch* and an optional *else branch*. Each branch consists of a boolean expression in parentheses, followed by either a single statement or a *block statement* (one or more statements in curly braces).
421+
An if/else statement consists of an *if branch* and an optional *else branch*. Each branch consists of a boolean expression in parentheses, followed by either a single statement or a *block statement* (one or more statements in curly braces). For example:
413422

414423
```groovy
415424
def x = Math.random()
@@ -423,7 +432,7 @@ else {
423432

424433
If the condition is true, the if branch will be executed, otherwise the else branch will be executed.
425434

426-
If / else statements can be chained any number of times by making the else branch another if / else statement:
435+
If/else statements can be chained any number of times by making the else branch another if/else statement:
427436

428437
```groovy
429438
def grade = 89
@@ -439,7 +448,7 @@ else
439448
println 'You failed.'
440449
```
441450

442-
A more verbose way to write the same code would be:
451+
A more verbose way to write the same code is:
443452

444453
```groovy
445454
def grade = 89
@@ -500,7 +509,9 @@ def isEven2(n) {
500509
}
501510
```
502511

503-
Note that if the last statement is not a return or expression statement (implicit return), it is equivalent to appending an empty return.
512+
:::{note}
513+
If the last statement is not a return or expression statement (implicit return), it is equivalent to appending an empty return.
514+
:::
504515

505516
### throw
506517

@@ -517,9 +528,9 @@ error 'something failed!'
517528
```
518529
:::
519530

520-
### try / catch
531+
### try/catch
521532

522-
A try / catch statement consists of a *try block* followed by any number of *catch clauses*:
533+
A try/catch statement consists of a *try block* followed by any number of *catch clauses*:
523534

524535
```groovy
525536
def text = null
@@ -531,7 +542,7 @@ catch( IOException e ) {
531542
}
532543
```
533544

534-
The try block will be executed, and if an error is raised and matches the expected error type of a catch clause, the code in that catch clause will be executed. If no catch clause is matched, the error will be raised to the next enclosing try / catch statement, or to the Nextflow runtime.
545+
The try block will be executed, and if an error is raised and matches the expected error type of a catch clause, the code in that catch clause will be executed. If no catch clause is matched, the error will be raised to the next enclosing try/catch statement, or to the Nextflow runtime.
535546

536547
## Expressions
537548

@@ -631,11 +642,13 @@ Logic unconfined.
631642
/
632643
```
633644

634-
Note that a slashy string cannot be empty because it would become a line comment.
645+
:::{note}
646+
A slashy string cannot be empty because it would become a line comment.
647+
:::
635648

636649
### Dynamic string
637650

638-
Double-quoted strings can be interpolated using the `${}` placeholder, which can contain any expression:
651+
Double-quoted strings can be interpolated using the `${}` placeholder with an expression:
639652

640653
```groovy
641654
def names = ['Thing 1', 'Thing 2']
@@ -729,7 +742,7 @@ println [1, 2, 3].collect { v -> factor * v }
729742
// -> [2, 4, 6]
730743
```
731744

732-
And they can declare local variables that exist only for the lifetime of each closure invocation:
745+
Closures can declare local variables that exist only for the lifetime of each closure invocation:
733746

734747
```groovy
735748
def result = 0
@@ -742,7 +755,7 @@ println result
742755
// -> 14
743756
```
744757

745-
Refer to the {ref}`standard library <stdlib-page>` and {ref}`operator <operator-page>` reference pages for examples of closures being used in practice.
758+
See {ref}`standard library <stdlib-page>` and {ref}`operator <operator-page>` for more examples of how closures are used in practice.
746759

747760
### Index expression
748761

@@ -782,15 +795,15 @@ The argument list may contain any number of *positional arguments* and *named ar
782795
file('hello.txt', checkIfExists: true)
783796
```
784797

785-
The named arguments are collected into a map and provided as the first positional argument to the function. Thus the above function call can be rewritten as:
798+
The named arguments are collected into a map and provided as the first positional argument to the function. The above function call can be rewritten as:
786799

787800
```groovy
788801
file([checkIfExists: true], 'hello.txt')
789802
```
790803

791804
The argument name must be an identifier or string literal.
792805

793-
When the function call is also an [expression statement](#expression-statement) and there is at least one argument, the parentheses can be omitted:
806+
The parentheses can be omitted when the function call is also an [expression statement](#expression-statement) and there is at least one argument:
794807

795808
```groovy
796809
// positional args
@@ -827,7 +840,7 @@ If the type is implicitly available in the script, the *fully-qualified type nam
827840
new Date()
828841
```
829842

830-
Refer to {ref}`stdlib-default-imports` for the set of types which are implicitly available in Nextflow scripts.
843+
See {ref}`stdlib-default-imports` for the set of types which are implicitly available in Nextflow scripts.
831844

832845
### Unary expressions
833846

0 commit comments

Comments
 (0)