Subject: bug#26487: Add antlr3

From: Julien Lepiller <[email-masked]>
Date: Mon, 17 Apr 2017 15:03:53 +0200

Le Sat, 15 Apr 2017 15:59:17 -0400,
Kei Kebreau <[email-masked]> a écrit :

> Julien Lepiller <[email-masked]> writes:
> 
> > Hi, here are patches to add antlr3, a parser generator.
> >
> > * 0001-guix-ant-build-system-Add-src-dir-parameter.patch: adds a
> >   #:src-dir parameter to the ant-build-system, to change the source
> >   directory and possibly specify more than one directory.
> > * 0002-gnu-Add-antlr2.patch: Adds antlr2, an older version of antlr,
> >   that is required by antlr3 to generate some of its source files,
> > and because parts of it is used by antlr3 (such as the CommonTree
> > class).
> > * 0003-gnu-Add-stringtemplate3.patch: Adds stringtemplate3, a
> > runtime requirement of antlr3. It is a library to generate strings
> > using templates.
> > * 0004-gnu-Add-antlr3-and-stringtemplate4.patch: a big patch that
> > adds antlr3 and stringtemplate4 (ST4). ST4 is a newer version of
> > ST3, is a runtime dependency of antlr3 and needs antlr3 to generate
> > some of its source files. antlr3 depends on ST4 too. I didn't find
> > a way to split this patch further.
> >
> > I wanted to package antlr4, but I'm still working on its
> > dependencies (antlr3 is one of them).  
> 
> Everything looks good except for the antlr2 jar file, where the
> timestamps inside the jar file differ. Is there a way to fix this that
> you are aware of?

Hi, I fixed the reproducibility issue by copying part of the
ant-build-system in antlr2's recipe.
From bc2eaf6e11cea60169f16c35ca052a67f83990af Mon Sep 17 00:00:00 2001
From: Julien Lepiller <[email-masked]>
Date: Thu, 13 Apr 2017 20:18:15 +0200
Subject: [PATCH 1/4] guix: ant-build-system: Add src-dir parameter.

* guix/build-system/ant.scm (ant-build): Add src-dir parameter.
* guix/build/ant-build-system.scm (configure): Add src-dir parameter.
(default-build.xml): Use it.
* doc/guix.texi: Document it.
---
 doc/guix.texi                   | 6 +++++-
 guix/build-system/ant.scm       | 2 ++
 guix/build/ant-build-system.scm | 9 +++++----
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 810139dbc..6e7e1f55f 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -3321,7 +3321,11 @@ parameters, respectively.
 When the original package does not provide a suitable Ant build file,
 the parameter @code{#:jar-name} can be used to generate a minimal Ant
 build file @file{build.xml} with tasks to build the specified jar
-archive.
+archive.  By default, @file{src} will be used as the source directory.
+The parameter @code{#:src-dir} can be used to change the default source
+directory.  This parameter accepts the ant syntax for @code{srcdir}
+parameters, allowing to specify more than one directory by separating
+each directory with a semicolon (as in @code{"src1:src2"}).
 
 The parameter @code{#:build-target} can be used to specify the Ant task
 that should be run during the @code{build} phase.  By default the
diff --git a/guix/build-system/ant.scm b/guix/build-system/ant.scm
index 550f92bc7..cd544ad0d 100644
--- a/guix/build-system/ant.scm
+++ b/guix/build-system/ant.scm
@@ -98,6 +98,7 @@
                     (make-flags ''())
                     (build-target "jar")
                     (jar-name #f)
+                    (src-dir "src")
                     (phases '(@ (guix build ant-build-system)
                                 %standard-phases))
                     (outputs '("out"))
@@ -126,6 +127,7 @@
                   #:test-target ,test-target
                   #:build-target ,build-target
                   #:jar-name ,jar-name
+                  #:src-dir ,src-dir
                   #:phases ,phases
                   #:outputs %outputs
                   #:search-paths ',(map search-path-specification->sexp
diff --git a/guix/build/ant-build-system.scm b/guix/build/ant-build-system.scm
index 00a4a46d8..e2f3d4a9e 100644
--- a/guix/build/ant-build-system.scm
+++ b/guix/build/ant-build-system.scm
@@ -35,7 +35,7 @@
 ;;
 ;; Code:
 
-(define (default-build.xml jar-name prefix)
+(define (default-build.xml jar-name prefix src-dir)
   "Create a simple build.xml with standard targets for Ant."
   (call-with-output-file "build.xml"
     (lambda (port)
@@ -58,7 +58,7 @@
                  (target (@ (name "compile"))
                          (mkdir (@ (dir "${classes.dir}")))
                          (javac (@ (includeantruntime "false")
-                                   (srcdir "src")
+                                   (srcdir ,src-dir)
                                    (destdir "${classes.dir}")
                                    (classpath (@ (refid "classpath"))))))
 
@@ -97,12 +97,13 @@ to the default GNU unpack strategy."
       ;; Use GNU unpack strategy for things that aren't jar archives.
       ((assq-ref gnu:%standard-phases 'unpack) #:source source)))
 
-(define* (configure #:key inputs outputs (jar-name #f)
+(define* (configure #:key inputs outputs (jar-name #f) (src-dir "src")
                     #:allow-other-keys)
   (when jar-name
     (default-build.xml jar-name
                        (string-append (assoc-ref outputs "out")
-                                      "/share/java")))
+                                      "/share/java")
+                       src-dir))
   (setenv "JAVA_HOME" (assoc-ref inputs "jdk"))
   (setenv "CLASSPATH" (generate-classpath inputs)))
 
-- 
2.12.2

From 320ebcdc61fb98ed48b49ddfc6595405ba6234a1 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <[email-masked]>
Date: Thu, 13 Apr 2017 20:03:54 +0200
Subject: [PATCH 2/4] gnu: Add antlr2.

* gnu/packages/java.scm (antlr2): New variable.
---
 gnu/packages/java.scm | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/gnu/packages/java.scm b/gnu/packages/java.scm
index 1d18a0b06..47c93aca9 100644
--- a/gnu/packages/java.scm
+++ b/gnu/packages/java.scm
@@ -3,6 +3,7 @@
 ;;; Copyright © 2016 Leo Famulari <[email-masked]>
 ;;; Copyright © 2016, 2017 Roel Janssen <[email-masked]>
 ;;; Copyright © 2017 Carlo Zancanaro <[email-masked]>
+;;; Copyright © 2017 Julien Lepiller <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -1178,3 +1179,79 @@ testing frameworks, mocking libraries and UI validation rules.")
 JUnit provides assertions for testing expected results, test fixtures for
 sharing common test data, and test runners for running tests.")
     (license license:epl1.0)))
+
+(define-public antlr2
+  (package
+    (name "antlr2")
+    (version "2.7.7")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append "http://www.antlr2.org/download/antlr-"
+                                  version ".tar.gz"))
+              (sha256
+               (base32
+                "1ffvcwdw73id0dk6pj2mlxjvbg0662qacx4ylayqcxgg381fnfl5"))
+              (modules '((guix build utils)))
+              (snippet
+               '(begin
+                  (delete-file "antlr.jar")
+                  (substitute* "lib/cpp/antlr/CharScanner.hpp"
+                    (("#include <map>")
+                     (string-append
+                       "#include <map>\n"
+                       "#define EOF (-1)\n"
+                       "#include <strings.h>")))
+                  (substitute* "configure"
+                    (("/bin/sh") "sh"))))))
+    (build-system gnu-build-system)
+    (arguments
+     `(#:tests? #f
+       #:phases
+       (modify-phases %standard-phases
+         (add-after 'install 'strip-jar-timestamps
+           (lambda* (#:key outputs #:allow-other-keys)
+             (let* ((out (assoc-ref outputs "out"))
+                    (jar1 (string-append out "/lib/antlr.jar"))
+                    (jar2 (string-append out "/share/antlr-2.7.7/antlr.jar")))
+               ;; XXX: copied from (guix build ant-build-system)
+               (define (strip-jar jar dir)
+                 (let ((manifest (string-append dir "/META-INF/MANIFEST.MF")))
+                   (mkdir-p dir)
+                   (and (with-directory-excursion dir
+                          (zero? (system* "jar" "xf" jar)))
+                        (delete-file jar)
+                        (for-each (lambda (file)
+                                    (let ((s (lstat file)))
+                                      (unless (eq? (stat:type s) 'symlink)
+                                                 (utime file 0 0 0 0))))
+                                  (find-files dir #:directories? #t))
+                        (with-directory-excursion dir
+                          (let* ((files (find-files "." ".*" #:directories? #t)))
+                            (unless (zero? (apply system*
+                                                  `("zip" "-X" ,jar ,manifest
+                                                    ,@files)))
+                              (error "'zip' failed"))))
+                        (utime jar 0 0)
+                        #t)))
+                (strip-jar jar1 "temp1")
+                (strip-jar jar2 "temp2"))))
+         (add-after 'configure 'fix-bin-ls
+           (lambda _
+             (for-each (lambda (file)
+                         (substitute* file
+                          (("/bin/ls") "ls")))
+               (find-files "." "Makefile")))))))
+    (native-inputs
+     `(("which" ,which)
+       ("zip" ,zip)
+       ("java" ,icedtea "jdk")))
+    (inputs
+     `(("java" ,icedtea)))
+    (home-page "http://www.antlr2.org")
+    (synopsis "Framework for constructing recognizers, compilers, and translators")
+    (description "ANTLR, ANother Tool for Language Recognition, (formerly PCCTS)
+is a language tool that provides a framework for constructing recognizers,
+compilers, and translators from grammatical descriptions containing Java, C#,
+C++, or Python actions.  ANTLR provides excellent support for tree construction,
+tree walking, and translation.")
+    (license license:public-domain)))
-- 
2.12.2

From 84011296bba87af30109457ad6539372d97e97cc Mon Sep 17 00:00:00 2001
From: Julien Lepiller <[email-masked]>
Date: Thu, 13 Apr 2017 20:13:14 +0200
Subject: [PATCH 3/4] gnu: Add stringtemplate3.

* gnu/packages/java.scm (stringtemplate3): New variable.
---
 gnu/packages/java.scm | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/gnu/packages/java.scm b/gnu/packages/java.scm
index 47c93aca9..624436a0a 100644
--- a/gnu/packages/java.scm
+++ b/gnu/packages/java.scm
@@ -1255,3 +1255,41 @@ compilers, and translators from grammatical descriptions containing Java, C#,
 C++, or Python actions.  ANTLR provides excellent support for tree construction,
 tree walking, and translation.")
     (license license:public-domain)))
+
+(define-public stringtemplate3
+  (package
+    (name "stringtemplate3")
+    (version "3.2.1")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append "https://github.com/antlr/website-st4/raw/"
+                                  "gh-pages/download/stringtemplate-"
+                                  version ".tar.gz"))
+              (sha256
+               (base32
+                "086yj68np1vqhkj7483diz3km6s6y4gmwqswa7524a0ca6vxn2is"))))
+    (build-system ant-build-system)
+    (arguments
+     `(#:jar-name "stringtemplate-3.2.1.jar"
+       #:tests? #f
+       #:phases
+       (modify-phases %standard-phases
+         (add-before 'build 'generate-grammar
+           (lambda _
+             (let ((dir "src/org/antlr/stringtemplate/language/"))
+               (for-each (lambda (file)
+                           (display file)
+                           (newline)
+                           (system* "antlr" "-o" dir (string-append dir file)))
+                         '("template.g" "angle.bracket.template.g" "action.g"
+                           "eval.g" "group.g" "interface.g"))))))))
+    (native-inputs
+     `(("antlr" ,antlr2)))
+    (home-page "http://www.stringtemplate.org")
+    (synopsis "Template engine to generate formatted text output")
+    (description "StringTemplate is a java template engine (with ports for C#,
+Objective-C, JavaScript, Scala) for generating source code, web pages, emails,
+or any other formatted text output.  StringTemplate is particularly good at
+code generators, multiple site skins, and internationalization / localization.
+StringTemplate also powers ANTLR.")
+    (license license:bsd-3)))
-- 
2.12.2

From da55fe9a311be5d4c758bc6441b60c8a689d8f54 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <[email-masked]>
Date: Thu, 13 Apr 2017 21:57:33 +0200
Subject: [PATCH 4/4] gnu: Add antlr3 and stringtemplate4.

* gnu/packages/java.scm (antlr3, stringtemplate4): New variables.
---
 gnu/packages/java.scm | 337 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 337 insertions(+)

diff --git a/gnu/packages/java.scm b/gnu/packages/java.scm
index 624436a0a..cb1c2bd0e 100644
--- a/gnu/packages/java.scm
+++ b/gnu/packages/java.scm
@@ -1293,3 +1293,340 @@ or any other formatted text output.  StringTemplate is particularly good at
 code generators, multiple site skins, and internationalization / localization.
 StringTemplate also powers ANTLR.")
     (license license:bsd-3)))
+
+;; antlr3 is partially written using antlr3 grammar files. It also depends on
+;; ST4 (stringtemplate4), which is also partially written using antlr3 grammar
+;; files and uses antlr3 at runtime. The latest version requires a recent version
+;; of antlr3 at runtime.
+;; Fortunately, ST4 4.0.6 can be built with an older antlr3, and we use antlr3.3.
+;; This version of ST4 is sufficient for the latest antlr3.
+;; We use ST4 4.0.6 to build a boostrap antlr3 (latest version), and build
+;; the latest ST4 with it. Then we build our final antlr3 that will be linked
+;; against the latest ST4.
+;; antlr3.3 still depends on antlr3 to generate some files, so we use an
+;; even older version, antlr3.1, to generate them. Fortunately antlr3.1 uses
+;; only grammar files with the antlr2 syntax.
+;; So we build antlr3.1 -> antlr3.3 -> ST4.0.6 -> antlr3-bootstrap -> ST4 -> antlr3.
+
+(define-public stringtemplate4
+  (package
+    (name "stringtemplate4")
+    (version "4.0.8")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append "https://github.com/antlr/stringtemplate4/archive/"
+                                  version ".tar.gz"))
+              (file-name (string-append name "-" version ".tar.gz"))
+              (sha256
+               (base32
+                "1pri8hqa95rfdkjy55icl5q1m09zwp5k67ib14abas39s4v3w087"))))
+    (build-system ant-build-system)
+    (arguments
+     `(#:tests? #f
+       #:jar-name (string-append ,name "-" ,version ".jar")
+       #:phases
+       (modify-phases %standard-phases
+         (add-before 'build 'generate-grammar
+           (lambda* (#:key inputs #:allow-other-keys)
+             (chdir "src/org/stringtemplate/v4/compiler/")
+             (for-each (lambda (file)
+                         (display file)
+                         (newline)
+                         (system* "antlr3" file))
+                       '("STParser.g" "Group.g" "CodeGenerator.g"))
+             (chdir "../../../../.."))))))
+    (inputs
+     `(("antlr3" ,antlr3-bootstrap)
+       ("antlr2" ,antlr2)
+       ("stringtemplate" ,stringtemplate3)))
+    (home-page "http://www.stringtemplate.org")
+    (synopsis "Template engine to generate formatted text output")
+    (description "StringTemplate is a java template engine (with ports for C#,
+Objective-C, JavaScript, Scala) for generating source code, web pages, emails,
+or any other formatted text output.  StringTemplate is particularly good at
+code generators, multiple site skins, and internationalization / localization.
+StringTemplate also powers ANTLR.")
+    (license license:bsd-3)))
+
+(define stringtemplate4-4.0.6
+  (package
+    (inherit stringtemplate4)
+    (name "stringtemplate4")
+    (version "4.0.6")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append "https://github.com/antlr/stringtemplate4/archive/ST-"
+                                  version ".tar.gz"))
+              (file-name (string-append name "-" version ".tar.gz"))
+              (sha256
+               (base32
+                "0hjmh1ahdsh3w825i67mli9l4nncc4l6hdbf9ma91jvlj590sljp"))))
+    (inputs
+     `(("antlr3" ,antlr3-3.3)
+       ("antlr2" ,antlr2)
+       ("stringtemplate" ,stringtemplate3)))))
+
+(define-public antlr3
+  (package
+    (name "antlr3")
+    (version "3.5.2")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append "https://github.com/antlr/antlr3/archive/"
+                                  version ".tar.gz"))
+              (file-name (string-append name "-" version ".tar.gz"))
+              (sha256
+               (base32
+                "07zff5frmjd53rnqdx31h0pmswz1lv0p2lp28cspfszh25ysz6sj"))))
+    (build-system ant-build-system)
+    (arguments
+     `(#:jar-name (string-append ,name "-" ,version ".jar")
+       #:src-dir "tool/src/main/java:runtime/Java/src/main/java:tool/src/main/antlr3"
+       #:tests? #f
+       #:phases
+       (modify-phases %standard-phases
+         (add-after 'install 'bin-install
+           (lambda* (#:key inputs outputs #:allow-other-keys)
+             (let ((jar (string-append (assoc-ref outputs "out") "/share/java"))
+                   (bin (string-append (assoc-ref outputs "out") "/bin")))
+               (mkdir-p bin)
+               (with-output-to-file (string-append bin "/antlr3")
+                 (lambda _
+                   (display
+                     (string-append "#!" (which "sh") "\n"
+                                    "java -cp " jar "/" ,name "-" ,version ".jar:"
+                                    (string-concatenate
+                                      (find-files (assoc-ref inputs "stringtemplate")
+                                                  ".*\\.jar"))
+                                    ":"
+                                    (string-concatenate
+                                      (find-files (assoc-ref inputs "stringtemplate4")
+                                                  ".*\\.jar"))
+                                    ":"
+                                    (string-concatenate
+                                      (find-files (string-append
+                                                    (assoc-ref inputs "antlr")
+                                                    "/lib")
+                                                  ".*\\.jar"))
+                                    " org.antlr.Tool $*"))))
+               (chmod (string-append bin "/antlr3") #o755))))
+         (add-before 'build 'generate-grammar
+           (lambda _
+             (chdir "tool/src/main/antlr3/org/antlr/grammar/v3/")
+             (for-each (lambda (file)
+                         (display file)
+                         (newline)
+                         (system* "antlr3" file))
+                       '("ANTLR.g" "ANTLRTreePrinter.g" "ActionAnalysis.g"
+                         "AssignTokenTypesWalker.g"
+                         "ActionTranslator.g" "TreeToNFAConverter.g"
+                         "ANTLRv3.g" "ANTLRv3Tree.g" "LeftRecursiveRuleWalker.g"
+                         "CodeGenTreeWalker.g" "DefineGrammarItemsWalker.g"))
+             (substitute* "ANTLRParser.java"
+               (("public Object getTree") "public GrammarAST getTree"))
+             (substitute* "ANTLRv3Parser.java"
+               (("public Object getTree") "public CommonTree getTree"))
+             (chdir "../../../../../java")
+             (system* "antlr" "-o" "org/antlr/tool"
+                      "org/antlr/tool/serialize.g")
+             (substitute* "org/antlr/tool/LeftRecursiveRuleAnalyzer.java"
+               (("import org.antlr.grammar.v3.\\*;") "import org.antlr.grammar.v3.*;
+import org.antlr.grammar.v3.ANTLRTreePrinter;"))
+             (substitute* "org/antlr/tool/ErrorManager.java"
+               (("case NO_SUCH_ATTRIBUTE_PASS_THROUGH:") ""))
+             (chdir "../../../..")))
+         (add-before 'build 'fix-build-xml
+           (lambda _
+             (substitute* "build.xml"
+               (("<exec") "<copy todir=\"${classes.dir}\">
+<fileset dir=\"tool/src/main/resources\">
+<include name=\"**/*.stg\"/>
+<include name=\"**/*.st\"/>
+<include name=\"**/*.sti\"/>
+<include name=\"**/STLexer.tokens\"/>
+</fileset>
+</copy><exec")))))))
+    (native-inputs
+     `(("antlr" ,antlr2)
+       ("antlr3" ,antlr3-bootstrap)))
+    (inputs
+     `(("junit" ,java-junit)
+       ("stringtemplate" ,stringtemplate3)
+       ("stringtemplate4" ,stringtemplate4)))
+    (propagated-inputs
+     `(("stringtemplate" ,stringtemplate3)
+       ("antlr" ,antlr2)
+       ("stringtemplate4" ,stringtemplate4-4.0.6)))
+    (home-page "http://www.antlr3.org")
+    (synopsis "Framework for constructing recognizers, compilers, and translators")
+    (description "ANTLR, ANother Tool for Language Recognition, (formerly PCCTS)
+is a language tool that provides a framework for constructing recognizers,
+compilers, and translators from grammatical descriptions containing Java, C#,
+C++, or Python actions.  ANTLR provides excellent support for tree construction,
+tree walking, and translation.")
+    (license license:bsd-3)))
+
+(define antlr3-bootstrap
+  (package
+    (inherit antlr3)
+    (name "antlr3-bootstrap")
+    (native-inputs
+     `(("antlr" ,antlr2)
+       ("antlr3" ,antlr3-3.3)))
+    (inputs
+     `(("junit" ,java-junit)))))
+
+(define antlr3-3.3
+  (package
+    (inherit antlr3)
+    (name "antlr3")
+    (version "3.3")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append "https://github.com/antlr/website-antlr3/raw/"
+                                  "gh-pages/download/antlr-"
+                                  version ".tar.gz"))
+              (sha256
+               (base32
+                "0qgg5vgsm4l1d6dj9pfbaa25dpv2ry2gny8ajy4vvgvfklw97b3m"))))
+    (arguments
+     `(#:jar-name (string-append ,name "-" ,version ".jar")
+       #:src-dir (string-append "tool/src/main/java:runtime/Java/src/main/java:"
+                                "tool/src/main/antlr2:tool/src/main/antlr3")
+       #:tests? #f
+       #:phases
+       (modify-phases %standard-phases
+         (add-after 'install 'bin-install
+           (lambda* (#:key inputs outputs #:allow-other-keys)
+             (let ((jar (string-append (assoc-ref outputs "out") "/share/java"))
+                   (bin (string-append (assoc-ref outputs "out") "/bin")))
+               (mkdir-p bin)
+               (with-output-to-file (string-append bin "/antlr3")
+                 (lambda _
+                   (display
+                     (string-append "#!" (which "sh") "\n"
+                                    "java -cp " jar "/antlr3-3.3.jar:"
+                                    (string-concatenate
+                                      (find-files (assoc-ref inputs "stringtemplate")
+                                                  ".*\\.jar"))
+                                    ":"
+                                    (string-concatenate
+                                      (find-files (string-append
+                                                    (assoc-ref inputs "antlr")
+                                                    "/lib")
+                                                  ".*\\.jar"))
+                                    " org.antlr.Tool $*"))))
+               (chmod (string-append bin "/antlr3") #o755))))
+         (add-before 'build 'generate-grammar
+           (lambda _
+             (let ((dir "tool/src/main/antlr2/org/antlr/grammar/v2/"))
+               (for-each (lambda (file)
+                           (display file)
+                           (newline)
+                           (system* "antlr" "-o" dir (string-append dir file)))
+                         '("antlr.g" "antlr.print.g" "assign.types.g"
+                           "buildnfa.g" "codegen.g" "define.g")))
+             (chdir "tool/src/main/antlr3/org/antlr/grammar/v3/")
+             (for-each (lambda (file)
+                         (display file)
+                         (newline)
+                         (system* "antlr3" file))
+                       '("ActionAnalysis.g" "ActionTranslator.g" "ANTLRv3.g"
+                         "ANTLRv3Tree.g"))
+             (chdir "../../../../../../../..")
+             (substitute* "tool/src/main/java/org/antlr/tool/Grammar.java"
+               (("import org.antlr.grammar.v2.\\*;")
+                "import org.antlr.grammar.v2.*;\n
+import org.antlr.grammar.v2.TreeToNFAConverter;\n
+import org.antlr.grammar.v2.DefineGrammarItemsWalker;\n
+import org.antlr.grammar.v2.ANTLRTreePrinter;"))))
+         (add-before 'build 'fix-build-xml
+           (lambda _
+             (substitute* "build.xml"
+               (("<exec") "<copy todir=\"${classes.dir}\">
+<fileset dir=\"tool/src/main/resources\">
+<include name=\"**/*.stg\"/>
+<include name=\"**/*.st\"/>
+<include name=\"**/*.sti\"/>
+<include name=\"**/STLexer.tokens\"/>
+</fileset>
+</copy><exec")))))))
+    (native-inputs
+     `(("antlr" ,antlr2)
+       ("antlr3" ,antlr3-3.1)))
+    (inputs
+     `(("junit" ,java-junit)))
+    (propagated-inputs
+     `(("stringtemplate" ,stringtemplate3)
+       ("antlr" ,antlr2)
+       ("antlr3" ,antlr3-3.1)))))
+
+(define antlr3-3.1
+  (package
+    (inherit antlr3)
+    (name "antlr3-3.1")
+    (version "3.1")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append "https://github.com/antlr/website-antlr3/raw/"
+                                  "gh-pages/download/antlr-"
+                                  version ".tar.gz"))
+              (sha256
+               (base32
+                "0sfimc9cpbgrihz4giyygc8afgpma2c93yqpwb951giriri6x66z"))))
+    (arguments
+     `(#:jar-name (string-append ,name "-" ,version ".jar")
+       #:src-dir "src:runtime/Java/src"
+       #:tests? #f
+       #:phases
+       (modify-phases %standard-phases
+         (add-after 'install 'bin-install
+           (lambda* (#:key inputs outputs #:allow-other-keys)
+             (let ((jar (string-append (assoc-ref outputs "out") "/share/java"))
+                   (bin (string-append (assoc-ref outputs "out") "/bin")))
+               (mkdir-p bin)
+               (with-output-to-file (string-append bin "/antlr3")
+                 (lambda _
+                   (display
+                     (string-append "#!" (which "sh") "\n"
+                                    "java -cp " jar "/antlr3-3.1-3.1.jar:"
+                                    (string-concatenate
+                                      (find-files (assoc-ref inputs "stringtemplate")
+                                                  ".*\\.jar"))
+                                    ":"
+                                    (string-concatenate
+                                      (find-files (string-append
+                                                    (assoc-ref inputs "antlr")
+                                                    "/lib")
+                                                  ".*\\.jar"))
+                                    " org.antlr.Tool $*"))))
+               (chmod (string-append bin "/antlr3") #o755))))
+         (add-before 'build 'generate-grammar
+           (lambda _
+             (let ((dir "src/org/antlr/tool/"))
+               (for-each (lambda (file)
+                           (display file)
+                           (newline)
+                           (system* "antlr" "-o" dir (string-append dir file)))
+                         '("antlr.g" "antlr.print.g" "assign.types.g"
+                           "buildnfa.g" "define.g")))
+             (format #t "codegen.g\n")
+             (system* "antlr" "-o" "src/org/antlr/codegen"
+                      "src/org/antlr/codegen/codegen.g")))
+         (add-before 'build 'fix-build-xml
+           (lambda _
+             (substitute* "build.xml"
+               (("<exec") "<copy todir=\"${classes.dir}\">
+<fileset dir=\"src\">
+<include name=\"**/*.stg\"/>
+<include name=\"**/*.st\"/>
+<include name=\"**/*.sti\"/>
+<include name=\"**/STLexer.tokens\"/>
+</fileset>
+</copy><exec")))))))
+    (native-inputs
+     `(("antlr" ,antlr2)))
+    (inputs
+     `(("junit" ,java-junit)))
+    (propagated-inputs
+     `(("stringtemplate" ,stringtemplate3)))))
-- 
2.12.2