Merge pull request #1560 from antmicro/verilog-ls-class

[LSP] Wrap implementation of the Language Server in a class
diff --git a/.github/bin/build-and-test.sh b/.github/bin/build-and-test.sh
index f0910c7..f059ac7 100755
--- a/.github/bin/build-and-test.sh
+++ b/.github/bin/build-and-test.sh
@@ -94,7 +94,6 @@
     bazel coverage \
           --combined_report=lcov \
           --coverage_report_generator=@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main \
-          --java_runtime_version=remotejdk_11 \
           ${CHOSEN_TARGETS}
     # output will be in bazel-out/_coverage/_coverage_report.dat
     ;;
diff --git a/.github/bin/github-pages-setup.sh b/.github/bin/github-pages-setup.sh
index 887b16b..d1c1bea 100755
--- a/.github/bin/github-pages-setup.sh
+++ b/.github/bin/github-pages-setup.sh
@@ -17,11 +17,21 @@
 # ---------------
 # Generate the GitHub pages to deploy
 
+set -eux
+
+BINDIR=${1:-/usr/bin}
 REPO_SLUG=${REPO_SLUG:-${GITHUB_REPOSITORY_SLUG:-google/verible}}
 GIT_DATE=${GIT_DATE:-$(git show -s --format=%ci)}
 GIT_HASH=${GIT_HASH:-$(git rev-parse HEAD)}
 GIT_VERSION=${GIT_VERSION:-$(git rev-parse --short "$GITHUB_SHA")}
 
+# Check if a file exists and is executable before running it.
+# Useful for running commands which return a non-zero exit code 
+# despite providing meaningful output, e.g. Verible binaries with the "-helpfull" param.
+check_and_run() {
+    test -x "$1" && ($@ || true)
+}
+
 echo "Git date:    $GIT_DATE"
 echo "Git hash:    $GIT_HASH"
 echo "Git version: $GIT_VERSION"
@@ -77,7 +87,7 @@
 
 # Generate lint rules documentation
 # --------------------------------
-bazel-bin/verilog/tools/lint/verible-verilog-lint -generate_markdown > $PAGES_DIR/lint.md
+${BINDIR}/verible-verilog-lint -generate_markdown > $PAGES_DIR/lint.md
 cat >> $PAGES_DIR/lint.md <<EOF
 
 ## Version
@@ -97,7 +107,7 @@
 ## Command line arguments
 \`\`\`
 EOF
-bazel-bin/verilog/tools/syntax/verible-verilog-syntax -helpfull >> $SYNTAX_DOC
+check_and_run ${BINDIR}/verible-verilog-syntax -helpfull >> $SYNTAX_DOC
 cat >> $SYNTAX_DOC <<EOF
 \`\`\`
 
@@ -120,14 +130,14 @@
 
 \`\`\`
 EOF
-bazel-bin/verilog/tools/lint/verible-verilog-lint -helpfull >> $LINT_DOC
+check_and_run ${BINDIR}/verible-verilog-lint -helpfull >> $LINT_DOC
 cat >> $LINT_DOC <<'EOF'
 ```
 
 ## Lint Rules
 
 EOF
-bazel-bin/verilog/tools/lint/verible-verilog-lint -generate_markdown >> $LINT_DOC
+${BINDIR}/verible-verilog-lint -generate_markdown >> $LINT_DOC
 cat >> $LINT_DOC <<EOF
 
 ## Version
@@ -147,7 +157,7 @@
 ## Command line arguments
 \`\`\`
 EOF
-bazel-bin/verilog/tools/formatter/verible-verilog-format -helpfull >> $FORMAT_DOC
+check_and_run ${BINDIR}/verible-verilog-format -helpfull >> $FORMAT_DOC
 cat >> $FORMAT_DOC <<EOF
 \`\`\`
 
diff --git a/.github/bin/github-releases-setup.sh b/.github/bin/github-releases-setup.sh
index 9490ab7..3cffa29 100755
--- a/.github/bin/github-releases-setup.sh
+++ b/.github/bin/github-releases-setup.sh
@@ -17,8 +17,10 @@
 # ---------------
 # Generate the GitHub Releases to deploy
 
+set -ex
+
 RELEASE_DIR=${1:-/tmp/releases}
-rm -rf $RELEASE_DIR
+rm -rf "$RELEASE_DIR/*"
 
 GIT_VERSION=${GIT_VERSION:-$(git rev-parse --short "$GITHUB_SHA")}
 
@@ -36,7 +38,7 @@
 for BIN in $PREFIX_BIN/*; do
     ls -l $BIN
     file $BIN
-    ldd $BIN
+    ldd $BIN || true # ignore the potential 'not a dynamic executable' errors for non-binaries
 done
 
 # Documentation
diff --git a/.github/bin/smoke-test.sh b/.github/bin/smoke-test.sh
index dd81f6e..c4f0f8a 100755
--- a/.github/bin/smoke-test.sh
+++ b/.github/bin/smoke-test.sh
@@ -72,7 +72,7 @@
 # array below, mapping them to Verible issue tracker numbers.
 readonly TEST_GIT_PROJECTS="https://github.com/lowRISC/ibex \
          https://github.com/lowRISC/opentitan \
-         https://github.com/chipsalliance/Cores-SweRV \
+         https://github.com/chipsalliance/Cores-VeeR-EH2 \
          https://github.com/openhwgroup/cva6 \
          https://github.com/SymbiFlow/uvm \
          https://github.com/taichi-ishitani/tnoc \
@@ -100,7 +100,7 @@
 declare -A KnownIssue
 
 #--- Opentitan
-KnownIssue[formatter:$BASE_TEST_DIR/opentitan/hw/ip/aes/dv/aes_model_dpi/aes_model_dpi_pkg.sv]=1006
+# Original issue - 1006 has been resolved
 # There is also bug 1008 which only shows up if compiled with asan
 
 #--- ivtest
@@ -136,14 +136,15 @@
 
 ExpectedFailCount[syntax:opentitan]=36
 ExpectedFailCount[lint:opentitan]=36
-ExpectedFailCount[formatter:opentitan]=1
-ExpectedFailCount[project:opentitan]=725
+ExpectedFailCount[project:opentitan]=727
 
-ExpectedFailCount[project:Cores-SweRV]=21
+ExpectedFailCount[syntax:Cores-VeeR-EH2]=2
+ExpectedFailCount[lint:Cores-VeeR-EH2]=2
+ExpectedFailCount[project:Cores-VeeR-EH2]=42
 
 ExpectedFailCount[syntax:cva6]=4
 ExpectedFailCount[lint:cva6]=4
-ExpectedFailCount[project:cva6]=38
+ExpectedFailCount[project:cva6]=70
 
 ExpectedFailCount[syntax:uvm]=1
 ExpectedFailCount[lint:uvm]=1
@@ -185,9 +186,9 @@
 ExpectedFailCount[formatter:scr1]=2
 ExpectedFailCount[project:scr1]=45
 
-ExpectedFailCount[syntax:basejump_stl]=460
-ExpectedFailCount[lint:basejump_stl]=460
-ExpectedFailCount[project:basejump_stl]=566
+ExpectedFailCount[syntax:basejump_stl]=461
+ExpectedFailCount[lint:basejump_stl]=461
+ExpectedFailCount[project:basejump_stl]=567
 
 # Ideally, we expect all tools to process all files with a zero exit code.
 # However, that is not always the case, so we document the current
diff --git a/.github/settings.sh b/.github/settings.sh
index 66b92a4..91028b0 100644
--- a/.github/settings.sh
+++ b/.github/settings.sh
@@ -32,7 +32,7 @@
 export BAZEL_OPTS="-c opt --show_progress_rate_limit=10.0"
 
 # Used to fetch the BAZEL version where needed.
-export BAZEL_VERSION=5.3.2
+export BAZEL_VERSION=4.0.0
 
 # TODO(b/171989992): revert to using release version after upgrading system
 #   libraries/image (need: GLIBCXX_3.4.26, CXXABI_1.3.11, GLIBC_2.29).
diff --git a/.github/workflows/verible-ci.yml b/.github/workflows/verible-ci.yml
index 7fdffdb..778829c 100644
--- a/.github/workflows/verible-ci.yml
+++ b/.github/workflows/verible-ci.yml
@@ -15,6 +15,8 @@
   BOT_USER: "Deployment Bot"
   BOT_EMAIL: "verible-dev@googlegroups.com"
   GHA_MACHINE_TYPE: "n2-standard-8"
+  DEBIAN_FRONTEND: "noninteractive"
+  VERIBLE_BINDIR: "/tmp/verible-bin"
 
 jobs:
 
@@ -46,7 +48,7 @@
 
     - name: 📤 Upload performance graphs
       uses: actions/upload-artifact@v2
-      if: success() || failure() 
+      if: success() || failure()
       with:
         name: "diag"
         path: "**/plot_*.svg"
@@ -98,7 +100,7 @@
 
     - name: 📤 Upload performance graphs
       uses: actions/upload-artifact@v2
-      if: success() || failure() 
+      if: success() || failure()
       with:
         name: "diag"
         path: "**/plot_*.svg"
@@ -154,7 +156,7 @@
       run: |
         set -x
         apt -qqy update
-        apt -qq -y install clang-10 build-essential wget git python3 python-is-python3 gcc-10 g++-10
+        apt -qq -y install clang-10 build-essential wget git python3 python-is-python3 gcc-10 g++-10 default-jdk
         source ./.github/settings.sh
         ./.github/bin/set-compiler.sh 10
         ./.github/bin/install-bazel.sh
@@ -162,10 +164,6 @@
     - name: ${{ matrix.mode }} Verible
       run: ./.github/bin/build-and-test.sh
 
-    - name: Set up things for GitHub Pages deployment
-      if: matrix.mode == 'compile'
-      run: ./.github/bin/github-pages-setup.sh
-
     - name: Upload coverage
       # will show up under https://app.codecov.io/gh/chipsalliance/verible
       if: matrix.mode == 'coverage'
@@ -175,18 +173,21 @@
         verbose: true
       continue-on-error: true
 
-    - name: Deployment
-      if: github.ref == 'refs/heads/master' && github.event_name != 'pull_request' && matrix.mode == 'compile'
-      uses: JamesIves/github-pages-deploy-action@3.7.1
+    - name: Gather and pack binaries
+      if: matrix.mode == 'compile'
+      run: bazel run :install -c fastbuild -- $VERIBLE_BINDIR && tar cfv verible-bin.tar -C $VERIBLE_BINDIR .
+
+    - name: Upload bazel-bin 
+      if: matrix.mode == 'compile'
+      uses: actions/upload-artifact@v2
       with:
-        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-        BRANCH: gh-pages
-        FOLDER: /tmp/pages
-        CLEAN: true
+        name: verible-bin
+        path: verible-bin.tar
+
 
     - name: 📤 Upload performance graphs
       uses: actions/upload-artifact@v2
-      if: success() || failure() 
+      if: success() || failure()
       with:
         name: "diag"
         path: "**/plot_*.svg"
@@ -289,7 +290,7 @@
 
     - name: 📤 Upload performance graphs
       uses: actions/upload-artifact@v2
-      if: success() || failure() 
+      if: success() || failure()
       with:
         name: "diag"
         path: "**/plot_*.svg"
@@ -309,7 +310,7 @@
         format: 'YYYY-MM-DD-HH-mm-ss'
 
     - name: Mount bazel cache
-      uses: actions/cache@v2
+      uses: actions/cache@v3
       with:
         path: "/private/var/tmp/_bazel_runner"
         key: bazelcache_macos_${{ steps.cache_timestamp.outputs.time }}
@@ -334,7 +335,7 @@
         format: 'YYYY-MM-DD-HH-mm-ss'
 
     - name: Mount bazel cache
-      uses: actions/cache@v2
+      uses: actions/cache@v3
       with:
         path: "c:/users/runneradmin/_bazel_runneradmin"
         key: bazelcache_windows2_${{ steps.cache_timestamp.outputs.time }}
@@ -411,3 +412,29 @@
         tag: ${{ env.TAG }}
         overwrite: true
         file_glob: true
+
+  GhPages:
+    name: Build and deploy GitHub pages
+    needs: [Check]
+    runs-on: ubuntu-22.04
+    steps:
+      - uses: actions/checkout@v3
+
+      - uses: actions/download-artifact@v3
+        with:
+          name: verible-bin
+
+      - name: Extract Verible binaries
+        run: mkdir -p $VERIBLE_BINDIR && tar xvf verible-bin.tar -C $VERIBLE_BINDIR
+
+      - name: Set up things for GitHub Pages deployment
+        run: ./.github/bin/github-pages-setup.sh $VERIBLE_BINDIR
+
+      - name: Deployment
+        if: github.ref == 'refs/heads/master' && github.event_name != 'pull_request' && matrix.mode == 'compile'
+        uses: JamesIves/github-pages-deploy-action@3.7.1
+        with:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+          BRANCH: gh-pages
+          FOLDER: /tmp/pages
+          CLEAN: true
diff --git a/WORKSPACE b/WORKSPACE
index ad7d61c..f7939dd 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -74,9 +74,9 @@
 
 win_flex_configure(
     name = "win_flex_bison",
-    sha256 = "095cf65cb3f12ee5888022f93109acbe6264e5f18f6ffce0bda77feb31b65bd8",
-    # bison 3.3.2, flex 2.6.4
-    url = "https://github.com/lexxmark/winflexbison/releases/download/v2.5.18/win_flex_bison-2.5.18.zip",
+    sha256 = "8d324b62be33604b2c45ad1dd34ab93d722534448f55a16ca7292de32b6ac135",
+    # bison 3.8.2, flex 2.6.4
+    url = "https://github.com/lexxmark/winflexbison/releases/download/v2.5.25/win_flex_bison-2.5.25.zip",
 )
 
 http_archive(
diff --git a/verilog/formatting/formatter_test.cc b/verilog/formatting/formatter_test.cc
index 8642878..f3c3773 100644
--- a/verilog/formatting/formatter_test.cc
+++ b/verilog/formatting/formatter_test.cc
@@ -4312,6 +4312,21 @@
      "    endfunction\n"
      "  endclass\n"
      "endpackage\n"},
+    {"package fedex;\n"
+     "  import \"asdf\" context function void bar(\n"
+     "    input bit              [2:1] aaaa,   // EOL COMMENT\n"
+     "                                          // another\n"
+     "    input bit foo\n"
+     "  );\n"
+     "endpackage\n",
+     "package fedex;\n"
+     "  import \"asdf\" context\n"
+     "      function void bar(\n"
+     "    input\n        bit [2:1] aaaa,  // EOL COMMENT\n"
+     "                         // another\n"
+     "    input bit foo\n"
+     "  );\n"
+     "endpackage\n"},
 
     // function test cases
     {"function f ;endfunction", "function f;\nendfunction\n"},
diff --git a/verilog/formatting/tree_unwrapper.cc b/verilog/formatting/tree_unwrapper.cc
index b7160a0..c8f6176 100644
--- a/verilog/formatting/tree_unwrapper.cc
+++ b/verilog/formatting/tree_unwrapper.cc
@@ -563,7 +563,7 @@
     case NodeEnum::kPortDeclarationList:
     case NodeEnum::kActualParameterByNameList:
     case NodeEnum::kPortActualList:
-    // case NodeEnum::kPortList:  // TODO(fangism): for task/function ports
+    case NodeEnum::kPortList:
     case NodeEnum::kModuleItemList:
     case NodeEnum::kGenerateItemList:
     case NodeEnum::kClassItems: