Skip to content

Commit 453ab6a

Browse files
authored
nf-lang: Add unit tests for formatting (#6002)
--------- Signed-off-by: Ben Sherman <[email protected]>
1 parent 38410fd commit 453ab6a

File tree

7 files changed

+365
-30
lines changed

7 files changed

+365
-30
lines changed

docs/reference/stdlib.md

+9
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ The following functions are available in Nextflow scripts:
238238
`multiMapCriteria( criteria: Closure ) -> Closure`
239239
: Create a multi-map criteria to use with the {ref}`operator-multiMap` operator.
240240

241+
`print( value )`
242+
: Print a value to standard output.
243+
244+
`printf( format: String, values... )`
245+
: Print a formatted string with the given values to standard output.
246+
247+
`println( value )`
248+
: Print a value to standard output with a newline.
249+
241250
`sendMail( [options] )`
242251
: Send an email. See {ref}`mail-page` for more information.
243252

docs/reference/syntax.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def x = 42
337337
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:
338338

339339
```nextflow
340-
def (x, y) = [ 1, 2 ]
340+
def (x, y) = [1, 2]
341341
```
342342

343343
Each variable has a *scope*, which is the region of code in which the variable can be used.
@@ -387,7 +387,7 @@ The target expression must be a [variable](#variable), [index](#binary-expressio
387387
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:
388388

389389
```nextflow
390-
(x, y) = [ 1, 2 ]
390+
(x, y) = [1, 2]
391391
```
392392

393393
### Expression statement
@@ -640,7 +640,7 @@ If the expression is a name or simple property expression (one or more identifie
640640

641641
```nextflow
642642
def name = [first: '<FIRST_NAME>', last: '<LAST_NAME>']
643-
println "Hello, ${name.first} ${name.last}!"
643+
println "Hello, $name.first $name.last!"
644644
// -> Hello, <FIRST_NAME> <LAST_NAME>!
645645
```
646646

modules/nf-lang/src/main/java/nextflow/config/parser/ConfigAstBuilder.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -833,27 +833,33 @@ private Expression literal(LiteralContext ctx) {
833833
}
834834

835835
private Expression integerLiteral(IntegerLiteralAltContext ctx) {
836+
var text = ctx.getText();
836837
Number num = null;
837838
try {
838-
num = Numbers.parseInteger(ctx.getText());
839+
num = Numbers.parseInteger(text);
839840
}
840841
catch( Exception e ) {
841842
numberFormatError = new Tuple2(ctx, e);
842843
}
843844

844-
return constX(num, true);
845+
var result = constX(num, true);
846+
result.putNodeMetaData(ASTNodeMarker.VERBATIM_TEXT, text);
847+
return result;
845848
}
846849

847850
private Expression floatingPointLiteral(FloatingPointLiteralAltContext ctx) {
851+
var text = ctx.getText();
848852
Number num = null;
849853
try {
850-
num = Numbers.parseDecimal(ctx.getText());
854+
num = Numbers.parseDecimal(text);
851855
}
852856
catch( Exception e ) {
853857
numberFormatError = new Tuple2(ctx, e);
854858
}
855859

856-
return constX(num, true);
860+
var result = constX(num, true);
861+
result.putNodeMetaData(ASTNodeMarker.VERBATIM_TEXT, text);
862+
return result;
857863
}
858864

859865
private ConstantExpression string(ParserRuleContext ctx) {

modules/nf-lang/src/main/java/nextflow/script/dsl/ScriptDsl.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ The directory where a module script is located (equivalent to `projectDir` if us
132132
@Description("""
133133
Print a value to standard output.
134134
""")
135-
void print(Object object);
135+
void print(Object value);
136+
137+
@Description("""
138+
Print a formatted string with the given values to standard output.
139+
""")
140+
void printf(String format, Object... values);
136141

137142
@Description("""
138143
Print a newline to standard output.
@@ -142,7 +147,7 @@ The directory where a module script is located (equivalent to `projectDir` if us
142147
@Description("""
143148
Print a value to standard output with a newline.
144149
""")
145-
void println(Object object);
150+
void println(Object value);
146151

147152
@Description("""
148153
Send an email.
@@ -155,7 +160,7 @@ The directory where a module script is located (equivalent to `projectDir` if us
155160
void sleep(long milliseconds);
156161

157162
@Description("""
158-
Create a tuple object from the given arguments.
163+
Create a tuple from the given arguments.
159164
""")
160165
List<?> tuple(Object... args);
161166

modules/nf-lang/src/main/java/nextflow/script/formatter/Formatter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void visitAssertStatement(AssertStatement node) {
217217
append("assert ");
218218
visit(node.getBooleanExpression());
219219
if( !(node.getMessageExpression() instanceof ConstantExpression ce && ce.isNullExpression()) ) {
220-
append(", ");
220+
append(" : ");
221221
visit(node.getMessageExpression());
222222
}
223223
appendNewLine();

modules/nf-lang/src/main/java/nextflow/script/parser/ScriptAstBuilder.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -1180,27 +1180,33 @@ private Expression literal(LiteralContext ctx) {
11801180
}
11811181

11821182
private Expression integerLiteral(IntegerLiteralAltContext ctx) {
1183+
var text = ctx.getText();
11831184
Number num = null;
11841185
try {
1185-
num = Numbers.parseInteger(ctx.getText());
1186+
num = Numbers.parseInteger(text);
11861187
}
11871188
catch( Exception e ) {
11881189
numberFormatError = new Tuple2(ctx, e);
11891190
}
11901191

1191-
return constX(num, true);
1192+
var result = constX(num, true);
1193+
result.putNodeMetaData(ASTNodeMarker.VERBATIM_TEXT, text);
1194+
return result;
11921195
}
11931196

11941197
private Expression floatingPointLiteral(FloatingPointLiteralAltContext ctx) {
1198+
var text = ctx.getText();
11951199
Number num = null;
11961200
try {
1197-
num = Numbers.parseDecimal(ctx.getText());
1201+
num = Numbers.parseDecimal(text);
11981202
}
11991203
catch( Exception e ) {
12001204
numberFormatError = new Tuple2(ctx, e);
12011205
}
12021206

1203-
return constX(num, true);
1207+
var result = constX(num, true);
1208+
result.putNodeMetaData(ASTNodeMarker.VERBATIM_TEXT, text);
1209+
return result;
12041210
}
12051211

12061212
private ConstantExpression string(ParserRuleContext ctx) {

0 commit comments

Comments
 (0)