__init__.py 8.9 KB

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