Skip to content

Commit

Permalink
Merge pull request #12 from mattmassicotte/feature/php
Browse files Browse the repository at this point in the history
PHP support
  • Loading branch information
krzyzanowskim authored Mar 29, 2022
2 parents e98ed15 + ebfccdc commit 668fc0b
Show file tree
Hide file tree
Showing 24 changed files with 246 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The `tree_sitter.xcframework` binary comes with:
- [tree-sitter-gomod](https://github.com/camdencheek/tree-sitter-go-mod)
- [tree-sitter-ruby](https://github.com/tree-sitter/tree-sitter-ruby)
- [tree-sitter-json](https://github.com/tree-sitter/tree-sitter-json)
- [tree-sitter-php](https://github.com/tree-sitter/tree-sitter-php)

This is a [work-in-progress](https://github.com/tree-sitter/tree-sitter/issues/1488). But, if the parser you'd like to use doesn't have a Makefile, let me know and I'll help get it set up.

Expand Down
5 changes: 5 additions & 0 deletions Sources/tree_sitter_language_resources/LanguageResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum LanguageResource: CaseIterable, Hashable {
case go
case gomod
case json
case php
case ruby
case swift

Expand All @@ -16,6 +17,8 @@ public enum LanguageResource: CaseIterable, Hashable {
return "gomod"
case .json:
return "json"
case .php:
return "php"
case .ruby:
return "ruby"
case .swift:
Expand All @@ -31,6 +34,8 @@ public enum LanguageResource: CaseIterable, Hashable {
return tree_sitter_gomod()
case .json:
return tree_sitter_json()
case .php:
return tree_sitter_php()
case .ruby:
return tree_sitter_ruby()
case .swift:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
(php_tag) @tag
"?>" @tag

; Types

(primitive_type) @type.builtin
(cast_type) @type.builtin
(named_type (name) @type) @type
(named_type (qualified_name) @type) @type

; Functions

(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)

(scoped_call_expression
name: (name) @function)

(member_call_expression
name: (name) @function.method)

(function_definition
name: (name) @function)

; 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

((name) @constant
(#match? @constant "^_?[A-Z][A-Z\\d_]+$"))

((name) @constructor
(#match? @constructor "^[A-Z]"))

((name) @variable.builtin
(#eq? @variable.builtin "this"))

(variable_name) @variable

; Basic tokens

(string) @string
(heredoc) @string
(boolean) @constant.builtin
(null) @constant.builtin
(integer) @number
(float) @number
(comment) @comment

"$" @operator

; Keywords

"abstract" @keyword
"as" @keyword
"break" @keyword
"case" @keyword
"catch" @keyword
"class" @keyword
"const" @keyword
"continue" @keyword
"declare" @keyword
"default" @keyword
"do" @keyword
"echo" @keyword
"else" @keyword
"elseif" @keyword
"enddeclare" @keyword
"endforeach" @keyword
"endif" @keyword
"endswitch" @keyword
"endwhile" @keyword
"extends" @keyword
"final" @keyword
"finally" @keyword
"foreach" @keyword
"function" @keyword
"global" @keyword
"if" @keyword
"implements" @keyword
"include_once" @keyword
"include" @keyword
"insteadof" @keyword
"interface" @keyword
"namespace" @keyword
"new" @keyword
"private" @keyword
"protected" @keyword
"public" @keyword
"require_once" @keyword
"require" @keyword
"return" @keyword
"static" @keyword
"switch" @keyword
"throw" @keyword
"trait" @keyword
"try" @keyword
"use" @keyword
"while" @keyword
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((text) @injection.content
(#set! injection.language "html")
(#set! injection.combined))
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(class_declaration
name: (name) @name) @definition.class

(function_definition
name: (name) @name) @definition.function

(method_declaration
name: (name) @name) @definition.function

(object_creation_expression
[
(qualified_name (name) @name)
(variable_name (name) @name)
]) @reference.class

(function_call_expression
function: [
(qualified_name (name) @name)
(variable_name (name)) @name
]) @reference.call

(scoped_call_expression
name: (name) @name) @reference.call

(member_call_expression
name: (name) @name) @reference.call
18 changes: 14 additions & 4 deletions scripts/build-tree-sitter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ gh pr checkout 19
popd
build_parser "json"

git clone --depth 1 https://github.com/tree-sitter/tree-sitter-php.git
pushd tree-sitter-php
gh pr checkout 127
popd
build_parser "php"

# now, build the frameworks

pushd $TMP_BUILD_DIR/build/macos
Expand All @@ -123,7 +129,8 @@ libtool -static -o libtree-sitter.a \
lib/libtree-sitter-go.a \
lib/libtree-sitter-gomod.a \
lib/libtree-sitter-ruby.a \
lib/libtree-sitter-json.a
lib/libtree-sitter-json.a \
lib/libtree-sitter-php.a \

mkdir -p tree_sitter.framework/Versions/A/{Headers,Modules,Resources}
cp -f libtree-sitter.a tree_sitter.framework/Versions/A/tree_sitter
Expand Down Expand Up @@ -153,7 +160,8 @@ libtool -static -o libtree-sitter.a \
lib/libtree-sitter-go.a \
lib/libtree-sitter-gomod.a \
lib/libtree-sitter-ruby.a \
lib/libtree-sitter-json.a
lib/libtree-sitter-json.a \
lib/libtree-sitter-php.a \

mkdir -p tree_sitter.framework/{Headers,Modules}
cp -f libtree-sitter.a tree_sitter.framework/tree_sitter
Expand All @@ -172,7 +180,8 @@ libtool -static -o libtree-sitter.a \
lib/libtree-sitter-go.a \
lib/libtree-sitter-gomod.a \
lib/libtree-sitter-ruby.a \
lib/libtree-sitter-json.a
lib/libtree-sitter-json.a \
lib/libtree-sitter-php.a \

mkdir -p tree_sitter.framework/{Headers,Modules}
cp -f libtree-sitter.a tree_sitter.framework/tree_sitter
Expand All @@ -191,7 +200,8 @@ libtool -static -o libtree-sitter.a \
lib/libtree-sitter-go.a \
lib/libtree-sitter-gomod.a \
lib/libtree-sitter-ruby.a \
lib/libtree-sitter-json.a
lib/libtree-sitter-json.a \
lib/libtree-sitter-php.a \

mkdir -p tree_sitter.framework/{Headers,Modules}
cp -f libtree-sitter.a tree_sitter.framework/tree_sitter
Expand Down
1 change: 1 addition & 0 deletions shim/tree_sitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#include <tree_sitter/gomod.h>
#include <tree_sitter/ruby.h>
#include <tree_sitter/tree-sitter-json.h>
#include <tree_sitter/php.h>
18 changes: 9 additions & 9 deletions tree_sitter.xcframework/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
<array>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<string>macos-arm64_x86_64</string>
<key>LibraryPath</key>
<string>tree_sitter.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<string>macos</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-maccatalyst</string>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>tree_sitter.framework</string>
<key>SupportedArchitectures</key>
Expand All @@ -29,11 +30,11 @@
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>maccatalyst</string>
<string>simulator</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<string>ios-arm64_x86_64-maccatalyst</string>
<key>LibraryPath</key>
<string>tree_sitter.framework</string>
<key>SupportedArchitectures</key>
Expand All @@ -44,20 +45,19 @@
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
<string>maccatalyst</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>macos-arm64_x86_64</string>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>tree_sitter.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>macos</string>
<string>ios</string>
</dict>
</array>
<key>CFBundlePackageType</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ extern TSLanguage *tree_sitter_go();
}
#endif

#endif // TREE_SITTER_GO_H_
#endif // TREE_SITTER_GO_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TREE_SITTER_PHP_H_
#define TREE_SITTER_PHP_H_

#include <tree_sitter/parser.h>

#ifdef __cplusplus
extern "C" {
#endif

extern TSLanguage *tree_sitter_php();

#ifdef __cplusplus
}
#endif

#endif // TREE_SITTER_PHP_H_
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#include <tree_sitter/gomod.h>
#include <tree_sitter/ruby.h>
#include <tree_sitter/tree-sitter-json.h>
#include <tree_sitter/php.h>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ extern TSLanguage *tree_sitter_go();
}
#endif

#endif // TREE_SITTER_GO_H_
#endif // TREE_SITTER_GO_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TREE_SITTER_PHP_H_
#define TREE_SITTER_PHP_H_

#include <tree_sitter/parser.h>

#ifdef __cplusplus
extern "C" {
#endif

extern TSLanguage *tree_sitter_php();

#ifdef __cplusplus
}
#endif

#endif // TREE_SITTER_PHP_H_
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#include <tree_sitter/gomod.h>
#include <tree_sitter/ruby.h>
#include <tree_sitter/tree-sitter-json.h>
#include <tree_sitter/php.h>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ extern TSLanguage *tree_sitter_go();
}
#endif

#endif // TREE_SITTER_GO_H_
#endif // TREE_SITTER_GO_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TREE_SITTER_PHP_H_
#define TREE_SITTER_PHP_H_

#include <tree_sitter/parser.h>

#ifdef __cplusplus
extern "C" {
#endif

extern TSLanguage *tree_sitter_php();

#ifdef __cplusplus
}
#endif

#endif // TREE_SITTER_PHP_H_
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#include <tree_sitter/gomod.h>
#include <tree_sitter/ruby.h>
#include <tree_sitter/tree-sitter-json.h>
#include <tree_sitter/php.h>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ extern TSLanguage *tree_sitter_go();
}
#endif

#endif // TREE_SITTER_GO_H_
#endif // TREE_SITTER_GO_H_
Loading

0 comments on commit 668fc0b

Please sign in to comment.