feat(injections): add printf format strings

This commit is contained in:
ObserverOfTime 2023-08-19 19:28:45 +03:00 committed by Christian Clason
parent 5b90ea2aba
commit 17fae3f4a3
13 changed files with 181 additions and 0 deletions

View file

@ -328,6 +328,7 @@ We are looking for maintainers to add more parsers and to write query files for
- [x] [pod](https://github.com/tree-sitter-perl/tree-sitter-pod) (maintained by @RabbiVeesh, @LeoNerd)
- [x] [Path of Exile item filter](https://github.com/ObserverOfTime/tree-sitter-poe-filter) (experimental, maintained by @ObserverOfTime)
- [x] [pony](https://github.com/amaanq/tree-sitter-pony) (maintained by @amaanq, @mfelsche)
- [x] [printf](https://github.com/ObserverOfTime/tree-sitter-printf) (maintained by @ObserverOfTime)
- [x] [prisma](https://github.com/victorhqc/tree-sitter-prisma) (maintained by @elianiva)
- [x] [promql](https://github.com/MichaHoffmann/tree-sitter-promql) (maintained by @MichaHoffmann)
- [x] [properties](https://github.com/ObserverOfTime/tree-sitter-properties) (maintained by @ObserverOfTime)

View file

@ -464,6 +464,9 @@
"pony": {
"revision": "16f930b250433cfcd4fb4144df92bb98ad344c20"
},
"printf": {
"revision": "8a3f07c1f620ae46d14873e5b7ee0d3e114c15e8"
},
"prisma": {
"revision": "eca2596a355b1a9952b4f80f8f9caed300a272b5"
},

View file

@ -1387,6 +1387,14 @@ list.pony = {
maintainers = { "@amaanq", "@mfelsche" },
}
list.printf = {
install_info = {
url = "https://github.com/ObserverOfTime/tree-sitter-printf",
files = { "src/parser.c" },
},
maintainers = { "@ObserverOfTime" },
}
list.prisma = {
install_info = {
url = "https://github.com/victorhqc/tree-sitter-prisma",

View file

@ -3,3 +3,11 @@
((regex) @injection.content
(#set! injection.language "regex"))
((print_statement
(exp_list . (string) @injection.content))
(#set! injection.language "printf"))
((printf_statement
(exp_list . (string) @injection.content))
(#set! injection.language "printf"))

View file

@ -8,3 +8,28 @@
(heredoc_body) @injection.content
(heredoc_end) @injection.language)
(#downcase! @injection.language))
; printf 'format'
((command
name: (command_name) @_command
. argument: [(string) (raw_string)] @injection.content)
(#eq? @_command "printf")
(#set! injection.language "printf"))
; printf -v var 'format'
((command
name: (command_name) @_command
argument: (word) @_arg
. (_) . argument: [(string) (raw_string)] @injection.content)
(#eq? @_command "printf")
(#eq? @_arg "-v")
(#set! injection.language "printf"))
; printf -- 'format'
((command
name: (command_name) @_command
argument: (word) @_arg
. argument: [(string) (raw_string)] @injection.content)
(#eq? @_command "printf")
(#eq? @_arg "--")
(#set! injection.language "printf"))

View file

@ -12,6 +12,73 @@
(#lua-match? @injection.content "/[*][!<*][^a-zA-Z]")
(#set! injection.language "doxygen"))
((call_expression
function: (identifier) @_function
arguments: (argument_list
. (string_literal (string_content) @injection.content)))
(#any-of? @_function "printf" "printf_s"
"vprintf" "vprintf_s"
"scanf" "scanf_s"
"vscanf" "vscanf_s"
"wprintf" "wprintf_s"
"vwprintf" "vwprintf_s"
"wscanf" "wscanf_s"
"vwscanf" "vwscanf_s"
"cscanf" "_cscanf"
"printw"
"scanw")
(#set! injection.language "printf"))
((call_expression
function: (identifier) @_function
arguments: (argument_list
(_) . (string_literal (string_content) @injection.content)))
(#any-of? @_function "fprintf" "fprintf_s"
"sprintf"
"dprintf"
"fscanf" "fscanf_s"
"sscanf" "sscanf_s"
"vsscanf" "vsscanf_s"
"vfprintf" "vfprintf_s"
"vsprintf"
"vdprintf"
"fwprintf" "fwprintf_s"
"vfwprintf" "vfwprintf_s"
"fwscanf" "fwscanf_s"
"swscanf" "swscanf_s"
"vswscanf" "vswscanf_s"
"vfscanf" "vfscanf_s"
"vfwscanf" "vfwscanf_s"
"wprintw"
"vw_printw" "vwprintw"
"wscanw"
"vw_scanw" "vwscanw")
(#set! injection.language "printf"))
((call_expression
function: (identifier) @_function
arguments: (argument_list
(_) . (_) . (string_literal (string_content) @injection.content)))
(#any-of? @_function "sprintf_s"
"snprintf" "snprintf_s"
"vsprintf_s"
"vsnprintf" "vsnprintf_s"
"swprintf" "swprintf_s"
"snwprintf_s"
"vswprintf" "vswprintf_s"
"vsnwprintf_s"
"mvprintw"
"mvscanw")
(#set! injection.language "printf"))
((call_expression
function: (identifier) @_function
arguments: (argument_list
(_) . (_) . (_) . (string_literal (string_content) @injection.content)))
(#any-of? @_function "mvwprintw"
"mvwscanw")
(#set! injection.language "printf"))
; TODO: add when asm is added
; (gnu_asm_expression assembly_code: (string_literal) @injection.content
; (#set! injection.language "asm"))

View file

@ -19,6 +19,20 @@
(#offset! @injection.content 0 1 0 -1)
(#set! injection.language "regex")))
((comment) @injection.content
(#match? @injection.content "/\\*!([a-zA-Z]+:)?re2c")
(#set! injection.language "re2c"))
((call_expression
function: (selector_expression field: (field_identifier) @_method)
arguments: (argument_list . (interpreted_string_literal) @injection.content))
(#any-of? @_method "Printf" "Sprintf" "Fatalf" "Scanf")
(#set! injection.language "printf"))
((call_expression
function: (selector_expression field: (field_identifier) @_method)
arguments: (argument_list (_) . (interpreted_string_literal) @injection.content))
(#eq? @_method "Fprintf")
(#set! injection.language "printf"))

View file

@ -7,3 +7,10 @@
((block_comment) @injection.content
(#lua-match? @injection.content "/[*][!<*][^a-zA-Z]")
(#set! injection.language "doxygen"))
((method_invocation
name: (identifier) @_method
arguments: (argument_list
. (string_literal . (_) @injection.content)))
(#any-of? @_method "format" "printf")
(#set! injection.language "printf"))

View file

@ -34,3 +34,11 @@
(value_arguments
(value_argument
(string_literal) @injection.content (#set! injection.language "regex")))))
; "pi = %.2f".format(3.14159)
((call_expression
(navigation_expression
(string_literal) @injection.content
(navigation_suffix (simple_identifier) @_method)))
(#eq? @_method "format")
(#set! injection.language "printf"))

View file

@ -135,6 +135,23 @@
(#set! injection.language "luap")
(#set! injection.include-children))))
; string.format("pi = %.2f", 3.14159)
((function_call
(dot_index_expression
field: (identifier) @_method)
arguments: (arguments
. (string (string_content) @injection.content)))
(#eq? @_method "format")
(#set! injection.language "printf"))
; ("pi = %.2f"):format(3.14159)
((function_call
(method_index_expression
table: (_ (string (string_content) @injection.content))
method: (identifier) @_method))
(#eq? @_method "format")
(#set! injection.language "printf"))
(comment
content: (_) @injection.content
(#set! injection.language "comment"))

View file

@ -0,0 +1 @@
(format) @character

View file

@ -6,5 +6,10 @@
(#eq? @_re "re")
(#set! injection.language "regex"))
((binary_operator
left: (string (string_content) @injection.content)
operator: "%")
(#set! injection.language "printf"))
((comment) @injection.content
(#set! injection.language "comment"))

View file

@ -26,5 +26,22 @@
(#set! injection.language "c")
)
; string.format('...')
((function_call
(index
(identifier) @_base
key: (identifier) @_entry)
(arguments . (string) @injection.content))
(#eq? @_base "string")
(#eq? @_entry "format")
(#set! injection.language "printf"))
; ('...'):format()
((function_call
(method_index (string) @printf
key: (identifier) @_func))
(#eq? @_func "format")
(#set! injection.language "printf"))
((comment) @injection.content
(#set! injection.language "comment"))