Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config parser (and loader) #4744

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1943,3 +1943,52 @@ Some features can be enabled using the `nextflow.enable` and `nextflow.preview`
: *Experimental: may change in a future release.*

: When `true`, enables {ref}`topic channels <channel-topic>` feature.

## Strict config mode

:::{versionadded} 24.10.0
:::

Strict config mode is an alternative implementation of the config parser in Nextflow which enforces a stricter syntax. Whereas the existing parser interprets config files as Groovy scripts, which makes it overly powerful and error-prone, the strict config mode allows only a strict subset of Groovy syntax which is relevant to configuration.

Strict config mode can be enabled by setting `NXF_ENABLE_STRICT_CONFIG=true`.

The following statements are allowed in strict config mode:

- Assignment (e.g. `foo.bar = '3'`)
- Block (e.g. `foo { bar { ... } }`)
- Include (e.g. `includeConfig 'foo.config'`)

When assigning a config option, the right-hand side can be any Groovy expression, including closures which may include Groovy statements. However, only a subset of Groovy is supported within this context.

Notable syntax that is supported:

- numbers
- strings
- lists
- maps
- function calls
- closures
- variables (within closures)
- if-else statements (within closures)

Notable syntax that is not supported:

- function definitions
- class definitions
- try-catch statements
- lambdas (use closures instead)

Other syntax restrictions:

- The implicit use of environment variables is no longer supported:
```groovy
env.MY_VAR = "$MY_VAR" // incorrect
env.MY_VAR = System.getenv('MY_VAR') // correct
```

- The implicit `it` variable in closures is no longer supported:
```groovy
foo.bar = ['foo', 'bar'].collect { "--$it" }.join(' ') // incorrect
foo.bar = ['foo', 'bar'].collect { it -> "--$it" }.join(' ') // correct
```
4 changes: 3 additions & 1 deletion modules/nextflow/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
apply plugin: 'groovy'
apply plugin: 'application'
apply plugin: 'antlr'

sourceSets {
main.java.srcDirs = []
main.java.srcDirs = ['build/generated-src/antlr/main', 'src/main/antlr']
main.groovy.srcDirs = ['src/main/java', 'src/main/groovy']
main.resources.srcDirs = ['src/main/resources']
test.java.srcDirs = []
Expand All @@ -15,6 +16,7 @@ compileGroovy {
}

dependencies {
antlr 'org.antlr:antlr4:4.9.2'
api(project(':nf-commons'))
api(project(':nf-httpfs'))
api "org.apache.groovy:groovy:4.0.18"
Expand Down
33 changes: 33 additions & 0 deletions modules/nextflow/src/main/antlr/AbstractLexer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package nextflow.antlr;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Lexer;
import org.apache.groovy.parser.antlr4.SyntaxErrorReportable;

/**
* Because antlr4 does not support generating lexer with specified interface,
* we have to create a super class for it and implement the interface.
*/
public abstract class AbstractLexer extends Lexer implements SyntaxErrorReportable {
public AbstractLexer(CharStream input) {
super(input);
}
}
33 changes: 33 additions & 0 deletions modules/nextflow/src/main/antlr/AbstractParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package nextflow.antlr;

import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.TokenStream;
import org.apache.groovy.parser.antlr4.SyntaxErrorReportable;

/**
* Because antlr4 does not support generating parser with specified interface,
* we have to create a super class for it and implement the interface.
*/
public abstract class AbstractParser extends Parser implements SyntaxErrorReportable {
public AbstractParser(TokenStream input) {
super(input);
}
}
Loading