Convert raw node information to a Node or Leaf instance. This is passed to the parser driver which calls it whenever a reduction of a grammar rule produces a new complete node, so that the tree is build strictly bottom-up.
(gr: Grammar, raw_node: RawNode)
| 468 | |
| 469 | |
| 470 | def convert(gr: Grammar, raw_node: RawNode) -> NL: |
| 471 | """ |
| 472 | Convert raw node information to a Node or Leaf instance. |
| 473 | |
| 474 | This is passed to the parser driver which calls it whenever a reduction of a |
| 475 | grammar rule produces a new complete node, so that the tree is build |
| 476 | strictly bottom-up. |
| 477 | """ |
| 478 | type, value, context, children = raw_node |
| 479 | if children or type in gr.number2symbol: |
| 480 | # If there's exactly one child, return that child instead of |
| 481 | # creating a new node. |
| 482 | assert children is not None |
| 483 | if len(children) == 1: |
| 484 | return children[0] |
| 485 | return Node(type, children, context=context) |
| 486 | else: |
| 487 | return Leaf(type, value or "", context=context) |
| 488 | |
| 489 | |
| 490 | _Results = dict[str, NL] |