forked from smacker/go-tree-sitter
-
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.
Script fixing up headers and the changed files due to this fix
- Loading branch information
Showing
40 changed files
with
2,143 additions
and
301 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
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,55 @@ | ||
|
||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.198057 // | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
|
||
#ifndef TREE_SITTER_ALLOC_H_ | ||
#define TREE_SITTER_ALLOC_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#if defined(TREE_SITTER_HIDDEN_SYMBOLS) || defined(_WIN32) | ||
#define TS_PUBLIC | ||
#else | ||
#define TS_PUBLIC __attribute__((visibility("default"))) | ||
#endif | ||
|
||
TS_PUBLIC extern void *(*ts_current_malloc)(size_t); | ||
TS_PUBLIC extern void *(*ts_current_calloc)(size_t, size_t); | ||
TS_PUBLIC extern void *(*ts_current_realloc)(void *, size_t); | ||
TS_PUBLIC extern void (*ts_current_free)(void *); | ||
|
||
// Allow clients to override allocation functions | ||
#ifndef ts_malloc | ||
#define ts_malloc ts_current_malloc | ||
#endif | ||
#ifndef ts_calloc | ||
#define ts_calloc ts_current_calloc | ||
#endif | ||
#ifndef ts_realloc | ||
#define ts_realloc ts_current_realloc | ||
#endif | ||
#ifndef ts_free | ||
#define ts_free ts_current_free | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // TREE_SITTER_ALLOC_H_ | ||
|
||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.198057 // | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
|
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,4 +1,4 @@ | ||
#include "../alloc.h" | ||
#include "alloc.h" // modified by Poolside fork | ||
#include "parser.h" | ||
|
||
#include <assert.h> | ||
|
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,55 @@ | ||
|
||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.082258 // | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
|
||
#ifndef TREE_SITTER_ALLOC_H_ | ||
#define TREE_SITTER_ALLOC_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#if defined(TREE_SITTER_HIDDEN_SYMBOLS) || defined(_WIN32) | ||
#define TS_PUBLIC | ||
#else | ||
#define TS_PUBLIC __attribute__((visibility("default"))) | ||
#endif | ||
|
||
TS_PUBLIC extern void *(*ts_current_malloc)(size_t); | ||
TS_PUBLIC extern void *(*ts_current_calloc)(size_t, size_t); | ||
TS_PUBLIC extern void *(*ts_current_realloc)(void *, size_t); | ||
TS_PUBLIC extern void (*ts_current_free)(void *); | ||
|
||
// Allow clients to override allocation functions | ||
#ifndef ts_malloc | ||
#define ts_malloc ts_current_malloc | ||
#endif | ||
#ifndef ts_calloc | ||
#define ts_calloc ts_current_calloc | ||
#endif | ||
#ifndef ts_realloc | ||
#define ts_realloc ts_current_realloc | ||
#endif | ||
#ifndef ts_free | ||
#define ts_free ts_current_free | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // TREE_SITTER_ALLOC_H_ | ||
|
||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.082258 // | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
|
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
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
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
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,146 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import datetime | ||
import os | ||
import shutil | ||
|
||
lang_dirs = [ | ||
'ada', | ||
'c', | ||
'lua', | ||
'csharp', | ||
'cpp', | ||
'elixir', | ||
'elm', | ||
'golang', | ||
'java', | ||
'javascript', | ||
'ocaml', | ||
'objc', | ||
'php', | ||
'python', | ||
'ruby', | ||
'rust', | ||
#'typescript', Oh you are so special. You make me feel dumb | ||
'kotlin', | ||
'scala', | ||
'zig' | ||
] | ||
|
||
# flatten a directory | ||
def flatten_dir(root): | ||
print(f'Flattening {root}') | ||
for dir, _, filenames in os.walk(root): | ||
if dir == root: | ||
continue | ||
for filename in filenames: | ||
src = os.path.join(dir, filename) | ||
dst = os.path.join(root, filename) | ||
force_move_file(src, dst) | ||
|
||
# list files in directory (not including its subdirs) that ends with any of the given extensions | ||
def ls_exts(directory, extensions): | ||
files = os.listdir(directory) | ||
return [os.path.join(directory, file) for file in files | ||
if os.path.isfile(os.path.join(directory, file)) and endswith_any(file, extensions)] | ||
|
||
|
||
# fix_headers assumes some pre-processing is already done. Then it changes: | ||
# #include "../foo/bar.h" | ||
# #include "./bar.h" | ||
# #include "foo/bar.h" | ||
# all into #include "bar.h" | ||
def fix_headers(file_path): | ||
print(f'Fixing headers in {os.path.abspath(file_path)}') | ||
file_dir = os.path.dirname(os.path.abspath(file_path)) | ||
with open(file_path, 'r') as file: | ||
content = file.read() | ||
new_content = [] | ||
needs_write = False | ||
for line in content.split('\n'): | ||
if not line.startswith('#include') or line.count('"') == 0: | ||
new_content.append(line) | ||
continue | ||
header = line.split('"')[1] | ||
idx = header.find('/') | ||
if idx == -1: | ||
new_content.append(line) | ||
continue | ||
needs_write = True | ||
if header.startswith('../'): | ||
copy_recur(os.path.join(file_dir, header), file_dir) | ||
line = line.replace('../', '') | ||
elif header.startswith('./'): | ||
line = line.replace('./', '') | ||
else: | ||
# this is a big assumption: That is the header it tries to include here is in a subdir we already flattened | ||
base = os.path.basename(header) | ||
line = f'#include "{base}"' | ||
line += ' // modified by Poolside fork' | ||
new_content.append(line) | ||
if needs_write: | ||
with open(file_path, 'w') as file: | ||
file.write('\n'.join(new_content)) | ||
|
||
def add_extra_line(file_path, extra_line): | ||
with open(file_path, 'r') as file: | ||
content = file.read() | ||
with open(file_path, 'w') as file: | ||
file.write(extra_line + content + extra_line) | ||
|
||
# generate a comment header/footer to make it clear a file is modified by us, not directly from upstream | ||
def make_comments(): | ||
comment_line = '////////////////////////////////////////////////////////////////////////////////\n' | ||
return f'\n\n{comment_line}// Poolside modification in the fork, generated at {datetime.datetime.now()} //\n{comment_line}\n\n' | ||
|
||
|
||
# move file from src to dst if src exists, overwrite dst if it exists. | ||
def force_move_file(src, dst): | ||
# if src no longer exists, we proabably done moving it already. Abort: | ||
if not os.path.exists(src): | ||
print(f'File {src} no longer exists, not going to move anything.') | ||
return | ||
# if dst exists, we want to overwrite it | ||
if os.path.exists(dst): | ||
print(f'Warning: removing existing file {dst}') | ||
os.remove(dst) | ||
os.rename(src, dst) | ||
add_extra_line(dst, make_comments()) | ||
print(f'File {src} has been moved to {dst}.') | ||
|
||
|
||
def endswith_any(filename, extenstions): | ||
return any(filename.endswith(extension) for extension in extenstions) | ||
|
||
|
||
def op_recur(src, dst_dir, operation): | ||
headers = [] | ||
with open(src, 'r') as f: | ||
for lines in f: | ||
if lines.startswith('#include') and lines.count('"') > 0: | ||
headers.append(lines.split('"')[1]) | ||
for h in headers: | ||
op_recur(h, dst_dir, operation) | ||
operation(src, os.path.join(dst_dir, os.path.basename(src))) | ||
|
||
|
||
def copy_with_extra_commends(src, dst): | ||
shutil.copy2(src, dst) | ||
add_extra_line(dst, make_comments()) | ||
|
||
def copy_recur(src, dst_dir): | ||
op_recur(src, dst_dir, copy_with_extra_commends) | ||
print(f'File {src} has been copied to {dst_dir}.') | ||
|
||
|
||
# (1) Fix up go-tree-sitter root level headers so they do not have #include "./foo.h" | ||
# (2) Flatten a list of language subdirs that we care about. No more go-tree-sitter/{lang}/foo/bar.h | ||
# (3) Then in these flatten subdirs, fix up any header inclusion that isn't directly a sibling file. | ||
if __name__ == '__main__': | ||
root_dir_headers = ls_exts(os.getcwd(), ['.h']) | ||
for h in root_dir_headers: | ||
fix_headers(h) | ||
for dir in lang_dirs: | ||
flatten_dir(dir) | ||
for f in ls_exts(dir, ['.h', '.c']): | ||
fix_headers(f) |
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,55 @@ | ||
|
||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.663188 // | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
|
||
#ifndef TREE_SITTER_ALLOC_H_ | ||
#define TREE_SITTER_ALLOC_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#if defined(TREE_SITTER_HIDDEN_SYMBOLS) || defined(_WIN32) | ||
#define TS_PUBLIC | ||
#else | ||
#define TS_PUBLIC __attribute__((visibility("default"))) | ||
#endif | ||
|
||
TS_PUBLIC extern void *(*ts_current_malloc)(size_t); | ||
TS_PUBLIC extern void *(*ts_current_calloc)(size_t, size_t); | ||
TS_PUBLIC extern void *(*ts_current_realloc)(void *, size_t); | ||
TS_PUBLIC extern void (*ts_current_free)(void *); | ||
|
||
// Allow clients to override allocation functions | ||
#ifndef ts_malloc | ||
#define ts_malloc ts_current_malloc | ||
#endif | ||
#ifndef ts_calloc | ||
#define ts_calloc ts_current_calloc | ||
#endif | ||
#ifndef ts_realloc | ||
#define ts_realloc ts_current_realloc | ||
#endif | ||
#ifndef ts_free | ||
#define ts_free ts_current_free | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // TREE_SITTER_ALLOC_H_ | ||
|
||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.663188 // | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
|
Oops, something went wrong.