feat: add ast_checker mappings for Python3 and C
This commit is contained in:
0
ast_checker/__init__.py
Normal file
0
ast_checker/__init__.py
Normal file
37
ast_checker/mappings/__init__.py
Normal file
37
ast_checker/mappings/__init__.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from tree_sitter import Language
|
||||
|
||||
from .c import C_MAPPING
|
||||
from .python import PYTHON_MAPPING
|
||||
|
||||
_MAPPINGS = {
|
||||
"Python3": PYTHON_MAPPING,
|
||||
"C": C_MAPPING,
|
||||
}
|
||||
|
||||
_LANGUAGES: dict[str, Language] = {}
|
||||
|
||||
|
||||
def _init_languages():
|
||||
try:
|
||||
import tree_sitter_python as tspython
|
||||
|
||||
_LANGUAGES["Python3"] = Language(tspython.language())
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
import tree_sitter_c as tsc
|
||||
|
||||
_LANGUAGES["C"] = Language(tsc.language())
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
_init_languages()
|
||||
|
||||
|
||||
def get_mapping(language: str) -> dict:
|
||||
return _MAPPINGS.get(language, {})
|
||||
|
||||
|
||||
def get_language(language: str) -> Language | None:
|
||||
return _LANGUAGES.get(language)
|
||||
39
ast_checker/mappings/c.py
Normal file
39
ast_checker/mappings/c.py
Normal file
@@ -0,0 +1,39 @@
|
||||
C_MAPPING = {
|
||||
"for_loop": "for_statement",
|
||||
"while_loop": "while_statement",
|
||||
"do_while": "do_statement",
|
||||
"if_statement": "if_statement",
|
||||
"else_clause": "else_clause",
|
||||
"break": "break_statement",
|
||||
"continue": "continue_statement",
|
||||
"function_definition": "function_definition",
|
||||
"return": "return_statement",
|
||||
"switch_statement": "switch_statement",
|
||||
"case_statement": "case_statement",
|
||||
"assignment": "assignment_expression",
|
||||
"struct": "struct_specifier",
|
||||
"include": "preproc_include",
|
||||
"+": "+",
|
||||
"-": "-",
|
||||
"*": "*",
|
||||
"/": "/",
|
||||
"%": "%",
|
||||
"+=": "+=",
|
||||
"-=": "-=",
|
||||
"*=": "*=",
|
||||
"/=": "/=",
|
||||
"%=": "%=",
|
||||
"==": "==",
|
||||
"!=": "!=",
|
||||
">": ">",
|
||||
">=": ">=",
|
||||
"<": "<",
|
||||
"<=": "<=",
|
||||
"and": "&&",
|
||||
"or": "||",
|
||||
"not": "!",
|
||||
"&": "&",
|
||||
"|": "|",
|
||||
"++": "++",
|
||||
"--": "--",
|
||||
}
|
||||
45
ast_checker/mappings/python.py
Normal file
45
ast_checker/mappings/python.py
Normal file
@@ -0,0 +1,45 @@
|
||||
PYTHON_MAPPING = {
|
||||
"for_loop": "for_statement",
|
||||
"while_loop": "while_statement",
|
||||
"if_statement": "if_statement",
|
||||
"else_clause": "else_clause",
|
||||
"elif_clause": "elif_clause",
|
||||
"break": "break_statement",
|
||||
"continue": "continue_statement",
|
||||
"function_definition": "function_definition",
|
||||
"return": "return_statement",
|
||||
"try_except": "try_statement",
|
||||
"with_statement": "with_statement",
|
||||
"list_comprehension": "list_comprehension",
|
||||
"list_literal": "list",
|
||||
"dict_literal": "dictionary",
|
||||
"set_literal": "set",
|
||||
"f_string": "format_string",
|
||||
"import": "import_statement",
|
||||
"import_from": "import_from_statement",
|
||||
"assignment": "assignment",
|
||||
"class_definition": "class_definition",
|
||||
"+": "+",
|
||||
"-": "-",
|
||||
"*": "*",
|
||||
"/": "/",
|
||||
"//": "//",
|
||||
"%": "%",
|
||||
"**": "**",
|
||||
"+=": "+=",
|
||||
"-=": "-=",
|
||||
"*=": "*=",
|
||||
"/=": "/=",
|
||||
"%=": "%=",
|
||||
"==": "==",
|
||||
"!=": "!=",
|
||||
">": ">",
|
||||
">=": ">=",
|
||||
"<": "<",
|
||||
"<=": "<=",
|
||||
"and": "and",
|
||||
"or": "or",
|
||||
"not": "not",
|
||||
"&": "&",
|
||||
"|": "|",
|
||||
}
|
||||
Reference in New Issue
Block a user