helpers.py 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. # helpers.py
  2. import html.entities
  3. import re
  4. from . import __diag__
  5. from .core import *
  6. from .util import _bslash, _flatten, _escape_regex_range_chars
  7. #
  8. # global helpers
  9. #
  10. def delimited_list(
  11. expr: Union[str, ParserElement],
  12. delim: Union[str, ParserElement] = ",",
  13. combine: bool = False,
  14. min: OptionalType[int] = None,
  15. max: OptionalType[int] = None,
  16. *,
  17. allow_trailing_delim: bool = False,
  18. ) -> ParserElement:
  19. """Helper to define a delimited list of expressions - the delimiter
  20. defaults to ','. By default, the list elements and delimiters can
  21. have intervening whitespace, and comments, but this can be
  22. overridden by passing ``combine=True`` in the constructor. If
  23. ``combine`` is set to ``True``, the matching tokens are
  24. returned as a single token string, with the delimiters included;
  25. otherwise, the matching tokens are returned as a list of tokens,
  26. with the delimiters suppressed.
  27. If ``allow_trailing_delim`` is set to True, then the list may end with
  28. a delimiter.
  29. Example::
  30. delimited_list(Word(alphas)).parse_string("aa,bb,cc") # -> ['aa', 'bb', 'cc']
  31. delimited_list(Word(hexnums), delim=':', combine=True).parse_string("AA:BB:CC:DD:EE") # -> ['AA:BB:CC:DD:EE']
  32. """
  33. if isinstance(expr, str_type):
  34. expr = ParserElement._literalStringClass(expr)
  35. dlName = "{expr} [{delim} {expr}]...{end}".format(
  36. expr=str(expr.copy().streamline()),
  37. delim=str(delim),
  38. end=" [{}]".format(str(delim)) if allow_trailing_delim else "",
  39. )
  40. if not combine:
  41. delim = Suppress(delim)
  42. if min is not None:
  43. if min < 1:
  44. raise ValueError("min must be greater than 0")
  45. min -= 1
  46. if max is not None:
  47. if min is not None and max <= min:
  48. raise ValueError("max must be greater than, or equal to min")
  49. max -= 1
  50. delimited_list_expr = expr + (delim + expr)[min, max]
  51. if allow_trailing_delim:
  52. delimited_list_expr += Opt(delim)
  53. if combine:
  54. return Combine(delimited_list_expr).set_name(dlName)
  55. else:
  56. return delimited_list_expr.set_name(dlName)
  57. def counted_array(
  58. expr: ParserElement,
  59. int_expr: OptionalType[ParserElement] = None,
  60. *,
  61. intExpr: OptionalType[ParserElement] = None,
  62. ) -> ParserElement:
  63. """Helper to define a counted list of expressions.
  64. This helper defines a pattern of the form::
  65. integer expr expr expr...
  66. where the leading integer tells how many expr expressions follow.
  67. The matched tokens returns the array of expr tokens as a list - the
  68. leading count token is suppressed.
  69. If ``int_expr`` is specified, it should be a pyparsing expression
  70. that produces an integer value.
  71. Example::
  72. counted_array(Word(alphas)).parse_string('2 ab cd ef') # -> ['ab', 'cd']
  73. # in this parser, the leading integer value is given in binary,
  74. # '10' indicating that 2 values are in the array
  75. binary_constant = Word('01').set_parse_action(lambda t: int(t[0], 2))
  76. counted_array(Word(alphas), int_expr=binary_constant).parse_string('10 ab cd ef') # -> ['ab', 'cd']
  77. # if other fields must be parsed after the count but before the
  78. # list items, give the fields results names and they will
  79. # be preserved in the returned ParseResults:
  80. count_with_metadata = integer + Word(alphas)("type")
  81. typed_array = counted_array(Word(alphanums), int_expr=count_with_metadata)("items")
  82. result = typed_array.parse_string("3 bool True True False")
  83. print(result.dump())
  84. # prints
  85. # ['True', 'True', 'False']
  86. # - items: ['True', 'True', 'False']
  87. # - type: 'bool'
  88. """
  89. intExpr = intExpr or int_expr
  90. array_expr = Forward()
  91. def count_field_parse_action(s, l, t):
  92. nonlocal array_expr
  93. n = t[0]
  94. array_expr <<= (expr * n) if n else Empty()
  95. # clear list contents, but keep any named results
  96. del t[:]
  97. if intExpr is None:
  98. intExpr = Word(nums).set_parse_action(lambda t: int(t[0]))
  99. else:
  100. intExpr = intExpr.copy()
  101. intExpr.set_name("arrayLen")
  102. intExpr.add_parse_action(count_field_parse_action, call_during_try=True)
  103. return (intExpr + array_expr).set_name("(len) " + str(expr) + "...")
  104. def match_previous_literal(expr: ParserElement) -> ParserElement:
  105. """Helper to define an expression that is indirectly defined from
  106. the tokens matched in a previous expression, that is, it looks for
  107. a 'repeat' of a previous expression. For example::
  108. first = Word(nums)
  109. second = match_previous_literal(first)
  110. match_expr = first + ":" + second
  111. will match ``"1:1"``, but not ``"1:2"``. Because this
  112. matches a previous literal, will also match the leading
  113. ``"1:1"`` in ``"1:10"``. If this is not desired, use
  114. :class:`match_previous_expr`. Do *not* use with packrat parsing
  115. enabled.
  116. """
  117. rep = Forward()
  118. def copy_token_to_repeater(s, l, t):
  119. if t:
  120. if len(t) == 1:
  121. rep << t[0]
  122. else:
  123. # flatten t tokens
  124. tflat = _flatten(t.as_list())
  125. rep << And(Literal(tt) for tt in tflat)
  126. else:
  127. rep << Empty()
  128. expr.add_parse_action(copy_token_to_repeater, callDuringTry=True)
  129. rep.set_name("(prev) " + str(expr))
  130. return rep
  131. def match_previous_expr(expr: ParserElement) -> ParserElement:
  132. """Helper to define an expression that is indirectly defined from
  133. the tokens matched in a previous expression, that is, it looks for
  134. a 'repeat' of a previous expression. For example::
  135. first = Word(nums)
  136. second = match_previous_expr(first)
  137. match_expr = first + ":" + second
  138. will match ``"1:1"``, but not ``"1:2"``. Because this
  139. matches by expressions, will *not* match the leading ``"1:1"``
  140. in ``"1:10"``; the expressions are evaluated first, and then
  141. compared, so ``"1"`` is compared with ``"10"``. Do *not* use
  142. with packrat parsing enabled.
  143. """
  144. rep = Forward()
  145. e2 = expr.copy()
  146. rep <<= e2
  147. def copy_token_to_repeater(s, l, t):
  148. matchTokens = _flatten(t.as_list())
  149. def must_match_these_tokens(s, l, t):
  150. theseTokens = _flatten(t.as_list())
  151. if theseTokens != matchTokens:
  152. raise ParseException(s, l, "Expected {}, found{}".format(matchTokens, theseTokens))
  153. rep.set_parse_action(must_match_these_tokens, callDuringTry=True)
  154. expr.add_parse_action(copy_token_to_repeater, callDuringTry=True)
  155. rep.set_name("(prev) " + str(expr))
  156. return rep
  157. def one_of(
  158. strs: Union[IterableType[str], str],
  159. caseless: bool = False,
  160. use_regex: bool = True,
  161. as_keyword: bool = False,
  162. *,
  163. useRegex: bool = True,
  164. asKeyword: bool = False,
  165. ) -> ParserElement:
  166. """Helper to quickly define a set of alternative :class:`Literal` s,
  167. and makes sure to do longest-first testing when there is a conflict,
  168. regardless of the input order, but returns
  169. a :class:`MatchFirst` for best performance.
  170. Parameters:
  171. - ``strs`` - a string of space-delimited literals, or a collection of
  172. string literals
  173. - ``caseless`` - treat all literals as caseless - (default= ``False``)
  174. - ``use_regex`` - as an optimization, will
  175. generate a :class:`Regex` object; otherwise, will generate
  176. a :class:`MatchFirst` object (if ``caseless=True`` or ``asKeyword=True``, or if
  177. creating a :class:`Regex` raises an exception) - (default= ``True``)
  178. - ``as_keyword`` - enforce :class:`Keyword`-style matching on the
  179. generated expressions - (default= ``False``)
  180. - ``asKeyword`` and ``useRegex`` are retained for pre-PEP8 compatibility,
  181. but will be removed in a future release
  182. Example::
  183. comp_oper = one_of("< = > <= >= !=")
  184. var = Word(alphas)
  185. number = Word(nums)
  186. term = var | number
  187. comparison_expr = term + comp_oper + term
  188. print(comparison_expr.search_string("B = 12 AA=23 B<=AA AA>12"))
  189. prints::
  190. [['B', '=', '12'], ['AA', '=', '23'], ['B', '<=', 'AA'], ['AA', '>', '12']]
  191. """
  192. asKeyword = asKeyword or as_keyword
  193. useRegex = useRegex and use_regex
  194. if (
  195. isinstance(caseless, str_type)
  196. and __diag__.warn_on_multiple_string_args_to_oneof
  197. ):
  198. warnings.warn(
  199. "More than one string argument passed to one_of, pass"
  200. " choices as a list or space-delimited string",
  201. stacklevel=2,
  202. )
  203. if caseless:
  204. isequal = lambda a, b: a.upper() == b.upper()
  205. masks = lambda a, b: b.upper().startswith(a.upper())
  206. parseElementClass = CaselessKeyword if asKeyword else CaselessLiteral
  207. else:
  208. isequal = lambda a, b: a == b
  209. masks = lambda a, b: b.startswith(a)
  210. parseElementClass = Keyword if asKeyword else Literal
  211. symbols: List[str] = []
  212. if isinstance(strs, str_type):
  213. symbols = strs.split()
  214. elif isinstance(strs, Iterable):
  215. symbols = list(strs)
  216. else:
  217. raise TypeError("Invalid argument to one_of, expected string or iterable")
  218. if not symbols:
  219. return NoMatch()
  220. # reorder given symbols to take care to avoid masking longer choices with shorter ones
  221. # (but only if the given symbols are not just single characters)
  222. if any(len(sym) > 1 for sym in symbols):
  223. i = 0
  224. while i < len(symbols) - 1:
  225. cur = symbols[i]
  226. for j, other in enumerate(symbols[i + 1 :]):
  227. if isequal(other, cur):
  228. del symbols[i + j + 1]
  229. break
  230. elif masks(cur, other):
  231. del symbols[i + j + 1]
  232. symbols.insert(i, other)
  233. break
  234. else:
  235. i += 1
  236. if useRegex:
  237. re_flags: int = re.IGNORECASE if caseless else 0
  238. try:
  239. if all(len(sym) == 1 for sym in symbols):
  240. # symbols are just single characters, create range regex pattern
  241. patt = "[{}]".format(
  242. "".join(_escape_regex_range_chars(sym) for sym in symbols)
  243. )
  244. else:
  245. patt = "|".join(re.escape(sym) for sym in symbols)
  246. # wrap with \b word break markers if defining as keywords
  247. if asKeyword:
  248. patt = r"\b(?:{})\b".format(patt)
  249. ret = Regex(patt, flags=re_flags).set_name(" | ".join(symbols))
  250. if caseless:
  251. # add parse action to return symbols as specified, not in random
  252. # casing as found in input string
  253. symbol_map = {sym.lower(): sym for sym in symbols}
  254. ret.add_parse_action(lambda s, l, t: symbol_map[t[0].lower()])
  255. return ret
  256. except sre_constants.error:
  257. warnings.warn(
  258. "Exception creating Regex for one_of, building MatchFirst", stacklevel=2
  259. )
  260. # last resort, just use MatchFirst
  261. return MatchFirst(parseElementClass(sym) for sym in symbols).set_name(
  262. " | ".join(symbols)
  263. )
  264. def dict_of(key: ParserElement, value: ParserElement) -> ParserElement:
  265. """Helper to easily and clearly define a dictionary by specifying
  266. the respective patterns for the key and value. Takes care of
  267. defining the :class:`Dict`, :class:`ZeroOrMore`, and
  268. :class:`Group` tokens in the proper order. The key pattern
  269. can include delimiting markers or punctuation, as long as they are
  270. suppressed, thereby leaving the significant key text. The value
  271. pattern can include named results, so that the :class:`Dict` results
  272. can include named token fields.
  273. Example::
  274. text = "shape: SQUARE posn: upper left color: light blue texture: burlap"
  275. attr_expr = (label + Suppress(':') + OneOrMore(data_word, stop_on=label).set_parse_action(' '.join))
  276. print(OneOrMore(attr_expr).parse_string(text).dump())
  277. attr_label = label
  278. attr_value = Suppress(':') + OneOrMore(data_word, stop_on=label).set_parse_action(' '.join)
  279. # similar to Dict, but simpler call format
  280. result = dict_of(attr_label, attr_value).parse_string(text)
  281. print(result.dump())
  282. print(result['shape'])
  283. print(result.shape) # object attribute access works too
  284. print(result.as_dict())
  285. prints::
  286. [['shape', 'SQUARE'], ['posn', 'upper left'], ['color', 'light blue'], ['texture', 'burlap']]
  287. - color: light blue
  288. - posn: upper left
  289. - shape: SQUARE
  290. - texture: burlap
  291. SQUARE
  292. SQUARE
  293. {'color': 'light blue', 'shape': 'SQUARE', 'posn': 'upper left', 'texture': 'burlap'}
  294. """
  295. return Dict(OneOrMore(Group(key + value)))
  296. def original_text_for(
  297. expr: ParserElement, as_string: bool = True, *, asString: bool = True
  298. ) -> ParserElement:
  299. """Helper to return the original, untokenized text for a given
  300. expression. Useful to restore the parsed fields of an HTML start
  301. tag into the raw tag text itself, or to revert separate tokens with
  302. intervening whitespace back to the original matching input text. By
  303. default, returns astring containing the original parsed text.
  304. If the optional ``as_string`` argument is passed as
  305. ``False``, then the return value is
  306. a :class:`ParseResults` containing any results names that
  307. were originally matched, and a single token containing the original
  308. matched text from the input string. So if the expression passed to
  309. :class:`original_text_for` contains expressions with defined
  310. results names, you must set ``as_string`` to ``False`` if you
  311. want to preserve those results name values.
  312. The ``asString`` pre-PEP8 argument is retained for compatibility,
  313. but will be removed in a future release.
  314. Example::
  315. src = "this is test <b> bold <i>text</i> </b> normal text "
  316. for tag in ("b", "i"):
  317. opener, closer = make_html_tags(tag)
  318. patt = original_text_for(opener + SkipTo(closer) + closer)
  319. print(patt.search_string(src)[0])
  320. prints::
  321. ['<b> bold <i>text</i> </b>']
  322. ['<i>text</i>']
  323. """
  324. asString = asString and as_string
  325. locMarker = Empty().set_parse_action(lambda s, loc, t: loc)
  326. endlocMarker = locMarker.copy()
  327. endlocMarker.callPreparse = False
  328. matchExpr = locMarker("_original_start") + expr + endlocMarker("_original_end")
  329. if asString:
  330. extractText = lambda s, l, t: s[t._original_start : t._original_end]
  331. else:
  332. def extractText(s, l, t):
  333. t[:] = [s[t.pop("_original_start") : t.pop("_original_end")]]
  334. matchExpr.set_parse_action(extractText)
  335. matchExpr.ignoreExprs = expr.ignoreExprs
  336. matchExpr.suppress_warning(Diagnostics.warn_ungrouped_named_tokens_in_collection)
  337. return matchExpr
  338. def ungroup(expr: ParserElement) -> ParserElement:
  339. """Helper to undo pyparsing's default grouping of And expressions,
  340. even if all but one are non-empty.
  341. """
  342. return TokenConverter(expr).add_parse_action(lambda t: t[0])
  343. def locatedExpr(expr: ParserElement) -> ParserElement:
  344. """
  345. (DEPRECATED - future code should use the Located class)
  346. Helper to decorate a returned token with its starting and ending
  347. locations in the input string.
  348. This helper adds the following results names:
  349. - ``locn_start`` - location where matched expression begins
  350. - ``locn_end`` - location where matched expression ends
  351. - ``value`` - the actual parsed results
  352. Be careful if the input text contains ``<TAB>`` characters, you
  353. may want to call :class:`ParserElement.parseWithTabs`
  354. Example::
  355. wd = Word(alphas)
  356. for match in locatedExpr(wd).searchString("ljsdf123lksdjjf123lkkjj1222"):
  357. print(match)
  358. prints::
  359. [[0, 'ljsdf', 5]]
  360. [[8, 'lksdjjf', 15]]
  361. [[18, 'lkkjj', 23]]
  362. """
  363. locator = Empty().set_parse_action(lambda ss, ll, tt: ll)
  364. return Group(
  365. locator("locn_start")
  366. + expr("value")
  367. + locator.copy().leaveWhitespace()("locn_end")
  368. )
  369. def nested_expr(
  370. opener: Union[str, ParserElement] = "(",
  371. closer: Union[str, ParserElement] = ")",
  372. content: OptionalType[ParserElement] = None,
  373. ignore_expr: ParserElement = quoted_string(),
  374. *,
  375. ignoreExpr: ParserElement = quoted_string(),
  376. ) -> ParserElement:
  377. """Helper method for defining nested lists enclosed in opening and
  378. closing delimiters (``"("`` and ``")"`` are the default).
  379. Parameters:
  380. - ``opener`` - opening character for a nested list
  381. (default= ``"("``); can also be a pyparsing expression
  382. - ``closer`` - closing character for a nested list
  383. (default= ``")"``); can also be a pyparsing expression
  384. - ``content`` - expression for items within the nested lists
  385. (default= ``None``)
  386. - ``ignore_expr`` - expression for ignoring opening and closing delimiters
  387. (default= :class:`quoted_string`)
  388. - ``ignoreExpr`` - this pre-PEP8 argument is retained for compatibility
  389. but will be removed in a future release
  390. If an expression is not provided for the content argument, the
  391. nested expression will capture all whitespace-delimited content
  392. between delimiters as a list of separate values.
  393. Use the ``ignore_expr`` argument to define expressions that may
  394. contain opening or closing characters that should not be treated as
  395. opening or closing characters for nesting, such as quoted_string or
  396. a comment expression. Specify multiple expressions using an
  397. :class:`Or` or :class:`MatchFirst`. The default is
  398. :class:`quoted_string`, but if no expressions are to be ignored, then
  399. pass ``None`` for this argument.
  400. Example::
  401. data_type = one_of("void int short long char float double")
  402. decl_data_type = Combine(data_type + Opt(Word('*')))
  403. ident = Word(alphas+'_', alphanums+'_')
  404. number = pyparsing_common.number
  405. arg = Group(decl_data_type + ident)
  406. LPAR, RPAR = map(Suppress, "()")
  407. code_body = nested_expr('{', '}', ignore_expr=(quoted_string | c_style_comment))
  408. c_function = (decl_data_type("type")
  409. + ident("name")
  410. + LPAR + Opt(delimited_list(arg), [])("args") + RPAR
  411. + code_body("body"))
  412. c_function.ignore(c_style_comment)
  413. source_code = '''
  414. int is_odd(int x) {
  415. return (x%2);
  416. }
  417. int dec_to_hex(char hchar) {
  418. if (hchar >= '0' && hchar <= '9') {
  419. return (ord(hchar)-ord('0'));
  420. } else {
  421. return (10+ord(hchar)-ord('A'));
  422. }
  423. }
  424. '''
  425. for func in c_function.search_string(source_code):
  426. print("%(name)s (%(type)s) args: %(args)s" % func)
  427. prints::
  428. is_odd (int) args: [['int', 'x']]
  429. dec_to_hex (int) args: [['char', 'hchar']]
  430. """
  431. if ignoreExpr != ignore_expr:
  432. ignoreExpr = ignore_expr if ignoreExpr == quoted_string() else ignoreExpr
  433. if opener == closer:
  434. raise ValueError("opening and closing strings cannot be the same")
  435. if content is None:
  436. if isinstance(opener, str_type) and isinstance(closer, str_type):
  437. if len(opener) == 1 and len(closer) == 1:
  438. if ignoreExpr is not None:
  439. content = Combine(
  440. OneOrMore(
  441. ~ignoreExpr
  442. + CharsNotIn(
  443. opener + closer + ParserElement.DEFAULT_WHITE_CHARS,
  444. exact=1,
  445. )
  446. )
  447. ).set_parse_action(lambda t: t[0].strip())
  448. else:
  449. content = empty.copy() + CharsNotIn(
  450. opener + closer + ParserElement.DEFAULT_WHITE_CHARS
  451. ).set_parse_action(lambda t: t[0].strip())
  452. else:
  453. if ignoreExpr is not None:
  454. content = Combine(
  455. OneOrMore(
  456. ~ignoreExpr
  457. + ~Literal(opener)
  458. + ~Literal(closer)
  459. + CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS, exact=1)
  460. )
  461. ).set_parse_action(lambda t: t[0].strip())
  462. else:
  463. content = Combine(
  464. OneOrMore(
  465. ~Literal(opener)
  466. + ~Literal(closer)
  467. + CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS, exact=1)
  468. )
  469. ).set_parse_action(lambda t: t[0].strip())
  470. else:
  471. raise ValueError(
  472. "opening and closing arguments must be strings if no content expression is given"
  473. )
  474. ret = Forward()
  475. if ignoreExpr is not None:
  476. ret <<= Group(
  477. Suppress(opener) + ZeroOrMore(ignoreExpr | ret | content) + Suppress(closer)
  478. )
  479. else:
  480. ret <<= Group(Suppress(opener) + ZeroOrMore(ret | content) + Suppress(closer))
  481. ret.set_name("nested %s%s expression" % (opener, closer))
  482. return ret
  483. def _makeTags(tagStr, xml, suppress_LT=Suppress("<"), suppress_GT=Suppress(">")):
  484. """Internal helper to construct opening and closing tag expressions, given a tag name"""
  485. if isinstance(tagStr, str_type):
  486. resname = tagStr
  487. tagStr = Keyword(tagStr, caseless=not xml)
  488. else:
  489. resname = tagStr.name
  490. tagAttrName = Word(alphas, alphanums + "_-:")
  491. if xml:
  492. tagAttrValue = dbl_quoted_string.copy().set_parse_action(remove_quotes)
  493. openTag = (
  494. suppress_LT
  495. + tagStr("tag")
  496. + Dict(ZeroOrMore(Group(tagAttrName + Suppress("=") + tagAttrValue)))
  497. + Opt("/", default=[False])("empty").set_parse_action(
  498. lambda s, l, t: t[0] == "/"
  499. )
  500. + suppress_GT
  501. )
  502. else:
  503. tagAttrValue = quoted_string.copy().set_parse_action(remove_quotes) | Word(
  504. printables, exclude_chars=">"
  505. )
  506. openTag = (
  507. suppress_LT
  508. + tagStr("tag")
  509. + Dict(
  510. ZeroOrMore(
  511. Group(
  512. tagAttrName.set_parse_action(lambda t: t[0].lower())
  513. + Opt(Suppress("=") + tagAttrValue)
  514. )
  515. )
  516. )
  517. + Opt("/", default=[False])("empty").set_parse_action(
  518. lambda s, l, t: t[0] == "/"
  519. )
  520. + suppress_GT
  521. )
  522. closeTag = Combine(Literal("</") + tagStr + ">", adjacent=False)
  523. openTag.set_name("<%s>" % resname)
  524. # add start<tagname> results name in parse action now that ungrouped names are not reported at two levels
  525. openTag.add_parse_action(
  526. lambda t: t.__setitem__(
  527. "start" + "".join(resname.replace(":", " ").title().split()), t.copy()
  528. )
  529. )
  530. closeTag = closeTag(
  531. "end" + "".join(resname.replace(":", " ").title().split())
  532. ).set_name("</%s>" % resname)
  533. openTag.tag = resname
  534. closeTag.tag = resname
  535. openTag.tag_body = SkipTo(closeTag())
  536. return openTag, closeTag
  537. def make_html_tags(
  538. tag_str: Union[str, ParserElement]
  539. ) -> Tuple[ParserElement, ParserElement]:
  540. """Helper to construct opening and closing tag expressions for HTML,
  541. given a tag name. Matches tags in either upper or lower case,
  542. attributes with namespaces and with quoted or unquoted values.
  543. Example::
  544. text = '<td>More info at the <a href="https://github.com/pyparsing/pyparsing/wiki">pyparsing</a> wiki page</td>'
  545. # make_html_tags returns pyparsing expressions for the opening and
  546. # closing tags as a 2-tuple
  547. a, a_end = make_html_tags("A")
  548. link_expr = a + SkipTo(a_end)("link_text") + a_end
  549. for link in link_expr.search_string(text):
  550. # attributes in the <A> tag (like "href" shown here) are
  551. # also accessible as named results
  552. print(link.link_text, '->', link.href)
  553. prints::
  554. pyparsing -> https://github.com/pyparsing/pyparsing/wiki
  555. """
  556. return _makeTags(tag_str, False)
  557. def make_xml_tags(
  558. tag_str: Union[str, ParserElement]
  559. ) -> Tuple[ParserElement, ParserElement]:
  560. """Helper to construct opening and closing tag expressions for XML,
  561. given a tag name. Matches tags only in the given upper/lower case.
  562. Example: similar to :class:`make_html_tags`
  563. """
  564. return _makeTags(tag_str, True)
  565. any_open_tag, any_close_tag = make_html_tags(
  566. Word(alphas, alphanums + "_:").set_name("any tag")
  567. )
  568. _htmlEntityMap = {k.rstrip(";"): v for k, v in html.entities.html5.items()}
  569. common_html_entity = Regex("&(?P<entity>" + "|".join(_htmlEntityMap) + ");").set_name(
  570. "common HTML entity"
  571. )
  572. def replace_html_entity(t):
  573. """Helper parser action to replace common HTML entities with their special characters"""
  574. return _htmlEntityMap.get(t.entity)
  575. class OpAssoc(Enum):
  576. LEFT = 1
  577. RIGHT = 2
  578. InfixNotationOperatorArgType = Union[
  579. ParserElement, str, Tuple[Union[ParserElement, str], Union[ParserElement, str]]
  580. ]
  581. InfixNotationOperatorSpec = Union[
  582. Tuple[
  583. InfixNotationOperatorArgType,
  584. int,
  585. OpAssoc,
  586. OptionalType[ParseAction],
  587. ],
  588. Tuple[
  589. InfixNotationOperatorArgType,
  590. int,
  591. OpAssoc,
  592. ],
  593. ]
  594. def infix_notation(
  595. base_expr: ParserElement,
  596. op_list: List[InfixNotationOperatorSpec],
  597. lpar: Union[str, ParserElement] = Suppress("("),
  598. rpar: Union[str, ParserElement] = Suppress(")"),
  599. ) -> ParserElement:
  600. """Helper method for constructing grammars of expressions made up of
  601. operators working in a precedence hierarchy. Operators may be unary
  602. or binary, left- or right-associative. Parse actions can also be
  603. attached to operator expressions. The generated parser will also
  604. recognize the use of parentheses to override operator precedences
  605. (see example below).
  606. Note: if you define a deep operator list, you may see performance
  607. issues when using infix_notation. See
  608. :class:`ParserElement.enable_packrat` for a mechanism to potentially
  609. improve your parser performance.
  610. Parameters:
  611. - ``base_expr`` - expression representing the most basic operand to
  612. be used in the expression
  613. - ``op_list`` - list of tuples, one for each operator precedence level
  614. in the expression grammar; each tuple is of the form ``(op_expr,
  615. num_operands, right_left_assoc, (optional)parse_action)``, where:
  616. - ``op_expr`` is the pyparsing expression for the operator; may also
  617. be a string, which will be converted to a Literal; if ``num_operands``
  618. is 3, ``op_expr`` is a tuple of two expressions, for the two
  619. operators separating the 3 terms
  620. - ``num_operands`` is the number of terms for this operator (must be 1,
  621. 2, or 3)
  622. - ``right_left_assoc`` is the indicator whether the operator is right
  623. or left associative, using the pyparsing-defined constants
  624. ``OpAssoc.RIGHT`` and ``OpAssoc.LEFT``.
  625. - ``parse_action`` is the parse action to be associated with
  626. expressions matching this operator expression (the parse action
  627. tuple member may be omitted); if the parse action is passed
  628. a tuple or list of functions, this is equivalent to calling
  629. ``set_parse_action(*fn)``
  630. (:class:`ParserElement.set_parse_action`)
  631. - ``lpar`` - expression for matching left-parentheses
  632. (default= ``Suppress('(')``)
  633. - ``rpar`` - expression for matching right-parentheses
  634. (default= ``Suppress(')')``)
  635. Example::
  636. # simple example of four-function arithmetic with ints and
  637. # variable names
  638. integer = pyparsing_common.signed_integer
  639. varname = pyparsing_common.identifier
  640. arith_expr = infix_notation(integer | varname,
  641. [
  642. ('-', 1, OpAssoc.RIGHT),
  643. (one_of('* /'), 2, OpAssoc.LEFT),
  644. (one_of('+ -'), 2, OpAssoc.LEFT),
  645. ])
  646. arith_expr.run_tests('''
  647. 5+3*6
  648. (5+3)*6
  649. -2--11
  650. ''', full_dump=False)
  651. prints::
  652. 5+3*6
  653. [[5, '+', [3, '*', 6]]]
  654. (5+3)*6
  655. [[[5, '+', 3], '*', 6]]
  656. -2--11
  657. [[['-', 2], '-', ['-', 11]]]
  658. """
  659. # captive version of FollowedBy that does not do parse actions or capture results names
  660. class _FB(FollowedBy):
  661. def parseImpl(self, instring, loc, doActions=True):
  662. self.expr.try_parse(instring, loc)
  663. return loc, []
  664. _FB.__name__ = "FollowedBy>"
  665. ret = Forward()
  666. lpar = Suppress(lpar)
  667. rpar = Suppress(rpar)
  668. lastExpr = base_expr | (lpar + ret + rpar)
  669. for i, operDef in enumerate(op_list):
  670. opExpr, arity, rightLeftAssoc, pa = (operDef + (None,))[:4]
  671. if isinstance(opExpr, str_type):
  672. opExpr = ParserElement._literalStringClass(opExpr)
  673. if arity == 3:
  674. if not isinstance(opExpr, (tuple, list)) or len(opExpr) != 2:
  675. raise ValueError(
  676. "if numterms=3, opExpr must be a tuple or list of two expressions"
  677. )
  678. opExpr1, opExpr2 = opExpr
  679. term_name = "{}{} term".format(opExpr1, opExpr2)
  680. else:
  681. term_name = "{} term".format(opExpr)
  682. if not 1 <= arity <= 3:
  683. raise ValueError("operator must be unary (1), binary (2), or ternary (3)")
  684. if rightLeftAssoc not in (OpAssoc.LEFT, OpAssoc.RIGHT):
  685. raise ValueError("operator must indicate right or left associativity")
  686. thisExpr = Forward().set_name(term_name)
  687. if rightLeftAssoc is OpAssoc.LEFT:
  688. if arity == 1:
  689. matchExpr = _FB(lastExpr + opExpr) + Group(lastExpr + opExpr[1, ...])
  690. elif arity == 2:
  691. if opExpr is not None:
  692. matchExpr = _FB(lastExpr + opExpr + lastExpr) + Group(
  693. lastExpr + (opExpr + lastExpr)[1, ...]
  694. )
  695. else:
  696. matchExpr = _FB(lastExpr + lastExpr) + Group(lastExpr[2, ...])
  697. elif arity == 3:
  698. matchExpr = _FB(
  699. lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr
  700. ) + Group(lastExpr + OneOrMore(opExpr1 + lastExpr + opExpr2 + lastExpr))
  701. elif rightLeftAssoc is OpAssoc.RIGHT:
  702. if arity == 1:
  703. # try to avoid LR with this extra test
  704. if not isinstance(opExpr, Opt):
  705. opExpr = Opt(opExpr)
  706. matchExpr = _FB(opExpr.expr + thisExpr) + Group(opExpr + thisExpr)
  707. elif arity == 2:
  708. if opExpr is not None:
  709. matchExpr = _FB(lastExpr + opExpr + thisExpr) + Group(
  710. lastExpr + (opExpr + thisExpr)[1, ...]
  711. )
  712. else:
  713. matchExpr = _FB(lastExpr + thisExpr) + Group(
  714. lastExpr + thisExpr[1, ...]
  715. )
  716. elif arity == 3:
  717. matchExpr = _FB(
  718. lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr
  719. ) + Group(lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr)
  720. if pa:
  721. if isinstance(pa, (tuple, list)):
  722. matchExpr.set_parse_action(*pa)
  723. else:
  724. matchExpr.set_parse_action(pa)
  725. thisExpr <<= (matchExpr | lastExpr).setName(term_name)
  726. lastExpr = thisExpr
  727. ret <<= lastExpr
  728. return ret
  729. def indentedBlock(blockStatementExpr, indentStack, indent=True, backup_stacks=[]):
  730. """
  731. (DEPRECATED - use IndentedBlock class instead)
  732. Helper method for defining space-delimited indentation blocks,
  733. such as those used to define block statements in Python source code.
  734. Parameters:
  735. - ``blockStatementExpr`` - expression defining syntax of statement that
  736. is repeated within the indented block
  737. - ``indentStack`` - list created by caller to manage indentation stack
  738. (multiple ``statementWithIndentedBlock`` expressions within a single
  739. grammar should share a common ``indentStack``)
  740. - ``indent`` - boolean indicating whether block must be indented beyond
  741. the current level; set to ``False`` for block of left-most statements
  742. (default= ``True``)
  743. A valid block must contain at least one ``blockStatement``.
  744. (Note that indentedBlock uses internal parse actions which make it
  745. incompatible with packrat parsing.)
  746. Example::
  747. data = '''
  748. def A(z):
  749. A1
  750. B = 100
  751. G = A2
  752. A2
  753. A3
  754. B
  755. def BB(a,b,c):
  756. BB1
  757. def BBA():
  758. bba1
  759. bba2
  760. bba3
  761. C
  762. D
  763. def spam(x,y):
  764. def eggs(z):
  765. pass
  766. '''
  767. indentStack = [1]
  768. stmt = Forward()
  769. identifier = Word(alphas, alphanums)
  770. funcDecl = ("def" + identifier + Group("(" + Opt(delimitedList(identifier)) + ")") + ":")
  771. func_body = indentedBlock(stmt, indentStack)
  772. funcDef = Group(funcDecl + func_body)
  773. rvalue = Forward()
  774. funcCall = Group(identifier + "(" + Opt(delimitedList(rvalue)) + ")")
  775. rvalue << (funcCall | identifier | Word(nums))
  776. assignment = Group(identifier + "=" + rvalue)
  777. stmt << (funcDef | assignment | identifier)
  778. module_body = OneOrMore(stmt)
  779. parseTree = module_body.parseString(data)
  780. parseTree.pprint()
  781. prints::
  782. [['def',
  783. 'A',
  784. ['(', 'z', ')'],
  785. ':',
  786. [['A1'], [['B', '=', '100']], [['G', '=', 'A2']], ['A2'], ['A3']]],
  787. 'B',
  788. ['def',
  789. 'BB',
  790. ['(', 'a', 'b', 'c', ')'],
  791. ':',
  792. [['BB1'], [['def', 'BBA', ['(', ')'], ':', [['bba1'], ['bba2'], ['bba3']]]]]],
  793. 'C',
  794. 'D',
  795. ['def',
  796. 'spam',
  797. ['(', 'x', 'y', ')'],
  798. ':',
  799. [[['def', 'eggs', ['(', 'z', ')'], ':', [['pass']]]]]]]
  800. """
  801. backup_stacks.append(indentStack[:])
  802. def reset_stack():
  803. indentStack[:] = backup_stacks[-1]
  804. def checkPeerIndent(s, l, t):
  805. if l >= len(s):
  806. return
  807. curCol = col(l, s)
  808. if curCol != indentStack[-1]:
  809. if curCol > indentStack[-1]:
  810. raise ParseException(s, l, "illegal nesting")
  811. raise ParseException(s, l, "not a peer entry")
  812. def checkSubIndent(s, l, t):
  813. curCol = col(l, s)
  814. if curCol > indentStack[-1]:
  815. indentStack.append(curCol)
  816. else:
  817. raise ParseException(s, l, "not a subentry")
  818. def checkUnindent(s, l, t):
  819. if l >= len(s):
  820. return
  821. curCol = col(l, s)
  822. if not (indentStack and curCol in indentStack):
  823. raise ParseException(s, l, "not an unindent")
  824. if curCol < indentStack[-1]:
  825. indentStack.pop()
  826. NL = OneOrMore(LineEnd().set_whitespace_chars("\t ").suppress())
  827. INDENT = (Empty() + Empty().set_parse_action(checkSubIndent)).set_name("INDENT")
  828. PEER = Empty().set_parse_action(checkPeerIndent).set_name("")
  829. UNDENT = Empty().set_parse_action(checkUnindent).set_name("UNINDENT")
  830. if indent:
  831. smExpr = Group(
  832. Opt(NL)
  833. + INDENT
  834. + OneOrMore(PEER + Group(blockStatementExpr) + Opt(NL))
  835. + UNDENT
  836. )
  837. else:
  838. smExpr = Group(
  839. Opt(NL)
  840. + OneOrMore(PEER + Group(blockStatementExpr) + Opt(NL))
  841. + Opt(UNDENT)
  842. )
  843. # add a parse action to remove backup_stack from list of backups
  844. smExpr.add_parse_action(
  845. lambda: backup_stacks.pop(-1) and None if backup_stacks else None
  846. )
  847. smExpr.set_fail_action(lambda a, b, c, d: reset_stack())
  848. blockStatementExpr.ignore(_bslash + LineEnd())
  849. return smExpr.set_name("indented block")
  850. # it's easy to get these comment structures wrong - they're very common, so may as well make them available
  851. c_style_comment = Combine(Regex(r"/\*(?:[^*]|\*(?!/))*") + "*/").set_name(
  852. "C style comment"
  853. )
  854. "Comment of the form ``/* ... */``"
  855. html_comment = Regex(r"<!--[\s\S]*?-->").set_name("HTML comment")
  856. "Comment of the form ``<!-- ... -->``"
  857. rest_of_line = Regex(r".*").leave_whitespace().set_name("rest of line")
  858. dbl_slash_comment = Regex(r"//(?:\\\n|[^\n])*").set_name("// comment")
  859. "Comment of the form ``// ... (to end of line)``"
  860. cpp_style_comment = Combine(
  861. Regex(r"/\*(?:[^*]|\*(?!/))*") + "*/" | dbl_slash_comment
  862. ).set_name("C++ style comment")
  863. "Comment of either form :class:`c_style_comment` or :class:`dbl_slash_comment`"
  864. java_style_comment = cpp_style_comment
  865. "Same as :class:`cpp_style_comment`"
  866. python_style_comment = Regex(r"#.*").set_name("Python style comment")
  867. "Comment of the form ``# ... (to end of line)``"
  868. # build list of built-in expressions, for future reference if a global default value
  869. # gets updated
  870. _builtin_exprs = [v for v in vars().values() if isinstance(v, ParserElement)]
  871. # pre-PEP8 compatible names
  872. delimitedList = delimited_list
  873. countedArray = counted_array
  874. matchPreviousLiteral = match_previous_literal
  875. matchPreviousExpr = match_previous_expr
  876. oneOf = one_of
  877. dictOf = dict_of
  878. originalTextFor = original_text_for
  879. nestedExpr = nested_expr
  880. makeHTMLTags = make_html_tags
  881. makeXMLTags = make_xml_tags
  882. anyOpenTag, anyCloseTag = any_open_tag, any_close_tag
  883. commonHTMLEntity = common_html_entity
  884. replaceHTMLEntity = replace_html_entity
  885. opAssoc = OpAssoc
  886. infixNotation = infix_notation
  887. cStyleComment = c_style_comment
  888. htmlComment = html_comment
  889. restOfLine = rest_of_line
  890. dblSlashComment = dbl_slash_comment
  891. cppStyleComment = cpp_style_comment
  892. javaStyleComment = java_style_comment
  893. pythonStyleComment = python_style_comment