__init__.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.parseString` 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.setResultsName`
  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:`oneOf`
  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. "{}.{}.{}".format(self.major, self.minor, self.micro)
  92. + (
  93. "{}{}{}".format(
  94. "r" if self.releaselevel[0] == "c" else "",
  95. self.releaselevel[0],
  96. self.serial,
  97. ),
  98. "",
  99. )[self.releaselevel == "final"]
  100. )
  101. def __str__(self):
  102. return "{} {} / {}".format(__name__, self.__version__, __version_time__)
  103. def __repr__(self):
  104. return "{}.{}({})".format(
  105. __name__,
  106. type(self).__name__,
  107. ", ".join("{}={!r}".format(*nv) for nv in zip(self._fields, self)),
  108. )
  109. __version_info__ = version_info(3, 0, 9, "final", 0)
  110. __version_time__ = "05 May 2022 07:02 UTC"
  111. __version__ = __version_info__.__version__
  112. __versionTime__ = __version_time__
  113. __author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>"
  114. from .util import *
  115. from .exceptions import *
  116. from .actions import *
  117. from .core import __diag__, __compat__
  118. from .results import *
  119. from .core import *
  120. from .core import _builtin_exprs as core_builtin_exprs
  121. from .helpers import *
  122. from .helpers import _builtin_exprs as helper_builtin_exprs
  123. from .unicode import unicode_set, UnicodeRangeList, pyparsing_unicode as unicode
  124. from .testing import pyparsing_test as testing
  125. from .common import (
  126. pyparsing_common as common,
  127. _builtin_exprs as common_builtin_exprs,
  128. )
  129. # define backward compat synonyms
  130. if "pyparsing_unicode" not in globals():
  131. pyparsing_unicode = unicode
  132. if "pyparsing_common" not in globals():
  133. pyparsing_common = common
  134. if "pyparsing_test" not in globals():
  135. pyparsing_test = testing
  136. core_builtin_exprs += common_builtin_exprs + helper_builtin_exprs
  137. __all__ = [
  138. "__version__",
  139. "__version_time__",
  140. "__author__",
  141. "__compat__",
  142. "__diag__",
  143. "And",
  144. "AtLineStart",
  145. "AtStringStart",
  146. "CaselessKeyword",
  147. "CaselessLiteral",
  148. "CharsNotIn",
  149. "Combine",
  150. "Dict",
  151. "Each",
  152. "Empty",
  153. "FollowedBy",
  154. "Forward",
  155. "GoToColumn",
  156. "Group",
  157. "IndentedBlock",
  158. "Keyword",
  159. "LineEnd",
  160. "LineStart",
  161. "Literal",
  162. "Located",
  163. "PrecededBy",
  164. "MatchFirst",
  165. "NoMatch",
  166. "NotAny",
  167. "OneOrMore",
  168. "OnlyOnce",
  169. "OpAssoc",
  170. "Opt",
  171. "Optional",
  172. "Or",
  173. "ParseBaseException",
  174. "ParseElementEnhance",
  175. "ParseException",
  176. "ParseExpression",
  177. "ParseFatalException",
  178. "ParseResults",
  179. "ParseSyntaxException",
  180. "ParserElement",
  181. "PositionToken",
  182. "QuotedString",
  183. "RecursiveGrammarException",
  184. "Regex",
  185. "SkipTo",
  186. "StringEnd",
  187. "StringStart",
  188. "Suppress",
  189. "Token",
  190. "TokenConverter",
  191. "White",
  192. "Word",
  193. "WordEnd",
  194. "WordStart",
  195. "ZeroOrMore",
  196. "Char",
  197. "alphanums",
  198. "alphas",
  199. "alphas8bit",
  200. "any_close_tag",
  201. "any_open_tag",
  202. "c_style_comment",
  203. "col",
  204. "common_html_entity",
  205. "counted_array",
  206. "cpp_style_comment",
  207. "dbl_quoted_string",
  208. "dbl_slash_comment",
  209. "delimited_list",
  210. "dict_of",
  211. "empty",
  212. "hexnums",
  213. "html_comment",
  214. "identchars",
  215. "identbodychars",
  216. "java_style_comment",
  217. "line",
  218. "line_end",
  219. "line_start",
  220. "lineno",
  221. "make_html_tags",
  222. "make_xml_tags",
  223. "match_only_at_col",
  224. "match_previous_expr",
  225. "match_previous_literal",
  226. "nested_expr",
  227. "null_debug_action",
  228. "nums",
  229. "one_of",
  230. "printables",
  231. "punc8bit",
  232. "python_style_comment",
  233. "quoted_string",
  234. "remove_quotes",
  235. "replace_with",
  236. "replace_html_entity",
  237. "rest_of_line",
  238. "sgl_quoted_string",
  239. "srange",
  240. "string_end",
  241. "string_start",
  242. "trace_parse_action",
  243. "unicode_string",
  244. "with_attribute",
  245. "indentedBlock",
  246. "original_text_for",
  247. "ungroup",
  248. "infix_notation",
  249. "locatedExpr",
  250. "with_class",
  251. "CloseMatch",
  252. "token_map",
  253. "pyparsing_common",
  254. "pyparsing_unicode",
  255. "unicode_set",
  256. "condition_as_parse_action",
  257. "pyparsing_test",
  258. # pre-PEP8 compatibility names
  259. "__versionTime__",
  260. "anyCloseTag",
  261. "anyOpenTag",
  262. "cStyleComment",
  263. "commonHTMLEntity",
  264. "countedArray",
  265. "cppStyleComment",
  266. "dblQuotedString",
  267. "dblSlashComment",
  268. "delimitedList",
  269. "dictOf",
  270. "htmlComment",
  271. "javaStyleComment",
  272. "lineEnd",
  273. "lineStart",
  274. "makeHTMLTags",
  275. "makeXMLTags",
  276. "matchOnlyAtCol",
  277. "matchPreviousExpr",
  278. "matchPreviousLiteral",
  279. "nestedExpr",
  280. "nullDebugAction",
  281. "oneOf",
  282. "opAssoc",
  283. "pythonStyleComment",
  284. "quotedString",
  285. "removeQuotes",
  286. "replaceHTMLEntity",
  287. "replaceWith",
  288. "restOfLine",
  289. "sglQuotedString",
  290. "stringEnd",
  291. "stringStart",
  292. "traceParseAction",
  293. "unicodeString",
  294. "withAttribute",
  295. "indentedBlock",
  296. "originalTextFor",
  297. "infixNotation",
  298. "locatedExpr",
  299. "withClass",
  300. "tokenMap",
  301. "conditionAsParseAction",
  302. "autoname_elements",
  303. ]