diff --git a/.travis.yml b/.travis.yml index 0220f4ca2..cd774ceae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -74,42 +74,42 @@ matrix: - clang-3.7 - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=cpp - GROUP=LEXER stage: extended-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=cpp - GROUP=PARSER stage: extended-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=cpp - GROUP=RECURSION stage: extended-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=swift - GROUP=LEXER stage: main-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=swift - GROUP=PARSER stage: main-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=swift - GROUP=RECURSION @@ -122,19 +122,19 @@ matrix: - GROUP=ALL stage: extended-test - os: osx - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=dotnet - GROUP=LEXER stage: extended-test - os: osx - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=dotnet - GROUP=PARSER stage: extended-test - os: osx - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=dotnet - GROUP=RECURSION diff --git a/.travis/before-install-linux-cpp.sh b/.travis/before-install-linux-cpp.sh deleted file mode 100755 index c496c2cbd..000000000 --- a/.travis/before-install-linux-cpp.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF -sudo apt-get update -qq diff --git a/.travis/before-install-linux-go.sh b/.travis/before-install-linux-go.sh index 71adcfadd..16c828180 100755 --- a/.travis/before-install-linux-go.sh +++ b/.travis/before-install-linux-go.sh @@ -2,7 +2,5 @@ set -euo pipefail -sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF -sudo apt-get update -qq eval "$(sudo gimme 1.7.3)" ( go version ; go env ) || true diff --git a/.travis/before-install-linux-java.sh b/.travis/before-install-linux-java.sh deleted file mode 100755 index c496c2cbd..000000000 --- a/.travis/before-install-linux-java.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF -sudo apt-get update -qq diff --git a/.travis/before-install-linux-javascript.sh b/.travis/before-install-linux-javascript.sh index c70757d53..28b59a6fc 100755 --- a/.travis/before-install-linux-javascript.sh +++ b/.travis/before-install-linux-javascript.sh @@ -2,7 +2,6 @@ set -euo pipefail -sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF sudo apt-get update -qq curl -sL https://deb.nodesource.com/setup_0.12 | sudo -E bash - sudo apt-get install -qq nodejs diff --git a/.travis/before-install-linux-python2.sh b/.travis/before-install-linux-python2.sh deleted file mode 100755 index d79e21610..000000000 --- a/.travis/before-install-linux-python2.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF -sudo apt-get update -qq -python --version diff --git a/.travis/before-install-linux-python3.sh b/.travis/before-install-linux-python3.sh deleted file mode 100755 index a3b091260..000000000 --- a/.travis/before-install-linux-python3.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -python3 --version diff --git a/.travis/run-tests-python2.sh b/.travis/run-tests-python2.sh index d2a22dc77..c5cd0ca99 100755 --- a/.travis/run-tests-python2.sh +++ b/.travis/run-tests-python2.sh @@ -2,8 +2,10 @@ set -euo pipefail +python --version + mvn -q -Dparallel=methods -DthreadCount=4 -Dtest=python2.* test cd ../runtime/Python2/tests -python run.py \ No newline at end of file +python run.py diff --git a/.travis/run-tests-python3.sh b/.travis/run-tests-python3.sh index 9b528f35b..8b74928c5 100755 --- a/.travis/run-tests-python3.sh +++ b/.travis/run-tests-python3.sh @@ -2,8 +2,10 @@ set -euo pipefail +python3 --version + mvn -q -Dparallel=methods -DthreadCount=4 -Dtest=python3.* test cd ../runtime/Python3/test -python3.6 run.py \ No newline at end of file +python3 run.py diff --git a/.travis/run-tests-swift.sh b/.travis/run-tests-swift.sh index 0e297c503..677f356b2 100755 --- a/.travis/run-tests-swift.sh +++ b/.travis/run-tests-swift.sh @@ -6,7 +6,7 @@ set -euo pipefail # here since environment variables doesn't pass # across scripts if [ $TRAVIS_OS_NAME == "linux" ]; then - export SWIFT_VERSION=swift-4.2.1 + export SWIFT_VERSION=swift-5.0.1 export SWIFT_HOME=$(pwd)/swift/$SWIFT_VERSION-RELEASE-ubuntu16.04/usr/bin/ export PATH=$SWIFT_HOME:$PATH diff --git a/contributors.txt b/contributors.txt index 7ba0f2025..1fe97ab8b 100644 --- a/contributors.txt +++ b/contributors.txt @@ -214,7 +214,9 @@ YYYY/MM/DD, github id, Full name, email 2018/11/29, hannemann-tamas, Ralf Hannemann-Tamas, ralf.ht@gmail.com 2018/12/20, WalterCouto, Walter Couto, WalterCouto@users.noreply.github.com 2018/12/23, youkaichao, Kaichao You, youkaichao@gmail.com +2019/01/16, kuegi, Markus Zancolo, markus.zancolo@roomle.com 2019/02/06, ralucado, Cristina Raluca Vijulie, ralucris.v[at]gmail[dot]com +2019/02/23, gedimitr, Gerasimos Dimitriadis, gedimitr@gmail.com 2019/03/13, base698, Justin Thomas, justin.thomas1@gmail.com 2019/03/18, carlodri, Carlo Dri, carlo.dri@gmail.com 2019/05/02, askingalot, Andy Collins, askingalot@gmail.com @@ -231,3 +233,7 @@ YYYY/MM/DD, github id, Full name, email 2019/09/17, kaz, Kazuki Sawada, kazuki@6715.jp 2019/09/28, lmy269, Mingyang Liu, lmy040758@gmail.com 2019/10/29, tehbone, Tabari Alexander, tehbone@gmail.com +2019/10/31, a-square, Alexei Averchenko, lex.aver@gmail.com +2019/11/11, foxeverl, Liu Xinfeng, liuxf1986[at]gmail[dot]com +2019/11/17, felixn, Felix Nieuwenhuizhen, felix@tdlrali.com +2019/11/18, mlilback, Mark Lilback, mark@lilback.com \ No newline at end of file diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/CSharp.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/CSharp.test.stg index fb95440ce..cd9270286 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/CSharp.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/CSharp.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= <%%> Concat(a,b) ::= "" -DeclareLocal(s,v) ::= "Object = ;" - AssertIsList(v) ::= "System.Collections.IList __ttt__ = ;" // just use static type system AssignLocal(s,v) ::= " = ;" @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%this. = ;%> AddMember(n,v) ::= <%this. += ;%> -PlusMember(v,n) ::= <% + this.%> - MemberEquals(n,v) ::= <%this. == %> ModMemberEquals(n,m,v) ::= <%this. % == %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Chrome.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Chrome.test.stg index 070409c21..f4a42e5a5 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Chrome.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Chrome.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= <%%> Concat(a,b) ::= "" -DeclareLocal(s,v) ::= "var = ;" - AssertIsList(v) ::= <> AssignLocal(s,v) ::= " = ;" @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%this. = ;%> AddMember(n,v) ::= <%this. += ;%> -PlusMember(v,n) ::= <% + this.%> - MemberEquals(n,v) ::= <%this. === %> ModMemberEquals(n,m,v) ::= <%this. % === %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Cpp.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Cpp.test.stg index bd3f0dd79..a5dab18bd 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Cpp.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Cpp.test.stg @@ -11,8 +11,6 @@ Append(a,b) ::= " + ->toString()" Concat(a,b) ::= "" AppendStr(a,b) ::= " + " -DeclareLocal(s,v) ::= " = " - AssertIsList(v) ::= "assert(.size() >= 0);" // Use a method that exists only on a list (vector actually). AssignLocal(s,v) ::= " = ;" @@ -25,7 +23,6 @@ VarRef(n) ::= "" GetMember(n) ::= "" SetMember(n,v) ::= " = ;" AddMember(n,v) ::= " += ;" -PlusMember(v,n) ::= " + " MemberEquals(n,v) ::= " == " ModMemberEquals(n,m,v) ::= " % == " ModMemberNotEquals(n,m,v) ::= " % != " diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Explorer.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Explorer.test.stg index a3d4cb026..cf2802edc 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Explorer.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Explorer.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= <%%> Concat(a,b) ::= "" -DeclareLocal(s,v) ::= "var = ;" - AssertIsList(v) ::= <> AssignLocal(s,v) ::= " = ;" @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%this. = ;%> AddMember(n,v) ::= <%this. += ;%> -PlusMember(v,n) ::= <% + this.%> - MemberEquals(n,v) ::= <%this. === %> ModMemberEquals(n,m,v) ::= <%this. % === %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Firefox.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Firefox.test.stg index c8a6c1d57..c19473f5e 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Firefox.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Firefox.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= <%%> Concat(a,b) ::= "" -DeclareLocal(s,v) ::= "var = ;" - AssertIsList(v) ::= <> AssignLocal(s,v) ::= " = ;" @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%this. = ;%> AddMember(n,v) ::= <%this. += ;%> -PlusMember(v,n) ::= <% + this.%> - MemberEquals(n,v) ::= <%this. === %> ModMemberEquals(n,m,v) ::= <%this. % === %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Go.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Go.test.stg index b2909f4e1..ec38c5604 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Go.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Go.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= " + " Concat(a,b) ::= "" -DeclareLocal(s, v) ::= "var = " - AssertIsList(v) ::= "" AssignLocal(s, v) ::= " = ;" @@ -40,8 +38,6 @@ SetMember(n, v) ::= <% = ;%> AddMember(n, v) ::= <% += ;%> -PlusMember(v, n) ::= <% + fmt.Sprint()%> - MemberEquals(n, v) ::= <% == %> ModMemberEquals(n, m, v) ::= <% % == %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Java.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Java.test.stg index 11f159743..16607933f 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Java.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Java.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= <%%> Concat(a,b) ::= "" -DeclareLocal(s,v) ::= "Object = ;" - AssertIsList(v) ::= "List\ __ttt__ = ;" // just use static type system AssignLocal(s,v) ::= " = ;" @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%this. = ;%> AddMember(n,v) ::= <%this. += ;%> -PlusMember(v,n) ::= <% + this.%> - MemberEquals(n,v) ::= <%this. == %> ModMemberEquals(n,m,v) ::= <%this. % == %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Node.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Node.test.stg index 81573d6c0..926bd6fe1 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Node.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Node.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= <%%> Concat(a,b) ::= "" -DeclareLocal(s,v) ::= "var = ;" - AssertIsList(v) ::= <> AssignLocal(s,v) ::= " = ;" @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%this. = ;%> AddMember(n,v) ::= <%this. += ;%> -PlusMember(v,n) ::= <% + this.%> - MemberEquals(n,v) ::= <%this. === %> ModMemberEquals(n,m,v) ::= <%this. % === %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python2.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python2.test.stg index 3102f4120..2292cafe7 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python2.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python2.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= " + " Concat(a,b) ::= "" -DeclareLocal(s,v) ::= " = " - AssertIsList(v) ::= "assert isinstance(v, (list, tuple))" AssignLocal(s,v) ::= " = " @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%self. = %> AddMember(n,v) ::= <%self. += %> -PlusMember(v,n) ::= <% + str(self.)%> - MemberEquals(n,v) ::= <%self. == %> ModMemberEquals(n,m,v) ::= <%self. % == %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python3.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python3.test.stg index 858b76113..65dcdcd83 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python3.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python3.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= " + " Concat(a,b) ::= "" -DeclareLocal(s,v) ::= " = " - AssertIsList(v) ::= "assert isinstance(v, (list, tuple))" AssignLocal(s,v) ::= " = " @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%self. = %> AddMember(n,v) ::= <%self. += %> -PlusMember(v,n) ::= <% + str(self.)%> - MemberEquals(n,v) ::= <%self. == %> ModMemberEquals(n,m,v) ::= <%self. % == %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Safari.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Safari.test.stg index 2a09970c2..e84ff373d 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Safari.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Safari.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= <%%> Concat(a,b) ::= "" -DeclareLocal(s,v) ::= "var = ;" - AssertIsList(v) ::= <> AssignLocal(s,v) ::= " = ;" @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%this. = ;%> AddMember(n,v) ::= <%this. += ;%> -PlusMember(v,n) ::= <% + this.%> - MemberEquals(n,v) ::= <%this. === %> ModMemberEquals(n,m,v) ::= <%this. % === %> diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Swift.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Swift.test.stg index d45168517..c2c12b63c 100755 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Swift.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Swift.test.stg @@ -18,8 +18,6 @@ AppendStr(a,b) ::= <%%> Concat(a,b) ::= "" -DeclareLocal(s,v) ::= "var = " - AssertIsList(v) ::= "var __ttt__ = ;" // just use static type system AssignLocal(s,v) ::= " = " @@ -40,8 +38,6 @@ SetMember(n,v) ::= <%self. = %> AddMember(n,v) ::= <%self. += %> -PlusMember(v,n) ::= <% + self.%> - MemberEquals(n,v) ::= <%self. == %> ModMemberEquals(n,m,v) ::= <%self. % == %> @@ -133,7 +129,7 @@ open func emit() -> Token { private func handleAcceptPositionForIdentifier() -> Bool { let tokenText = getText() var identifierLength = 0 - while ((identifierLength \< tokenText.characters.count) && isIdentifierChar(tokenText[tokenText.characters.index(tokenText.startIndex, offsetBy: identifierLength)])) { + while ((identifierLength \< tokenText.count) && isIdentifierChar(tokenText[tokenText.index(tokenText.startIndex, offsetBy: identifierLength)])) { identifierLength += 1 } @@ -147,8 +143,8 @@ private func handleAcceptPositionForIdentifier() -> Bool { } private func handleAcceptPositionForKeyword(_ keyword:String) -> Bool { - if getInputStream()!.index() > _tokenStartCharIndex + keyword.characters.count { - let offset = keyword.characters.count - 1 + if getInputStream()!.index() > _tokenStartCharIndex + keyword.count { + let offset = keyword.count - 1 (getInterpreter() as! PositionAdjustingLexerATNSimulator).resetAcceptPosition(getInputStream()!, _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset) return true } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java index 34bb56ff7..2e71deb9b 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java @@ -57,12 +57,23 @@ public class BaseSwiftTest implements RuntimeTestSupport { throw new RuntimeException("Swift runtime file not found at:" + swiftRuntime.getPath()); } ANTLR_RUNTIME_PATH = swiftRuntime.getPath(); - fastFailRunProcess(ANTLR_RUNTIME_PATH, SWIFT_CMD, "build"); + try { + fastFailRunProcess(ANTLR_RUNTIME_PATH, SWIFT_CMD, "build"); + } + catch (IOException | InterruptedException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } // shutdown logic Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { - fastFailRunProcess(ANTLR_RUNTIME_PATH, SWIFT_CMD, "package", "clean"); + try { + fastFailRunProcess(ANTLR_RUNTIME_PATH, SWIFT_CMD, "package", "clean"); + } + catch (IOException | InterruptedException e) { + e.printStackTrace(); + } } }); } @@ -145,8 +156,14 @@ public class BaseSwiftTest implements RuntimeTestSupport { String projectName = "testcase-" + System.currentTimeMillis(); String projectDir = getTmpDir() + "/" + projectName; - buildProject(projectDir, projectName); - return execTest(projectDir, projectName); + try { + buildProject(projectDir, projectName); + return execTest(projectDir, projectName); + } + catch (IOException | InterruptedException e) { + e.printStackTrace(); + return null; + } } @Override @@ -183,7 +200,7 @@ public class BaseSwiftTest implements RuntimeTestSupport { Collections.addAll(this.sourceFiles, files); } - private void buildProject(String projectDir, String projectName) { + private void buildProject(String projectDir, String projectName) throws IOException, InterruptedException { mkdir(projectDir); fastFailRunProcess(projectDir, SWIFT_CMD, "package", "init", "--type", "executable"); for (String sourceFile: sourceFiles) { @@ -191,20 +208,16 @@ public class BaseSwiftTest implements RuntimeTestSupport { fastFailRunProcess(getTmpDir(), "mv", "-f", absPath, projectDir + "/Sources/" + projectName); } fastFailRunProcess(getTmpDir(), "mv", "-f", "input", projectDir); - - try { - String dylibPath = ANTLR_RUNTIME_PATH + "/.build/debug/"; - Pair buildResult = runProcess(projectDir, SWIFT_CMD, "build", - "-Xswiftc", "-I"+dylibPath, - "-Xlinker", "-L"+dylibPath, - "-Xlinker", "-lAntlr4", - "-Xlinker", "-rpath", - "-Xlinker", dylibPath); - if (buildResult.b.length() > 0) { - throw new RuntimeException("unit test build failed: " + buildResult.a + "\n" + buildResult.b); - } - } catch (IOException | InterruptedException e) { - e.printStackTrace(); + String dylibPath = ANTLR_RUNTIME_PATH + "/.build/debug/"; +// System.err.println(dylibPath); + Pair buildResult = runProcess(projectDir, SWIFT_CMD, "build", + "-Xswiftc", "-I"+dylibPath, + "-Xlinker", "-L"+dylibPath, + "-Xlinker", "-lAntlr4", + "-Xlinker", "-rpath", + "-Xlinker", dylibPath); + if (buildResult.b.length() > 0) { + throw new IOException("unit test build failed: " + buildResult.a + "\n" + buildResult.b); } } @@ -214,20 +227,22 @@ public class BaseSwiftTest implements RuntimeTestSupport { StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream()); stdoutVacuum.start(); stderrVacuum.start(); - process.waitFor(); + int status = process.waitFor(); stdoutVacuum.join(); stderrVacuum.join(); + if (status != 0) { + throw new IOException("Process exited with status " + status + ":\n" + stdoutVacuum.toString() + "\n" + stderrVacuum.toString()); + } return new Pair<>(stdoutVacuum.toString(), stderrVacuum.toString()); } - private static void fastFailRunProcess(String workingDir, String... command) { + private static void fastFailRunProcess(String workingDir, String... command) throws IOException, InterruptedException { ProcessBuilder builder = new ProcessBuilder(command); builder.directory(new File(workingDir)); - try { - Process p = builder.start(); - p.waitFor(); - } catch (Exception e) { - e.printStackTrace(); + Process p = builder.start(); + int status = p.waitFor(); + if (status != 0) { + throw new IOException("Process exited with status " + status); } } @@ -251,8 +266,14 @@ public class BaseSwiftTest implements RuntimeTestSupport { addSourceFiles("main.swift"); String projectName = "testcase-" + System.currentTimeMillis(); String projectDir = getTmpDir() + "/" + projectName; - buildProject(projectDir, projectName); - return execTest(projectDir, projectName); + try { + buildProject(projectDir, projectName); + return execTest(projectDir, projectName); + } + catch (IOException | InterruptedException e) { + e.printStackTrace(); + return null; + } } private void writeParserTestFile(String parserName, diff --git a/runtime/Cpp/CMakeLists.txt b/runtime/Cpp/CMakeLists.txt index 28b19a195..390078151 100644 --- a/runtime/Cpp/CMakeLists.txt +++ b/runtime/Cpp/CMakeLists.txt @@ -122,7 +122,9 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND ( CMAKE_SYSTEM_NAME MATCH endif() elseif(MSVC_VERSION GREATER 1800 OR MSVC_VERSION EQUAL 1800) # Visual Studio 2012+ supports c++11 features -else () +elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++") +else() message(FATAL_ERROR "Your C++ compiler does not support C++11.") endif() diff --git a/runtime/Cpp/cmake/ExternalAntlr4Cpp.cmake b/runtime/Cpp/cmake/ExternalAntlr4Cpp.cmake index 675cbf579..3523cf2d3 100644 --- a/runtime/Cpp/cmake/ExternalAntlr4Cpp.cmake +++ b/runtime/Cpp/cmake/ExternalAntlr4Cpp.cmake @@ -109,6 +109,12 @@ else() endif() # Seperate build step as rarely people want both +set(ANTLR4_BUILD_DIR ${ANTLR4_ROOT}) +if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.14.0") + # CMake 3.14 builds in above's SOURCE_SUBDIR when BUILD_IN_SOURCE is true + set(ANTLR4_BUILD_DIR ${ANTLR4_ROOT}/runtime/Cpp) +endif() + ExternalProject_Add_Step( antlr4_runtime build_static @@ -118,7 +124,7 @@ ExternalProject_Add_Step( DEPENDS antlr4_runtime BYPRODUCTS ${ANTLR4_STATIC_LIBRARIES} EXCLUDE_FROM_MAIN 1 - WORKING_DIRECTORY ${ANTLR4_ROOT}) + WORKING_DIRECTORY ${ANTLR4_BUILD_DIR}) ExternalProject_Add_StepTargets(antlr4_runtime build_static) add_library(antlr4_static STATIC IMPORTED) @@ -135,7 +141,7 @@ ExternalProject_Add_Step( DEPENDS antlr4_runtime BYPRODUCTS ${ANTLR4_SHARED_LIBRARIES} ${ANTLR4_RUNTIME_LIBRARIES} EXCLUDE_FROM_MAIN 1 - WORKING_DIRECTORY ${ANTLR4_ROOT}) + WORKING_DIRECTORY ${ANTLR4_BUILD_DIR}) ExternalProject_Add_StepTargets(antlr4_runtime build_shared) add_library(antlr4_shared SHARED IMPORTED) diff --git a/runtime/Cpp/cmake/README.md b/runtime/Cpp/cmake/README.md index e27c77e50..1db315321 100644 --- a/runtime/Cpp/cmake/README.md +++ b/runtime/Cpp/cmake/README.md @@ -40,7 +40,8 @@ antlr_target(SampleGrammarLexer TLexer.g4 LEXER PACKAGE antlrcpptest) antlr_target(SampleGrammarParser TParser.g4 PARSER PACKAGE antlrcpptest - DEPENDS_ANTLR SampleGrammarLexer) + DEPENDS_ANTLR SampleGrammarLexer + COMPILE_FLAGS -lib ${ANTLR_SampleGrammarLexer_OUTPUT_DIR}) # include generated files in project environment include_directories(${ANTLR_SampleGrammarLexer_OUTPUT_DIR}) diff --git a/runtime/Cpp/runtime/antlr4cpp-vs2019.vcxproj b/runtime/Cpp/runtime/antlr4cpp-vs2019.vcxproj new file mode 100644 index 000000000..b93d8f641 --- /dev/null +++ b/runtime/Cpp/runtime/antlr4cpp-vs2019.vcxproj @@ -0,0 +1,651 @@ + + + + + Debug Static + Win32 + + + Debug Static + x64 + + + Debug DLL + Win32 + + + Debug DLL + x64 + + + Release Static + Win32 + + + Release Static + x64 + + + Release DLL + Win32 + + + Release DLL + x64 + + + + {83BE66CD-9C4F-4F84-B72A-DD1855C8FC8A} + Win32Proj + antlr4cpp + 10.0 + + + + DynamicLibrary + true + Unicode + v142 + + + StaticLibrary + true + Unicode + v142 + + + DynamicLibrary + true + Unicode + v142 + + + StaticLibrary + true + Unicode + v142 + + + DynamicLibrary + false + true + Unicode + v142 + + + StaticLibrary + false + true + Unicode + v142 + + + DynamicLibrary + false + true + Unicode + v142 + + + StaticLibrary + false + true + Unicode + v142 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)bin\vs-2019\$(PlatformTarget)\$(Configuration)\ + $(SolutionDir)obj\$(PlatformTarget)\$(Configuration)\$(ProjectName)\ + antlr4-runtime + + + true + $(SolutionDir)bin\vs-2019\$(PlatformTarget)\$(Configuration)\ + $(SolutionDir)obj\$(PlatformTarget)\$(Configuration)\$(ProjectName)\ + antlr4-runtime + + + true + $(SolutionDir)bin\vs-2019\$(PlatformTarget)\$(Configuration)\ + $(SolutionDir)obj\$(PlatformTarget)\$(Configuration)\$(ProjectName)\ + antlr4-runtime + + + true + $(SolutionDir)bin\vs-2019\$(PlatformTarget)\$(Configuration)\ + $(SolutionDir)obj\$(PlatformTarget)\$(Configuration)\$(ProjectName)\ + antlr4-runtime + + + false + $(SolutionDir)bin\vs-2019\$(PlatformTarget)\$(Configuration)\ + $(SolutionDir)obj\$(PlatformTarget)\$(Configuration)\$(ProjectName)\ + antlr4-runtime + + + false + $(SolutionDir)bin\vs-2019\$(PlatformTarget)\$(Configuration)\ + $(SolutionDir)obj\$(PlatformTarget)\$(Configuration)\$(ProjectName)\ + antlr4-runtime + + + false + $(SolutionDir)bin\vs-2019\$(PlatformTarget)\$(Configuration)\ + $(SolutionDir)obj\$(PlatformTarget)\$(Configuration)\$(ProjectName)\ + antlr4-runtime + + + false + $(SolutionDir)bin\vs-2019\$(PlatformTarget)\$(Configuration)\ + $(SolutionDir)obj\$(PlatformTarget)\$(Configuration)\$(ProjectName)\ + antlr4-runtime + + + + Level4 + Disabled + ANTLR4CPP_EXPORTS;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) + + + + + 4251 + true + false + + + Windows + true + + + + + Level4 + Disabled + ANTLR4CPP_EXPORTS;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) + + + + + 4251 + true + false + + + Windows + true + + + + + Level4 + Disabled + ANTLR4CPP_EXPORTS;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) + + + + + 4251 + true + false + + + Windows + true + + + + + Level4 + Disabled + ANTLR4CPP_STATIC;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) + + + + + 4251 + true + false + + + Windows + true + + + + + Level4 + MaxSpeed + true + true + ANTLR4CPP_DLL;ANTLR4CPP_EXPORTS;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) + + + + + 4251 + true + + + Windows + true + true + true + + + + + Level4 + MaxSpeed + true + true + ANTLR4CPP_EXPORTS;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) + + + + + 4251 + true + + + Windows + true + true + true + + + + + Level4 + MaxSpeed + true + true + ANTLR4CPP_DLL;ANTLR4CPP_EXPORTS;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) + + + + + 4251 + true + + + Windows + true + true + true + + + + + Level4 + MaxSpeed + true + true + ANTLR4CPP_STATIC;%(PreprocessorDefinitions) + src;%(AdditionalIncludeDirectories) + + + + + 4251 + true + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/runtime/Cpp/runtime/antlr4cpp-vs2019.vcxproj.filters b/runtime/Cpp/runtime/antlr4cpp-vs2019.vcxproj.filters new file mode 100644 index 000000000..eb3b687be --- /dev/null +++ b/runtime/Cpp/runtime/antlr4cpp-vs2019.vcxproj.filters @@ -0,0 +1,987 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {587a2726-4856-4d21-937a-fbaebaa90232} + + + {2662156f-1508-4dad-b991-a8298a6db9bf} + + + {5b1e59b1-7fa5-46a5-8d92-965bd709cca0} + + + {9de9fe74-5d67-441d-a972-3cebe6dfbfcc} + + + {89fd3896-0ab1-476d-8d64-a57f10a5e73b} + + + {23939d7b-8e11-421e-80eb-b2cfdfdd64e9} + + + {05f2bacb-b5b2-4ca3-abe1-ca9a7239ecaa} + + + {d3b2ae2d-836b-4c73-8180-aca4ebb7d658} + + + {6674a0f0-c65d-4a00-a9e5-1f243b89d0a2} + + + {1893fffe-7a2b-4708-8ce5-003aa9b749f7} + + + {053a0632-27bc-4043-b5e8-760951b3b5b9} + + + {048c180d-44cf-49ca-a7aa-d0053fea07f5} + + + {3181cae5-cc15-4050-8c45-22af44a823de} + + + {290632d2-c56e-4005-a417-eb83b9531e1a} + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\dfa + + + Header Files\dfa + + + Header Files\dfa + + + Header Files\dfa + + + Header Files\misc + + + Header Files\misc + + + Header Files\misc + + + Header Files\support + + + Header Files\support + + + Header Files\support + + + Header Files\support + + + Header Files\support + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree + + + Header Files\tree\pattern + + + Header Files\tree\pattern + + + Header Files\tree\pattern + + + Header Files\tree\pattern + + + Header Files\tree\pattern + + + Header Files\tree\pattern + + + Header Files\tree\pattern + + + Header Files\tree\pattern + + + Header Files\tree\xpath + + + Header Files + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\atn + + + Header Files\misc + + + Header Files + + + Header Files + + + Header Files\support + + + Header Files\tree\xpath + + + Header Files\tree\xpath + + + Header Files\tree\xpath + + + Header Files\tree\xpath + + + Header Files\tree\xpath + + + Header Files\tree\xpath + + + Header Files\tree\xpath + + + Header Files\tree\xpath + + + Header Files\tree\xpath + + + Header Files + + + Header Files + + + Header Files\tree + + + Header Files + + + Header Files\support + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\dfa + + + Source Files\dfa + + + Source Files\dfa + + + Source Files\dfa + + + Source Files\misc + + + Source Files\misc + + + Source Files\misc + + + Source Files\support + + + Source Files\support + + + Source Files\support + + + Source Files\tree + + + Source Files\tree + + + Source Files\tree + + + Source Files\tree + + + Source Files\tree\pattern + + + Source Files\tree\pattern + + + Source Files\tree\pattern + + + Source Files\tree\pattern + + + Source Files\tree\pattern + + + Source Files\tree\pattern + + + Source Files\tree\pattern + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files\atn + + + Source Files + + + Source Files + + + Source Files\support + + + Source Files\tree\xpath + + + Source Files\tree\xpath + + + Source Files\tree\xpath + + + Source Files\tree\xpath + + + Source Files\tree\xpath + + + Source Files\tree\xpath + + + Source Files\tree\xpath + + + Source Files\tree\xpath + + + Source Files\tree\xpath + + + Source Files\tree\xpath + + + Source Files + + + Source Files\tree + + + Source Files\tree + + + Source Files + + + Source Files + + + Source Files + + + Source Files\atn + + + Source Files\atn + + + Source Files\misc + + + Source Files + + + Source Files + + + Source Files + + + Source Files\support + + + Source Files\tree + + + Source Files\tree + + + Source Files\tree + + + Source Files\tree + + + Source Files\tree\pattern + + + \ No newline at end of file diff --git a/runtime/Cpp/runtime/antlrcpp.xcodeproj/xcshareddata/xcschemes/antlr4.xcscheme b/runtime/Cpp/runtime/antlrcpp.xcodeproj/xcshareddata/xcschemes/antlr4.xcscheme index f2e132436..dc8e3432a 100644 --- a/runtime/Cpp/runtime/antlrcpp.xcodeproj/xcshareddata/xcschemes/antlr4.xcscheme +++ b/runtime/Cpp/runtime/antlrcpp.xcodeproj/xcshareddata/xcschemes/antlr4.xcscheme @@ -29,8 +29,6 @@ shouldUseLaunchSchemeArgsEnv = "YES"> - - - - = _tokens.size()) { stop = _tokens.size() - 1; } diff --git a/runtime/Cpp/runtime/src/NoViableAltException.cpp b/runtime/Cpp/runtime/src/NoViableAltException.cpp index ced7f827f..273c208c7 100755 --- a/runtime/Cpp/runtime/src/NoViableAltException.cpp +++ b/runtime/Cpp/runtime/src/NoViableAltException.cpp @@ -9,6 +9,20 @@ using namespace antlr4; +namespace { + +// Create a normal shared pointer if the configurations are to be deleted. If not, then +// the shared pointer is created with a deleter that does nothing. +Ref buildConfigsRef(atn::ATNConfigSet *configs, bool deleteConfigs) { + if (deleteConfigs) { + return Ref(configs); + } else { + return Ref(configs, [](atn::ATNConfigSet *){}); + } +} + +} + NoViableAltException::NoViableAltException(Parser *recognizer) : NoViableAltException(recognizer, recognizer->getTokenStream(), recognizer->getCurrentToken(), recognizer->getCurrentToken(), nullptr, recognizer->getContext(), false) { @@ -17,12 +31,10 @@ NoViableAltException::NoViableAltException(Parser *recognizer) NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input,Token *startToken, Token *offendingToken, atn::ATNConfigSet *deadEndConfigs, ParserRuleContext *ctx, bool deleteConfigs) : RecognitionException("No viable alternative", recognizer, input, ctx, offendingToken), - _deadEndConfigs(deadEndConfigs), _startToken(startToken), _deleteConfigs(deleteConfigs) { + _deadEndConfigs(buildConfigsRef(deadEndConfigs, deleteConfigs)), _startToken(startToken) { } NoViableAltException::~NoViableAltException() { - if (_deleteConfigs) - delete _deadEndConfigs; } Token* NoViableAltException::getStartToken() const { @@ -30,5 +42,5 @@ Token* NoViableAltException::getStartToken() const { } atn::ATNConfigSet* NoViableAltException::getDeadEndConfigs() const { - return _deadEndConfigs; + return _deadEndConfigs.get(); } diff --git a/runtime/Cpp/runtime/src/NoViableAltException.h b/runtime/Cpp/runtime/src/NoViableAltException.h index 94d43c54c..b15039d0c 100755 --- a/runtime/Cpp/runtime/src/NoViableAltException.h +++ b/runtime/Cpp/runtime/src/NoViableAltException.h @@ -27,10 +27,9 @@ namespace antlr4 { private: /// Which configurations did we try at input.index() that couldn't match input.LT(1)? - atn::ATNConfigSet* _deadEndConfigs; - - // Flag that indicates if we own the dead end config set and have to delete it on destruction. - bool _deleteConfigs; + /// Shared pointer that conditionally deletes the configurations (based on flag + /// passed during construction) + Ref _deadEndConfigs; /// The token object at the start index; the input stream might /// not be buffering tokens so get a reference to it. (At the diff --git a/runtime/Cpp/runtime/src/RuleContext.cpp b/runtime/Cpp/runtime/src/RuleContext.cpp index 73cfe24e1..467e5ec42 100755 --- a/runtime/Cpp/runtime/src/RuleContext.cpp +++ b/runtime/Cpp/runtime/src/RuleContext.cpp @@ -75,16 +75,16 @@ antlrcpp::Any RuleContext::accept(tree::ParseTreeVisitor *visitor) { return visitor->visitChildren(this); } -std::string RuleContext::toStringTree(Parser *recog) { - return tree::Trees::toStringTree(this, recog); +std::string RuleContext::toStringTree(Parser *recog, bool pretty) { + return tree::Trees::toStringTree(this, recog, pretty); } -std::string RuleContext::toStringTree(std::vector &ruleNames) { - return tree::Trees::toStringTree(this, ruleNames); +std::string RuleContext::toStringTree(std::vector &ruleNames, bool pretty) { + return tree::Trees::toStringTree(this, ruleNames, pretty); } -std::string RuleContext::toStringTree() { - return toStringTree(nullptr); +std::string RuleContext::toStringTree(bool pretty) { + return toStringTree(nullptr, pretty); } diff --git a/runtime/Cpp/runtime/src/RuleContext.h b/runtime/Cpp/runtime/src/RuleContext.h index bf0ff6631..9ee0d2def 100755 --- a/runtime/Cpp/runtime/src/RuleContext.h +++ b/runtime/Cpp/runtime/src/RuleContext.h @@ -110,15 +110,15 @@ namespace antlr4 { /// (root child1 .. childN). Print just a node if this is a leaf. /// We have to know the recognizer so we can get rule names. /// - virtual std::string toStringTree(Parser *recog) override; + virtual std::string toStringTree(Parser *recog, bool pretty = false) override; /// /// Print out a whole tree, not just a node, in LISP format /// (root child1 .. childN). Print just a node if this is a leaf. /// - virtual std::string toStringTree(std::vector &ruleNames); + virtual std::string toStringTree(std::vector &ruleNames, bool pretty = false); - virtual std::string toStringTree() override; + virtual std::string toStringTree(bool pretty = false) override; virtual std::string toString() override; std::string toString(Recognizer *recog); std::string toString(const std::vector &ruleNames); diff --git a/runtime/Cpp/runtime/src/tree/ParseTree.h b/runtime/Cpp/runtime/src/tree/ParseTree.h index ee50b8039..088aac3ef 100755 --- a/runtime/Cpp/runtime/src/tree/ParseTree.h +++ b/runtime/Cpp/runtime/src/tree/ParseTree.h @@ -39,12 +39,12 @@ namespace tree { /// Print out a whole tree, not just a node, in LISP format /// {@code (root child1 .. childN)}. Print just a node if this is a leaf. - virtual std::string toStringTree() = 0; + virtual std::string toStringTree(bool pretty = false) = 0; virtual std::string toString() = 0; /// Specialize toStringTree so that it can print out more information /// based upon the parser. - virtual std::string toStringTree(Parser *parser) = 0; + virtual std::string toStringTree(Parser *parser, bool pretty = false) = 0; virtual bool operator == (const ParseTree &other) const; diff --git a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp index 0f806d4e7..7ab121b73 100755 --- a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp +++ b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp @@ -41,7 +41,7 @@ std::string TerminalNodeImpl::getText() { return symbol->getText(); } -std::string TerminalNodeImpl::toStringTree(Parser * /*parser*/) { +std::string TerminalNodeImpl::toStringTree(Parser * /*parser*/, bool /*pretty*/) { return toString(); } @@ -52,6 +52,6 @@ std::string TerminalNodeImpl::toString() { return symbol->getText(); } -std::string TerminalNodeImpl::toStringTree() { +std::string TerminalNodeImpl::toStringTree(bool /*pretty*/) { return toString(); } diff --git a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.h b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.h index d865c58e8..6f65d8204 100755 --- a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.h +++ b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.h @@ -23,9 +23,9 @@ namespace tree { virtual antlrcpp::Any accept(ParseTreeVisitor *visitor) override; virtual std::string getText() override; - virtual std::string toStringTree(Parser *parser) override; + virtual std::string toStringTree(Parser *parser, bool pretty = false) override; virtual std::string toString() override; - virtual std::string toStringTree() override; + virtual std::string toStringTree(bool pretty = false) override; }; diff --git a/runtime/Cpp/runtime/src/tree/Trees.cpp b/runtime/Cpp/runtime/src/tree/Trees.cpp index fabad0141..72b9b8b48 100755 --- a/runtime/Cpp/runtime/src/tree/Trees.cpp +++ b/runtime/Cpp/runtime/src/tree/Trees.cpp @@ -25,17 +25,17 @@ using namespace antlrcpp; Trees::Trees() { } -std::string Trees::toStringTree(ParseTree *t) { - return toStringTree(t, nullptr); +std::string Trees::toStringTree(ParseTree *t, bool pretty) { + return toStringTree(t, nullptr, pretty); } -std::string Trees::toStringTree(ParseTree *t, Parser *recog) { +std::string Trees::toStringTree(ParseTree *t, Parser *recog, bool pretty) { if (recog == nullptr) - return toStringTree(t, std::vector()); - return toStringTree(t, recog->getRuleNames()); + return toStringTree(t, std::vector(), pretty); + return toStringTree(t, recog->getRuleNames(), pretty); } -std::string Trees::toStringTree(ParseTree *t, const std::vector &ruleNames) { +std::string Trees::toStringTree(ParseTree *t, const std::vector &ruleNames, bool pretty) { std::string temp = antlrcpp::escapeWhitespace(Trees::getNodeText(t, ruleNames), false); if (t->children.empty()) { return temp; @@ -48,6 +48,7 @@ std::string Trees::toStringTree(ParseTree *t, const std::vector &ru std::stack stack; size_t childIndex = 0; ParseTree *run = t; + size_t indentationLevel = 1; while (childIndex < run->children.size()) { if (childIndex > 0) { ss << ' '; @@ -59,6 +60,13 @@ std::string Trees::toStringTree(ParseTree *t, const std::vector &ru stack.push(childIndex); run = child; childIndex = 0; + if (pretty) { + ++indentationLevel; + ss << std::endl; + for (size_t i = 0; i < indentationLevel; ++i) { + ss << " "; + } + } ss << "(" << temp << " "; } else { ss << temp; @@ -68,6 +76,9 @@ std::string Trees::toStringTree(ParseTree *t, const std::vector &ru childIndex = stack.top(); stack.pop(); run = run->parent; + if (pretty) { + --indentationLevel; + } ss << ")"; } else { break; diff --git a/runtime/Cpp/runtime/src/tree/Trees.h b/runtime/Cpp/runtime/src/tree/Trees.h index e6a1bb88e..d9d04624f 100755 --- a/runtime/Cpp/runtime/src/tree/Trees.h +++ b/runtime/Cpp/runtime/src/tree/Trees.h @@ -18,17 +18,17 @@ namespace tree { /// Print out a whole tree in LISP form. getNodeText is used on the /// node payloads to get the text for the nodes. Detect /// parse trees and extract data appropriately. - static std::string toStringTree(ParseTree *t); + static std::string toStringTree(ParseTree *t, bool pretty = false); /// Print out a whole tree in LISP form. getNodeText is used on the /// node payloads to get the text for the nodes. Detect /// parse trees and extract data appropriately. - static std::string toStringTree(ParseTree *t, Parser *recog); + static std::string toStringTree(ParseTree *t, Parser *recog, bool pretty = false); /// Print out a whole tree in LISP form. getNodeText is used on the /// node payloads to get the text for the nodes. Detect /// parse trees and extract data appropriately. - static std::string toStringTree(ParseTree *t, const std::vector &ruleNames); + static std::string toStringTree(ParseTree *t, const std::vector &ruleNames, bool pretty = false); static std::string getNodeText(ParseTree *t, Parser *recog); static std::string getNodeText(ParseTree *t, const std::vector &ruleNames); diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp b/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp index 6dd13832d..c7cc3b864 100755 --- a/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp +++ b/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp @@ -25,11 +25,10 @@ const std::string XPath::NOT = "!"; XPath::XPath(Parser *parser, const std::string &path) { _parser = parser; _path = path; - _elements = split(path); } -std::vector XPath::split(const std::string &path) { - ANTLRFileStream in(path); +std::vector> XPath::split(const std::string &path) { + ANTLRInputStream in(path); XPathLexer lexer(&in); lexer.removeErrorListeners(); XPathLexerErrorListener listener; @@ -44,7 +43,7 @@ std::vector XPath::split(const std::string &path) { } std::vector tokens = tokenStream.getTokens(); - std::vector elements; + std::vector> elements; size_t n = tokens.size(); size_t i = 0; bool done = false; @@ -62,9 +61,9 @@ std::vector XPath::split(const std::string &path) { i++; next = tokens[i]; } - XPathElement pathElement = getXPathElement(next, anywhere); - pathElement.setInvert(invert); - elements.push_back(pathElement); + std::unique_ptr pathElement = getXPathElement(next, anywhere); + pathElement->setInvert(invert); + elements.push_back(std::move(pathElement)); i++; break; @@ -81,25 +80,26 @@ std::vector XPath::split(const std::string &path) { break; default : - throw IllegalArgumentException("Unknow path element " + el->toString()); + throw IllegalArgumentException("Unknown path element " + el->toString()); } } return elements; } -XPathElement XPath::getXPathElement(Token *wordToken, bool anywhere) { +std::unique_ptr XPath::getXPathElement(Token *wordToken, bool anywhere) { if (wordToken->getType() == Token::EOF) { throw IllegalArgumentException("Missing path element at end of path"); } + std::string word = wordToken->getText(); size_t ttype = _parser->getTokenType(word); ssize_t ruleIndex = _parser->getRuleIndex(word); switch (wordToken->getType()) { case XPathLexer::WILDCARD : if (anywhere) - return XPathWildcardAnywhereElement(); - return XPathWildcardElement(); + return std::unique_ptr(new XPathWildcardAnywhereElement()); + return std::unique_ptr(new XPathWildcardElement()); case XPathLexer::TOKEN_REF: case XPathLexer::STRING : @@ -107,35 +107,42 @@ XPathElement XPath::getXPathElement(Token *wordToken, bool anywhere) { throw IllegalArgumentException(word + " at index " + std::to_string(wordToken->getStartIndex()) + " isn't a valid token name"); } if (anywhere) - return XPathTokenAnywhereElement(word, (int)ttype); - return XPathTokenElement(word, (int)ttype); + return std::unique_ptr(new XPathTokenAnywhereElement(word, (int)ttype)); + return std::unique_ptr(new XPathTokenElement(word, (int)ttype)); default : if (ruleIndex == -1) { throw IllegalArgumentException(word + " at index " + std::to_string(wordToken->getStartIndex()) + " isn't a valid rule name"); } if (anywhere) - return XPathRuleAnywhereElement(word, (int)ruleIndex); - return XPathRuleElement(word, (int)ruleIndex); + return std::unique_ptr(new XPathRuleAnywhereElement(word, (int)ruleIndex)); + return std::unique_ptr(new XPathRuleElement(word, (int)ruleIndex)); } } static ParserRuleContext dummyRoot; +std::vector XPath::findAll(ParseTree *tree, std::string const& xpath, Parser *parser) { + XPath p(parser, xpath); + return p.evaluate(tree); +} + std::vector XPath::evaluate(ParseTree *t) { dummyRoot.children = { t }; // don't set t's parent. std::vector work = { &dummyRoot }; size_t i = 0; - while (i < _elements.size()) { + std::vector> elements = split(_path); + + while (i < elements.size()) { std::vector next; for (auto node : work) { if (!node->children.empty()) { // only try to match next element if it has children // e.g., //func/*/stat might have a token node for which // we can't go looking for stat nodes. - auto matching = _elements[i].evaluate(node); + auto matching = elements[i]->evaluate(node); next.insert(next.end(), matching.begin(), matching.end()); } } diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPath.h b/runtime/Cpp/runtime/src/tree/xpath/XPath.h index 07a35921c..e38d482d5 100755 --- a/runtime/Cpp/runtime/src/tree/xpath/XPath.h +++ b/runtime/Cpp/runtime/src/tree/xpath/XPath.h @@ -61,8 +61,10 @@ namespace xpath { XPath(Parser *parser, const std::string &path); virtual ~XPath() {} - // TO_DO: check for invalid token/rule names, bad syntax - virtual std::vector split(const std::string &path); + // TODO: check for invalid token/rule names, bad syntax + virtual std::vector> split(const std::string &path); + + static std::vector findAll(ParseTree *tree, std::string const& xpath, Parser *parser); /// Return a list of all nodes starting at {@code t} as root that satisfy the /// path. The root {@code /} is relative to the node passed to @@ -71,13 +73,12 @@ namespace xpath { protected: std::string _path; - std::vector _elements; Parser *_parser; /// Convert word like {@code *} or {@code ID} or {@code expr} to a path /// element. {@code anywhere} is {@code true} if {@code //} precedes the /// word. - virtual XPathElement getXPathElement(Token *wordToken, bool anywhere); + virtual std::unique_ptr getXPathElement(Token *wordToken, bool anywhere); }; } // namespace xpath diff --git a/runtime/Swift/Sources/Antlr4/DiagnosticErrorListener.swift b/runtime/Swift/Sources/Antlr4/DiagnosticErrorListener.swift index 4109f672b..144d34921 100644 --- a/runtime/Swift/Sources/Antlr4/DiagnosticErrorListener.swift +++ b/runtime/Swift/Sources/Antlr4/DiagnosticErrorListener.swift @@ -101,7 +101,7 @@ public class DiagnosticErrorListener: BaseErrorListener { let decision: Int = dfa.decision let ruleIndex: Int = dfa.atnStartState.ruleIndex! - var ruleNames: [String] = recognizer.getRuleNames() + let ruleNames: [String] = recognizer.getRuleNames() if ruleIndex < 0 || ruleIndex >= ruleNames.count { return String(decision) } diff --git a/runtime/Swift/Sources/Antlr4/Parser.swift b/runtime/Swift/Sources/Antlr4/Parser.swift index 1f70f2eea..75c134ce5 100644 --- a/runtime/Swift/Sources/Antlr4/Parser.swift +++ b/runtime/Swift/Sources/Antlr4/Parser.swift @@ -941,7 +941,7 @@ open class Parser: Recognizer { public func getRuleInvocationStack(_ p: RuleContext?) -> [String] { var p = p - var ruleNames = getRuleNames() + let ruleNames = getRuleNames() var stack = [String]() while let pWrap = p { // compute what follows who invoked us diff --git a/runtime/Swift/Sources/Antlr4/UnbufferedCharStream.swift b/runtime/Swift/Sources/Antlr4/UnbufferedCharStream.swift index 7ef18facb..6b2841d2a 100644 --- a/runtime/Swift/Sources/Antlr4/UnbufferedCharStream.swift +++ b/runtime/Swift/Sources/Antlr4/UnbufferedCharStream.swift @@ -343,6 +343,8 @@ fileprivate struct UInt8StreamIterator: IteratorProtocol { return nil case .opening, .open, .reading: break + @unknown default: + fatalError() } let count = stream.read(&buffer, maxLength: buffer.count) diff --git a/runtime/Swift/Sources/Antlr4/VocabularySingle.swift b/runtime/Swift/Sources/Antlr4/VocabularySingle.swift index 663ff5372..37cd3b23b 100644 --- a/runtime/Swift/Sources/Antlr4/VocabularySingle.swift +++ b/runtime/Swift/Sources/Antlr4/VocabularySingle.swift @@ -155,11 +155,9 @@ public class Vocabulary: Hashable { return String(tokenType) } - public var hashValue: Int { - return Unmanaged.passUnretained(self).toOpaque().hashValue -// return unsafeAddress(of: self).hashValue + public func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) } - } public func ==(lhs: Vocabulary, rhs: Vocabulary) -> Bool { diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift b/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift index b1069a126..cfbe8b615 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift @@ -126,20 +126,11 @@ public class ATNConfig: Hashable, CustomStringConvertible { } } - /// - /// An ATN configuration is equal to another if both have - /// the same state, they predict the same alternative, and - /// syntactic/semantic contexts are the same. - /// - - public var hashValue: Int { - var hashCode = MurmurHash.initialize(7) - hashCode = MurmurHash.update(hashCode, state.stateNumber) - hashCode = MurmurHash.update(hashCode, alt) - hashCode = MurmurHash.update(hashCode, context) - hashCode = MurmurHash.update(hashCode, semanticContext) - return MurmurHash.finish(hashCode, 4) - + public func hash(into hasher: inout Hasher) { + hasher.combine(state.stateNumber) + hasher.combine(alt) + hasher.combine(context) + hasher.combine(semanticContext) } public var description: String { @@ -166,6 +157,11 @@ public class ATNConfig: Hashable, CustomStringConvertible { } } +/// +/// An ATN configuration is equal to another if both have +/// the same state, they predict the same alternative, and +/// syntactic/semantic contexts are the same. +/// public func ==(lhs: ATNConfig, rhs: ATNConfig) -> Bool { if lhs === rhs { diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift b/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift index d1f6a1bb3..7da94377d 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift @@ -203,16 +203,16 @@ public final class ATNConfigSet: Hashable, CustomStringConvertible { return false } - public var hashValue: Int { + public func hash(into hasher: inout Hasher) { if isReadonly() { if cachedHashCode == -1 { - cachedHashCode = configsHashValue//configs.hashValue ; + cachedHashCode = configsHashValue } - - return cachedHashCode + hasher.combine(cachedHashCode) + } + else { + hasher.combine(configsHashValue) } - - return configsHashValue // configs.hashValue; } private var configsHashValue: Int { diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift b/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift index 65b02efc6..ce8349bc8 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift @@ -78,8 +78,8 @@ public class ATNDeserializer { /// internal func isFeatureSupported(_ feature: UUID, _ actualUuid: UUID) -> Bool { let supported = ATNDeserializer.SUPPORTED_UUIDS - guard let featureIndex = supported.index(of: feature), - let actualIndex = supported.index(of: actualUuid) else { + guard let featureIndex = supported.firstIndex(of: feature), + let actualIndex = supported.firstIndex(of: actualUuid) else { return false } return actualIndex >= featureIndex diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNState.swift b/runtime/Swift/Sources/Antlr4/atn/ATNState.swift index 02481d381..24f2dfab3 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNState.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNState.swift @@ -123,11 +123,10 @@ public class ATNState: Hashable, CustomStringConvertible { public internal(set) final var nextTokenWithinRule: IntervalSet? - public var hashValue: Int { - return stateNumber + public func hash(into hasher: inout Hasher) { + hasher.combine(stateNumber) } - public func isNonGreedyExitState() -> Bool { return false } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerATNConfig.swift b/runtime/Swift/Sources/Antlr4/atn/LexerATNConfig.swift index 3cacb48cb..1c4a0bb13 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerATNConfig.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerATNConfig.swift @@ -72,22 +72,14 @@ public class LexerATNConfig: ATNConfig { return passedThroughNonGreedyDecision } - override - /*public func hashCode() -> Int { - - }*/ - public var hashValue: Int { - var hashCode = MurmurHash.initialize(7) - hashCode = MurmurHash.update(hashCode, state.stateNumber) - hashCode = MurmurHash.update(hashCode, alt) - hashCode = MurmurHash.update(hashCode, context) - hashCode = MurmurHash.update(hashCode, semanticContext) - hashCode = MurmurHash.update(hashCode, passedThroughNonGreedyDecision ? 1 : 0) - hashCode = MurmurHash.update(hashCode, lexerActionExecutor) - return MurmurHash.finish(hashCode, 6) - + public override func hash(into hasher: inout Hasher) { + hasher.combine(state.stateNumber) + hasher.combine(alt) + hasher.combine(context) + hasher.combine(semanticContext) + hasher.combine(passedThroughNonGreedyDecision) + hasher.combine(lexerActionExecutor) } - } //useless diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerAction.swift index 99d604da5..5920e405b 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerAction.swift @@ -56,7 +56,7 @@ public class LexerAction: Hashable { fatalError(#function + " must be overridden") } - public var hashValue: Int { + public func hash(into hasher: inout Hasher) { fatalError(#function + " must be overridden") } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerActionExecutor.swift b/runtime/Swift/Sources/Antlr4/atn/LexerActionExecutor.swift index e33e92e9b..365f41a58 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerActionExecutor.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerActionExecutor.swift @@ -176,11 +176,9 @@ public class LexerActionExecutor: Hashable { } - public var hashValue: Int { - return self.hashCode + public func hash(into hasher: inout Hasher) { + hasher.combine(hashCode) } - - } public func ==(lhs: LexerActionExecutor, rhs: LexerActionExecutor) -> Bool { diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerChannelAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerChannelAction.swift index 4d099f28c..a8fd17db6 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerChannelAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerChannelAction.swift @@ -63,12 +63,9 @@ public final class LexerChannelAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, channel) - return MurmurHash.finish(hash, 2) + public override func hash(into hasher: inout Hasher) { + hasher.combine(getActionType()) + hasher.combine(channel) } public var description: String { diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerCustomAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerCustomAction.swift index 3fe954236..fc6741ec2 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerCustomAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerCustomAction.swift @@ -92,24 +92,17 @@ public final class LexerCustomAction: LexerAction { try lexer.action(nil, ruleIndex, actionIndex) } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, ruleIndex) - hash = MurmurHash.update(hash, actionIndex) - return MurmurHash.finish(hash, 3) + public override func hash(into hasher: inout Hasher) { + hasher.combine(ruleIndex) + hasher.combine(actionIndex) } - } public func ==(lhs: LexerCustomAction, rhs: LexerCustomAction) -> Bool { - if lhs === rhs { return true } - return lhs.ruleIndex == rhs.ruleIndex && lhs.actionIndex == rhs.actionIndex } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerIndexedCustomAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerIndexedCustomAction.swift index 501b2e637..b9db97289 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerIndexedCustomAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerIndexedCustomAction.swift @@ -96,24 +96,17 @@ public final class LexerIndexedCustomAction: LexerAction { } - public override var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, offset) - hash = MurmurHash.update(hash, action) - return MurmurHash.finish(hash, 2) + public override func hash(into hasher: inout Hasher) { + hasher.combine(offset) + hasher.combine(action) } - - } public func ==(lhs: LexerIndexedCustomAction, rhs: LexerIndexedCustomAction) -> Bool { - if lhs === rhs { return true } - return lhs.offset == rhs.offset && lhs.action == rhs.action - } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerModeAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerModeAction.swift index bdcaf6fdc..fca51619a 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerModeAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerModeAction.swift @@ -62,25 +62,20 @@ public final class LexerModeAction: LexerAction, CustomStringConvertible { public func execute(_ lexer: Lexer) { lexer.mode(mode) } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, mode) - return MurmurHash.finish(hash, 2) + + public override func hash(into hasher: inout Hasher) { + hasher.combine(mode) } + public var description: String { return "mode(\(mode))" } } public func ==(lhs: LexerModeAction, rhs: LexerModeAction) -> Bool { - if lhs === rhs { return true } - return lhs.mode == rhs.mode - } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerMoreAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerMoreAction.swift index bb9f197f3..c84f54ffd 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerMoreAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerMoreAction.swift @@ -56,23 +56,15 @@ public final class LexerMoreAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - return MurmurHash.finish(hash, 1) - + public override func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) } - public var description: String { return "more" } } public func ==(lhs: LexerMoreAction, rhs: LexerMoreAction) -> Bool { - return lhs === rhs - - } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerPopModeAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerPopModeAction.swift index f35e78304..68fbe3fa6 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerPopModeAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerPopModeAction.swift @@ -57,21 +57,15 @@ public final class LexerPopModeAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - return MurmurHash.finish(hash, 1) - + public override func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) } + public var description: String { return "popMode" } } public func ==(lhs: LexerPopModeAction, rhs: LexerPopModeAction) -> Bool { - return lhs === rhs - - } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerPushModeAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerPushModeAction.swift index fb432c497..ffac047aa 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerPushModeAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerPushModeAction.swift @@ -63,15 +63,10 @@ public final class LexerPushModeAction: LexerAction, CustomStringConvertible { lexer.pushMode(mode) } - - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, mode) - return MurmurHash.finish(hash, 2) - + public override func hash(into hasher: inout Hasher) { + hasher.combine(mode) } + public var description: String { return "pushMode(\(mode))" } @@ -79,10 +74,8 @@ public final class LexerPushModeAction: LexerAction, CustomStringConvertible { public func ==(lhs: LexerPushModeAction, rhs: LexerPushModeAction) -> Bool { - if lhs === rhs { return true } - return lhs.mode == rhs.mode } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerSkipAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerSkipAction.swift index bbdd06d2f..db8b1d8e7 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerSkipAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerSkipAction.swift @@ -56,19 +56,15 @@ public final class LexerSkipAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - return MurmurHash.finish(hash, 1) + public override func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) } + public var description: String { return "skip" } - } public func ==(lhs: LexerSkipAction, rhs: LexerSkipAction) -> Bool { - return lhs === rhs } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerTypeAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerTypeAction.swift index c08649ea3..8c7157be2 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerTypeAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerTypeAction.swift @@ -62,24 +62,18 @@ public class LexerTypeAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, type) - return MurmurHash.finish(hash, 2) + public override func hash(into hasher: inout Hasher) { + hasher.combine(type) } + public var description: String { return "type(\(type))" } - } public func ==(lhs: LexerTypeAction, rhs: LexerTypeAction) -> Bool { - if lhs === rhs { return true } - return lhs.type == rhs.type } diff --git a/runtime/Swift/Sources/Antlr4/atn/LookupATNConfig.swift b/runtime/Swift/Sources/Antlr4/atn/LookupATNConfig.swift index aef419b1d..d3a04a1cc 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LookupATNConfig.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LookupATNConfig.swift @@ -20,17 +20,12 @@ public class LookupATNConfig: Hashable { // dup config = old } - public var hashValue: Int { - - var hashCode: Int = 7 - hashCode = 31 * hashCode + config.state.stateNumber - hashCode = 31 * hashCode + config.alt - hashCode = 31 * hashCode + config.semanticContext.hashValue - return hashCode + public func hash(into hasher: inout Hasher) { + hasher.combine(config.state.stateNumber) + hasher.combine(config.alt) + hasher.combine(config.semanticContext) } - - } public func ==(lhs: LookupATNConfig, rhs: LookupATNConfig) -> Bool { diff --git a/runtime/Swift/Sources/Antlr4/atn/ParseInfo.swift b/runtime/Swift/Sources/Antlr4/atn/ParseInfo.swift index 244226a5c..2eb404592 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ParseInfo.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ParseInfo.swift @@ -40,7 +40,7 @@ public class ParseInfo { /// full-context predictions during parsing. /// public func getLLDecisions() -> Array { - var decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() + let decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() var LL: Array = Array() let length = decisions.count for i in 0.. Int64 { - var decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() + let decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() var t: Int64 = 0 let length = decisions.count for i in 0.. Int64 { - var decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() + let decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() var k: Int64 = 0 let length = decisions.count for i in 0.. Int64 { - var decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() + let decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() var k: Int64 = 0 let length = decisions.count for i in 0.. Int64 { - var decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() + let decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() var k: Int64 = 0 let length = decisions.count for i in 0.. Int64 { - var decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() + let decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() var k: Int64 = 0 let length = decisions.count for i in 0.. Int64 { - var decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() + let decisions: [DecisionInfo] = atnSimulator.getDecisionInfo() var k: Int64 = 0 let length = decisions.count for i in 0.. DFAState? { - var edges = previousD.edges + let edges = previousD.edges if edges == nil || (t + 1) < 0 || (t + 1) >= (edges!.count) { return nil } diff --git a/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift b/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift index 376088905..0e7026df4 100644 --- a/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift +++ b/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift @@ -105,8 +105,8 @@ public class PredictionContext: Hashable, CustomStringConvertible { return getReturnState(size() - 1) == PredictionContext.EMPTY_RETURN_STATE } - public final var hashValue: Int { - return cachedHashCode + public func hash(into hasher: inout Hasher) { + hasher.combine(cachedHashCode) } static func calculateEmptyHashCode() -> Int { @@ -668,7 +668,7 @@ public class PredictionContext: Hashable, CustomStringConvertible { } public func toString(_ recog: Recognizer) -> String { - return NSStringFromClass(PredictionContext.self) + return String(describing: PredictionContext.self) // return toString(recog, ParserRuleContext.EMPTY); } diff --git a/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift b/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift index 118f549f5..dffe24a1f 100644 --- a/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift +++ b/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift @@ -61,7 +61,7 @@ public class SemanticContext: Hashable, CustomStringConvertible { return self } - public var hashValue: Int { + public func hash(into hasher: inout Hasher) { fatalError(#function + " must be overridden") } @@ -94,16 +94,12 @@ public class SemanticContext: Hashable, CustomStringConvertible { return try parser.sempred(localctx, ruleIndex, predIndex) } - override - public var hashValue: Int { - var hashCode = MurmurHash.initialize() - hashCode = MurmurHash.update(hashCode, ruleIndex) - hashCode = MurmurHash.update(hashCode, predIndex) - hashCode = MurmurHash.update(hashCode, isCtxDependent ? 1 : 0) - return MurmurHash.finish(hashCode, 3) + public override func hash(into hasher: inout Hasher) { + hasher.combine(ruleIndex) + hasher.combine(predIndex) + hasher.combine(isCtxDependent) } - override public var description: String { return "{\(ruleIndex):\(predIndex)}?" @@ -138,11 +134,8 @@ public class SemanticContext: Hashable, CustomStringConvertible { } - override - public var hashValue: Int { - var hashCode: Int = 1 - hashCode = 31 * hashCode + precedence - return hashCode + public override func hash(into hasher: inout Hasher) { + hasher.combine(precedence) } override @@ -214,12 +207,8 @@ public class SemanticContext: Hashable, CustomStringConvertible { } - override - public var hashValue: Int { - //MurmurHash.hashCode(opnds, AND.class.hashCode()); - let seed = 1554547125 - //NSStringFromClass(AND.self).hashValue - return MurmurHash.hashCode(opnds, seed) + public override func hash(into hasher: inout Hasher) { + hasher.combine(opnds) } /// @@ -323,11 +312,8 @@ public class SemanticContext: Hashable, CustomStringConvertible { return opnds } - - override - public var hashValue: Int { - - return MurmurHash.hashCode(opnds, NSStringFromClass(OR.self).hashValue) + public override func hash(into hasher: inout Hasher) { + hasher.combine(opnds) } /// diff --git a/runtime/Swift/Sources/Antlr4/atn/Transition.swift b/runtime/Swift/Sources/Antlr4/atn/Transition.swift index 5af20e22a..af31c134e 100644 --- a/runtime/Swift/Sources/Antlr4/atn/Transition.swift +++ b/runtime/Swift/Sources/Antlr4/atn/Transition.swift @@ -54,16 +54,16 @@ public class Transition { public static let serializationTypes: Dictionary = [ - NSStringFromClass(EpsilonTransition.self): EPSILON, - NSStringFromClass(RangeTransition.self): RANGE, - NSStringFromClass(RuleTransition.self): RULE, - NSStringFromClass(PredicateTransition.self): PREDICATE, - NSStringFromClass(AtomTransition.self): ATOM, - NSStringFromClass(ActionTransition.self): ACTION, - NSStringFromClass(SetTransition.self): SET, - NSStringFromClass(NotSetTransition.self): NOT_SET, - NSStringFromClass(WildcardTransition.self): WILDCARD, - NSStringFromClass(PrecedencePredicateTransition.self): PRECEDENCE, + String(describing: EpsilonTransition.self): EPSILON, + String(describing: RangeTransition.self): RANGE, + String(describing: RuleTransition.self): RULE, + String(describing: PredicateTransition.self): PREDICATE, + String(describing: AtomTransition.self): ATOM, + String(describing: ActionTransition.self): ACTION, + String(describing: SetTransition.self): SET, + String(describing: NotSetTransition.self): NOT_SET, + String(describing: WildcardTransition.self): WILDCARD, + String(describing: PrecedencePredicateTransition.self): PRECEDENCE, ] diff --git a/runtime/Swift/Sources/Antlr4/dfa/DFAState.swift b/runtime/Swift/Sources/Antlr4/dfa/DFAState.swift index d44dd34ec..8a705872c 100644 --- a/runtime/Swift/Sources/Antlr4/dfa/DFAState.swift +++ b/runtime/Swift/Sources/Antlr4/dfa/DFAState.swift @@ -109,10 +109,8 @@ public final class DFAState: Hashable, CustomStringConvertible { } - public var hashValue: Int { - var hash = MurmurHash.initialize(7) - hash = MurmurHash.update(hash, configs.hashValue) - return MurmurHash.finish(hash, 1) + public func hash(into hasher: inout Hasher) { + hasher.combine(configs) } public var description: String { diff --git a/runtime/Swift/Sources/Antlr4/misc/BitSet.swift b/runtime/Swift/Sources/Antlr4/misc/BitSet.swift index ef03b8aa7..5e611efc2 100644 --- a/runtime/Swift/Sources/Antlr4/misc/BitSet.swift +++ b/runtime/Swift/Sources/Antlr4/misc/BitSet.swift @@ -1053,7 +1053,7 @@ public class BitSet: Hashable, CustomStringConvertible { /// /// - returns: the hash code value for this bit set /// - public var hashValue: Int { + private var hashCode: Int { var h: Int64 = 1234 var i: Int = wordsInUse i -= 1 @@ -1065,6 +1065,10 @@ public class BitSet: Hashable, CustomStringConvertible { return Int(Int32((h >> 32) ^ h)) } + public func hash(into hasher: inout Hasher) { + hasher.combine(hashCode) + } + /// /// Returns the number of bits of space actually in use by this /// `BitSet` to represent bit values. diff --git a/runtime/Swift/Sources/Antlr4/misc/Interval.swift b/runtime/Swift/Sources/Antlr4/misc/Interval.swift index e3b4ad49f..f03ad9b08 100644 --- a/runtime/Swift/Sources/Antlr4/misc/Interval.swift +++ b/runtime/Swift/Sources/Antlr4/misc/Interval.swift @@ -62,13 +62,12 @@ public class Interval: Hashable { } - public var hashValue: Int { - var hash: Int = 23 - hash = hash * 31 + a - hash = hash * 31 + b - return hash + public func hash(into hasher: inout Hasher) { + hasher.combine(a) + hasher.combine(b) } - /// + + /// /// Does this start completely before other? Disjoint /// public func startsBeforeDisjoint(_ other: Interval) -> Bool { diff --git a/runtime/Swift/Sources/Antlr4/misc/IntervalSet.swift b/runtime/Swift/Sources/Antlr4/misc/IntervalSet.swift index 04c590db0..16bbb929a 100644 --- a/runtime/Swift/Sources/Antlr4/misc/IntervalSet.swift +++ b/runtime/Swift/Sources/Antlr4/misc/IntervalSet.swift @@ -326,8 +326,8 @@ public class IntervalSet: IntSet, Hashable, CustomStringConvertible { return nil // nothing in common with null set } - var myIntervals = self.intervals - var theirIntervals = (other as! IntervalSet).intervals + let myIntervals = self.intervals + let theirIntervals = (other as! IntervalSet).intervals var intersection: IntervalSet? = nil let mySize = myIntervals.count let theirSize = theirIntervals.count @@ -470,25 +470,13 @@ public class IntervalSet: IntSet, Hashable, CustomStringConvertible { return intervals } - - public func hashCode() -> Int { - var hash = MurmurHash.initialize() - for I: Interval in intervals { - hash = MurmurHash.update(hash, I.a) - hash = MurmurHash.update(hash, I.b) + public func hash(into hasher: inout Hasher) { + for interval in intervals { + hasher.combine(interval.a) + hasher.combine(interval.b) } - - return MurmurHash.finish(hash, intervals.count * 2) } - public var hashValue: Int { - var hash = MurmurHash.initialize() - for I: Interval in intervals { - hash = MurmurHash.update(hash, I.a) - hash = MurmurHash.update(hash, I.b) - } - return MurmurHash.finish(hash, intervals.count * 2) - } /// /// Are two IntervalSets equal? Because all intervals are sorted /// and disjoint, equals is a simple linear walk over both lists diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Swift/Swift.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Swift/Swift.stg index 7d74dc5d0..cefb1e248 100755 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Swift/Swift.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Swift/Swift.stg @@ -848,11 +848,11 @@ StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMe func getRuleIndex() -> Int { return .RULE_ } - - init() { }!> + - func copyFrom(_ ctx: ) { - super.copyFrom(ctx) + override func copyFrom(_ ctx_: ParserRuleContext) { + super.copyFrom(ctx_) + let ctx = ctx_ as! = ctx.;}; separator="\n"> }