Day 19: Monster Messages¶
Classes:
|
Represent a rule that valid messages should obey. |
|
Represent an union composition of rules (at least one rule matches). |
|
Represent a chain of rules where all rules need to match in sequence. |
|
Represent a rule where a literal is exactly matched. |
|
Represent a node in the abstract syntax tree. |
|
Represent a literal in the rule syntax. |
|
Represent a reference to a rule. |
|
Represent a parsed sequence of node references. |
|
Represent a parsed union of node sequences. |
Functions:
|
Parse the rule from the |
|
Parse the rules from |
|
Yield the |
|
Represent the abstract syntax tree |
|
Interpret the rule trees and construct the rule 0. |
|
Count the |
- class Rule[source]¶
Represent a rule that valid messages should obey.
Methods:
match
(text)Match the
text
and return the remaining unmatched text.
- class RuleOr(rules: List[Rule])[source]¶
Represent an union composition of rules (at least one rule matches).
Methods:
__init__
(rules)Initialize with the given values.
match
(text)Check whether at least one rule from
rules
matches.Attributes:
Union of rules
- class RuleSequence(rules: List[Rule])[source]¶
Represent a chain of rules where all rules need to match in sequence.
Methods:
__init__
(rules)Initialize with the given values.
match
(text)Check whether
text
matches the whole sequence ofrules
.Attributes:
Rule chain
- class RuleLiteral(literal: str)[source]¶
Represent a rule where a literal is exactly matched.
Methods:
__init__
(literal)Initialize with the given values.
match
(text)Check whether
text
matches exactly theliteral
.Attributes:
Literal to be matched
- literal: Final[str]¶
Literal to be matched
- class NodeLiteral(literal: str)[source]¶
Represent a literal in the rule syntax.
Methods:
__init__
(literal)Initialize with the given values.
Attributes:
Parsed literal
- literal: Final[str]¶
Parsed literal
- class NodeReference(identifier: int)[source]¶
Represent a reference to a rule.
Methods:
__init__
(identifier)Initialize with the given values.
Attributes:
Identifier of the referenced rule
- identifier: Final[int]¶
Identifier of the referenced rule
- class NodeSequence(references: List[NodeReference])[source]¶
Represent a parsed sequence of node references.
Methods:
__init__
(references)Initialize with the given values.
Attributes:
Parsed references
- __init__(references: List[NodeReference]) None [source]¶
Initialize with the given values.
- references: Final[List[NodeReference]]¶
Parsed references
- class NodeOr(sequences: List[NodeSequence])[source]¶
Represent a parsed union of node sequences.
Methods:
__init__
(sequences)Initialize with the given values.
Attributes:
Union
- __init__(sequences: List[NodeSequence]) None [source]¶
Initialize with the given values.
- sequences: Final[List[NodeSequence]]¶
Union
- parse_rule(line: str) Tuple[int, Node] [source]¶
Parse the rule from the
line
into (rule identifier, abstract syntax tree).- Requires
RULE_RE.match(line)
- parse_rules(lines: Lines) MutableMapping[int, Node] [source]¶
Parse the rules from
lines
into a dictionary of identifier 🠒 AST.- Requires
all(RULE_RE.match(line) for line in lines)
- repr_rule_tree(rule_tree: Node) str [source]¶
Represent the abstract syntax tree
rule_tree
as a string for inspection.
- interpret_rule_0(rule_trees: Mapping[int, Node]) Rule [source]¶
Interpret the rule trees and construct the rule 0.
- Requires
all( all( node.identifier in rule_trees for node in iterate(rule_tree) if isinstance(node, NodeReference) ) for rule_tree in rule_trees.values() )
(No dangling references)
0 in rule_trees
(The initial rule is present.)