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

Adding support for OpenFOAM file format #719

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ fn main() {
src_dir: "vendored_parsers/tree-sitter-erlang-src",
extra_files: vec![],
},
TreeSitterParser {
name: "tree-sitter-foam",
src_dir: "vendored_parsers/tree-sitter-foam-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-f-sharp",
src_dir: "vendored_parsers/tree-sitter-f-sharp-src",
Expand Down
1 change: 1 addition & 0 deletions manual/src/languages_supported.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ with `difft --list-languages`.
| Erlang | [WhatsApp/tree-sitter-erlang](https://github.com/WhatsApp/tree-sitter-erlang) |
| Emacs Lisp | [wilfred/tree-sitter-elisp](https://github.com/Wilfred/tree-sitter-elisp) |
| F# | [ionide/tree-sitter-fsharp](https://github.com/ionide/tree-sitter-fsharp) |
| Foam | [FoamScience/tree-sitter-foam](https://github.com/FoamScience/tree-sitter-foam) |
| Gleam | [gleam-lang/tree-sitter-gleam](https://github.com/gleam-lang/tree-sitter-gleam) |
| Go | [tree-sitter/tree-sitter-go](https://github.com/tree-sitter/tree-sitter-go) |
| Hack | [slackhq/tree-sitter-hack](https://github.com/slackhq/tree-sitter-hack) |
Expand Down
18 changes: 18 additions & 0 deletions sample_files/foamAfterDict
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SampleDictName {
key 8;
internalField uniform (0 5.1 0);
}

runTimeVal #calc "1/4*$SampleDictName.key";

phases
(
oil // Describe why oil is here
air
);

code
#{
// C++ comment modified
const auto a = doNothing();
#};
16 changes: 16 additions & 0 deletions sample_files/foamBeforeDict
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// OpenFOAM comment

SampleDictName {
key 5;
internalField uniform (0 0 1.2);
}

runTimeVal #calc "3*$SampleDictName.key";

phases ( oil air );

code
#{
// C++ comment
const auto a = doSomething();
#};
9 changes: 9 additions & 0 deletions src/parse/guess_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub(crate) enum Language {
EmacsLisp,
Erlang,
FSharp,
Foam,
Gleam,
Go,
Hack,
Expand Down Expand Up @@ -128,6 +129,7 @@ pub(crate) fn language_name(language: Language) -> &'static str {
Elvish => "Elvish",
EmacsLisp => "Emacs Lisp",
Erlang => "Erlang",
Foam => "OpenFOAM",
FSharp => "F#",
Gleam => "Gleam",
Go => "Go",
Expand Down Expand Up @@ -265,6 +267,13 @@ pub(crate) fn language_globs(language: Language) -> Vec<glob::Pattern> {
"Emakefile",
],
FSharp => &["*.fs", "*.fsx", "*.fsi"],
Foam => &[
"*Dict",
"*Properties",
"fvSolution",
"fvSchemes",
"fvOptions",
],
Gleam => &["*.gleam"],
Go => &["*.go"],
Hack => &["*.hack", "*.hck", "*.hhi"],
Expand Down
20 changes: 20 additions & 0 deletions src/parse/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ extern "C" {
fn tree_sitter_elvish() -> ts::Language;
fn tree_sitter_erlang() -> ts::Language;
fn tree_sitter_fsharp() -> ts::Language;
fn tree_sitter_foam() -> ts::Language;
fn tree_sitter_gleam() -> ts::Language;
fn tree_sitter_go() -> ts::Language;
fn tree_sitter_hare() -> ts::Language;
Expand Down Expand Up @@ -426,6 +427,25 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
sub_languages: vec![],
}
}
Foam => {
let language = unsafe { tree_sitter_foam() };
TreeSitterConfig {
language,
atom_nodes: vec!["identifier", "number_literal", "string_literal"]
.into_iter()
.collect(),
delimiter_tokens: vec![("(", ")"), ("{", "}"), ("[…", "]"), ("#{", "#}")],
highlight_query: ts::Query::new(
language,
include_str!("../../vendored_parsers/highlights/foam.scm"),
)
.unwrap(),
sub_languages: vec![TreeSitterSubLanguage {
query: ts::Query::new(language, "(code (code_body) @content)").unwrap(),
parse_as: CPlusPlus,
}],
}
}
Gleam => {
let language = unsafe { tree_sitter_gleam() };
TreeSitterConfig {
Expand Down
1 change: 1 addition & 0 deletions vendored_parsers/highlights/foam.scm
1 change: 1 addition & 0 deletions vendored_parsers/tree-sitter-foam-src
20 changes: 20 additions & 0 deletions vendored_parsers/tree-sitter-foam/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
env: {
commonjs: true,
es2021: true,
},
extends: 'google',
overrides: [
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
rules: {
'indent': ['error', 4, {'SwitchCase': 1}],
'max-len': [
'error',
{'code': 120, 'ignoreComments': true, 'ignoreUrls': true, 'ignoreStrings': true},
],
},
};
34 changes: 34 additions & 0 deletions vendored_parsers/tree-sitter-foam/.github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build & Test
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build-and-test-on-ubuntu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm install
- run: node_modules/.bin/tree-sitter test
- uses: docker-practice/actions-setup-docker@master
- run: node_modules/.bin/tree-sitter build-wasm
- uses: actions/upload-artifact@v3
with:
name: wasm-file
path: tree-sitter-foam.wasm
build-and-test-on-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm install
- run: mv -force test/corpus_windows/* test/corpus/
- run: node_modules/.bin/tree-sitter test
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Publish package to NPM

on:
release:
types: [created]

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

publish-gpr:
needs: build
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
registry-url: https://npm.pkg.github.com/
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
6 changes: 6 additions & 0 deletions vendored_parsers/tree-sitter-foam/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Cargo.lock
node_modules
build
package-lock.json
/target/
*.wasm
9 changes: 9 additions & 0 deletions vendored_parsers/tree-sitter-foam/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js

sudo: false

node_js: 10

branches:
only:
- master
26 changes: 26 additions & 0 deletions vendored_parsers/tree-sitter-foam/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "tree-sitter-foam"
description = "OpenFoam grammar for the tree-sitter parsing library"
version = "0.4.0"
keywords = ["incremental", "parsing", "OpenFOAM", "highlighting"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/FoamScience/tree-sitter-foam"
edition = "2018"
license = "MIT"

build = "bindings/rust/build.rs"
include = [
"bindings/rust/*",
"grammar.js",
"queries/*",
"src/*",
]

[lib]
path = "bindings/rust/lib.rs"

[dependencies]
tree-sitter = "~0.20.0"

[build-dependencies]
cc = "1.0"
21 changes: 21 additions & 0 deletions vendored_parsers/tree-sitter-foam/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022-2023 Mohammed Elwardi Fadeli <[email protected]>, Amaan Qureshi <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
44 changes: 44 additions & 0 deletions vendored_parsers/tree-sitter-foam/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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-foam.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:
@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-foam
# NOTE: requires patched version of web-tree-sitter
tree-sitter-foam.wasm: src/parser.c src/scanner.c
npx tree-sitter build-wasm

CC := cc
OURCFLAGS := -shared -fPIC -g -O0 -I src

clean:
rm -f debug *.o *.a

debug.so: src/parser.c src/scanner.cc
$(CC) $(OURCFLAGS) $(CFLAGS) -o parser.o src/parser.c
$(CC) $(OURCFLAGS) $(CFLAGS) -o scanner.o src/scanner.cc
$(CC) $(OURCFLAGS) $(CFLAGS) -o debug.so $(PWD)/scanner.o $(PWD)/parser.o
rm -f $(HOME)/.cache/tree-sitter/lib/foam.so
cp $(PWD)/debug.so $(HOME)/.cache/tree-sitter/lib/foam.so
41 changes: 41 additions & 0 deletions vendored_parsers/tree-sitter-foam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# OpenFOAM grammar for Tree-Sitter

> This project is in early phases of development; expect things to change considerably

> DISCLAIMER:
> This offering is not approved or endorsed by OpenCFD Limited, producer and distributor
> of the OpenFOAM software and owner of the OPENFOAM® and OpenCFD® trade marks.

![OpenFOAM dictionary syntax highlighting](syntax-highlighting.png)

This is a generic **fail-tolerant** parser for OpenFOAM case dictionaries.
The goal is to reach a state that is "good enough" for syntax highlighting
and feasible symbols extraction.

## Features

- Semantic understanding of OpenFOAM IO entries (Dictionaries, key-value pairs, ... etc)
- Syntax highlighting (Targeting [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) mainly)
- Highlighting of C++ code blocks and regular expressions, so if your client does not install
those parsers automatically, you might have to install them manually
- Scope-awareness is there (Per-dictionary) although not used yet
- Text-objects for dictionaries and key-value pairs to use with
[TreeSitter textobjects plugin for NeoVim](https://github.com/nvim-treesitter/nvim-treesitter-textobjects)
- Text-subjects to use with [TreeSitter textsubjects plugin for NeoVim](https://github.com/RRethy/nvim-treesitter-textsubjects)
- Expression-based folding

## Running tests

- Unit tests for both parsing and highlighting (Highlighting is not consistent between `tree-sitter highlight`
and `nvim-treesitter`) are executed with `tree-sitter test`
(Check the `test` folder)
- `testOFFiles.sh $FOAM_TUTORIALS` parses all OpenFOAM dictionaries in the tutorials directory
- Currently, the parser works on almost all OpenFOAM 8 and Foam-Extend 4 tutorial files
(Well, some files are faulty from source!)

## Contributing

- Pull requests are welcome!
- And, no I'm not going to support specific keyword highlighting
(eg. `scalarField` will never be treated in a special way) unless I find an "unattended"
way to do that.
20 changes: 20 additions & 0 deletions vendored_parsers/tree-sitter-foam/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"targets": [
{
"target_name": "tree_sitter_foam_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
],
"sources": [
"src/parser.c",
"bindings/node/binding.cc",
"src/scanner.c",
# If your language uses an external scanner, add it here.
],
"cflags_cc": [
"-std=c99"
],
}
]
}
Loading
Loading