Представьте себе маленькую грамматику PEG
, такую как
from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor
grammar = Grammar(
r"""
term = lpar (number comma? ws?)+ rpar
number = ~"\d+"
lpar = "("
rpar = ")"
comma = ","
ws = ~"\s*"
"""
)
tree = grammar.parse("(5, 4, 3)")
print(tree)
Какие выходные данные
<Node called "term" matching "(5, 4, 3)">
<Node called "lpar" matching "(">
<Node matching "5, 4, 3">
<Node matching "5, ">
<RegexNode called "number" matching "5">
<Node matching ",">
<Node called "comma" matching ",">
<Node matching " ">
<RegexNode called "ws" matching " ">
<Node matching "4, ">
<RegexNode called "number" matching "4">
<Node matching ",">
<Node called "comma" matching ",">
<Node matching " ">
<RegexNode called "ws" matching " ">
<Node matching "3">
<RegexNode called "number" matching "3">
<Node matching "">
<Node matching "">
<RegexNode called "ws" matching "">
<Node called "rpar" matching ")">
Как получить number
регулярное выражение из term
в этом примере?Я знаю, что могу использовать класс NodeVisitor и исследовать каждое число, но я хотел бы получить часть регулярного выражения из term
.