forked from pstuifzand/tree-sitter-printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add license file - Bump dependencies - Don't enforce double quotes - Add corpus tests - Add CI workflow
- Loading branch information
1 parent
425c915
commit 8a3f07c
Showing
15 changed files
with
614 additions
and
1,114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, /[^%]+/) | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.