3 # Copyright (c) 2003-2022 Paul T. McGuire
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 pyparsing module - Classes and methods to define and execute parsing grammars
27 =============================================================================
29 The pyparsing module is an alternative approach to creating and
30 executing simple grammars, vs. the traditional lex/yacc approach, or the
31 use of regular expressions. With pyparsing, you don't need to learn
32 a new syntax for defining grammars or matching expressions - the parsing
33 module provides a library of classes that you use to construct the
34 grammar directly in Python.
36 Here is a program to parse "Hello, World!" (or any greeting of the form
37 ``"<salutation>, <addressee>!"``), built up using :class:`Word`,
38 :class:`Literal`, and :class:`And` elements
39 (the :meth:`'+'<ParserElement.__add__>` operators create :class:`And` expressions,
40 and the strings are auto-converted to :class:`Literal` expressions)::
42 from pyparsing import Word, alphas
44 # define grammar of a greeting
45 greet = Word(alphas) + "," + Word(alphas) + "!"
47 hello = "Hello, World!"
48 print(hello, "->", greet.parse_string(hello))
50 The program outputs the following::
52 Hello, World! -> ['Hello', ',', 'World', '!']
54 The Python representation of the grammar is quite readable, owing to the
55 self-explanatory class names, and the use of :class:`'+'<And>`,
56 :class:`'|'<MatchFirst>`, :class:`'^'<Or>` and :class:`'&'<Each>` operators.
58 The :class:`ParseResults` object returned from
59 :class:`ParserElement.parseString` can be
60 accessed as a nested list, a dictionary, or an object with named
63 The pyparsing module handles some of the problems that are typically
64 vexing when writing text parsers:
66 - extra or missing whitespace (the above program will also handle
67 "Hello,World!", "Hello , World !", etc.)
74 Visit the classes :class:`ParserElement` and :class:`ParseResults` to
75 see the base classes that most other pyparsing
76 classes inherit from. Use the docstrings for examples of how to:
78 - construct literal match expressions from :class:`Literal` and
79 :class:`CaselessLiteral` classes
80 - construct character word-group expressions using the :class:`Word`
82 - see how to create repetitive expressions using :class:`ZeroOrMore`
83 and :class:`OneOrMore` classes
84 - use :class:`'+'<And>`, :class:`'|'<MatchFirst>`, :class:`'^'<Or>`,
85 and :class:`'&'<Each>` operators to combine simple expressions into
87 - associate names with your parsed results using
88 :class:`ParserElement.setResultsName`
89 - access the parsed data, which is returned as a :class:`ParseResults`
91 - find some helpful expression short-cuts like :class:`delimitedList`
93 - find more useful common expressions in the :class:`pyparsing_common`
96 from typing import NamedTuple
99 class version_info(NamedTuple):
107 def __version__(self):
109 "{}.{}.{}".format(self.major, self.minor, self.micro)
112 "r" if self.releaselevel[0] == "c" else "",
113 self.releaselevel[0],
117 )[self.releaselevel == "final"]
121 return "{} {} / {}".format(__name__, self.__version__, __version_time__)
124 return "{}.{}({})".format(
127 ", ".join("{}={!r}".format(*nv) for nv in zip(self._fields, self)),
131 __version_info__ = version_info(3, 0, 9, "final", 0)
132 __version_time__ = "05 May 2022 07:02 UTC"
133 __version__ = __version_info__.__version__
134 __versionTime__ = __version_time__
135 __author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>"
138 from .exceptions import *
139 from .actions import *
140 from .core import __diag__, __compat__
141 from .results import *
143 from .core import _builtin_exprs as core_builtin_exprs
144 from .helpers import *
145 from .helpers import _builtin_exprs as helper_builtin_exprs
147 from .unicode import unicode_set, UnicodeRangeList, pyparsing_unicode as unicode
148 from .testing import pyparsing_test as testing
149 from .common import (
150 pyparsing_common as common,
151 _builtin_exprs as common_builtin_exprs,
154 # define backward compat synonyms
155 if "pyparsing_unicode" not in globals():
156 pyparsing_unicode = unicode
157 if "pyparsing_common" not in globals():
158 pyparsing_common = common
159 if "pyparsing_test" not in globals():
160 pyparsing_test = testing
162 core_builtin_exprs += common_builtin_exprs + helper_builtin_exprs
201 "ParseBaseException",
202 "ParseElementEnhance",
205 "ParseFatalException",
207 "ParseSyntaxException",
211 "RecursiveGrammarException",
232 "common_html_entity",
244 "java_style_comment",
252 "match_previous_expr",
253 "match_previous_literal",
260 "python_style_comment",
264 "replace_html_entity",
270 "trace_parse_action",
284 "condition_as_parse_action",
286 # pre-PEP8 compatibility names
306 "matchPreviousLiteral",
311 "pythonStyleComment",
329 "conditionAsParseAction",