def create_ast(self, file_path): parser = Parser() ast = parser.parse_file(file_path) id_classifier = IdentifierClassifier() attached_ast = id_classifier.attach_identifier_attributes(ast) return attached_ast
def assertProcessing(self, file_path, expected_scope_tree): parser = Parser() ast = parser.parse_file(file_path) plugin = ScopePlugin() plugin.process(ast) self.assertScopeTree(ast['vint_scope_tree'], expected_scope_tree)
def test_process_with_builtin(self): parser = Parser() ast = parser.parse_file(Fixtures['BUILTIN']) plugin = ScopePlugin() plugin.process(ast) expected_builtin_flags = { 'abs': True, 'sin': True, 'strlen': True, 'g:MyFunction': False, } # Keep identifier name that traverser visited identifiers_checking_map = { 'abs': False, 'sin': False, 'strlen': False, 'g:MyFunction': False, } def test_identifier(node): if NodeType(node['type']) is not NodeType.IDENTIFIER: return identifier = node # We focus to non-definition identifier if identifier[ScopePlugin.DEFINITION_IDENTIFIER_FLAG_KEY]: return identifier_name = identifier['value'] identifiers_checking_map[identifier_name] = True is_builtin_identifier = identifier[ ScopePlugin.BUILTIN_IDENTIFIER_FLAG_KEY] expected_builtin_flag = expected_builtin_flags[identifier_name] self.assertEqual(is_builtin_identifier, expected_builtin_flag) traverse(ast, on_enter=test_identifier) self.assertTrue(all(identifiers_checking_map.values()))
def test_process_with_builtin(self): parser = Parser() ast = parser.parse_file(Fixtures['BUILTIN']) plugin = ScopePlugin() plugin.process(ast) expected_builtin_flags = { 'abs': True, 'sin': True, 'strlen': True, 'g:MyFunction': False, } # Keep identifier name that traverser visited identifiers_checking_map = { 'abs': False, 'sin': False, 'strlen': False, 'g:MyFunction': False, } def test_identifier(node): if NodeType(node['type']) is not NodeType.IDENTIFIER: return identifier = node # We focus to non-definition identifier if identifier[ScopePlugin.DEFINITION_IDENTIFIER_FLAG_KEY]: return identifier_name = identifier['value'] identifiers_checking_map[identifier_name] = True is_builtin_identifier = identifier[ScopePlugin.BUILTIN_IDENTIFIER_FLAG_KEY] expected_builtin_flag = expected_builtin_flags[identifier_name] self.assertEqual(is_builtin_identifier, expected_builtin_flag) traverse(ast, on_enter=test_identifier) self.assertTrue(all(identifiers_checking_map.values()))
def setUp(self): parser = Parser() self.ast = parser.parse_file(FIXTURE_FILE)
def test_parse_file(self): parser = Parser() ast = parser.parse_file(FIXTURE_FILE) self.assertIs(ast['type'], 1)
def prettify_node_type(node): node['type'] = NodeType(node['type']) if __name__ == '__main__': arg_parser = ArgumentParser(prog='show_ast', description='Show AST') arg_parser.add_argument('--enable-neovim', action='store_true', help='Enable Neovim syntax') arg_parser.add_argument('files', nargs='*', help='File to parse') namespace = vars(arg_parser.parse_args(sys.argv[1:])) filepaths = map(Path, namespace['files']) enable_neovim = namespace['enable_neovim'] scope_plugin = ScopePlugin() parser = Parser(plugins={'scope': scope_plugin}, enable_neovim=enable_neovim) for filepath in filepaths: ast = parser.parse_file(filepath) traverse(ast, on_enter=prettify_node_type) print("////////// AST //////////\n") pprint(ast) print("\n\n") print("////////// SCOPE TREE //////////\n") pprint(scope_plugin._ref_tester._scope_tree)
from vint.ast.plugin.scope_plugin import ScopePlugin def prettify_node_type(node): node['type'] = NodeType(node['type']) if __name__ == '__main__': arg_parser = ArgumentParser(prog='show_ast', description='Show AST') arg_parser.add_argument('--enable-neovim', action='store_true', help='Enable Neovim syntax') arg_parser.add_argument('files', nargs='*', help='File to parse') namespace = vars(arg_parser.parse_args(sys.argv[1:])) filepaths = map(Path, namespace['files']) enable_neovim = namespace['enable_neovim'] scope_plugin = ScopePlugin() parser = Parser(plugins=[scope_plugin], enable_neovim=enable_neovim) for filepath in filepaths: ast = parser.parse_file(filepath) traverse(ast, on_enter=prettify_node_type) print("////////// SCOPE TREE //////////\n") pprint(scope_plugin._ref_tester._scope_linker.scope_tree) print("\n\n") print("////////// LINK REGISTRY //////////\n") pprint(scope_plugin._ref_tester._scope_linker.link_registry._vars_to_declarative_ids_map) pprint(scope_plugin._ref_tester._scope_linker.link_registry._ids_to_scopes_map)
def test_parse_file_when_neovim_enabled(self): parser = Parser(enable_neovim=True) ast = parser.parse_file(FIXTURE_FILE_NEOVIM) self.assertIs(ast['type'], 1)
#! /usr/bin/env python import sys from pprint import pprint from pathlib import Path vint_root = Path(__file__).parent.parent sys.path.append(str(vint_root)) from vint.ast.node_type import NodeType from vint.ast.parsing import Parser from vint.ast.traversing import traverse def prettify_node_type(node): node['type'] = NodeType(node['type']) if __name__ == '__main__': parser = Parser() ast = parser.parse_file(Path(sys.argv[1])) traverse(ast, on_enter=prettify_node_type) pprint(ast)
def test_parse_file_on_ff_dos_and_fenc_cp932(self): parser = Parser() ast = parser.parse_file(FIXTURE_FILE_FF_DOS_FENC_CP932) self.assertIs(ast['type'], 1)
def create_ast(self, file_path): parser = Parser() ast = parser.parse_file(file_path.value) return ast