MCPcopy Create free account
hub / github.com/Notgnoshi/generative / RuleParser

Class RuleParser

generative/lsystem/production.py:19–153  ·  view source on GitHub ↗

Parse plaintext rules from e.g. unit tests, commandline args, JSON, to a Rule object. Rule Format "Rules" have one of two formats. 1. A list of tokens to ignore when considering context. 2. A production rule. The ignore tokens have the format #igno

Source from the content-addressed store, hash-verified

17
18
19class RuleParser:
20 """Parse plaintext rules from e.g. unit tests, commandline args, JSON, to a Rule object.
21
22 Rule Format
23 "Rules" have one of two formats.
24
25 1. A list of tokens to ignore when considering context.
26 2. A production rule.
27
28 The ignore tokens have the format
29
30 #ignore: tok1,tok2,tok3
31
32 The production rules have the format
33
34 [left_context <] lhs [> right_context] [: probability] -> rhs[,rhs[...]]
35 """
36
37 def __init__(self, long_tokens=False):
38 """Create a rule parser.
39
40 Parsing rule after rule will construct the RuleParser.rule and RuleParser.ignore members.
41
42 :param long_tokens: Whether to support multiple character tokens. Requires the tokens be
43 comma or whitespace (or both) separated. Defaults to False.
44 """
45 self.rules: MultiDict[TokenName, RuleMapping] = MultiDict()
46 self.ignore: Set[TokenName] = set()
47 self.long_tokens = long_tokens
48
49 @staticmethod
50 def __get_grammars(long_tokens: bool):
51 """Get the grammars for parsing rules and ignore lists.
52
53 :param long_tokens: Whether the tokens must be delimited to support long tokens.
54 """
55 ParserElement.enablePackrat()
56
57 COLON = Literal(":")
58 LESS_THAN = Literal("<")
59 GREATER_THAN = Literal(">")
60 ARROW = Literal("->")
61
62 PITCH_UP = Literal("^")
63 PITCH_DOWN = Literal("v")
64 ROLL_CCW = Literal("<")
65 ROLL_CW = Literal(">")
66 YAW_LEFT = Literal("-")
67 YAW_RIGHT = Literal("+")
68 PUSH_STACK = Literal("[")
69 POP_STACK = Literal("]")
70 FLIP_DIRECTION = Literal("|")
71
72 token = (
73 (Word(alphanums) if long_tokens else Word(alphanums, min=1, exact=1))
74 | PITCH_UP
75 | PITCH_DOWN
76 | ROLL_CCW

Callers 15

parse_rulesFunction · 0.90
test_simpleMethod · 0.90
test_simple_delimitedMethod · 0.90
test_probabilityMethod · 0.90
test_left_contextMethod · 0.90
test_right_contextMethod · 0.90
test_both_contextMethod · 0.90
test_context_rollMethod · 0.90
test_ignoreMethod · 0.90
test_ignore_delimitedMethod · 0.90
test_fractal_plantMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_simpleMethod · 0.72
test_simple_delimitedMethod · 0.72
test_probabilityMethod · 0.72
test_left_contextMethod · 0.72
test_right_contextMethod · 0.72
test_both_contextMethod · 0.72
test_context_rollMethod · 0.72
test_ignoreMethod · 0.72
test_ignore_delimitedMethod · 0.72
test_fractal_plantMethod · 0.72