~rockorager/tree-sitter-aerc

5a8272bc67e910c3624711775f0280c240ae1ce2 — Tim Culverhouse 1 year, 6 months ago main
initial commit

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
7 files changed, 220 insertions(+), 0 deletions(-)

A .gitignore
A LICENSE
A README.md
A corpus/log.txt
A grammar.js
A package.json
A queries/highlights.scm
A  => .gitignore +6 -0
@@ 1,6 @@
/src/
/bindings/
/Cargo.toml
binding.gyp
Cargo.lock
/target/

A  => LICENSE +21 -0
@@ 1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Tim Culverhouse

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.

A  => README.md +23 -0
@@ 1,23 @@
# tree-sitter-aerc

tree-sitter grammar for aerc logs

## Usage

### neovim

1. Manually add the parser to your nvim config:
	```lua
	require("nvim-treesitter.parsers").get_parser_configs().aerc = {
		install_info = {
			url = "https://git.sr.ht/~rockorager/tree-sitter-aerc",
			files = {"src/parser.c"},
		},
		filetype = "aerc",
		requires_generate_from_grammar = true,
	}
	```
2. Link or copy `queries/highlights.scm` to
   `$XDG_CONFIG_HOME/nvim/queries/aerc/highlights.scm`
3. Open nvim, and run :TSInstall aerc
4. Open a log from aerc, and run :set ft=aerc

A  => corpus/log.txt +24 -0
@@ 1,24 @@
================================================================================
TRACE single line
================================================================================

TRACE 2023/01/02 09:16:56.028598 worker.go:89: PostAction *types.Configure
--------------------------------------------------------------------------------

================================================================================
TRACE multi line
================================================================================

TRACE 2023/01/02 09:16:56.028598 worker.go:89: PostAction *types.Configure
TRACE 2023/01/02 09:16:56.028598 worker.go:89: PostAction *types.Configure
--------------------------------------------------------------------------------

================================================================================
Worker Message
================================================================================

TRACE 2023/01/02 09:16:56.028598 worker.go:89: PostAction *types.Configure
TRACE 2023/01/02 09:16:56.631122 worker.go:110: PostMessage *types.Done:*types.FetchDirectoryContents
TRACE 2023/01/02 09:16:56.584155 worker.go:89: PostAction *types.FetchDirectoryContents
TRACE 2023/01/02 09:16:56.584309 worker.go:149: ProcessAction *types.FetchDirectoryContents(217)
--------------------------------------------------------------------------------

A  => grammar.js +104 -0
@@ 1,104 @@
const 
	digit = /[0-9]/,
	letter = choice(/\p{L}/, '_');

module.exports = grammar({
	name: "aerc",

	rules: {
		unit: $ => repeat($.line),

		line: $ => seq(
			$.level,
			$.datetime,
			$.reference,
			$.message,
			optional("\n"),
		),

		level: $ => choice(
			"TRACE",
			"DEBUG",
			"INFO",
			"WARN",
			"ERROR",
		),

		datetime: $ => seq(
			$.date,
			" ",
			$.time,
		),

		date: $ => seq(
			$.year,
			"/",
			$.month,
			"/",
			$.day,
		),

		year: $ => seq(
			digit,
			digit,
			digit,
			digit,
		),
		month: $ => seq(
			digit,
			digit,
		),
		day: $ => seq(
			digit,
			digit,
		),
		
		time: $ => seq(
			$.hour,
			":",
			$.minute,
			":",
			$.second,
			".",
			$.microsecond,
		),

		hour: $ => seq(
			digit,
			digit,
		),
		minute: $ => seq(
			digit,
			digit,
		),
		second: $ => seq(
			digit,
			digit,
		),
		microsecond: $ => seq(
			digit,
			digit,
			digit,
			digit,
			digit,
			digit,
		),

		reference: $ => seq(
			$.filename,
			":",
			$.linenumber,
			":",
		),

		filename: $ => seq(
			repeat1($._char),
		),

		linenumber: $ => repeat1(digit),

		message: $ => /.*/,

		_char: $ => /./,
	}
})

A  => package.json +19 -0
@@ 1,19 @@
{
  "name": "tree-sitter-aerclog",
  "version": "0.0.1",
  "description": "aerclog grammar for tree-sitter",
  "main": "bindings/node",
  "keywords": [
    "parsing",
    "incremental"
  ],
  "dependencies": {
    "nan": "^2.12.1"
  },
  "devDependencies": {
    "tree-sitter-cli": "^0.20.7"
  },
  "scripts": {
    "test": "tree-sitter test"
  }
}

A  => queries/highlights.scm +23 -0
@@ 1,23 @@
"TRACE" @comment
"DEBUG" @debug
"WARN" @string.regex
"INFO" @text.note

(level) @tag


[
 "/"
 ":"
 "."
] @punctuation

(datetime
  (date) @property
  (time) @number)

(reference
  (filename) @text.reference
  (linenumber) @tag)

(message) @string