Qucs-S S-parameter Viewer & RF Synthesis Tools
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
parsimonious.grammar.Grammar Class Reference
Inheritance diagram for parsimonious.grammar.Grammar:
Inheritance graph
[legend]
Collaboration diagram for parsimonious.grammar.Grammar:
Collaboration graph
[legend]

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)
 

Detailed Description

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?]

Constructor & Destructor Documentation

◆ __init__()

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.

Member Function Documentation

◆ __repr__()

parsimonious.grammar.Grammar.__repr__ (   self)
Return an expression that will reconstitute the grammar.

◆ __str__()

parsimonious.grammar.Grammar.__str__ (   self)
Return a rule string that, when passed to the constructor, would
reconstitute the grammar.

◆ _check_default_rule()

parsimonious.grammar.Grammar._check_default_rule (   self)
protected
Raise RuntimeError if there is no default rule defined.

◆ _copy()

parsimonious.grammar.Grammar._copy (   self)
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.

◆ _expressions_from_rules()

parsimonious.grammar.Grammar._expressions_from_rules (   self,
  rules,
  custom_rules 
)
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.

◆ default()

parsimonious.grammar.Grammar.default (   self,
  rule_name 
)
Return a new Grammar whose :term:`default rule` is ``rule_name``.

◆ match()

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

◆ parse()

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

The documentation for this class was generated from the following file: