![]() |
Qucs-S S-parameter Viewer & RF Synthesis Tools
|


Public Member Functions | |
| __init__ (self, rules='', **more_rules) | |
| default (self, rule_name) | |
| parse (self, text, pos=0) | |
| match (self, text, pos=0) | |
| __str__ (self) | |
| __repr__ (self) | |
Public Attributes | |
| default_rule | |
Protected Member Functions | |
| _copy (self) | |
| _expressions_from_rules (self, rules, custom_rules) | |
| _check_default_rule (self) | |
A collection of rules that describe a language
You can start parsing from the default rule by calling ``parse()``
directly on the ``Grammar`` object::
g = Grammar('''
polite_greeting = greeting ", my good " title
greeting = "Hi" / "Hello"
title = "madam" / "sir"
''')
g.parse('Hello, my good sir')
Or start parsing from any of the other rules; you can pull them out of the
grammar as if it were a dictionary::
g['title'].parse('sir')
You could also just construct a bunch of ``Expression`` objects yourself
and stitch them together into a language, but using a ``Grammar`` has some
important advantages:
* Languages are much easier to define in the nice syntax it provides.
* Circular references aren't a pain.
* It does all kinds of whizzy space- and time-saving optimizations, like
factoring up repeated subexpressions into a single object, which should
increase cache hit ratio. [Is this implemented yet?] | parsimonious.grammar.Grammar.__init__ | ( | self, | |
rules = '', |
|||
| ** | more_rules | ||
| ) |
Construct a grammar.
:arg rules: A string of production rules, one per line.
:arg default_rule: The name of the rule invoked when you call
:meth:`parse()` or :meth:`match()` on the grammar. Defaults to the
first rule. Falls back to None if there are no string-based rules
in this grammar.
:arg more_rules: Additional kwargs whose names are rule names and
values are Expressions or custom-coded callables which accomplish
things the built-in rule syntax cannot. These take precedence over
``rules`` in case of naming conflicts.
| parsimonious.grammar.Grammar.__repr__ | ( | self | ) |
Return an expression that will reconstitute the grammar.
| parsimonious.grammar.Grammar.__str__ | ( | self | ) |
Return a rule string that, when passed to the constructor, would reconstitute the grammar.
|
protected |
Raise RuntimeError if there is no default rule defined.
|
protected |
Return a shallow copy of myself. Deep is unnecessary, since Expression trees are immutable. Subgrammars recreate all the Expressions from scratch, and AbstractGrammars have no Expressions.
|
protected |
Return a 2-tuple: a dict of rule names pointing to their
expressions, and then the first rule.
It's a web of expressions, all referencing each other. Typically,
there's a single root to the web of references, and that root is the
starting symbol for parsing, but there's nothing saying you can't have
multiple roots.
:arg custom_rules: A map of rule names to custom-coded rules:
Expressions
Reimplemented in parsimonious.grammar.BootstrappingGrammar, and parsimonious.grammar.TokenGrammar.
| parsimonious.grammar.Grammar.default | ( | self, | |
| rule_name | |||
| ) |
Return a new Grammar whose :term:`default rule` is ``rule_name``.
| parsimonious.grammar.Grammar.match | ( | self, | |
| text, | |||
pos = 0 |
|||
| ) |
Parse some text with the :term:`default rule` but not necessarily all the way to the end. :arg pos: The index at which to start parsing
| parsimonious.grammar.Grammar.parse | ( | self, | |
| text, | |||
pos = 0 |
|||
| ) |
Parse some text with the :term:`default rule`. :arg pos: The index at which to start parsing