Repository: tree-sitter/tree-sitter-haskell (language: python)

Issue:
Support c++03
This increases portability. See https://github.com/tree-sitter/tree-sitter-haskell/issues/34#issue-884495183.
@tek this should be ready to merge.

This compiles with no output with `g++ -Wall -std=c++03 -c -o out.o src/scanner.cc`.
I've tested the changes with tree-sitter (my compiler uses c++14 by default), but not with an older compiler.

@wenkokke this should remove the need for most of the `web-tree-sitter` patch, can you give us an update on that?  
(Sorry, I would compile web-tree-sitter et al. myself, but unless there's a nix derivation, doing so is difficult on my machine)


Discussion / hints:
@414owen It still needs `__ZNSt3__25ctypeIcE2idE`, which [@maxbrunsfeld mentioned](https://github.com/tree-sitter/tree-sitter-haskell/pull/56#issuecomment-1003756899) is "needed because of the use of iostream for logging." If you could remove that, that would be huge!
> If you could remove that, that would be huge!

I have a branch that will definitely remove that: https://github.com/414owen/tree-sitter-haskell/tree/pure-c

but it has a kind of infinite-loop type bug at the moment :)

Fix patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 00000000..efd6d7e7
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,46 @@
+name: CI
+
+on:
+  push:
+    branches:
+      - "**"
+  pull_request:
+    types:
+      - opened
+      - synchronize
+
+jobs:
+  test:
+    name: test / ${{ matrix.os }}
+    runs-on: ${{ matrix.os }}
+    strategy:
+      fail-fast: false
+      matrix:
+        os: [macOS-latest, ubuntu-latest]
+
+    steps:
+      - name: Checkout repo
+        uses: actions/checkout@v2
+
+      - name: Install node
+        uses: actions/setup-node@v2
+        with:
+          node-version: '14'
+
+      # - name: Install emscripten
+      #   uses: mymindstorm/setup-emsdk@v10
+      #   with:
+      #     version: '2.0.24'
+
+      - name: Build tree-sitter-haskell
+        run: npm install
+
+      - name: Run tests
+        run: npm test
+
+      - name: Parse examples
+        run: npm run examples
+
+      # - name: Parse examples with web binding
+      #   run: npm run examples-wasm
+
diff --git a/Makefile b/Makefile
index f25e456b..8132e942 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-WEB_TREE_SITTER=README.md package.json tree-sitter-web.d.ts tree-sitter.js tree-sitter.wasm
+WEB_TREE_SITTER_FILES=README.md package.json tree-sitter-web.d.ts tree-sitter.js tree-sitter.wasm
 TREE_SITTER_VERSION=v0.20.1
 
 all: node_modules/web-tree-sitter tree-sitter-haskell.wasm
@@ -9,16 +9,21 @@ src/parser.c: grammar.js
 
 # build patched version of web-tree-sitter
 node_modules/web-tree-sitter:
-	if [ ! -d "tmp/tree-sitter" ]; then git clone https://github.com/tree-sitter/tree-sitter.git tmp/tree-sitter; fi
-	cd tmp/tree-sitter && git checkout $(TREE_SITTER_VERSION)
-	cp tree-sitter.patch tmp/tree-sitter/
-	cd tmp/tree-sitter\
-		&& git apply tree-sitter.patch\
-		&& ./script/build-wasm
-	mkdir -p node_modules/web-tree-sitter
-	cp tmp/tree-sitter/LICENSE node_modules/web-tree-sitter
-	cp $(addprefix tmp/tree-sitter/lib/binding_web/,$(WEB_TREE_SITTER)) node_modules/web-tree-sitter
-	rm -rf tmp/tree-sitter
+	@rm -rf tmp/tree-sitter
+	@git clone                                       \
+		-c advice.detachedHead=false --quiet           \
+		--depth=1 --branch=$(TREE_SITTER_VERSION)      \
+		https://github.com/tree-sitter/tree-sitter.git \
+		tmp/tree-sitter
+	@cp tree-sitter.patch tmp/tree-sitter/
+	@>/dev/null                      \
+		&& cd tmp/tree-sitter          \
+		&& git apply tree-sitter.patch \
+		&& ./script/build-wasm --debug
+	@mkdir -p node_modules/web-tree-sitter
+	@cp tmp/tree-sitter/LICENSE node_modules/web-tree-sitter
+	@cp $(addprefix tmp/tree-sitter/lib/binding_web/,$(WEB_TREE_SITTER_FILES)) node_modules/web-tree-sitter
+	@rm -rf tmp/tree-sitter
 
 # build web version of tree-sitter-haskell
 # NOTE: requires patched version of web-tree-sitter
diff --git a/README.md b/README.md
index bfced306..220250f4 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,10 @@
 # tree-sitter-haskell
 
-[![Test the grammar](https://github.com/tree-sitter/tree-sitter-haskell/actions/workflows/test.yml/badge.svg)](https://github.com/tree-sitter/tree-sitter-haskell/actions/workflows/test.yml)
+[![build](https://github.com/tree-sitter/tree-sitter-haskell/actions/workflows/ci.yml/badge.svg)](https://github.com/tree-sitter/tree-sitter-haskell/actions/workflows/ci.yml)
 
 Haskell grammar for [tree-sitter].
 
-**Note** This grammar needs at least tree-sitter `0.19.4` and C++-14.
+**Note** This grammar needs at least tree-sitter 0.19.4.
 
 # References
 
@@ -27,50 +27,6 @@ parser_config.haskell = {
 EOF
 ```
 
-Depending on what compilers are installed in your system, it may be necessary to force `nvim-treesitter` to use a
-specific one to satisfy the C++-14 requirement (see
-[this issue](https://github.com/tree-sitter/tree-sitter-haskell/issues/34) for more info):
-
-```vim
-lua require'nvim-treesitter.install'.compilers = { "clang" }
-" or
-lua require'nvim-treesitter.install'.compilers = { "clang++" }
-```
-
-or
-
-```vim
-lua require'nvim-treesitter.install'.compilers = { "gcc" }
-```
-
-## Building on MacOS
-
-If you get loads of C++ errors when building the parser, a workaround might be to install GCC with `homebrew`, in
-addition to selecting `gcc` as the compiler used by tree-sitter, as described for Neovim in the previous section.
-
-First, run this command in a terminal:
-
-```shell
-$ brew install gcc
-```
-
-This may not be enough, since `clang` might still be used by default.
-You can find out whether the `gcc` executable is linked to something else with:
-
-```shell
-$ ls -l $(which gcc)
-```
-
-If this doesn't point to something like `gcc-11`, you can remove it (if it is a symlink) and/or link the real `gcc-N`
-binary to a `bin` directory that's at the head of your `$PATH`, something like:
-
-```shell
-ln -sf /opt/homebrew/bin/gcc-11 /usr/local/bin/gcc
-```
-or this for earlier versions of homebrew.
-```shell
-ln -sf /usr/local/bin/gcc-11 /usr/local/bin/gcc
-```
 
 # Supported Language Extensions
 
diff --git a/script/parse-examples b/script/parse-examples
index 8184fe5e..a12684c7 100755
--- a/script/parse-examples
+++ b/script/parse-examples
@@ -20,12 +20,13 @@ function clone_repo {
 
   path=examples/$name
   if [ ! -d "$path" ]; then
-    git clone "https://github.com/$owner/$name" "$path"
+    git clone "https://github.com/$owner/$name" "$path" --quiet
   fi
 
-  pushd "$path" && git fetch
-  git reset --hard $sha
-  popd
+  pushd "$path" >/dev/null
+  git fetch --quiet
+  git reset --hard $sha --quiet
+  popd >/dev/null
 }
 
 clone_repo joshvera effects 08f5f36f2600362685af593f4b327e933b60bf97
@@ -36,5 +37,5 @@ clone_repo github semantic b162132339622fe1e80e243f630fe092d5c0cbe1
 clone_repo haskell haskell-language-server d397ef491ef1689d43028f4d3d01a42118292235
 
 for name in effects postgrest ivory polysemy semantic haskell-language-server; do
-  script/parse-example $name $mode
+  script/parse-example "$name" "$mode"
 done
diff --git a/script/tree-sitter-parse.js b/script/tree-sitter-parse.js
index d206d177..98c56ffb 100755
--- a/script/tree-sitter-parse.js
+++ b/script/tree-sitter-parse.js
@@ -1,4 +1,4 @@
-#!/usr/bin/env node
+#!/usr/bin/env node --trace-warnings --unhandled-rejections=strict
 
 const fs = require('fs');
 const Parser = require('web-tree-sitter');
@@ -13,7 +13,7 @@ Parser.init().then(() => {
     const parser = new Parser;
     parser.setLanguage(Haskell);
     for (let i = 2; i < process.argv.length - 1; i++) {
-      const fileName = process.argv[2]
+      const fileName = process.argv[i]
       const sourceCode = fs.readFileSync(fileName, 'utf8')
       const tree = parser.parse(sourceCode);
     }
diff --git a/tree-sitter-haskell.wasm b/tree-sitter-haskell.wasm
index e1c08299..ed9745e3 100755
Binary files a/tree-sitter-haskell.wasm and b/tree-sitter-haskell.wasm differ
diff --git a/tree-sitter.patch b/tree-sitter.patch
index 13c25959..bd17f15a 100644
--- a/tree-sitter.patch
+++ b/tree-sitter.patch
@@ -4,7 +4,7 @@
    "_free",
    "_malloc",
 
-+  "__ZNSt3__25ctypeIcE2idE",
++  "_realloc",
 +
    "__ZNKSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4copyEPcmm",
    "__ZNKSt3__220__vector_base_commonILb1EE20__throw_length_errorEv",


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
deleted file mode 100644
index 3dcace81..00000000
--- a/.github/workflows/test.yml
+++ /dev/null
@@ -1,35 +0,0 @@
-name: Test the grammar
-on:
-  push:
-    branches:
-      - "**"
-    pull_request:
-      - "**"
-jobs:
-  test:
-    runs-on: ubuntu-latest
-    steps:
-      - uses: actions/checkout@v2
-      - uses: actions/setup-node@v2
-        with:
-          node-version: 14
-      - run: npm install
-        env:
-          CXX: g++-9
-      - run: npm test
-        env:
-          CXX: g++-9
-      - run: npm run examples
-        env:
-          CXX: g++-9
-      - run: npm run examples-wasm
-  # build-mac:
-  #   runs-on: macos-latest
-  #   steps:
-  #     - uses: actions/checkout@v2
-  #     - uses: actions/setup-node@v2
-  #       with:
-  #         node-version: 14
-  #     - run: npm install
-  #       env:
-  #         CXX: g++-9