Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support c++03 #1

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- run: npm run examples
env:
CXX: g++-9
- run: npm run examples-wasm
# build-mac:
# runs-on: macos-latest
# steps:
Expand Down
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
WEB_TREE_SITTER=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

# build parser.c
src/parser.c: grammar.js
npx tree-sitter generate

# 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

# build web version of tree-sitter-haskell
# NOTE: requires patched version of web-tree-sitter
tree-sitter-haskell.wasm: src/parser.c src/scanner.cc
emcc \
-o tree-sitter-haskell.wasm \
-Os \
-std=c++11 \
-s WASM=1 \
-s SIDE_MODULE=1 \
-s TOTAL_MEMORY=33554432 \
-s NODEJS_CATCH_EXIT=0 \
-s EXPORTED_FUNCTIONS=["_tree_sitter_haskell"] \
-fno-exceptions \
-Wno-reorder-init-list \
-Wno-c99-designator \
-I src \
-xc++ \
src/scanner.cc \
src/parser.c

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"scripts": {
"test": "tree-sitter test",
"examples": "script/parse-examples"
"examples": "script/parse-examples",
"examples-wasm": "script/parse-examples wasm"
},
"tree-sitter": [
{
Expand Down
29 changes: 23 additions & 6 deletions script/parse-example
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#!/usr/bin/env bash

# Usage: script/parse-examples [repo_name] [native|wasm]

# Exit immediately if a command exits with a non-zero status.
set -e

name=$1
root=$(dirname $(dirname $(realpath "$0")))
cd $root
# Change directory to project root.
cd "$(dirname "$0")/.."

# Get the repository name
name=$1
repo=examples/$name

# Parse examples in 'native' or 'wasm' mode.
mode=${2:-native}

known_failures=$(cat "script/known-failures-$name.txt")
examples_to_parse=$(
for example in $(find "$repo" -name '*.hs'); do
Expand All @@ -17,11 +24,21 @@ examples_to_parse=$(
done
)

# ensure the scanner was recompiled
tree-sitter test -f 'just compile it' >/dev/null
if [ "$mode" == "native" ]; then
# Ensure the scanner was recompiled
tree-sitter test -f 'just compile it' >/dev/null
elif [ "$mode" == "wasm" ]; then
# Ensure tree-sitter-haskell.wasm was compiled
make node_modules/web-tree-sitter
make tree-sitter-haskell.wasm
fi

start=$(date '+%s.%N')
echo $examples_to_parse | xargs -n 2000 tree-sitter parse -q
if [ "$mode" == "native" ]; then
echo $examples_to_parse | xargs -n 2000 tree-sitter parse -q
elif [ "$mode" == "wasm" ]; then
echo $examples_to_parse | xargs -n 2000 ./script/tree-sitter-parse.js
fi
end=$(date '+%s.%N')

skipped=$( echo $known_failures | wc -w )
Expand Down
10 changes: 9 additions & 1 deletion script/parse-examples
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#!/usr/bin/env bash

# Usage: script/parse-examples [native|wasm]

# Exit immediately if a command exits with a non-zero status.
set -e

# Parse examples in 'native' or 'wasm' mode.
mode=${1:-native}

# Change directory to project root.
cd "$(dirname "$0")/.."

# Clone a GitHub repository to the examples directory
# clone_repo OWNER NAME SHA
function clone_repo {
owner=$1
Expand All @@ -28,5 +36,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
script/parse-example $name $mode
done
21 changes: 21 additions & 0 deletions script/tree-sitter-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

const fs = require('fs');
const Parser = require('web-tree-sitter');

if (process.argv.length < 3) {
console.log('Usage: script/tree-sitter-parse.js <haskell-file..>')
process.exit(1)
}

Parser.init().then(() => {
Parser.Language.load('tree-sitter-haskell.wasm').then((Haskell) => {
const parser = new Parser;
parser.setLanguage(Haskell);
for (let i = 2; i < process.argv.length - 1; i++) {
const fileName = process.argv[2]
const sourceCode = fs.readFileSync(fileName, 'utf8')
const tree = parser.parse(sourceCode);
}
});
});
Loading