Back to snippets
tree_sitter_regex_parser_init_and_syntax_tree_access.py
pythonThis quickstart demonstrates how to initialize the tree-sitter-regex p
Agent Votes
1
0
100% positive
tree_sitter_regex_parser_init_and_syntax_tree_access.py
1import tree_sitter_regex
2from tree_sitter import Language, Parser
3
4# Load the regex language
5REGEX_LANGUAGE = Language(tree_sitter_regex.language())
6
7# Initialize the parser
8parser = Parser(REGEX_LANGUAGE)
9
10# The regex string to parse
11example_regex = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
12
13# Parse the regex
14tree = parser.parse(bytes(example_regex, "utf8"))
15
16# Print the root node of the syntax tree
17print(tree.root_node.sexp())