Skip to content

Commit

Permalink
Merge branch 'topic/bbannier/bump-tree-sitter-zeek'
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Jan 24, 2025
2 parents 279e3ab + eec98e2 commit c551e3b
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: check-added-large-files

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.2
rev: v0.9.3
hooks:
- id: ruff
- id: ruff-format
Expand Down
25 changes: 25 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
1.3.0-18 | 2025-01-24 17:02:50 +0100

* Bump pre-commit hooks (Benjamin Bannier, Corelight)

* Add test for formatting of IDs (Benjamin Bannier, Corelight)

* Fix formatting of redefs changing field attributes (Benjamin Bannier, Corelight)

* Add formatter for `@pragma` (Benjamin Bannier, Corelight)

* Make sample numbering deterministic (Benjamin Bannier, Corelight)

* Do not include path in snapshot name (Benjamin Bannier, Corelight)

* Bump tree-sitter-zeek (Benjamin Bannier, Corelight)

This bump brings in e.g., support for parsing more attributes and
support for parsing some additional forms of IDs, record field removal
redefs and some forms of pragmas.

It also introduces some changes to behavior on errors. Since technically
`zeek-format` should not format inputs with parse errors this does seem
slightly invariant, but since we currently test for the exact error
behavior here I updated test baselines for that.

1.3.0-10 | 2025-01-22 07:32:36 +0100

* build(deps-dev): bump tree-sitter from 0.23.2 to 0.24.0 (dependabot[bot])
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.0-10
1.3.0-18
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ requires-python = ">= 3.10"

dependencies = [
"tree-sitter==0.24.0",
"tree-sitter-zeek==0.2.1",
"tree-sitter-zeek==0.2.2",
]

[project.optional-dependencies]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global a: bool;
global mod::a: bool;
global mod::sub_mod::a: bool;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
event run_sync_hook()
{
hook Telemetry::sync();
@pragma push ignore-deprecations
schedule sync_interval { run_sync_hook() };
@pragma pop ignore-deprecations
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
redef record fa_file += {
pe: Info &optional;
};

redef enum Log::ID += { LOG };

# Single.
redef record Conn::Info$ip_proto -= { &log } ;
# Multiple (actually only `&log` can be redefined).
redef record Conn::Info$ip_proto -= { &log &optional } ;
3 changes: 3 additions & 0 deletions tests/samples/id.zeek
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global a: bool;
global mod::a: bool;
global mod::sub_mod::a: bool;
6 changes: 6 additions & 0 deletions tests/samples/pragma.zeek
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
event run_sync_hook() {
hook Telemetry::sync();
@pragma push ignore-deprecations
schedule sync_interval { run_sync_hook() };
@pragma pop ignore-deprecations
}
10 changes: 10 additions & 0 deletions tests/samples/redef.zeek
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
redef record fa_file += {
pe: Info &optional;
};

redef enum Log::ID += { LOG };

# Single.
redef record Conn::Info$ip_proto-={&log};
# Multiple (actually only `&log` can be redefined).
redef record Conn::Info$ip_proto-={&log &optional};
4 changes: 2 additions & 2 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ def test_mid_record_error(self):
""",
"""type foo: record {
a: count; ##< A field
b count; ##< A broken field
b count; ##< A broken field
c: count; ##< Another field, better not skipped!
d: count; ##< Ditto.
};
""",
(
"\tb count; ##< A broken field",
2,
'cannot parse line 2, col 1: "b count;"',
'cannot parse line 2, col 1: "b count; ##< A broken field\n\tc: count; ##< Another[...]"',
),
)

Expand Down
6 changes: 4 additions & 2 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import io
import os
from pathlib import Path

import pytest
Expand Down Expand Up @@ -39,7 +40,8 @@ def _get_samples():
# We exclude directories since we store snapshots along with the samples.
# This assumes that there are no tests in subdirectories of `SAMPLES_DIR`.
try:
return [sample for sample in SAMPLES_DIR.iterdir() if sample.is_file()]
# Make sample order deterministic.
return sorted([sample for sample in SAMPLES_DIR.iterdir() if sample.is_file()])
except FileNotFoundError:
return []

Expand All @@ -52,7 +54,7 @@ def test_samples(sample: Path, snapshot: SnapshotAssertion):
assert input_.parse(), f"failed to parse input {sample}"
assert not input_.has_error(), f"parse result for {sample} has parse errors"

name = str(sample.relative_to(SAMPLES_DIR.parent.parent))
name = str(os.path.basename(sample))

output = _format(input_)
assert output == snapshot(name=name), (
Expand Down
2 changes: 1 addition & 1 deletion zeekscript/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Wrapper around more low-level tests."""

__version__ = "1.3.0-10"
__version__ = "1.3.0-18"
__all__ = [
"Formatter",
"Script",
Expand Down
36 changes: 25 additions & 11 deletions zeekscript/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ def format(self):
self.ostream.use_linebreaks(True)


class PragmaFormatter(LineFormatter):
def format(self):
self._format_token()


class ModuleDeclFormatter(Formatter):
def format(self):
self._format_child() # 'module'
Expand Down Expand Up @@ -598,19 +603,28 @@ def format(self):
self._write_sp()
self._format_child() # 'record'
self._write_sp()
self._format_child() # <id>

# We could either be change fields in a type, or change attributes on a
# field. In the first case we would hold an id here, else and expr.
is_redef_attr = self._get_child_name() == "expr"
self._format_child() # <id>/<expr>

self._write_sp()
self._format_child() # '+='
self._format_child() # '+='/'-='
self._write_sp()
self._format_child() # '{'
self._write_nl()
while self._get_child_name() == "type_spec": # any number of type_specs
self._format_child(indent=True)
self._format_child() # '}'
if self._get_child_name() == "attr_list":
self._write_sp()
self._format_child() # <attr_list>
self._format_child(hints=Hint.NO_LB_BEFORE) # ';'

if is_redef_attr:
self._format_children(sep=" ")
else:
self._format_child() # '{'
self._write_nl()
while self._get_child_name() == "type_spec":
self._format_child(indent=True)
self._format_child() # '}'
if self._get_child_name() == "attr_list":
self._write_sp()
self._format_child() # <attr_list>
self._format_child(hints=Hint.NO_LB_BEFORE) # ';'
self._write_nl()


Expand Down

0 comments on commit c551e3b

Please sign in to comment.