__init__.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # module pyparsing.py
  2. #
  3. # Copyright (c) 2003-2022 Paul T. McGuire
  4. #
  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:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  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.
  23. #
  24. __doc__ = """
  25. pyparsing module - Classes and methods to define and execute parsing grammars
  26. =============================================================================
  27. The pyparsing module is an alternative approach to creating and
  28. executing simple grammars, vs. the traditional lex/yacc approach, or the
  29. use of regular expressions. With pyparsing, you don't need to learn
  30. a new syntax for defining grammars or matching expressions - the parsing
  31. module provides a library of classes that you use to construct the
  32. grammar directly in Python.
  33. Here is a program to parse "Hello, World!" (or any greeting of the form
  34. ``"<salutation>, <addressee>!"``), built up using :class:`Word`,
  35. :class:`Literal`, and :class:`And` elements
  36. (the :meth:`'+'<ParserElement.__add__>` operators create :class:`And` expressions,
  37. and the strings are auto-converted to :class:`Literal` expressions)::
  38. from pyparsing import Word, alphas
  39. # define grammar of a greeting
  40. greet = Word(alphas) + "," + Word(alphas) + "!"
  41. hello = "Hello, World!"
  42. print(hello, "->", greet.parse_string(hello))
  43. The program outputs the following::
  44. Hello, World! -> ['Hello', ',', 'World', '!']
  45. The Python representation of the grammar is quite readable, owing to the
  46. self-explanatory class names, and the use of :class:`'+'<And>`,
  47. :class:`'|'<MatchFirst>`, :class:`'^'<Or>` and :class:`'&'<Each>` operators.
  48. The :class:`ParseResults` object returned from
  49. :class:`ParserElement.parse_string` can be
  50. accessed as a nested list, a dictionary, or an object with named
  51. attributes.
  52. The pyparsing module handles some of the problems that are typically
  53. vexing when writing text parsers:
  54. - extra or missing whitespace (the above program will also handle
  55. "Hello,World!", "Hello , World !", etc.)
  56. - quoted strings
  57. - embedded comments
  58. Getting Started -
  59. -----------------
  60. Visit the classes :class:`ParserElement` and :class:`ParseResults` to
  61. see the base classes that most other pyparsing
  62. classes inherit from. Use the docstrings for examples of how to:
  63. - construct literal match expressions from :class:`Literal` and
  64. :class:`CaselessLiteral` classes
  65. - construct character word-group expressions using the :class:`Word`
  66. class
  67. - see how to create repetitive expressions using :class:`ZeroOrMore`
  68. and :class:`OneOrMore` classes
  69. - use :class:`'+'<And>`, :class:`'|'<MatchFirst>`, :class:`'^'<Or>`,
  70. and :class:`'&'<Each>` operators to combine simple expressions into
  71. more complex ones
  72. - associate names with your parsed results using
  73. :class:`ParserElement.set_results_name`
  74. - access the parsed data, which is returned as a :class:`ParseResults`
  75. object
  76. - find some helpful expression short-cuts like :class:`DelimitedList`
  77. and :class:`one_of`
  78. - find more useful common expressions in the :class:`pyparsing_common`
  79. namespace class
  80. """
  81. from typing import NamedTuple
  82. class version_info(NamedTuple):
  83. major: int
  84. minor: int
  85. micro: int
  86. releaselevel: str
  87. serial: int
  88. @property
  89. def __version__(self):
  90. return (
  91. f"{self.major}.{self.minor}.{self.micro}"
  92. + (
  93. f"{'r' if self.releaselevel[0] == 'c' else ''}{self.releaselevel[0]}{self.serial}",
  94. "",
  95. )[self.releaselevel == "final"]
  96. )
  97. def __str__(self):
  98. return f"{__name__} {self.__version__} / {__version_time__}"
  99. def __repr__(self):
  100. return f"{__name__}.{type(self).__name__}({', '.join('{}={!r}'.format(*nv) for nv in zip(self._fields, self))})"
  101. __version_info__ = version_info(3, 1, 1, "final", 1)
  102. __version_time__ = "29 Jul 2023 22:27 UTC"
  103. __version__ = __version_info__.__version__
  104. __versionTime__ = __version_time__
  105. __author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>"
  106. from .util import *
  107. from .exceptions import *
  108. from .actions import *
  109. from .core import __diag__, __compat__
  110. from .results import *
  111. from .core import * # type: ignore[misc, assignment]
  112. from .core import _builtin_exprs as core_builtin_exprs
  113. from .helpers import * # type: ignore[misc, assignment]
  114. from .helpers import _builtin_exprs as helper_builtin_exprs
  115. from .unicode import unicode_set, UnicodeRangeList, pyparsing_unicode as unicode
  116. from .testing import pyparsing_test as testing
  117. from .common import (
  118. pyparsing_common as common,
  119. _builtin_exprs as common_builtin_exprs,
  120. )
  121. # define backward compat synonyms
  122. if "pyparsing_unicode" not in globals():
  123. pyparsing_unicode = unicode # type: ignore[misc]
  124. if "pyparsing_common" not in globals():
  125. pyparsing_common = common # type: ignore[misc]
  126. if "pyparsing_test" not in globals():
  127. pyparsing_test = testing # type: ignore[misc]
  128. core_builtin_exprs += common_builtin_exprs + helper_builtin_exprs
  129. __all__ = [
  130. "__version__",
  131. "__version_time__",
  132. "__author__",
  133. "__compat__",
  134. "__diag__",
  135. "And",
  136. "AtLineStart",
  137. "AtStringStart",
  138. "CaselessKeyword",
  139. "CaselessLiteral",
  140. "CharsNotIn",
  141. "CloseMatch",
  142. "Combine",
  143. "DelimitedList",
  144. "Dict",
  145. "Each",
  146. "Empty",
  147. "FollowedBy",
  148. "Forward",
  149. "GoToColumn",
  150. "Group",
  151. "IndentedBlock",
  152. "Keyword",
  153. "LineEnd",
  154. "LineStart",
  155. "Literal",
  156. "Located",
  157. "PrecededBy",
  158. "MatchFirst",
  159. "NoMatch",
  160. "NotAny",
  161. "OneOrMore",
  162. "OnlyOnce",
  163. "OpAssoc",
  164. "Opt",
  165. "Optional",
  166. "Or",
  167. "ParseBaseException",
  168. "ParseElementEnhance",
  169. "ParseException",
  170. "ParseExpression",
  171. "ParseFatalException",
  172. "ParseResults",
  173. "ParseSyntaxException",
  174. "ParserElement",
  175. "PositionToken",
  176. "QuotedString",
  177. "RecursiveGrammarException",
  178. "Regex",
  179. "SkipTo",
  180. "StringEnd",
  181. "StringStart",
  182. "Suppress",
  183. "Token",
  184. "TokenConverter",
  185. "White",
  186. "Word",
  187. "WordEnd",
  188. "WordStart",
  189. "ZeroOrMore",
  190. "Char",
  191. "alphanums",
  192. "alphas",
  193. "alphas8bit",
  194. "any_close_tag",
  195. "any_open_tag",
  196. "autoname_elements",
  197. "c_style_comment",
  198. "col",
  199. "common_html_entity",
  200. "condition_as_parse_action",
  201. "counted_array",
  202. "cpp_style_comment",
  203. "dbl_quoted_string",
  204. "dbl_slash_comment",
  205. "delimited_list",
  206. "dict_of",
  207. "empty",
  208. "hexnums",
  209. "html_comment",
  210. "identchars",
  211. "identbodychars",
  212. "infix_notation",
  213. "java_style_comment",
  214. "line",
  215. "line_end",
  216. "line_start",
  217. "lineno",
  218. "make_html_tags",
  219. "make_xml_tags",
  220. "match_only_at_col",
  221. "match_previous_expr",
  222. "match_previous_literal",
  223. "nested_expr",
  224. "null_debug_action",
  225. "nums",
  226. "one_of",
  227. "original_text_for",
  228. "printables",
  229. "punc8bit",
  230. "pyparsing_common",
  231. "pyparsing_test",
  232. "pyparsing_unicode",
  233. "python_style_comment",
  234. "quoted_string",
  235. "remove_quotes",
  236. "replace_with",
  237. "replace_html_entity",
  238. "rest_of_line",
  239. "sgl_quoted_string",
  240. "srange",
  241. "string_end",
  242. "string_start",
  243. "token_map",
  244. "trace_parse_action",
  245. "ungroup",
  246. "unicode_set",
  247. "unicode_string",
  248. "with_attribute",
  249. "with_class",
  250. # pre-PEP8 compatibility names
  251. "__versionTime__",
  252. "anyCloseTag",
  253. "anyOpenTag",
  254. "cStyleComment",
  255. "commonHTMLEntity",
  256. "conditionAsParseAction",
  257. "countedArray",
  258. "cppStyleComment",
  259. "dblQuotedString",
  260. "dblSlashComment",
  261. "delimitedList",
  262. "dictOf",
  263. "htmlComment",
  264. "indentedBlock",
  265. "infixNotation",
  266. "javaStyleComment",
  267. "lineEnd",
  268. "lineStart",
  269. "locatedExpr",
  270. "makeHTMLTags",
  271. "makeXMLTags",
  272. "matchOnlyAtCol",
  273. "matchPreviousExpr",
  274. "matchPreviousLiteral",
  275. "nestedExpr",
  276. "nullDebugAction",
  277. "oneOf",
  278. "opAssoc",
  279. "originalTextFor",
  280. "pythonStyleComment",
  281. "quotedString",
  282. "removeQuotes",
  283. "replaceHTMLEntity",
  284. "replaceWith",
  285. "restOfLine",
  286. "sglQuotedString",
  287. "stringEnd",
  288. "stringStart",
  289. "tokenMap",
  290. "traceParseAction",
  291. "unicodeString",
  292. "withAttribute",
  293. "withClass",
  294. "common",
  295. "unicode",
  296. "testing",
  297. ]