API - grap.core#
grap.core#
grap.core.action#
- class grap.core.action.Action(value)#
An action influences the behaviour of the parser.
There is generally no need to use them. The predefined rules in
grap.core.commoncover most of them.- GO_BACK = 1#
Moves the pointer to the previous position.
- IS_MATCH = 3#
Acts like a rule that always matches anything.
- NO_MATCH = 2#
Acts like a rule that never matches anything.
- OPTIONAL = 4#
Suppresses
errors.ParseErrors for the current rule and each subrule.
- REQUIRE = 5#
The parser will raise a
ParseErrors when following rules are not consumed. This has no effect whenOPTIONALwas yielded by a parent rule.
grap.core.common#
- class grap.core.common.OnceOrMore(rule, name=_Nothing.NOTHING)#
Match a rule one or more times.
Method generated by attrs for class OnceOrMore.
- Parameters:
rule (Rule) –
name (str) –
- for ... in grammar()#
- Return type:
Grammar
- name: str#
The name of the rule.
- class grap.core.common.Optional(rule, name=_Nothing.NOTHING)#
Optionally match a rule.
Method generated by attrs for class Optional.
- Parameters:
rule (Rule) –
name (str) –
- for ... in grammar()#
- Return type:
Grammar
- name: str#
The name of the rule.
- class grap.core.common.RuleUnion(rules, name=_Nothing.NOTHING)#
Match one of multiple rules.
Method generated by attrs for class RuleUnion.
- Parameters:
rules (Sequence[Rule]) –
name (str) –
- for ... in grammar()#
- Return type:
Grammar
- name: str#
The name of the rule.
grap.core.errors#
grap.core.parser#
- class grap.core.parser.ParsedRule(*, name, rule, match, span, parent=None, inner=_Nothing.NOTHING)#
- Parameters:
name (str) –
rule (Rule) –
match (str) –
span (tuple[int, int]) –
parent (Optional[Rule]) –
inner (list[grap.core.rules.Rule]) –
- name#
The name of the rule.
- Type:
str
- rule#
The parsed rule object.
- Type:
- match#
The consumed characters.
- Type:
str
- span#
The index of the consumed characters in the parsed text.
- Type:
tuple[int, int]
- parent#
The parent rule. This is None when the rule is the root.
- Type:
- inner#
All parsed subrules.
- Type:
list[grap.core.rules.Rule]
Method generated by attrs for class ParsedRule.
- inner: list[grap.core.rules.Rule]#
- match: str#
- name: str#
- span: tuple[int, int]#
grap.core.rules#
- class grap.core.rules.Rule(name=None)#
- Parameters:
name (Optional[str]) – The name of the rule. Defaults to the class’s name.
- grap.core.rules.rule(fn: Callable[[], Generator[Union[Rule, Action, str], str, None]], /, *, name: None = None, doc: None = None) type[grap.core.rules.Rule]#
- grap.core.rules.rule(fn: None = None, /, *, name: Optional[str] = None, doc: Optional[str] = None) Callable[[Callable[[], Generator[Union[Rule, Action, str], str, None]]], type[grap.core.rules.Rule]]
Decorator to quickly define a rule.
The function that is decorated should not take any arguments. The function is converted into a staic method of the newly created rule. The docstring of the decorated function is assigned to the rule class.
- Parameters:
fn – The function to decorate.
name – The name of the rule. Defaults to the function’s name.
doc – The docstring of the rule. Defaults to function’s docstring.
Examples
from grap.core import Grammar, rule @rule def pet() -> Grammar: yield "p" yield "e" yield "t"
from grap.core import Grammar, rule @rule(name = "dog") def pet() -> Grammar: yield "d" yield "o" yield "g"