tree-sitter/treesitter_grammar.req
Björn Bidar b23614c9a8 - update to 0.24.2:
* Drop legacy binding updates
  * templates: Properly replace author email
  * templates: Update npm packages
  * bindings: Improve cmake file
  * cmake: Support amalgamated build
  * cmake: Correct library scopes
  * make: Fix tree-sitter.pc generation

- update to 0.24.1:
  * fix(generate): move generated header files into the generate crate
- update do 0.24.0:
  * docs: add Kotlin to the playground
  * fix(generate): remove necessary files from gitignore template
  * feat(generate): bump tree-sitter dev dependency to 0.23
  * fix(cli): remove conflicting short flags in the fuzz subcommand
  * feat(bindings): bump go-tree-sitter version
  * docs(changelog): add 0.23.0 release notes
  * feat: add an API to time out query executions
  * fix(generate): disallow inline variables referencing themselves
  * fix(rust): add missing TSNode functions
  * fix(lib): correct extra node creation from non-zero root-alias cursors
  * fix(test): exit with an error if a test marked with :error has no error
  * fix(test): retain attributes when running test -u
  * feat(language): derive Clone and Copy on LanguageFn
  * fix(lib): backtrack to the last relevant iterator if no child was found
  * fix(generate): add tree-sitter to the dev-dependencies of the Cargo.toml
  * fix(binding_web): correct edit signature
  * build(lib): build using cmake
  * fix(cli): keep skipped tests unchanged in the test/corpus

OBS-URL: https://build.opensuse.org/package/show/editors/tree-sitter?expand=0&rev=31
2024-10-08 10:10:46 +00:00

99 lines
3.8 KiB
Python

#!/usr/bin/python3
"""Scan grammar JavaScript sources for their dependencies"""
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2024 Björn Bidar
# pylint: disable=invalid-name
import platform
import fileinput
import re
from typing import Optional
from pathlib import Path
if platform.sys.version_info.minor < 9:
def remove_prefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text
def remove_suffix(text, suffix):
if text.endswith(suffix):
return text[:-len(suffix):]
return text
treeSitterGrammarSrcPath = "/usr/include/tree_sitter"
treeSitterGrammarSymbolToken = "treesitter_grammar_src"
grammarPaths = []
def resolveFile(grammarFile: str) -> str:
"""Resolve GRAMMARFILE from grammarPaths return found file"""
fullGrammarFilePath = Path(grammarFile)
currentGrammarPath = fullGrammarFilePath.parent
currentGrammarFile = Path(grammarFile)
if currentGrammarPath != Path('.') and currentGrammarPath.parts[0] == "..":
# resolve path relative to file found last
currentGrammarPath = grammarPaths[-1] / fullGrammarFilePath
if not currentGrammarPath.exists():
return False
return currentGrammarPath
if currentGrammarPath.is_absolute():
grammarPaths.append(currentGrammarPath)
if Path(grammarFile).exists():
return fullGrammarFilePath
for path in grammarPaths:
maybeFound = path / currentGrammarFile
if maybeFound.exists():
return maybeFound.exists()
return False
def dummyRequire(requiredFile_fd: str, maxDepth: Optional[int] = 5 ) -> str:
"""Dummy version of node's require function that spits out dependency symbols"""
if maxDepth <= 0:
return
if not requiredFile_fd.endswith(".js"):
# Append .js to required file name in case is not specified
# we have to remove .js suffix later in any case.
requiredFile_fd+=".js"
resolvedFile_fd = resolveFile(requiredFile_fd)
if resolvedFile_fd:
try:
with open(resolvedFile_fd, mode='r', encoding='utf8') as requiredFile:
for r_line in requiredFile:
require_re = r'.*require\((.*)\).*'
requiredLvl2 = re.match(require_re, r_line)
#print(r_line)
if requiredLvl2 is not None:
if platform.sys.version_info.minor < 9:
requiredLvl2_grp_cleaned = \
remove_suffix(remove_prefix(requiredLvl2.group(1), "'"), "'")
requiredLvl2_grp_cleaned = \
remove_suffix(remove_prefix(requiredLvl2_grp_cleaned, "\""),
"\"")
else:
requiredLvl2_grp_cleaned = \
requiredLvl2.group(1).removeprefix("'").removesuffix("'")
requiredLvl2_grp_cleaned = \
requiredLvl2_grp_cleaned.removeprefix("\"").removesuffix("\"")
if not requiredLvl2_grp_cleaned.split('/')[0] == "..":
# Don't emit dependencies which are local
pass
dummyRequire(requiredLvl2_grp_cleaned, maxDepth - 1)
except FileNotFoundError:
pass
else:
if maxDepth == 5:
# In case we immediately fail to open the first grammar source file
return
# We only want to resolve dependencies outside of the package
print(f"{treeSitterGrammarSymbolToken}({Path(requiredFile_fd).parent})")
for line in fileinput.input():
line = line.rstrip('\n')
if line.endswith('.js'):
dummyRequire(line)