Antlr4Task.groovy 4.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.babeloff.plugins.antlr

import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory;

import java.nio.file.Files;
import org.antlr.v4.Tool;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

class Antlr4Task extends DefaultTask {
    /**
     *    g grammar        FILE    str    "grammar file"
     *    l target         LIB_DIR str    "specify location of grammars, tokens files"
     *    e encoding       CODE    str    "specify grammar file encoding; e.g., euc-jp"
     *    f message-format STYLE   str    "specify output STYLE for messages in antlr, gnu, vs2005"
     *    p package        NAME    str    "specify a package/namespace for the generated code"
     *    D override       OPT     [str]  "<option>=value  set/override a grammar-level option"
     *    s show                   bool   "show the constructed properties"
     *    a atn                    bool   "generate rule augmented transition network diagrams"
     *    v long-messages          bool   "show exception details when available for errors and warnings"
     *    _ listener               bool   "generate parse tree listener"
     *    _ visitor                bool   "generate parse tree visitor"
     *    d depend                 bool   "generate file dependencies"
     *    w warn-error             bool   "treat warnings as errors"
     *    _ save-lexer             bool   "extract lexer from combined grammar"
     *    _ debug-st               bool   "launch StringTemplate visualizer on generated code"
     *    _ debug-st-wait          bool   "wait for STViz to close before continuing"
     *    _ force-atn              bool   "use the ATN simulator for all predictions"
     *    _ log                    bool   "dump lots of logging info to antlr-timestamp.log"]
     */
Fred Eisele's avatar
Fred Eisele committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

    @InputFile public String lexerGrammar;
    @InputFile public String parserGrammar;

    @InputDirectory public File sourceDir;
    @OutputDirectory public File targetDir;

    @Input @Optional public String maxHeapSize;
    @Input @Optional public String encoding;
    @Input @Optional public String msgFormat;
    @Input @Optional public String module;
    @Input @Optional public String override;

    @Input @Optional public Boolean show;
    @Input @Optional public Boolean atn;
    @Input @Optional public Boolean longMsg;
    @Input @Optional public Boolean listener;
    @Input @Optional public Boolean visitor;

    @Input @Optional public Boolean depend;
    @Input @Optional public Boolean warnError;
    @Input @Optional public Boolean saveLexer;
    @Input @Optional public Boolean debugStrTempl;
    @Input @Optional public Boolean debugStrTemplWait;
    @Input @Optional public Boolean forceAtn;
    @Input @Optional public Boolean log;
63
64
65
66
67
68
69
70
71
72

    Antlr4Task() {
        description = 'An antlr4 grammar generates a java parser'
        group = 'org.babeloff'
    }

    @TaskAction
    void start() {
        ArrayList<String> args = new ArrayList<>();

Fred Eisele's avatar
Fred Eisele committed
73
74
        args.add('-o');
        args.add(targetDir.canonicalFile.toString())
75
76
        logger.quiet 'target dir: ' + targetDir.canonicalFile

Fred Eisele's avatar
Fred Eisele committed
77
78
        args.add('-lib');
        args.add(sourceDir.canonicalFile.toString())
79
80
        logger.quiet 'source dir: ' + sourceDir.canonicalFile

Fred Eisele's avatar
Fred Eisele committed
81
82
        args.add('-package')
        args.add(module)
83
84

        def lexerArgs = args.clone();
Fred Eisele's avatar
Fred Eisele committed
85
        lexerArgs.add(lexerGrammar);
86
        String[] lexerArgsArray = lexerArgs.toArray(new String[lexerArgs.size()]);
Fred Eisele's avatar
Fred Eisele committed
87

88
89
90
91
92
        try {
            Tool lexerTool = new Tool(lexerArgsArray);
            lexerTool.processGrammarsOnCommandLine();
        }
        catch (Exception ex) {
Fred Eisele's avatar
Fred Eisele committed
93
94
            logger.quiet 'lexer: ' + lexerArgsArray;
            logger.quiet 'cause: '+ ex.getLocalizedMessage();
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
            throw new GradleException(ex.message)
        }

        // make the token file available to antlr so
        // the same tokens get used by the parser as by the lexer.
        def parserArgs = args.clone();
        if (visitor) ( parserArgs.push('-visitor'))
        if (listener) ( parserArgs.push('-listener'))
        parserArgs.push(parserGrammar);

        String[] parserArgsArray = parserArgs.toArray(new String[parserArgs.size()]);
        logger.quiet 'parsing: ' + parserArgsArray;
        try {
            Tool parserTool = new Tool(parserArgsArray);
            parserTool.processGrammarsOnCommandLine();
        }
        catch (Exception ex) {
            throw new GradleException(ex.message)
        }

    }

}