Skip to content

Commit

Permalink
Update grammar
Browse files Browse the repository at this point in the history
- Add license file
- Bump dependencies
- Don't enforce double quotes
- Add corpus tests
- Add CI workflow
  • Loading branch information
ObserverOfTime committed Aug 19, 2023
1 parent 425c915 commit 8a3f07c
Show file tree
Hide file tree
Showing 15 changed files with 614 additions and 1,114 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* text=auto

src/** linguist-generated
bindings/** linguist-generated
binding.gyp linguist-generated
Cargo.toml linguist-generated
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test grammar

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
name: Checkout repository
- uses: actions/setup-node@v3
name: Set up NodeJS
with:
node-version: 16
- run: npm install
name: Install dependencies
- run: npm test
name: Run tests
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
node_modules/
package-lock.json
yarn.lock
Cargo.lock

build/
target/
*.wasm
7 changes: 3 additions & 4 deletions Cargo.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Copyright 2023 ObserverOfTime
Copyright 2021 Peter Stuifzand

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 changes: 1 addition & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
# tree-sitter-printf

## Add to Neovim

Add this to your Neovim config.

```lua
lua << EOF
if pcall(require, "nvim-treesitter.parsers") then
local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.printf = {
install_info = {
url = "https://github.com/pstuifzand/tree-sitter-printf",
files = { "src/parser.c" },
},
filetype = "printf",
}
end

EOF
```

## Injections

### `~/.config/nvim/after/queries/go/injections.scm`

Example of how to inject the printf grammar into the Go grammar.

```tree-sitter
(call_expression
function:
(selector_expression field: (field_identifier) @_method)
(#match? @_method "^Sprintf|Printf|Fprintf|Fatalf$")
arguments: (argument_list . (interpreted_string_literal) @printf)
)
```

### `~/.config/nvim/after/queries/printf/highlights.scm`

Highlight the formats. This will highlight the format with one color.

```tree-sitter
(format) @printf
```

### Author

Peter Stuifzand
Injectable grammar for printf formats.
79 changes: 52 additions & 27 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
module.exports = grammar({
name: 'printf',

rules: {
format_string: $ => seq('"', optional(repeat($.text_parts)), '"'),

text_parts: $ => choice($.text, $.format, '%%'),

format: $ => seq('%',
optional(field('flags', $.flags)),
optional(field('width', $.width)),
optional(field('precision', $.precision)),
optional(field('size', $.size)),
field('type', $.type)
),

type: $ => /[a-z]/,
/**
* @file Tree-sitter grammar definition
* @author Peter Stuifzand
* @license ISC
* @todo Support {} format
* @see {@link https://manned.org/printf.3|C}
* @see {@link https://learn.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions|MSVC}
* @see {@link https://pkg.go.dev/fmt|Go}
*/

flags: $ => /[ +0#-]/,

width: $ => /0?[0-9*]+/,

precision: $ => /\.[0-9]*/,

size: $ => choice('hh', 'h', 'j', 'l', 'L', 'll', 't', 'w', 'z', 'I', 'I32', 'I64'),

text: $ => /[^%]+/,
}
module.exports = grammar({
name: 'printf',

rules: {
format_string: $ => repeat(
choice($._text, $.format, '%%')
),

format: $ => seq(
'%',
optional($.flags),
optional($.width),
optional($.precision),
optional($.size),
$.type
),

// TODO: restrict?
type: _ => /[a-zA-Z]/,

flags: _ => /[-#0 +'I]/,

width: _ => /0?[0-9*]+/,

precision: _ => /\.[0-9]*/,

size: _ => token(prec(1, choice(
'hh',
'h',
'l',
'll',
'L',
'j',
'z',
't',
'I',
'I32',
'I64',
'w',
))),

_text: _ => prec(-1, /[^%]+/)
}
});
9 changes: 9 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files": ["grammar.js"],
"exclude": ["node_modules"],
"compilerOptions": {
"checkJs": true,
"module": "Node16",
"types": ["tree-sitter-cli/dsl"]
}
}
19 changes: 0 additions & 19 deletions package-lock.json

This file was deleted.

23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
{
"name": "tree-sitter-printf",
"version": "0.1.0",
"version": "0.2.0",
"description": "Injectable grammar for printf formats",
"repository": "ObserverOfTime/tree-sitter-printf",
"main": "bindings/node",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "tree-sitter generate",
"test": "tree-sitter test"
},
"author": "",
"author": {
"name": "Peter Stuifzand"
},
"maintainers": [
{"name": "ObserverOfTime"}
],
"keywords": [
"tree-sitter",
"parser",
"lexer",
"printf"
],
"license": "ISC",
"dependencies": {
"nan": "^2.14.2"
"nan": "^2.17.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.19.5"
"tree-sitter-cli": "^0.20.8"
}
}

0 comments on commit 8a3f07c

Please sign in to comment.