feat: add the php_only parser included in tree-sitter-php (#5876)

Add parser  `php_only` for PHP files without HTML embedded.
Make queries for combined parser `php` inherit from `php_only` (no extensions needed).

---------

Co-authored-by: shirasaka <tk.shirasaka@gmail>
This commit is contained in:
tk-shirasaka 2024-01-20 21:34:49 +09:00 committed by GitHub
parent a8fa3047b5
commit 64b3d5e569
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 574 additions and 557 deletions

View file

@ -322,6 +322,7 @@ We are looking for maintainers to add more parsers and to write query files for
- [x] [pem](https://github.com/ObserverOfTime/tree-sitter-pem) (maintained by @ObserverOfTime)
- [x] [perl](https://github.com/tree-sitter-perl/tree-sitter-perl) (maintained by @RabbiVeesh, @LeoNerd)
- [x] [php](https://github.com/tree-sitter/tree-sitter-php) (maintained by @tk-shirasaka)
- [x] [php_only](https://github.com/tree-sitter/tree-sitter-php) (maintained by @tk-shirasaka)
- [x] [phpdoc](https://github.com/claytonrcarter/tree-sitter-phpdoc) (experimental, maintained by @mikehaertl)
- [x] [pioasm](https://github.com/leo60228/tree-sitter-pioasm) (maintained by @leo60228)
- [x] [po](https://github.com/erasin/tree-sitter-po) (maintained by @amaanq)

View file

@ -446,6 +446,9 @@
"php": {
"revision": "b569a5f2c0d592e67430520d1a0e1f765d83ceb0"
},
"php_only": {
"revision": "b569a5f2c0d592e67430520d1a0e1f765d83ceb0"
},
"phpdoc": {
"revision": "915a527d5aafa81b31acf67fab31b0ac6b6319c0"
},

View file

@ -1332,6 +1332,15 @@ list.php = {
maintainers = { "@tk-shirasaka" },
}
list.php_only = {
install_info = {
url = "https://github.com/tree-sitter/tree-sitter-php",
location = "php_only",
files = { "src/parser.c", "src/scanner.c" },
},
maintainers = { "@tk-shirasaka" },
}
-- Parsers for injections
list.phpdoc = {
install_info = {

View file

@ -1,16 +1 @@
[
(if_statement)
(switch_statement)
(while_statement)
(do_statement)
(for_statement)
(foreach_statement)
(try_statement)
(function_definition)
(class_declaration)
(interface_declaration)
(trait_declaration)
(enum_declaration)
(function_static_declaration)
(method_declaration)
] @fold
; inherits: php_only

View file

@ -1,373 +1 @@
; Variables
(variable_name) @variable
; Constants
((name) @constant
(#lua-match? @constant "^_?[A-Z][A-Z%d_]*$"))
((name) @constant.builtin
(#lua-match? @constant.builtin "^__[A-Z][A-Z%d_]+__$"))
(const_declaration
(const_element
(name) @constant))
; Types
[
(primitive_type)
(cast_type)
(bottom_type)
] @type.builtin
(named_type
[
(name) @type
(qualified_name
(name) @type)
])
(class_declaration
name: (name) @type)
(base_clause
[
(name) @type
(qualified_name
(name) @type)
])
(enum_declaration
name: (name) @type)
(interface_declaration
name: (name) @type)
(namespace_use_clause
[
(name) @type
(qualified_name
(name) @type)
])
(namespace_aliasing_clause
(name) @type.definition)
(class_interface_clause
[
(name) @type
(qualified_name
(name) @type)
])
(scoped_call_expression
scope:
[
(name) @type
(qualified_name
(name) @type)
])
(class_constant_access_expression
.
[
(name) @type
(qualified_name
(name) @type)
]
(name) @constant)
(trait_declaration
name: (name) @type)
(use_declaration
(name) @type)
(binary_expression
operator: "instanceof"
right:
[
(name) @type
(qualified_name
(name) @type)
])
; Functions, methods, constructors
(array_creation_expression
"array" @function.builtin)
(list_literal
"list" @function.builtin)
(method_declaration
name: (name) @function.method)
(function_call_expression
function:
(qualified_name
(name) @function.call))
(function_call_expression
(name) @function.call)
(scoped_call_expression
name: (name) @function.call)
(member_call_expression
name: (name) @function.method.call)
(function_definition
name: (name) @function)
(nullsafe_member_call_expression
name: (name) @function.method)
(method_declaration
name: (name) @constructor
(#eq? @constructor "__construct"))
(object_creation_expression
[
(name) @constructor
(qualified_name
(name) @constructor)
])
; Parameters
[
(simple_parameter)
(variadic_parameter)
] @variable.parameter
(argument
(name) @variable.parameter)
; Member
(property_element
(variable_name) @property)
(member_access_expression
name:
(variable_name
(name)) @property)
(member_access_expression
name: (name) @property)
; Variables
(relative_scope) @variable.builtin
((variable_name) @variable.builtin
(#eq? @variable.builtin "$this"))
; Namespace
(namespace_definition
name:
(namespace_name
(name) @module))
(namespace_name_as_prefix
(namespace_name
(name) @module))
; Attributes
(attribute_list) @attribute
; Conditions ( ? : )
(conditional_expression) @keyword.conditional
; Directives
(declare_directive
[
"strict_types"
"ticks"
"encoding"
] @variable.parameter)
; Basic tokens
[
(string)
(encapsed_string)
(heredoc_body)
(nowdoc_body)
(shell_command_expression) ; backtick operator: `ls -la`
] @string
(escape_sequence) @string.escape
[
(heredoc_start)
(heredoc_end)
] @label
(nowdoc
"'" @label)
(boolean) @boolean
(null) @constant.builtin
(integer) @number
(float) @number.float
(comment) @comment @spell
(named_label_statement) @label
; Keywords
[
"and"
"as"
"instanceof"
"or"
"xor"
] @keyword.operator
[
"fn"
"function"
] @keyword.function
[
"break"
"class"
"clone"
"declare"
"default"
"echo"
"enddeclare"
"enum"
"extends"
"global"
"goto"
"implements"
"insteadof"
"interface"
"namespace"
"new"
"trait"
"unset"
] @keyword
[
"abstract"
"const"
"final"
"private"
"protected"
"public"
"readonly"
"static"
] @type.qualifier
[
"return"
"yield"
] @keyword.return
[
"case"
"else"
"elseif"
"endif"
"endswitch"
"if"
"switch"
"match"
"??"
] @keyword.conditional
[
"continue"
"do"
"endfor"
"endforeach"
"endwhile"
"for"
"foreach"
"while"
] @keyword.repeat
[
"catch"
"finally"
"throw"
"try"
] @keyword.exception
[
"include_once"
"include"
"require_once"
"require"
"use"
] @keyword.import
[
","
";"
":"
"\\"
] @punctuation.delimiter
[
(php_tag)
"?>"
"("
")"
"["
"]"
"{"
"}"
"#["
] @punctuation.bracket
[
"="
"."
"-"
"*"
"/"
"+"
"%"
"**"
"~"
"|"
"^"
"&"
"<<"
">>"
"<<<"
"->"
"?->"
"=>"
"<"
"<="
">="
">"
"<>"
"=="
"!="
"==="
"!=="
"!"
"&&"
"||"
".="
"-="
"+="
"*="
"/="
"%="
"**="
"&="
"|="
"^="
"<<="
">>="
"??="
"--"
"++"
"@"
"::"
] @operator
; inherits: php_only

View file

@ -1,38 +1 @@
[
(array_creation_expression)
(compound_statement)
(declaration_list)
(binary_expression)
(return_statement)
(arguments)
(formal_parameters)
(enum_declaration_list)
(switch_block)
(match_block)
(case_statement)
] @indent.begin
[
")"
"}"
"]"
] @indent.branch
(comment) @indent.auto
(compound_statement
"}" @indent.end)
(ERROR
"(" @indent.align
.
(_)
(#set! indent.open_delimiter "(")
(#set! indent.close_delimiter ")"))
(ERROR
"[" @indent.align
.
(_)
(#set! indent.open_delimiter "[")
(#set! indent.close_delimiter "]"))
; inherits: php_only

View file

@ -1,47 +1,4 @@
; inherits: php_only
((text) @injection.content
(#set! injection.language "html")
(#set! injection.combined))
((comment) @injection.content
(#set! injection.language "phpdoc"))
((heredoc
(heredoc_body) @injection.content
(heredoc_end) @injection.language
(#set! injection.include-children)
(#downcase! @injection.language)))
((nowdoc
(nowdoc_body) @injection.content
(heredoc_end) @injection.language
(#set! injection.include-children)
(#downcase! @injection.language)))
; regex
((function_call_expression
function: (_) @_preg_func_identifier
arguments:
(arguments
.
(argument
(_
(string_value) @injection.content))))
(#set! injection.language "regex")
(#lua-match? @_preg_func_identifier "^preg_"))
; bash
((function_call_expression
function: (_) @_shell_func_identifier
arguments:
(arguments
.
(argument
(_
(string_value) @injection.content))))
(#set! injection.language "bash")
(#any-of? @_shell_func_identifier "shell_exec" "escapeshellarg" "escapeshellcmd" "exec" "passthru" "proc_open" "shell_exec" "system"))
(expression_statement
(shell_command_expression
(string_value) @injection.content)
(#set! injection.language "bash"))

View file

@ -1,86 +1 @@
; Scopes
;-------
((class_declaration
name: (name) @local.definition.type) @local.scope
(#set! definition.type.scope "parent"))
((method_declaration
name: (name) @local.definition.method) @local.scope
(#set! definition.method.scope "parent"))
((function_definition
name: (name) @local.definition.function) @local.scope
(#set! definition.function.scope "parent"))
(anonymous_function_creation_expression
(anonymous_function_use_clause
(variable_name
(name) @local.definition.var))) @local.scope
; Definitions
;------------
(simple_parameter
(variable_name
(name) @local.definition.var))
(foreach_statement
(pair
(variable_name
(name) @local.definition.var)))
(foreach_statement
(variable_name
(name) @local.reference
(#set! reference.kind "var"))
(variable_name
(name) @local.definition.var))
(property_declaration
(property_element
(variable_name
(name) @local.definition.field)))
(namespace_use_clause
(qualified_name
(name) @local.definition.type))
; References
;------------
(named_type
(name) @local.reference
(#set! reference.kind "type"))
(named_type
(qualified_name) @local.reference
(#set! reference.kind "type"))
(variable_name
(name) @local.reference
(#set! reference.kind "var"))
(member_access_expression
name: (name) @local.reference
(#set! reference.kind "field"))
(member_call_expression
name: (name) @local.reference
(#set! reference.kind "method"))
(function_call_expression
function:
(qualified_name
(name) @local.reference
(#set! reference.kind "function")))
(object_creation_expression
(qualified_name
(name) @local.reference
(#set! reference.kind "type")))
(scoped_call_expression
scope:
(qualified_name
(name) @local.reference
(#set! reference.kind "type"))
name: (name) @local.reference
(#set! reference.kind "method"))
; inherits: php_only

View file

@ -0,0 +1,16 @@
[
(if_statement)
(switch_statement)
(while_statement)
(do_statement)
(for_statement)
(foreach_statement)
(try_statement)
(function_definition)
(class_declaration)
(interface_declaration)
(trait_declaration)
(enum_declaration)
(function_static_declaration)
(method_declaration)
] @fold

View file

@ -0,0 +1,373 @@
; Variables
(variable_name) @variable
; Constants
((name) @constant
(#lua-match? @constant "^_?[A-Z][A-Z%d_]*$"))
((name) @constant.builtin
(#lua-match? @constant.builtin "^__[A-Z][A-Z%d_]+__$"))
(const_declaration
(const_element
(name) @constant))
; Types
[
(primitive_type)
(cast_type)
(bottom_type)
] @type.builtin
(named_type
[
(name) @type
(qualified_name
(name) @type)
])
(class_declaration
name: (name) @type)
(base_clause
[
(name) @type
(qualified_name
(name) @type)
])
(enum_declaration
name: (name) @type)
(interface_declaration
name: (name) @type)
(namespace_use_clause
[
(name) @type
(qualified_name
(name) @type)
])
(namespace_aliasing_clause
(name) @type.definition)
(class_interface_clause
[
(name) @type
(qualified_name
(name) @type)
])
(scoped_call_expression
scope:
[
(name) @type
(qualified_name
(name) @type)
])
(class_constant_access_expression
.
[
(name) @type
(qualified_name
(name) @type)
]
(name) @constant)
(trait_declaration
name: (name) @type)
(use_declaration
(name) @type)
(binary_expression
operator: "instanceof"
right:
[
(name) @type
(qualified_name
(name) @type)
])
; Functions, methods, constructors
(array_creation_expression
"array" @function.builtin)
(list_literal
"list" @function.builtin)
(method_declaration
name: (name) @function.method)
(function_call_expression
function:
(qualified_name
(name) @function.call))
(function_call_expression
(name) @function.call)
(scoped_call_expression
name: (name) @function.call)
(member_call_expression
name: (name) @function.method.call)
(function_definition
name: (name) @function)
(nullsafe_member_call_expression
name: (name) @function.method)
(method_declaration
name: (name) @constructor
(#eq? @constructor "__construct"))
(object_creation_expression
[
(name) @constructor
(qualified_name
(name) @constructor)
])
; Parameters
[
(simple_parameter)
(variadic_parameter)
] @variable.parameter
(argument
(name) @variable.parameter)
; Member
(property_element
(variable_name) @property)
(member_access_expression
name:
(variable_name
(name)) @property)
(member_access_expression
name: (name) @property)
; Variables
(relative_scope) @variable.builtin
((variable_name) @variable.builtin
(#eq? @variable.builtin "$this"))
; Namespace
(namespace_definition
name:
(namespace_name
(name) @module))
(namespace_name_as_prefix
(namespace_name
(name) @module))
; Attributes
(attribute_list) @attribute
; Conditions ( ? : )
(conditional_expression) @keyword.conditional
; Directives
(declare_directive
[
"strict_types"
"ticks"
"encoding"
] @variable.parameter)
; Basic tokens
[
(string)
(encapsed_string)
(heredoc_body)
(nowdoc_body)
(shell_command_expression) ; backtick operator: `ls -la`
] @string
(escape_sequence) @string.escape
[
(heredoc_start)
(heredoc_end)
] @label
(nowdoc
"'" @label)
(boolean) @boolean
(null) @constant.builtin
(integer) @number
(float) @number.float
(comment) @comment @spell
(named_label_statement) @label
; Keywords
[
"and"
"as"
"instanceof"
"or"
"xor"
] @keyword.operator
[
"fn"
"function"
] @keyword.function
[
"break"
"class"
"clone"
"declare"
"default"
"echo"
"enddeclare"
"enum"
"extends"
"global"
"goto"
"implements"
"insteadof"
"interface"
"namespace"
"new"
"trait"
"unset"
] @keyword
[
"abstract"
"const"
"final"
"private"
"protected"
"public"
"readonly"
"static"
] @type.qualifier
[
"return"
"yield"
] @keyword.return
[
"case"
"else"
"elseif"
"endif"
"endswitch"
"if"
"switch"
"match"
"??"
] @keyword.conditional
[
"continue"
"do"
"endfor"
"endforeach"
"endwhile"
"for"
"foreach"
"while"
] @keyword.repeat
[
"catch"
"finally"
"throw"
"try"
] @keyword.exception
[
"include_once"
"include"
"require_once"
"require"
"use"
] @keyword.import
[
","
";"
":"
"\\"
] @punctuation.delimiter
[
(php_tag)
"?>"
"("
")"
"["
"]"
"{"
"}"
"#["
] @punctuation.bracket
[
"="
"."
"-"
"*"
"/"
"+"
"%"
"**"
"~"
"|"
"^"
"&"
"<<"
">>"
"<<<"
"->"
"?->"
"=>"
"<"
"<="
">="
">"
"<>"
"=="
"!="
"==="
"!=="
"!"
"&&"
"||"
".="
"-="
"+="
"*="
"/="
"%="
"**="
"&="
"|="
"^="
"<<="
">>="
"??="
"--"
"++"
"@"
"::"
] @operator

View file

@ -0,0 +1,38 @@
[
(array_creation_expression)
(compound_statement)
(declaration_list)
(binary_expression)
(return_statement)
(arguments)
(formal_parameters)
(enum_declaration_list)
(switch_block)
(match_block)
(case_statement)
] @indent.begin
[
")"
"}"
"]"
] @indent.branch
(comment) @indent.auto
(compound_statement
"}" @indent.end)
(ERROR
"(" @indent.align
.
(_)
(#set! indent.open_delimiter "(")
(#set! indent.close_delimiter ")"))
(ERROR
"[" @indent.align
.
(_)
(#set! indent.open_delimiter "[")
(#set! indent.close_delimiter "]"))

View file

@ -0,0 +1,43 @@
((comment) @injection.content
(#set! injection.language "phpdoc"))
((heredoc
(heredoc_body) @injection.content
(heredoc_end) @injection.language
(#set! injection.include-children)
(#downcase! @injection.language)))
((nowdoc
(nowdoc_body) @injection.content
(heredoc_end) @injection.language
(#set! injection.include-children)
(#downcase! @injection.language)))
; regex
((function_call_expression
function: (_) @_preg_func_identifier
arguments:
(arguments
.
(argument
(_
(string_value) @injection.content))))
(#set! injection.language "regex")
(#lua-match? @_preg_func_identifier "^preg_"))
; bash
((function_call_expression
function: (_) @_shell_func_identifier
arguments:
(arguments
.
(argument
(_
(string_value) @injection.content))))
(#set! injection.language "bash")
(#any-of? @_shell_func_identifier "shell_exec" "escapeshellarg" "escapeshellcmd" "exec" "passthru" "proc_open" "shell_exec" "system"))
(expression_statement
(shell_command_expression
(string_value) @injection.content)
(#set! injection.language "bash"))

View file

@ -0,0 +1,86 @@
; Scopes
;-------
((class_declaration
name: (name) @local.definition.type) @local.scope
(#set! definition.type.scope "parent"))
((method_declaration
name: (name) @local.definition.method) @local.scope
(#set! definition.method.scope "parent"))
((function_definition
name: (name) @local.definition.function) @local.scope
(#set! definition.function.scope "parent"))
(anonymous_function_creation_expression
(anonymous_function_use_clause
(variable_name
(name) @local.definition.var))) @local.scope
; Definitions
;------------
(simple_parameter
(variable_name
(name) @local.definition.var))
(foreach_statement
(pair
(variable_name
(name) @local.definition.var)))
(foreach_statement
(variable_name
(name) @local.reference
(#set! reference.kind "var"))
(variable_name
(name) @local.definition.var))
(property_declaration
(property_element
(variable_name
(name) @local.definition.field)))
(namespace_use_clause
(qualified_name
(name) @local.definition.type))
; References
;------------
(named_type
(name) @local.reference
(#set! reference.kind "type"))
(named_type
(qualified_name) @local.reference
(#set! reference.kind "type"))
(variable_name
(name) @local.reference
(#set! reference.kind "var"))
(member_access_expression
name: (name) @local.reference
(#set! reference.kind "field"))
(member_call_expression
name: (name) @local.reference
(#set! reference.kind "method"))
(function_call_expression
function:
(qualified_name
(name) @local.reference
(#set! reference.kind "function")))
(object_creation_expression
(qualified_name
(name) @local.reference
(#set! reference.kind "type")))
(scoped_call_expression
scope:
(qualified_name
(name) @local.reference
(#set! reference.kind "type"))
name: (name) @local.reference
(#set! reference.kind "method"))