Fixed tree-sitter FutureWarning and refactored parsing logic in linter.

This commit is contained in:
Paul Gauthier 2024-05-17 14:00:41 -07:00
parent 3cc0a3c263
commit c26833419d

View file

@ -1,22 +1,24 @@
import os
import tree_sitter
import sys
import warnings
# tree_sitter is throwing a FutureWarning
warnings.simplefilter("ignore", category=FutureWarning)
from tree_sitter_languages import get_language, get_parser # noqa: E402
def parse_file_for_errors(file_path):
"""
Parses the given file using tree-sitter and prints out the line number of every syntax/parse error.
"""
# Load the language
Language = tree_sitter.Language
PARSER = tree_sitter.Parser()
LANGUAGE = Language(os.path.join('build', 'my-languages.so'), 'python')
PARSER.set_language(LANGUAGE)
lang = "python"
language = get_language(lang)
parser = get_parser(lang)
# Read the file content
with open(file_path, 'r') as file:
content = file.read()
# Parse the content
tree = PARSER.parse(bytes(content, "utf8"))
tree = parser.parse(bytes(content, "utf8"))
# Traverse the tree to find errors
def traverse_tree(node):
@ -26,3 +28,18 @@ def parse_file_for_errors(file_path):
traverse_tree(child)
traverse_tree(tree.root_node)
def main():
"""
Main function to parse files provided as command line arguments.
"""
if len(sys.argv) < 2:
print("Usage: python linter.py <file1> <file2> ...")
sys.exit(1)
for file_path in sys.argv[1:]:
print(f"Checking file: {file_path}")
parse_file_for_errors(file_path)
if __name__ == "__main__":
main()