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

Bring parser up to date with language changes in Zeek #4

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7cbd815
Add README
ckreibich Feb 2, 2022
ebe4dd4
Bump generated sources
ckreibich Feb 2, 2022
a5a8a53
Adjust paths to point to github.com/zeek repos
ckreibich Feb 2, 2022
1fef7dd
More README tweaks
ckreibich Feb 2, 2022
fd649cc
Model function-like decls as top-level decls
bbannier Jan 14, 2022
69d1756
Hide artifical decl node
bbannier Jan 14, 2022
59e76e0
Use `decl` for scoped `local` or `const` declarations
bbannier Jan 27, 2022
b52e214
fixup! Use `decl` for scoped `local` or `const` declarations
bbannier Feb 2, 2022
9939d22
Surface field access and check
bbannier Jan 29, 2022
e8f5db8
Expose for loops as node
bbannier Feb 15, 2022
e99bf35
Fix extraction of enum value doc strings
bbannier Mar 30, 2023
d7e485b
Backport 41494c8dc13930754a3ab8b72520bf6562fa29c7
bbannier Aug 25, 2023
01d9a6f
Add proper parsing for function calls
bbannier Dec 16, 2023
a004d85
Add &group attribute to grammar
awelzel Nov 21, 2022
e422b04
Fix parsing of "minor" comments
ckreibich Jan 26, 2023
94b946e
Fix "{}"-initializers in expressions, and support trailing commas
ckreibich Mar 17, 2023
a0fd27e
Add GH dependabot setup
bbannier Mar 4, 2024
0695ab8
Fix parsing of globals in >=zeek-7
bbannier Aug 5, 2024
00435fe
Stop highlighting zeekygen comments as properties
bbannier Oct 6, 2024
e1614a4
Merge branch 'topic/bbannier/usability'
bbannier Oct 6, 2024
51c499c
Highlight function calls
bbannier Oct 7, 2024
485a207
Add highlighting of `option` keyword
bbannier Oct 9, 2024
a8903d1
Add assert handling
evantypanski Nov 6, 2024
606fa04
Bump GH actions
bbannier Nov 12, 2024
1fded95
Bump node version in CI
bbannier Nov 12, 2024
464b5f8
Fix highlighting of definitions of function-like things
bbannier Nov 16, 2024
1b1277a
Add playground
bbannier Nov 16, 2024
21e9d14
Simplify CI
bbannier Nov 16, 2024
88cb114
Add pre-commit setup
bbannier Nov 16, 2024
bf6e662
Backport upstream's 6ca36fa
bbannier Nov 24, 2024
e503849
Add schema to grammar for in-editor typechecking.
bbannier Nov 24, 2024
67234e3
Fix parsing of IDs with more than two parts
bbannier Nov 24, 2024
fb978c9
fixup! Fix parsing of IDs with more than two parts
bbannier Nov 24, 2024
06ba0d6
Add parsing for record redefs removing field attributes
bbannier Nov 24, 2024
23730b8
fixup! Add parsing for record redefs removing field attributes
bbannier Nov 24, 2024
b536d42
Add support for parsing `@pragma` constructs
bbannier Nov 24, 2024
857d298
fixup! Add support for parsing `@pragma` constructs
bbannier Nov 24, 2024
7da5493
Regenerate parser
bbannier Nov 24, 2024
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/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @bbannier
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: "cargo" # See documentation for possible values
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "npm" # See documentation for possible values
directory: "/vscode"
schedule:
interval: "weekly"
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/"
schedule:
interval: "weekly"
67 changes: 17 additions & 50 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,59 +1,26 @@
name: Build and tests
name: Check

on:
pull_request:
push:
branches: [main]
tags:
schedule:
- cron: '0 0 * * *'

jobs:
tests:
name: "Build and run parser tests"
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
fail-fast: false
test:
runs-on: ubuntu-latest
steps:
- name: Checkout parser sources
uses: actions/checkout@v2

- name: Checkout Zeek sources
uses: actions/checkout@v2
with:
repository: zeek/zeek
path: test/zeek-src

- uses: actions/setup-node@v2
with:
node-version: '14'

- run: npm install

- name: Add Node.js to PATH
run: echo "$PWD/node_modules/.bin" >> $GITHUB_PATH

- name: Setup tree-sitter
# "tree-sitter init-config" adds $HOME/github as a default
# directory to look for languages in, so we create a symlink
# that directs it to our local clone.
run: |
ln -s $(cd .. && pwd) $HOME/github
tree-sitter init-config

- name: Generate parser
run: tree-sitter generate

- name: Test Zeek grammar
run: cd test && ./parse-zeek-tree.sh ./zeek-src/scripts

- name: Upload test artifacts
uses: actions/upload-artifact@v2
if: failure()
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
name: parser-errors
path: |
*.zeek
*.err
node-version: latest
- run: |
npm ci
- run: |
npm test

pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: pre-commit/[email protected]
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
Cargo.toml
binding.gyp
bindings
src
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "src"]
path = src
url = https://github.com/ckreibich/tree-sitter-zeek-src
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier

exclude: ^(src|bindings)
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# tree-sitter-zeek

[![Tests](https://github.com/zeek/tree-sitter-zeek/actions/workflows/test.yaml/badge.svg)](https://github.com/zeek/tree-sitter-zeek/actions/workflows/test.yaml)

A [Zeek](https://zeek.org) grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter).

## Background

This grammar parses scripts written in the [Zeek scripting
language](https://docs.zeek.org/en/master/script-reference/index.html).

The goal of this grammar is to facilitate tooling around Zeek
scripts. For that reason, its structure resembles Zeek's grammar but differs in
a number of ways. For example, it tracks newlines explicitly and relies more
strongly on precedence and associativity to resolve ambiguities. Like Zeek's
parser, this one currently doesn't name symbols deeply: for example, the grammar
features an `expr` rule that covers any kind of expression, but the choices
aren't currently broken down into, say, `addition_expr`, `or_expr`, and
similars.

## Usage

To use the generated parser directly (e.g. via any of tree-sitter's
[language bindings](https://tree-sitter.github.io/tree-sitter/#language-bindings)),
clone this repository recursively. We maintain a separate
[git repository](https://github.com/zeek/tree-sitter-zeek-src)
to track generated sources. You do not need the tree-sitter CLI
to use those sources in your tooling, but you'll likely want it
anyway to explore the parser. For example, `tree-sitter parse <script>`
produces the script's syntax tree, and `tree-sitter highlight <script>`
shows syntax-highlighted sources.

## Building the parser

- Install [tree-sitter](https://tree-sitter.github.io/tree-sitter/creating-parsers#installation) on your machine.
- Generate the parser: run `tree-sitter generate`.

## Testing

There's currently no `tree-sitter test` testsuite. Instead, a test driver
clones the Zeek repository and runs on every Zeek script in the distribution.
19 changes: 19 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"targets": [
{
"target_name": "tree_sitter_zeek_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
],
"sources": [
"bindings/node/binding.cc",
"src/parser.c",
# If your language uses an external scanner, add it here.
],
"cflags_c": [
"-std=c99",
]
}
]
}
28 changes: 28 additions & 0 deletions bindings/node/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"

using namespace v8;

extern "C" TSLanguage * tree_sitter_zeek();

namespace {

NAN_METHOD(New) {}

void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_zeek());

Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("zeek").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
}

NODE_MODULE(tree_sitter_zeek_binding, Init)

} // namespace
19 changes: 19 additions & 0 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
try {
module.exports = require("../../build/Release/tree_sitter_zeek_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_zeek_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
} catch (_) {}
Loading