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

antlr plugin should respect include files #13005

Closed
snazy opened this issue May 4, 2020 · 2 comments
Closed

antlr plugin should respect include files #13005

snazy opened this issue May 4, 2020 · 2 comments
Labels

Comments

@snazy
Copy link
Contributor

snazy commented May 4, 2020

For antlr source grammar files that include other files, the antlr-plugin will consider those "include fragments" as "full grammar files" and tries to build those via antlr.

For example:
File Cql.g (e.g. this line)

import Parser,Lexer;

includes the files Parser.g and Lexer.g.

Excluding the Lexer.g+Parser.g files is not an option, because that would completely ignore any changes to those files and the grammar wouldn't be built.

The only workaround that I could find so far is to implement a custom task that extends org.gradle.api.plugins.antlr.AntlrTask, then disable all tasks of type AntlrTask and just use the task with the custom class. The execute() method is a little bit different (compared to the "original one" here):

    private Set<String> includeFiles = new LinkedHashSet<>();

    @Input
    public Set<String> getIncludeFiles()
    {
        return includeFiles;
    }

    public void setIncludeFiles(Set<String> includeFiles)
    {
        this.includeFiles = includeFiles;
    }

    public void execute(InputChanges inputChanges)
    {
        FileCollection stableSources = getStableSources();
        // Note: cannot really handle incremental grammar generation (but doesn't actually matter, as there's
        // just one Cql.g file in C* anyway).
        Set<File> grammarFiles = new HashSet<>(stableSources.getFiles());

        SourceDirectorySet sourceDirectorySet;
        try
        {
            Field f = AntlrTask.class.getDeclaredField("sourceDirectorySet");
            f.setAccessible(true);
            sourceDirectorySet = (SourceDirectorySet) f.get(antlrTask);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }

        Set<File> processGrammarFiles = grammarFiles.stream()
                                                    .filter(f -> !includeFiles.contains(f.getName()))
                                                    .collect(Collectors.toSet());

        AntlrWorkerManager manager = new AntlrWorkerManager();
        AntlrSpec spec = new AntlrSpecFactory().create(this, processGrammarFiles, sourceDirectorySet);
        File projectDir = getProjectLayout().getProjectDirectory().getAsFile();
        AntlrResult result = manager.runWorker(projectDir, getWorkerProcessBuilderFactory(), getAntlrClasspath(), spec);
        evaluate(result);
    }

I.e. it basically trades the support for include files with incremental builds for antlr grammar, as there's no information for incremental grammar-builds about which include file is used in which grammar source file. I think, it's a fair tradeoff, at least for my use case.

I'm proposing to extend the AntlrTask with a new property includeFiles as a set of regular expressions. If that set is not empty, skip the incremental grammar-builds. Also don't pass the include files (i.e. source files matching any of the regular expressions) to the antlr-tool.

@snazy snazy added a:feature A new functionality from:contributor labels May 4, 2020
snazy added a commit to snazy/gradle that referenced this issue Sep 16, 2020
Adds a new `@Input` property `Set<Pattern> includeFilesPatterns` to `AntlrTask` to contain the regex patterns to match the include-files (applied on the project-relative-path of each input file). If `includeFilesPatterns` is not empty, "incremental grammar builds" are disabled, as there is no way to know which include file is used in which grammar. Include-files are not passed to the antlr tool.

fixes gradle#13005

Signed-off-by: Robert Stupp <snazy@snazy.de>
@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. If you're interested in how we try to keep the backlog in a healthy state, please read our blog post on how we refine our backlog. If you feel this is something you could contribute, please have a look at our Contributor Guide. Thank you for your contribution.

@stale stale bot added the stale label Apr 16, 2022
@stale
Copy link

stale bot commented Jun 19, 2022

This issue has been automatically closed due to inactivity. If you can reproduce this on a recent version of Gradle or if you have a good use case for this feature, please feel free to to let know so we can reopen the issue. Please try to provide steps to reproduce, a quick explanation of your use case or a high-quality pull request.

@stale stale bot closed this as completed Jun 19, 2022
@wolfs wolfs closed this as not planned Won't fix, can't repro, duplicate, stale Sep 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants