PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/rlib/parsing/regexparse.py

https://bitbucket.org/dac_io/pypy
Python | 2004 lines | 1993 code | 4 blank | 7 comment | 20 complexity | f7ca7bdc0e0d30e8f19327a0fd59ed02 MD5 | raw file
  1. import py
  2. from pypy.rlib.parsing.parsing import PackratParser, Rule
  3. from pypy.rlib.parsing.tree import Nonterminal
  4. from pypy.rlib.parsing.regex import StringExpression, RangeExpression
  5. from pypy.rlib.parsing.lexer import Lexer, DummyLexer
  6. from pypy.rlib.parsing.deterministic import compress_char_set, DFA
  7. import string
  8. set = py.builtin.set
  9. ESCAPES = {
  10. "a": "\a",
  11. "b": "\b",
  12. "e": "\x1b",
  13. "f": "\f",
  14. "n": "\n",
  15. "r": "\r",
  16. "t": "\t",
  17. "v": "\v",
  18. }
  19. for i in range(256):
  20. # Add the ctrl-x types:
  21. # Rule, according to PCRE:
  22. # if x is a lower case letter, it is converted to upper case.
  23. # Then bit 6 of the character (hex 40) is inverted.
  24. # Thus, \cz => 0x1A, \c{ => 0x3B, \c; => 0x7B.
  25. escaped = "c%s" % chr(i)
  26. ESCAPES[escaped] = chr(ord(chr(i).upper()) ^ 0x40)
  27. def unescape_muncher(string):
  28. """Return a tuple, representing the first character of the string
  29. (appropriately unescaped) and the rest of the string that wasn't
  30. handled."""
  31. if string[0] != '\\':
  32. # Not an escape character
  33. return string[0], string[1:]
  34. if string[1] == 'x':
  35. # Hex char, must have two hex digits
  36. char = chr(int(string[2:4], 16))
  37. return char, string[4:]
  38. if string[1] in '01234567':
  39. # Octal number, up to three digits long
  40. span = 2
  41. span += (span < len(string)) and (string[span] in '01234567')
  42. span += (span < len(string)) and (string[span] in '01234567')
  43. char = chr(int(string[1:span], 8))
  44. return char, string[span:]
  45. if string[1] == 'c':
  46. # Special \cx types
  47. return ESCAPES['c'+string[2]], string[3:]
  48. if string[1] in ESCAPES:
  49. # Special escapes are in ESCAPE
  50. return ESCAPES[string[1]], string[2:]
  51. # Otherwise, it's just the character it's meant to be (e.g., '\.')
  52. return string[1], string[2:]
  53. def unescape(s):
  54. """Unescape a whole string."""
  55. result = []
  56. while s:
  57. char, s = unescape_muncher(s)
  58. result.append(char)
  59. return "".join(result)
  60. syntax = r"""
  61. EOF:
  62. !__any__;
  63. parse:
  64. regex
  65. [EOF];
  66. regex:
  67. r1 = concatenation
  68. '|'
  69. r2 = regex
  70. return {r1 | r2}
  71. | concatenation;
  72. concatenation:
  73. l = repetition*
  74. return {reduce(operator.add, l, regex.StringExpression(""))};
  75. repetition:
  76. r1 = primary
  77. '*'
  78. return {r1.kleene()}
  79. | r1 = primary
  80. '+'
  81. return {r1 + r1.kleene()}
  82. | r1 = primary
  83. '?'
  84. return {regex.StringExpression("") | r1}
  85. | r1 = primary
  86. '{'
  87. n = clippednumrange
  88. '}'
  89. return {r1 * n + r1.kleene()}
  90. | r1 = primary
  91. '{'
  92. n = numrange
  93. '}'
  94. return {r1 * n[0] + reduce(operator.or_, [r1 * i for i in range(n[1] - n[0] + 1)], regex.StringExpression(""))}
  95. | '{'
  96. return {regex.StringExpression("{")}
  97. | primary;
  98. primary:
  99. ['('] regex [')']
  100. | range
  101. | cc = charclass
  102. return {reduce(operator.or_, [regex.RangeExpression(a, chr(ord(a) + b - 1)) for a, b in compress_char_set(cc)])}
  103. | c = char
  104. return {regex.StringExpression(c)}
  105. | '.'
  106. return {regex.RangeExpression(chr(0), chr(255))}
  107. | '-'
  108. return {regex.StringExpression('-')}
  109. | '\'
  110. return {regex.StringExpression('\\')}
  111. | ']'
  112. return {regex.StringExpression(']')};
  113. char:
  114. c = QUOTEDCHAR
  115. return {unescape(c)}
  116. | c = CHAR
  117. return {c};
  118. QUOTEDCHAR:
  119. `(\\x[0-9a-fA-F]{2})|(\\[0-3]?[0-7][0-7])|(\\c.)|(\\[^dswDSW])`;
  120. CHAR:
  121. `[^\*\+\(\)\[\]\{\|\.\-\?\^\\]`;
  122. range:
  123. '['
  124. s = rangeinner
  125. ']'
  126. return {reduce(operator.or_, [regex.RangeExpression(a, chr(ord(a) + b - 1)) for a, b in compress_char_set(s)])};
  127. rangeinner:
  128. '^'
  129. s = subrange
  130. return {set([chr(c) for c in range(256)]) - s}
  131. | subrange;
  132. subrange:
  133. ']'
  134. l = rangeelement*
  135. return {reduce(operator.or_, [set(["]"])] + l)}
  136. | l = rangeelement+
  137. return {reduce(operator.or_, l)};
  138. rangeelement:
  139. charclass
  140. | c1 = char
  141. '-'
  142. c2 = char
  143. return {set([chr(i) for i in range(ord(c1), ord(c2) + 1)])}
  144. | '.'
  145. return { set(['.']) }
  146. | '*'
  147. return { set(['*']) }
  148. | '+'
  149. return { set(['+']) }
  150. | '?'
  151. return { set(['?']) }
  152. | '-'
  153. return { set(['-']) }
  154. | '['
  155. return { set(['[']) }
  156. | c = char
  157. return { set([c]) };
  158. numrange:
  159. n1 = NUM
  160. ','
  161. n2 = NUM
  162. return {n1, n2}
  163. | n1 = NUM
  164. return {n1, n1};
  165. clippednumrange:
  166. n1 = NUM
  167. ','
  168. return {n1};
  169. charclass:
  170. '\' 'd'
  171. return { set([chr(c) for c in range(ord('0'), ord('9')+1)]) }
  172. | '\' 's'
  173. return { set(['\t', '\n', '\f', '\r', ' ']) }
  174. | '\' 'w'
  175. return { set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_']) }
  176. | '\' 'D'
  177. return { set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('0'), ord('9')+1)]) }
  178. | '\' 'S'
  179. return { set([chr(c) for c in range(256)]) - set(['\t', '\n', '\f', '\r', ' ']) }
  180. | '\' 'W'
  181. return { set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_'])};
  182. NUM:
  183. c = `0|([1-9][0-9]*)`
  184. return {int(c)};
  185. """
  186. def parse_regex(s):
  187. p = RegexParser(s)
  188. r = p.parse()
  189. return r
  190. def make_runner(regex, view=False):
  191. r = parse_regex(regex)
  192. nfa = r.make_automaton()
  193. dfa = nfa.make_deterministic()
  194. if view:
  195. dfa.view()
  196. dfa.optimize()
  197. if view:
  198. dfa.view()
  199. r = dfa.get_runner()
  200. return r
  201. # generated code between this line and its other occurence
  202. from pypy.rlib.parsing.pypackrat import PackratParser, Status
  203. from pypy.rlib.parsing.pypackrat import BacktrackException
  204. from pypy.rlib.parsing import regex
  205. import operator
  206. class Parser(object):
  207. def EOF(self):
  208. return self._EOF().result
  209. def _EOF(self):
  210. _key = self._pos
  211. _status = self._dict_EOF.get(_key, None)
  212. if _status is None:
  213. _status = self._dict_EOF[_key] = Status()
  214. else:
  215. _statusstatus = _status.status
  216. if _statusstatus == _status.NORMAL:
  217. self._pos = _status.pos
  218. return _status
  219. elif _statusstatus == _status.ERROR:
  220. raise BacktrackException(_status.error)
  221. elif (_statusstatus == _status.INPROGRESS or
  222. _statusstatus == _status.LEFTRECURSION):
  223. _status.status = _status.LEFTRECURSION
  224. if _status.result is not None:
  225. self._pos = _status.pos
  226. return _status
  227. else:
  228. raise BacktrackException(None)
  229. elif _statusstatus == _status.SOMESOLUTIONS:
  230. _status.status = _status.INPROGRESS
  231. _startingpos = self._pos
  232. try:
  233. _result = None
  234. _error = None
  235. _choice0 = self._pos
  236. _stored_result1 = _result
  237. try:
  238. _result = self.__any__()
  239. except BacktrackException:
  240. self._pos = _choice0
  241. _result = _stored_result1
  242. else:
  243. raise BacktrackException(None)
  244. if _status.status == _status.LEFTRECURSION:
  245. if _status.result is not None:
  246. if _status.pos >= self._pos:
  247. _status.status = _status.NORMAL
  248. self._pos = _status.pos
  249. return _status
  250. _status.pos = self._pos
  251. _status.status = _status.SOMESOLUTIONS
  252. _status.result = _result
  253. _status.error = _error
  254. self._pos = _startingpos
  255. return self._EOF()
  256. _status.status = _status.NORMAL
  257. _status.pos = self._pos
  258. _status.result = _result
  259. _status.error = _error
  260. return _status
  261. except BacktrackException, _exc:
  262. _status.pos = -1
  263. _status.result = None
  264. _error = _exc.error
  265. _status.error = _error
  266. _status.status = _status.ERROR
  267. raise BacktrackException(_error)
  268. def parse(self):
  269. return self._parse().result
  270. def _parse(self):
  271. _key = self._pos
  272. _status = self._dict_parse.get(_key, None)
  273. if _status is None:
  274. _status = self._dict_parse[_key] = Status()
  275. else:
  276. _statusstatus = _status.status
  277. if _statusstatus == _status.NORMAL:
  278. self._pos = _status.pos
  279. return _status
  280. elif _statusstatus == _status.ERROR:
  281. raise BacktrackException(_status.error)
  282. elif (_statusstatus == _status.INPROGRESS or
  283. _statusstatus == _status.LEFTRECURSION):
  284. _status.status = _status.LEFTRECURSION
  285. if _status.result is not None:
  286. self._pos = _status.pos
  287. return _status
  288. else:
  289. raise BacktrackException(None)
  290. elif _statusstatus == _status.SOMESOLUTIONS:
  291. _status.status = _status.INPROGRESS
  292. _startingpos = self._pos
  293. try:
  294. _result = None
  295. _error = None
  296. _call_status = self._regex()
  297. _result = _call_status.result
  298. _error = _call_status.error
  299. _before_discard0 = _result
  300. _call_status = self._EOF()
  301. _result = _call_status.result
  302. _error = self._combine_errors(_error, _call_status.error)
  303. _result = _before_discard0
  304. if _status.status == _status.LEFTRECURSION:
  305. if _status.result is not None:
  306. if _status.pos >= self._pos:
  307. _status.status = _status.NORMAL
  308. self._pos = _status.pos
  309. return _status
  310. _status.pos = self._pos
  311. _status.status = _status.SOMESOLUTIONS
  312. _status.result = _result
  313. _status.error = _error
  314. self._pos = _startingpos
  315. return self._parse()
  316. _status.status = _status.NORMAL
  317. _status.pos = self._pos
  318. _status.result = _result
  319. _status.error = _error
  320. return _status
  321. except BacktrackException, _exc:
  322. _status.pos = -1
  323. _status.result = None
  324. _error = self._combine_errors(_error, _exc.error)
  325. _status.error = _error
  326. _status.status = _status.ERROR
  327. raise BacktrackException(_error)
  328. def regex(self):
  329. return self._regex().result
  330. def _regex(self):
  331. _key = self._pos
  332. _status = self._dict_regex.get(_key, None)
  333. if _status is None:
  334. _status = self._dict_regex[_key] = Status()
  335. else:
  336. _statusstatus = _status.status
  337. if _statusstatus == _status.NORMAL:
  338. self._pos = _status.pos
  339. return _status
  340. elif _statusstatus == _status.ERROR:
  341. raise BacktrackException(_status.error)
  342. elif (_statusstatus == _status.INPROGRESS or
  343. _statusstatus == _status.LEFTRECURSION):
  344. _status.status = _status.LEFTRECURSION
  345. if _status.result is not None:
  346. self._pos = _status.pos
  347. return _status
  348. else:
  349. raise BacktrackException(None)
  350. elif _statusstatus == _status.SOMESOLUTIONS:
  351. _status.status = _status.INPROGRESS
  352. _startingpos = self._pos
  353. try:
  354. _result = None
  355. _error = None
  356. while 1:
  357. _choice0 = self._pos
  358. try:
  359. _call_status = self._concatenation()
  360. _result = _call_status.result
  361. _error = _call_status.error
  362. r1 = _result
  363. _result = self.__chars__('|')
  364. _call_status = self._regex()
  365. _result = _call_status.result
  366. _error = self._combine_errors(_error, _call_status.error)
  367. r2 = _result
  368. _result = (r1 | r2)
  369. break
  370. except BacktrackException, _exc:
  371. _error = self._combine_errors(_error, _exc.error)
  372. self._pos = _choice0
  373. _choice1 = self._pos
  374. try:
  375. _call_status = self._concatenation()
  376. _result = _call_status.result
  377. _error = self._combine_errors(_error, _call_status.error)
  378. break
  379. except BacktrackException, _exc:
  380. _error = self._combine_errors(_error, _exc.error)
  381. self._pos = _choice1
  382. raise BacktrackException(_error)
  383. _call_status = self._concatenation()
  384. _result = _call_status.result
  385. _error = self._combine_errors(_error, _call_status.error)
  386. break
  387. if _status.status == _status.LEFTRECURSION:
  388. if _status.result is not None:
  389. if _status.pos >= self._pos:
  390. _status.status = _status.NORMAL
  391. self._pos = _status.pos
  392. return _status
  393. _status.pos = self._pos
  394. _status.status = _status.SOMESOLUTIONS
  395. _status.result = _result
  396. _status.error = _error
  397. self._pos = _startingpos
  398. return self._regex()
  399. _status.status = _status.NORMAL
  400. _status.pos = self._pos
  401. _status.result = _result
  402. _status.error = _error
  403. return _status
  404. except BacktrackException, _exc:
  405. _status.pos = -1
  406. _status.result = None
  407. _error = self._combine_errors(_error, _exc.error)
  408. _status.error = _error
  409. _status.status = _status.ERROR
  410. raise BacktrackException(_error)
  411. def concatenation(self):
  412. return self._concatenation().result
  413. def _concatenation(self):
  414. _key = self._pos
  415. _status = self._dict_concatenation.get(_key, None)
  416. if _status is None:
  417. _status = self._dict_concatenation[_key] = Status()
  418. else:
  419. _statusstatus = _status.status
  420. if _statusstatus == _status.NORMAL:
  421. self._pos = _status.pos
  422. return _status
  423. elif _statusstatus == _status.ERROR:
  424. raise BacktrackException(_status.error)
  425. elif (_statusstatus == _status.INPROGRESS or
  426. _statusstatus == _status.LEFTRECURSION):
  427. _status.status = _status.LEFTRECURSION
  428. if _status.result is not None:
  429. self._pos = _status.pos
  430. return _status
  431. else:
  432. raise BacktrackException(None)
  433. elif _statusstatus == _status.SOMESOLUTIONS:
  434. _status.status = _status.INPROGRESS
  435. _startingpos = self._pos
  436. try:
  437. _result = None
  438. _error = None
  439. _all0 = []
  440. while 1:
  441. _choice1 = self._pos
  442. try:
  443. _call_status = self._repetition()
  444. _result = _call_status.result
  445. _error = _call_status.error
  446. _all0.append(_result)
  447. except BacktrackException, _exc:
  448. _error = self._combine_errors(_error, _exc.error)
  449. self._pos = _choice1
  450. break
  451. _result = _all0
  452. l = _result
  453. _result = (reduce(operator.add, l, regex.StringExpression("")))
  454. if _status.status == _status.LEFTRECURSION:
  455. if _status.result is not None:
  456. if _status.pos >= self._pos:
  457. _status.status = _status.NORMAL
  458. self._pos = _status.pos
  459. return _status
  460. _status.pos = self._pos
  461. _status.status = _status.SOMESOLUTIONS
  462. _status.result = _result
  463. _status.error = _error
  464. self._pos = _startingpos
  465. return self._concatenation()
  466. _status.status = _status.NORMAL
  467. _status.pos = self._pos
  468. _status.result = _result
  469. _status.error = _error
  470. return _status
  471. except BacktrackException, _exc:
  472. _status.pos = -1
  473. _status.result = None
  474. _error = self._combine_errors(_error, _exc.error)
  475. _status.error = _error
  476. _status.status = _status.ERROR
  477. raise BacktrackException(_error)
  478. def repetition(self):
  479. return self._repetition().result
  480. def _repetition(self):
  481. _key = self._pos
  482. _status = self._dict_repetition.get(_key, None)
  483. if _status is None:
  484. _status = self._dict_repetition[_key] = Status()
  485. else:
  486. _statusstatus = _status.status
  487. if _statusstatus == _status.NORMAL:
  488. self._pos = _status.pos
  489. return _status
  490. elif _statusstatus == _status.ERROR:
  491. raise BacktrackException(_status.error)
  492. elif (_statusstatus == _status.INPROGRESS or
  493. _statusstatus == _status.LEFTRECURSION):
  494. _status.status = _status.LEFTRECURSION
  495. if _status.result is not None:
  496. self._pos = _status.pos
  497. return _status
  498. else:
  499. raise BacktrackException(None)
  500. elif _statusstatus == _status.SOMESOLUTIONS:
  501. _status.status = _status.INPROGRESS
  502. _startingpos = self._pos
  503. try:
  504. _result = None
  505. _error = None
  506. while 1:
  507. _choice0 = self._pos
  508. try:
  509. _call_status = self._primary()
  510. _result = _call_status.result
  511. _error = _call_status.error
  512. r1 = _result
  513. _result = self.__chars__('*')
  514. _result = (r1.kleene())
  515. break
  516. except BacktrackException, _exc:
  517. _error = self._combine_errors(_error, _exc.error)
  518. self._pos = _choice0
  519. _choice1 = self._pos
  520. try:
  521. _call_status = self._primary()
  522. _result = _call_status.result
  523. _error = self._combine_errors(_error, _call_status.error)
  524. r1 = _result
  525. _result = self.__chars__('+')
  526. _result = (r1 + r1.kleene())
  527. break
  528. except BacktrackException, _exc:
  529. _error = self._combine_errors(_error, _exc.error)
  530. self._pos = _choice1
  531. _choice2 = self._pos
  532. try:
  533. _call_status = self._primary()
  534. _result = _call_status.result
  535. _error = self._combine_errors(_error, _call_status.error)
  536. r1 = _result
  537. _result = self.__chars__('?')
  538. _result = (regex.StringExpression("") | r1)
  539. break
  540. except BacktrackException, _exc:
  541. _error = self._combine_errors(_error, _exc.error)
  542. self._pos = _choice2
  543. _choice3 = self._pos
  544. try:
  545. _call_status = self._primary()
  546. _result = _call_status.result
  547. _error = self._combine_errors(_error, _call_status.error)
  548. r1 = _result
  549. _result = self.__chars__('{')
  550. _call_status = self._clippednumrange()
  551. _result = _call_status.result
  552. _error = self._combine_errors(_error, _call_status.error)
  553. n = _result
  554. _result = self.__chars__('}')
  555. _result = (r1 * n + r1.kleene())
  556. break
  557. except BacktrackException, _exc:
  558. _error = self._combine_errors(_error, _exc.error)
  559. self._pos = _choice3
  560. _choice4 = self._pos
  561. try:
  562. _call_status = self._primary()
  563. _result = _call_status.result
  564. _error = self._combine_errors(_error, _call_status.error)
  565. r1 = _result
  566. _result = self.__chars__('{')
  567. _call_status = self._numrange()
  568. _result = _call_status.result
  569. _error = self._combine_errors(_error, _call_status.error)
  570. n = _result
  571. _result = self.__chars__('}')
  572. _result = (r1 * n[0] + reduce(operator.or_, [r1 * i for i in range(n[1] - n[0] + 1)], regex.StringExpression("")))
  573. break
  574. except BacktrackException, _exc:
  575. _error = self._combine_errors(_error, _exc.error)
  576. self._pos = _choice4
  577. _choice5 = self._pos
  578. try:
  579. _result = self.__chars__('{')
  580. _result = (regex.StringExpression("{"))
  581. break
  582. except BacktrackException, _exc:
  583. _error = self._combine_errors(_error, _exc.error)
  584. self._pos = _choice5
  585. _choice6 = self._pos
  586. try:
  587. _call_status = self._primary()
  588. _result = _call_status.result
  589. _error = self._combine_errors(_error, _call_status.error)
  590. break
  591. except BacktrackException, _exc:
  592. _error = self._combine_errors(_error, _exc.error)
  593. self._pos = _choice6
  594. raise BacktrackException(_error)
  595. _call_status = self._primary()
  596. _result = _call_status.result
  597. _error = self._combine_errors(_error, _call_status.error)
  598. break
  599. if _status.status == _status.LEFTRECURSION:
  600. if _status.result is not None:
  601. if _status.pos >= self._pos:
  602. _status.status = _status.NORMAL
  603. self._pos = _status.pos
  604. return _status
  605. _status.pos = self._pos
  606. _status.status = _status.SOMESOLUTIONS
  607. _status.result = _result
  608. _status.error = _error
  609. self._pos = _startingpos
  610. return self._repetition()
  611. _status.status = _status.NORMAL
  612. _status.pos = self._pos
  613. _status.result = _result
  614. _status.error = _error
  615. return _status
  616. except BacktrackException, _exc:
  617. _status.pos = -1
  618. _status.result = None
  619. _error = self._combine_errors(_error, _exc.error)
  620. _status.error = _error
  621. _status.status = _status.ERROR
  622. raise BacktrackException(_error)
  623. def primary(self):
  624. return self._primary().result
  625. def _primary(self):
  626. _key = self._pos
  627. _status = self._dict_primary.get(_key, None)
  628. if _status is None:
  629. _status = self._dict_primary[_key] = Status()
  630. else:
  631. _statusstatus = _status.status
  632. if _statusstatus == _status.NORMAL:
  633. self._pos = _status.pos
  634. return _status
  635. elif _statusstatus == _status.ERROR:
  636. raise BacktrackException(_status.error)
  637. elif (_statusstatus == _status.INPROGRESS or
  638. _statusstatus == _status.LEFTRECURSION):
  639. _status.status = _status.LEFTRECURSION
  640. if _status.result is not None:
  641. self._pos = _status.pos
  642. return _status
  643. else:
  644. raise BacktrackException(None)
  645. elif _statusstatus == _status.SOMESOLUTIONS:
  646. _status.status = _status.INPROGRESS
  647. _startingpos = self._pos
  648. try:
  649. _result = None
  650. _error = None
  651. while 1:
  652. _choice0 = self._pos
  653. try:
  654. _before_discard1 = _result
  655. _result = self.__chars__('(')
  656. _result = _before_discard1
  657. _call_status = self._regex()
  658. _result = _call_status.result
  659. _error = _call_status.error
  660. _before_discard2 = _result
  661. _result = self.__chars__(')')
  662. _result = _before_discard2
  663. break
  664. except BacktrackException, _exc:
  665. _error = self._combine_errors(_error, _exc.error)
  666. self._pos = _choice0
  667. _choice3 = self._pos
  668. try:
  669. _call_status = self._range()
  670. _result = _call_status.result
  671. _error = self._combine_errors(_error, _call_status.error)
  672. break
  673. except BacktrackException, _exc:
  674. _error = self._combine_errors(_error, _exc.error)
  675. self._pos = _choice3
  676. _choice4 = self._pos
  677. try:
  678. _call_status = self._charclass()
  679. _result = _call_status.result
  680. _error = self._combine_errors(_error, _call_status.error)
  681. cc = _result
  682. _result = (reduce(operator.or_, [regex.RangeExpression(a, chr(ord(a) + b - 1)) for a, b in compress_char_set(cc)]))
  683. break
  684. except BacktrackException, _exc:
  685. _error = self._combine_errors(_error, _exc.error)
  686. self._pos = _choice4
  687. _choice5 = self._pos
  688. try:
  689. _call_status = self._char()
  690. _result = _call_status.result
  691. _error = self._combine_errors(_error, _call_status.error)
  692. c = _result
  693. _result = (regex.StringExpression(c))
  694. break
  695. except BacktrackException, _exc:
  696. _error = self._combine_errors(_error, _exc.error)
  697. self._pos = _choice5
  698. _choice6 = self._pos
  699. try:
  700. _result = self.__chars__('.')
  701. _result = (regex.RangeExpression(chr(0), chr(255)))
  702. break
  703. except BacktrackException, _exc:
  704. _error = self._combine_errors(_error, _exc.error)
  705. self._pos = _choice6
  706. _choice7 = self._pos
  707. try:
  708. _result = self.__chars__('-')
  709. _result = (regex.StringExpression('-'))
  710. break
  711. except BacktrackException, _exc:
  712. _error = self._combine_errors(_error, _exc.error)
  713. self._pos = _choice7
  714. _choice8 = self._pos
  715. try:
  716. _result = self.__chars__('\\')
  717. _result = (regex.StringExpression('\\'))
  718. break
  719. except BacktrackException, _exc:
  720. _error = self._combine_errors(_error, _exc.error)
  721. self._pos = _choice8
  722. _choice9 = self._pos
  723. try:
  724. _result = self.__chars__(']')
  725. _result = (regex.StringExpression(']'))
  726. break
  727. except BacktrackException, _exc:
  728. _error = self._combine_errors(_error, _exc.error)
  729. self._pos = _choice9
  730. raise BacktrackException(_error)
  731. _result = self.__chars__(']')
  732. _result = (regex.StringExpression(']'))
  733. break
  734. if _status.status == _status.LEFTRECURSION:
  735. if _status.result is not None:
  736. if _status.pos >= self._pos:
  737. _status.status = _status.NORMAL
  738. self._pos = _status.pos
  739. return _status
  740. _status.pos = self._pos
  741. _status.status = _status.SOMESOLUTIONS
  742. _status.result = _result
  743. _status.error = _error
  744. self._pos = _startingpos
  745. return self._primary()
  746. _status.status = _status.NORMAL
  747. _status.pos = self._pos
  748. _status.result = _result
  749. _status.error = _error
  750. return _status
  751. except BacktrackException, _exc:
  752. _status.pos = -1
  753. _status.result = None
  754. _error = self._combine_errors(_error, _exc.error)
  755. _status.error = _error
  756. _status.status = _status.ERROR
  757. raise BacktrackException(_error)
  758. def char(self):
  759. return self._char().result
  760. def _char(self):
  761. _key = self._pos
  762. _status = self._dict_char.get(_key, None)
  763. if _status is None:
  764. _status = self._dict_char[_key] = Status()
  765. else:
  766. _statusstatus = _status.status
  767. if _statusstatus == _status.NORMAL:
  768. self._pos = _status.pos
  769. return _status
  770. elif _statusstatus == _status.ERROR:
  771. raise BacktrackException(_status.error)
  772. elif (_statusstatus == _status.INPROGRESS or
  773. _statusstatus == _status.LEFTRECURSION):
  774. _status.status = _status.LEFTRECURSION
  775. if _status.result is not None:
  776. self._pos = _status.pos
  777. return _status
  778. else:
  779. raise BacktrackException(None)
  780. elif _statusstatus == _status.SOMESOLUTIONS:
  781. _status.status = _status.INPROGRESS
  782. _startingpos = self._pos
  783. try:
  784. _result = None
  785. _error = None
  786. while 1:
  787. _choice0 = self._pos
  788. try:
  789. _call_status = self._QUOTEDCHAR()
  790. _result = _call_status.result
  791. _error = _call_status.error
  792. c = _result
  793. _result = (unescape(c))
  794. break
  795. except BacktrackException, _exc:
  796. _error = self._combine_errors(_error, _exc.error)
  797. self._pos = _choice0
  798. _choice1 = self._pos
  799. try:
  800. _call_status = self._CHAR()
  801. _result = _call_status.result
  802. _error = self._combine_errors(_error, _call_status.error)
  803. c = _result
  804. _result = (c)
  805. break
  806. except BacktrackException, _exc:
  807. _error = self._combine_errors(_error, _exc.error)
  808. self._pos = _choice1
  809. raise BacktrackException(_error)
  810. _call_status = self._CHAR()
  811. _result = _call_status.result
  812. _error = self._combine_errors(_error, _call_status.error)
  813. c = _result
  814. _result = (c)
  815. break
  816. if _status.status == _status.LEFTRECURSION:
  817. if _status.result is not None:
  818. if _status.pos >= self._pos:
  819. _status.status = _status.NORMAL
  820. self._pos = _status.pos
  821. return _status
  822. _status.pos = self._pos
  823. _status.status = _status.SOMESOLUTIONS
  824. _status.result = _result
  825. _status.error = _error
  826. self._pos = _startingpos
  827. return self._char()
  828. _status.status = _status.NORMAL
  829. _status.pos = self._pos
  830. _status.result = _result
  831. _status.error = _error
  832. return _status
  833. except BacktrackException, _exc:
  834. _status.pos = -1
  835. _status.result = None
  836. _error = self._combine_errors(_error, _exc.error)
  837. _status.error = _error
  838. _status.status = _status.ERROR
  839. raise BacktrackException(_error)
  840. def QUOTEDCHAR(self):
  841. return self._QUOTEDCHAR().result
  842. def _QUOTEDCHAR(self):
  843. _key = self._pos
  844. _status = self._dict_QUOTEDCHAR.get(_key, None)
  845. if _status is None:
  846. _status = self._dict_QUOTEDCHAR[_key] = Status()
  847. else:
  848. _statusstatus = _status.status
  849. if _statusstatus == _status.NORMAL:
  850. self._pos = _status.pos
  851. return _status
  852. elif _statusstatus == _status.ERROR:
  853. raise BacktrackException(_status.error)
  854. _startingpos = self._pos
  855. try:
  856. _result = None
  857. _error = None
  858. _result = self._regex1423754537()
  859. assert _status.status != _status.LEFTRECURSION
  860. _status.status = _status.NORMAL
  861. _status.pos = self._pos
  862. _status.result = _result
  863. _status.error = _error
  864. return _status
  865. except BacktrackException, _exc:
  866. _status.pos = -1
  867. _status.result = None
  868. _error = _exc.error
  869. _status.error = _error
  870. _status.status = _status.ERROR
  871. raise BacktrackException(_error)
  872. def CHAR(self):
  873. return self._CHAR().result
  874. def _CHAR(self):
  875. _key = self._pos
  876. _status = self._dict_CHAR.get(_key, None)
  877. if _status is None:
  878. _status = self._dict_CHAR[_key] = Status()
  879. else:
  880. _statusstatus = _status.status
  881. if _statusstatus == _status.NORMAL:
  882. self._pos = _status.pos
  883. return _status
  884. elif _statusstatus == _status.ERROR:
  885. raise BacktrackException(_status.error)
  886. _startingpos = self._pos
  887. try:
  888. _result = None
  889. _error = None
  890. _result = self._regex2132196932()
  891. assert _status.status != _status.LEFTRECURSION
  892. _status.status = _status.NORMAL
  893. _status.pos = self._pos
  894. _status.result = _result
  895. _status.error = _error
  896. return _status
  897. except BacktrackException, _exc:
  898. _status.pos = -1
  899. _status.result = None
  900. _error = _exc.error
  901. _status.error = _error
  902. _status.status = _status.ERROR
  903. raise BacktrackException(_error)
  904. def range(self):
  905. return self._range().result
  906. def _range(self):
  907. _key = self._pos
  908. _status = self._dict_range.get(_key, None)
  909. if _status is None:
  910. _status = self._dict_range[_key] = Status()
  911. else:
  912. _statusstatus = _status.status
  913. if _statusstatus == _status.NORMAL:
  914. self._pos = _status.pos
  915. return _status
  916. elif _statusstatus == _status.ERROR:
  917. raise BacktrackException(_status.error)
  918. elif (_statusstatus == _status.INPROGRESS or
  919. _statusstatus == _status.LEFTRECURSION):
  920. _status.status = _status.LEFTRECURSION
  921. if _status.result is not None:
  922. self._pos = _status.pos
  923. return _status
  924. else:
  925. raise BacktrackException(None)
  926. elif _statusstatus == _status.SOMESOLUTIONS:
  927. _status.status = _status.INPROGRESS
  928. _startingpos = self._pos
  929. try:
  930. _result = None
  931. _error = None
  932. _result = self.__chars__('[')
  933. _call_status = self._rangeinner()
  934. _result = _call_status.result
  935. _error = _call_status.error
  936. s = _result
  937. _result = self.__chars__(']')
  938. _result = (reduce(operator.or_, [regex.RangeExpression(a, chr(ord(a) + b - 1)) for a, b in compress_char_set(s)]))
  939. if _status.status == _status.LEFTRECURSION:
  940. if _status.result is not None:
  941. if _status.pos >= self._pos:
  942. _status.status = _status.NORMAL
  943. self._pos = _status.pos
  944. return _status
  945. _status.pos = self._pos
  946. _status.status = _status.SOMESOLUTIONS
  947. _status.result = _result
  948. _status.error = _error
  949. self._pos = _startingpos
  950. return self._range()
  951. _status.status = _status.NORMAL
  952. _status.pos = self._pos
  953. _status.result = _result
  954. _status.error = _error
  955. return _status
  956. except BacktrackException, _exc:
  957. _status.pos = -1
  958. _status.result = None
  959. _error = self._combine_errors(_error, _exc.error)
  960. _status.error = _error
  961. _status.status = _status.ERROR
  962. raise BacktrackException(_error)
  963. def rangeinner(self):
  964. return self._rangeinner().result
  965. def _rangeinner(self):
  966. _key = self._pos
  967. _status = self._dict_rangeinner.get(_key, None)
  968. if _status is None:
  969. _status = self._dict_rangeinner[_key] = Status()
  970. else:
  971. _statusstatus = _status.status
  972. if _statusstatus == _status.NORMAL:
  973. self._pos = _status.pos
  974. return _status
  975. elif _statusstatus == _status.ERROR:
  976. raise BacktrackException(_status.error)
  977. elif (_statusstatus == _status.INPROGRESS or
  978. _statusstatus == _status.LEFTRECURSION):
  979. _status.status = _status.LEFTRECURSION
  980. if _status.result is not None:
  981. self._pos = _status.pos
  982. return _status
  983. else:
  984. raise BacktrackException(None)
  985. elif _statusstatus == _status.SOMESOLUTIONS:
  986. _status.status = _status.INPROGRESS
  987. _startingpos = self._pos
  988. try:
  989. _result = None
  990. _error = None
  991. while 1:
  992. _choice0 = self._pos
  993. try:
  994. _result = self.__chars__('^')
  995. _call_status = self._subrange()
  996. _result = _call_status.result
  997. _error = _call_status.error
  998. s = _result
  999. _result = (set([chr(c) for c in range(256)]) - s)
  1000. break
  1001. except BacktrackException, _exc:
  1002. _error = self._combine_errors(_error, _exc.error)
  1003. self._pos = _choice0
  1004. _choice1 = self._pos
  1005. try:
  1006. _call_status = self._subrange()
  1007. _result = _call_status.result
  1008. _error = self._combine_errors(_error, _call_status.error)
  1009. break
  1010. except BacktrackException, _exc:
  1011. _error = self._combine_errors(_error, _exc.error)
  1012. self._pos = _choice1
  1013. raise BacktrackException(_error)
  1014. _call_status = self._subrange()
  1015. _result = _call_status.result
  1016. _error = self._combine_errors(_error, _call_status.error)
  1017. break
  1018. if _status.status == _status.LEFTRECURSION:
  1019. if _status.result is not None:
  1020. if _status.pos >= self._pos:
  1021. _status.status = _status.NORMAL
  1022. self._pos = _status.pos
  1023. return _status
  1024. _status.pos = self._pos
  1025. _status.status = _status.SOMESOLUTIONS
  1026. _status.result = _result
  1027. _status.error = _error
  1028. self._pos = _startingpos
  1029. return self._rangeinner()
  1030. _status.status = _status.NORMAL
  1031. _status.pos = self._pos
  1032. _status.result = _result
  1033. _status.error = _error
  1034. return _status
  1035. except BacktrackException, _exc:
  1036. _status.pos = -1
  1037. _status.result = None
  1038. _error = self._combine_errors(_error, _exc.error)
  1039. _status.error = _error
  1040. _status.status = _status.ERROR
  1041. raise BacktrackException(_error)
  1042. def subrange(self):
  1043. return self._subrange().result
  1044. def _subrange(self):
  1045. _key = self._pos
  1046. _status = self._dict_subrange.get(_key, None)
  1047. if _status is None:
  1048. _status = self._dict_subrange[_key] = Status()
  1049. else:
  1050. _statusstatus = _status.status
  1051. if _statusstatus == _status.NORMAL:
  1052. self._pos = _status.pos
  1053. return _status
  1054. elif _statusstatus == _status.ERROR:
  1055. raise BacktrackException(_status.error)
  1056. elif (_statusstatus == _status.INPROGRESS or
  1057. _statusstatus == _status.LEFTRECURSION):
  1058. _status.status = _status.LEFTRECURSION
  1059. if _status.result is not None:
  1060. self._pos = _status.pos
  1061. return _status
  1062. else:
  1063. raise BacktrackException(None)
  1064. elif _statusstatus == _status.SOMESOLUTIONS:
  1065. _status.status = _status.INPROGRESS
  1066. _startingpos = self._pos
  1067. try:
  1068. _result = None
  1069. _error = None
  1070. while 1:
  1071. _choice0 = self._pos
  1072. try:
  1073. _result = self.__chars__(']')
  1074. _all1 = []
  1075. while 1:
  1076. _choice2 = self._pos
  1077. try:
  1078. _call_status = self._rangeelement()
  1079. _result = _call_status.result
  1080. _error = _call_status.error
  1081. _all1.append(_result)
  1082. except BacktrackException, _exc:
  1083. _error = self._combine_errors(_error, _exc.error)
  1084. self._pos = _choice2
  1085. break
  1086. _result = _all1
  1087. l = _result
  1088. _result = (reduce(operator.or_, [set(["]"])] + l))
  1089. break
  1090. except BacktrackException, _exc:
  1091. _error = self._combine_errors(_error, _exc.error)
  1092. self._pos = _choice0
  1093. _choice3 = self._pos
  1094. try:
  1095. _all4 = []
  1096. _call_status = self._rangeelement()
  1097. _result = _call_status.result
  1098. _error = self._combine_errors(_error, _call_status.error)
  1099. _all4.append(_result)
  1100. while 1:
  1101. _choice5 = self._pos
  1102. try:
  1103. _call_status = self._rangeelement()
  1104. _result = _call_status.result
  1105. _error = self._combine_errors(_error, _call_status.error)
  1106. _all4.append(_result)
  1107. except BacktrackException, _exc:
  1108. _error = self._combine_errors(_error, _exc.error)
  1109. self._pos = _choice5
  1110. break
  1111. _result = _all4
  1112. l = _result
  1113. _result = (reduce(operator.or_, l))
  1114. break
  1115. except BacktrackException, _exc:
  1116. _error = self._combine_errors(_error, _exc.error)
  1117. self._pos = _choice3
  1118. raise BacktrackException(_error)
  1119. _all6 = []
  1120. _call_status = self._rangeelement()
  1121. _result = _call_status.result
  1122. _error = self._combine_errors(_error, _call_status.error)
  1123. _all6.append(_result)
  1124. while 1:
  1125. _choice7 = self._pos
  1126. try:
  1127. _call_status = self._rangeelement()
  1128. _result = _call_status.result
  1129. _error = self._combine_errors(_error, _call_status.error)
  1130. _all6.append(_result)
  1131. except BacktrackException, _exc:
  1132. _error = self._combine_errors(_error, _exc.error)
  1133. self._pos = _choice7
  1134. break
  1135. _result = _all6
  1136. l = _result
  1137. _result = (reduce(operator.or_, l))
  1138. break
  1139. if _status.status == _status.LEFTRECURSION:
  1140. if _status.result is not None:
  1141. if _status.pos >= self._pos:
  1142. _status.status = _status.NORMAL
  1143. self._pos = _status.pos
  1144. return _status
  1145. _status.pos = self._pos
  1146. _status.status = _status.SOMESOLUTIONS
  1147. _status.result = _result
  1148. _status.error = _error
  1149. self._pos = _startingpos
  1150. return self._subrange()
  1151. _status.status = _status.NORMAL
  1152. _status.pos = self._pos
  1153. _status.result = _result
  1154. _status.error = _error
  1155. return _status
  1156. except BacktrackException, _exc:
  1157. _status.pos = -1
  1158. _status.result = None
  1159. _error = self._combine_errors(_error, _exc.error)
  1160. _status.error = _error
  1161. _status.status = _status.ERROR
  1162. raise BacktrackException(_error)
  1163. def rangeelement(self):
  1164. return self._rangeelement().result
  1165. def _rangeelement(self):
  1166. _key = self._pos
  1167. _status = self._dict_rangeelement.get(_key, None)
  1168. if _status is None:
  1169. _status = self._dict_rangeelement[_key] = Status()
  1170. else:
  1171. _statusstatus = _status.status
  1172. if _statusstatus == _status.NORMAL:
  1173. self._pos = _status.pos
  1174. return _status
  1175. elif _statusstatus == _status.ERROR:
  1176. raise BacktrackException(_status.error)
  1177. elif (_statusstatus == _status.INPROGRESS or
  1178. _statusstatus == _status.LEFTRECURSION):
  1179. _status.status = _status.LEFTRECURSION
  1180. if _status.result is not None:
  1181. self._pos = _status.pos
  1182. return _status
  1183. else:
  1184. raise BacktrackException(None)
  1185. elif _statusstatus == _status.SOMESOLUTIONS:
  1186. _status.status = _status.INPROGRESS
  1187. _startingpos = self._pos
  1188. try:
  1189. _result = None
  1190. _error = None
  1191. while 1:
  1192. _choice0 = self._pos
  1193. try:
  1194. _call_status = self._charclass()
  1195. _result = _call_status.result
  1196. _error = _call_status.error
  1197. break
  1198. except BacktrackException, _exc:
  1199. _error = self._combine_errors(_error, _exc.error)
  1200. self._pos = _choice0
  1201. _choice1 = self._pos
  1202. try:
  1203. _call_status = self._char()
  1204. _result = _call_status.result
  1205. _error = self._combine_errors(_error, _call_status.error)
  1206. c1 = _result
  1207. _result = self.__chars__('-')
  1208. _call_status = self._char()
  1209. _result = _call_status.result
  1210. _error = self._combine_errors(_error, _call_status.error)
  1211. c2 = _result
  1212. _result = (set([chr(i) for i in range(ord(c1), ord(c2) + 1)]))
  1213. break
  1214. except BacktrackException, _exc:
  1215. _error = self._combine_errors(_error, _exc.error)
  1216. self._pos = _choice1
  1217. _choice2 = self._pos
  1218. try:
  1219. _result = self.__chars__('.')
  1220. _result = ( set(['.']) )
  1221. break
  1222. except BacktrackException, _exc:
  1223. _error = self._combine_errors(_error, _exc.error)
  1224. self._pos = _choice2
  1225. _choice3 = self._pos
  1226. try:
  1227. _result = self.__chars__('*')
  1228. _result = ( set(['*']) )
  1229. break
  1230. except BacktrackException, _exc:
  1231. _error = self._combine_errors(_error, _exc.error)
  1232. self._pos = _choice3
  1233. _choice4 = self._pos
  1234. try:
  1235. _result = self.__chars__('+')
  1236. _result = ( set(['+']) )
  1237. break
  1238. except BacktrackException, _exc:
  1239. _error = self._combine_errors(_error, _exc.error)
  1240. self._pos = _choice4
  1241. _choice5 = self._pos
  1242. try:
  1243. _result = self.__chars__('?')
  1244. _result = ( set(['?']) )
  1245. break
  1246. except BacktrackException, _exc:
  1247. _error = self._combine_errors(_error, _exc.error)
  1248. self._pos = _choice5
  1249. _choice6 = self._pos
  1250. try:
  1251. _result = self.__chars__('-')
  1252. _result = ( set(['-']) )
  1253. break
  1254. except BacktrackException, _exc:
  1255. _error = self._combine_errors(_error, _exc.error)
  1256. self._pos = _choice6
  1257. _choice7 = self._pos
  1258. try:
  1259. _result = self.__chars__('[')
  1260. _result = ( set(['[']) )
  1261. break
  1262. except BacktrackException, _exc:
  1263. _error = self._combine_errors(_error, _exc.error)
  1264. self._pos = _choice7
  1265. _choice8 = self._pos
  1266. try:
  1267. _call_status = self._char()
  1268. _result = _call_status.result
  1269. _error = self._combine_errors(_error, _call_status.error)
  1270. c = _result
  1271. _result = ( set([c]) )
  1272. break
  1273. except BacktrackException, _exc:
  1274. _error = self._combine_errors(_error, _exc.error)
  1275. self._pos = _choice8
  1276. raise BacktrackException(_error)
  1277. _call_status = self._char()
  1278. _result = _call_status.result
  1279. _error = self._combine_errors(_error, _call_status.error)
  1280. c = _result
  1281. _result = ( set([c]) )
  1282. break
  1283. if _status.status == _status.LEFTRECURSION:
  1284. if _status.result is not None:
  1285. if _status.pos >= self._pos:
  1286. _status.status = _status.NORMAL
  1287. self._pos = _status.pos
  1288. return _status
  1289. _status.pos = self._pos
  1290. _status.status = _status.SOMESOLUTIONS
  1291. _status.result = _result
  1292. _status.error = _error
  1293. self._pos = _startingpos
  1294. return self._rangeelement()
  1295. _status.status = _status.NORMAL
  1296. _status.pos = self._pos
  1297. _status.result = _result
  1298. _status.error = _error
  1299. return _status
  1300. except BacktrackException, _exc:
  1301. _status.pos = -1
  1302. _status.result = None
  1303. _error = self._combine_errors(_error, _exc.error)
  1304. _status.error = _error
  1305. _status.status = _status.ERROR
  1306. raise BacktrackException(_error)
  1307. def numrange(self):
  1308. return self._numrange().result
  1309. def _numrange(self):
  1310. _key = self._pos
  1311. _status = self._dict_numrange.get(_key, None)
  1312. if _status is None:
  1313. _status = self._dict_numrange[_key] = Status()
  1314. else:
  1315. _statusstatus = _status.status
  1316. if _statusstatus == _status.NORMAL:
  1317. self._pos = _status.pos
  1318. return _status
  1319. elif _statusstatus == _status.ERROR:
  1320. raise BacktrackException(_status.error)
  1321. elif (_statusstatus == _status.INPROGRESS or
  1322. _statusstatus == _status.LEFTRECURSION):
  1323. _status.status = _status.LEFTRECURSION
  1324. if _status.result is not None:
  1325. self._pos = _status.pos
  1326. return _status
  1327. else:
  1328. raise BacktrackException(None)
  1329. elif _statusstatus == _status.SOMESOLUTIONS:
  1330. _status.status = _status.INPROGRESS
  1331. _startingpos = self._pos
  1332. try:
  1333. _result = None
  1334. _error = None
  1335. while 1:
  1336. _choice0 = self._pos
  1337. try:
  1338. _call_status = self._NUM()
  1339. _result = _call_status.result
  1340. _error = _call_status.error
  1341. n1 = _result
  1342. _result = self.__chars__(',')
  1343. _call_status = self._NUM()
  1344. _result = _call_status.result
  1345. _error = self._combine_errors(_error, _call_status.error)
  1346. n2 = _result
  1347. _result = (n1, n2)
  1348. break
  1349. except BacktrackException, _exc:
  1350. _error = self._combine_errors(_error, _exc.error)
  1351. self._pos = _choice0
  1352. _choice1 = self._pos
  1353. try:
  1354. _call_status = self._NUM()
  1355. _result = _call_status.result
  1356. _error = self._combine_errors(_error, _call_status.error)
  1357. n1 = _result
  1358. _result = (n1, n1)
  1359. break
  1360. except BacktrackException, _exc:
  1361. _error = self._combine_errors(_error, _exc.error)
  1362. self._pos = _choice1
  1363. raise BacktrackException(_error)
  1364. _call_status = self._NUM()
  1365. _result = _call_status.result
  1366. _error = self._combine_errors(_error, _call_status.error)
  1367. n1 = _result
  1368. _result = (n1, n1)
  1369. break
  1370. if _status.status == _status.LEFTRECURSION:
  1371. if _status.result is not None:
  1372. if _status.pos >= self._pos:
  1373. _status.status = _status.NORMAL
  1374. self._pos = _status.pos
  1375. return _status
  1376. _status.pos = self._pos
  1377. _status.status = _status.SOMESOLUTIONS
  1378. _status.result = _result
  1379. _status.error = _error
  1380. self._pos = _startingpos
  1381. return self._numrange()
  1382. _status.status = _status.NORMAL
  1383. _status.pos = self._pos
  1384. _status.result = _result
  1385. _status.error = _error
  1386. return _status
  1387. except BacktrackException, _exc:
  1388. _status.pos = -1
  1389. _status.result = None
  1390. _error = self._combine_errors(_error, _exc.error)
  1391. _status.error = _error
  1392. _status.status = _status.ERROR
  1393. raise BacktrackException(_error)
  1394. def clippednumrange(self):
  1395. return self._clippednumrange().result
  1396. def _clippednumrange(self):
  1397. _key = self._pos
  1398. _status = self._dict_clippednumrange.get(_key, None)
  1399. if _status is None:
  1400. _status = self._dict_clippednumrange[_key] = Status()
  1401. else:
  1402. _statusstatus = _status.status
  1403. if _statusstatus == _status.NORMAL:
  1404. self._pos = _status.pos
  1405. return _status
  1406. elif _statusstatus == _status.ERROR:
  1407. raise BacktrackException(_status.error)
  1408. elif (_statusstatus == _status.INPROGRESS or
  1409. _statusstatus == _status.LEFTRECURSION):
  1410. _status.status = _status.LEFTRECURSION
  1411. if _status.result is not None:
  1412. self._pos = _status.pos
  1413. return _status
  1414. else:
  1415. raise BacktrackException(None)
  1416. elif _statusstatus == _status.SOMESOLUTIONS:
  1417. _status.status = _status.INPROGRESS
  1418. _startingpos = self._pos
  1419. try:
  1420. _result = None
  1421. _error = None
  1422. _call_status = self._NUM()
  1423. _result = _call_status.result
  1424. _error = _call_status.error
  1425. n1 = _result
  1426. _result = self.__chars__(',')
  1427. _result = (n1)
  1428. if _status.status == _status.LEFTRECURSION:
  1429. if _status.result is not None:
  1430. if _status.pos >= self._pos:
  1431. _status.status = _status.NORMAL
  1432. self._pos = _status.pos
  1433. return _status
  1434. _status.pos = self._pos
  1435. _status.status = _status.SOMESOLUTIONS
  1436. _status.result = _result
  1437. _status.error = _error
  1438. self._pos = _startingpos
  1439. return self._clippednumrange()
  1440. _status.status = _status.NORMAL
  1441. _status.pos = self._pos
  1442. _status.result = _result
  1443. _status.error = _error
  1444. return _status
  1445. except BacktrackException, _exc:
  1446. _status.pos = -1
  1447. _status.result = None
  1448. _error = self._combine_errors(_error, _exc.error)
  1449. _status.error = _error
  1450. _status.status = _status.ERROR
  1451. raise BacktrackException(_error)
  1452. def charclass(self):
  1453. return self._charclass().result
  1454. def _charclass(self):
  1455. _key = self._pos
  1456. _status = self._dict_charclass.get(_key, None)
  1457. if _status is None:
  1458. _status = self._dict_charclass[_key] = Status()
  1459. else:
  1460. _statusstatus = _status.status
  1461. if _statusstatus == _status.NORMAL:
  1462. self._pos = _status.pos
  1463. return _status
  1464. elif _statusstatus == _status.ERROR:
  1465. raise BacktrackException(_status.error)
  1466. _startingpos = self._pos
  1467. try:
  1468. _result = None
  1469. _error = None
  1470. while 1:
  1471. _choice0 = self._pos
  1472. try:
  1473. _result = self.__chars__('\\')
  1474. _result = self.__chars__('d')
  1475. _result = ( set([chr(c) for c in range(ord('0'), ord('9')+1)]) )
  1476. break
  1477. except BacktrackException, _exc:
  1478. _error = _exc.error
  1479. self._pos = _choice0
  1480. _choice1 = self._pos
  1481. try:
  1482. _result = self.__chars__('\\')
  1483. _result = self.__chars__('s')
  1484. _result = ( set(['\t', '\n', '\f', '\r', ' ']) )
  1485. break
  1486. except BacktrackException, _exc:
  1487. _error = self._combine_errors(_error, _exc.error)
  1488. self._pos = _choice1
  1489. _choice2 = self._pos
  1490. try:
  1491. _result = self.__chars__('\\')
  1492. _result = self.__chars__('w')
  1493. _result = ( set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_']) )
  1494. break
  1495. except BacktrackException, _exc:
  1496. _error = self._combine_errors(_error, _exc.error)
  1497. self._pos = _choice2
  1498. _choice3 = self._pos
  1499. try:
  1500. _result = self.__chars__('\\')
  1501. _result = self.__chars__('D')
  1502. _result = ( set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('0'), ord('9')+1)]) )
  1503. break
  1504. except BacktrackException, _exc:
  1505. _error = self._combine_errors(_error, _exc.error)
  1506. self._pos = _choice3
  1507. _choice4 = self._pos
  1508. try:
  1509. _result = self.__chars__('\\')
  1510. _result = self.__chars__('S')
  1511. _result = ( set([chr(c) for c in range(256)]) - set(['\t', '\n', '\f', '\r', ' ']) )
  1512. break
  1513. except BacktrackException, _exc:
  1514. _error = self._combine_errors(_error, _exc.error)
  1515. self._pos = _choice4
  1516. _choice5 = self._pos
  1517. try:
  1518. _result = self.__chars__('\\')
  1519. _result = self.__chars__('W')
  1520. _result = ( set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_']))
  1521. break
  1522. except BacktrackException, _exc:
  1523. _error = self._combine_errors(_error, _exc.error)
  1524. self._pos = _choice5
  1525. raise BacktrackException(_error)
  1526. _result = self.__chars__('\\')
  1527. _result = self.__chars__('W')
  1528. _result = ( set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_']))
  1529. break
  1530. assert _status.status != _status.LEFTRECURSION
  1531. _status.status = _status.NORMAL
  1532. _status.pos = self._pos
  1533. _status.result = _result
  1534. _status.error = _error
  1535. return _status
  1536. except BacktrackException, _exc:
  1537. _status.pos = -1
  1538. _status.result = None
  1539. _error = self._combine_errors(_error, _exc.error)
  1540. _status.error = _error
  1541. _status.status = _status.ERROR
  1542. raise BacktrackException(_error)
  1543. def NUM(self):
  1544. return self._NUM().result
  1545. def _NUM(self):
  1546. _key = self._pos
  1547. _status = self._dict_NUM.get(_key, None)
  1548. if _status is None:
  1549. _status = self._dict_NUM[_key] = Status()
  1550. else:
  1551. _statusstatus = _status.status
  1552. if _statusstatus == _status.NORMAL:
  1553. self._pos = _status.pos
  1554. return _status
  1555. elif _statusstatus == _status.ERROR:
  1556. raise BacktrackException(_status.error)
  1557. _startingpos = self._pos
  1558. try:
  1559. _result = None
  1560. _error = None
  1561. _result = self._regex1166214427()
  1562. c = _result
  1563. _result = (int(c))
  1564. assert _status.status != _status.LEFTRECURSION
  1565. _status.status = _status.NORMAL
  1566. _status.pos = self._pos
  1567. _status.result = _result
  1568. _status.error = _error
  1569. return _status
  1570. except BacktrackException, _exc:
  1571. _status.pos = -1
  1572. _status.result = None
  1573. _error = _exc.error
  1574. _status.error = _error
  1575. _status.status = _status.ERROR
  1576. raise BacktrackException(_error)
  1577. def __init__(self, inputstream):
  1578. self._dict_EOF = {}
  1579. self._dict_parse = {}
  1580. self._dict_regex = {}
  1581. self._dict_concatenation = {}
  1582. self._dict_repetition = {}
  1583. self._dict_primary = {}
  1584. self._dict_char = {}
  1585. self._dict_QUOTEDCHAR = {}
  1586. self._dict_CHAR = {}
  1587. self._dict_range = {}
  1588. self._dict_rangeinner = {}
  1589. self._dict_subrange = {}
  1590. self._dict_rangeelement = {}
  1591. self._dict_numrange = {}
  1592. self._dict_clippednumrange = {}
  1593. self._dict_charclass = {}
  1594. self._dict_NUM = {}
  1595. self._pos = 0
  1596. self._inputstream = inputstream
  1597. def _regex2132196932(self):
  1598. _choice0 = self._pos
  1599. _runner = self._Runner(self._inputstream, self._pos)
  1600. _i = _runner.recognize_2132196932(self._pos)
  1601. if _runner.last_matched_state == -1:
  1602. self._pos = _choice0
  1603. raise BacktrackException
  1604. _upto = _runner.last_matched_index + 1
  1605. _pos = self._pos
  1606. assert _pos >= 0
  1607. assert _upto >= 0
  1608. _result = self._inputstream[_pos: _upto]
  1609. self._pos = _upto
  1610. return _result
  1611. def _regex1166214427(self):
  1612. _choice1 = self._pos
  1613. _runner = self._Runner(self._inputstream, self._pos)
  1614. _i = _runner.recognize_1166214427(self._pos)
  1615. if _runner.last_matched_state == -1:
  1616. self._pos = _choice1
  1617. raise BacktrackException
  1618. _upto = _runner.last_matched_index + 1
  1619. _pos = self._pos
  1620. assert _pos >= 0
  1621. assert _upto >= 0
  1622. _result = self._inputstream[_pos: _upto]
  1623. self._pos = _upto
  1624. return _result
  1625. def _regex1423754537(self):
  1626. _choice2 = self._pos
  1627. _runner = self._Runner(self._inputstream, self._pos)
  1628. _i = _runner.recognize_1423754537(self._pos)
  1629. if _runner.last_matched_state == -1:
  1630. self._pos = _choice2
  1631. raise BacktrackException
  1632. _upto = _runner.last_matched_index + 1
  1633. _pos = self._pos
  1634. assert _pos >= 0
  1635. assert _upto >= 0
  1636. _result = self._inputstream[_pos: _upto]
  1637. self._pos = _upto
  1638. return _result
  1639. class _Runner(object):
  1640. def __init__(self, text, pos):
  1641. self.text = text
  1642. self.pos = pos
  1643. self.last_matched_state = -1
  1644. self.last_matched_index = -1
  1645. self.state = -1
  1646. def recognize_2132196932(runner, i):
  1647. #auto-generated code, don't edit
  1648. assert i >= 0
  1649. input = runner.text
  1650. state = 0
  1651. while 1:
  1652. if state == 0:
  1653. try:
  1654. char = input[i]
  1655. i += 1
  1656. except IndexError:
  1657. runner.state = 0
  1658. return ~i
  1659. if '}' <= char <= '\xff':
  1660. state = 1
  1661. elif '\x00' <= char <= "'":
  1662. state = 1
  1663. elif '_' <= char <= 'z':
  1664. state = 1
  1665. elif '@' <= char <= 'Z':
  1666. state = 1
  1667. elif '/' <= char <= '>':
  1668. state = 1
  1669. elif char == ',':
  1670. state = 1
  1671. else:
  1672. break
  1673. runner.last_matched_state = state
  1674. runner.last_matched_index = i - 1
  1675. runner.state = state
  1676. if i == len(input):
  1677. return i
  1678. else:
  1679. return ~i
  1680. break
  1681. runner.state = state
  1682. return ~i
  1683. def recognize_1166214427(runner, i):
  1684. #auto-generated code, don't edit
  1685. assert i >= 0
  1686. input = runner.text
  1687. state = 0
  1688. while 1:
  1689. if state == 0:
  1690. try:
  1691. char = input[i]
  1692. i += 1
  1693. except IndexError:
  1694. runner.state = 0
  1695. return ~i
  1696. if char == '0':
  1697. state = 1
  1698. elif '1' <= char <= '9':
  1699. state = 2
  1700. else:
  1701. break
  1702. if state == 2:
  1703. runner.last_matched_index = i - 1
  1704. runner.last_matched_state = state
  1705. try:
  1706. char = input[i]
  1707. i += 1
  1708. except IndexError:
  1709. runner.state = 2
  1710. return i
  1711. if '0' <= char <= '9':
  1712. state = 2
  1713. continue
  1714. else:
  1715. break
  1716. runner.last_matched_state = state
  1717. runner.last_matched_index = i - 1
  1718. runner.state = state
  1719. if i == len(input):
  1720. return i
  1721. else:
  1722. return ~i
  1723. break
  1724. runner.state = state
  1725. return ~i
  1726. def recognize_1423754537(runner, i):
  1727. #auto-generated code, don't edit
  1728. assert i >= 0
  1729. input = runner.text
  1730. state = 0
  1731. while 1:
  1732. if state == 0:
  1733. try:
  1734. char = input[i]
  1735. i += 1
  1736. except IndexError:
  1737. runner.state = 0
  1738. return ~i
  1739. if char == '\\':
  1740. state = 5
  1741. else:
  1742. break
  1743. if state == 1:
  1744. runner.last_matched_index = i - 1
  1745. runner.last_matched_state = state
  1746. try:
  1747. char = input[i]
  1748. i += 1
  1749. except IndexError:
  1750. runner.state = 1
  1751. return i
  1752. if '0' <= char <= '9':
  1753. state = 6
  1754. elif 'A' <= char <= 'F':
  1755. state = 6
  1756. elif 'a' <= char <= 'f':
  1757. state = 6
  1758. else:
  1759. break
  1760. if state == 2:
  1761. runner.last_matched_index = i - 1
  1762. runner.last_matched_state = state
  1763. try:
  1764. char = input[i]
  1765. i += 1
  1766. except IndexError:
  1767. runner.state = 2
  1768. return i
  1769. if '0' <= char <= '7':
  1770. state = 4
  1771. else:
  1772. break
  1773. if state == 3:
  1774. runner.last_matched_index = i - 1
  1775. runner.last_matched_state = state
  1776. try:
  1777. char = input[i]
  1778. i += 1
  1779. except IndexError:
  1780. runner.state = 3
  1781. return i
  1782. if '0' <= char <= '7':
  1783. state = 2
  1784. continue
  1785. else:
  1786. break
  1787. if state == 5:
  1788. try:
  1789. char = input[i]
  1790. i += 1
  1791. except IndexError:
  1792. runner.state = 5
  1793. return ~i
  1794. if char == 'x':
  1795. state = 1
  1796. continue
  1797. elif '4' <= char <= '7':
  1798. state = 2
  1799. continue
  1800. elif '0' <= char <= '3':
  1801. state = 3
  1802. continue
  1803. elif 'y' <= char <= '\xff':
  1804. state = 4
  1805. elif '\x00' <= char <= '/':
  1806. state = 4
  1807. elif 'E' <= char <= 'R':
  1808. state = 4
  1809. elif 'e' <= char <= 'r':
  1810. state = 4
  1811. elif '8' <= char <= 'C':
  1812. state = 4
  1813. elif 'X' <= char <= 'b':
  1814. state = 4
  1815. elif 'T' <= char <= 'V':
  1816. state = 4
  1817. elif 't' <= char <= 'v':
  1818. state = 4
  1819. elif char == 'c':
  1820. state = 7
  1821. else:
  1822. break
  1823. if state == 6:
  1824. try:
  1825. char = input[i]
  1826. i += 1
  1827. except IndexError:
  1828. runner.state = 6
  1829. return ~i
  1830. if '0' <= char <= '9':
  1831. state = 4
  1832. elif 'A' <= char <= 'F':
  1833. state = 4
  1834. elif 'a' <= char <= 'f':
  1835. state = 4
  1836. else:
  1837. break
  1838. if state == 7:
  1839. runner.last_matched_index = i - 1
  1840. runner.last_matched_state = state
  1841. try:
  1842. char = input[i]
  1843. i += 1
  1844. except IndexError:
  1845. runner.state = 7
  1846. return i
  1847. if '\x00' <= char <= '\xff':
  1848. state = 4
  1849. else:
  1850. break
  1851. runner.last_matched_state = state
  1852. runner.last_matched_index = i - 1
  1853. runner.state = state
  1854. if i == len(input):
  1855. return i
  1856. else:
  1857. return ~i
  1858. break
  1859. runner.state = state
  1860. return ~i
  1861. class RegexParser(PackratParser):
  1862. def __init__(self, stream):
  1863. self.init_parser(stream)
  1864. forbidden = dict.fromkeys(("__weakref__ __doc__ "
  1865. "__dict__ __module__").split())
  1866. initthere = "__init__" in RegexParser.__dict__
  1867. for key, value in Parser.__dict__.iteritems():
  1868. if key not in RegexParser.__dict__ and key not in forbidden:
  1869. setattr(RegexParser, key, value)
  1870. RegexParser.init_parser = Parser.__init__.im_func
  1871. # generated code between this line and its other occurence
  1872. def test_generate():
  1873. f = py.path.local(__file__)
  1874. oldcontent = f.read()
  1875. s = "# GENERATED CODE BETWEEN THIS LINE AND ITS OTHER OCCURENCE\n".lower()
  1876. pre, gen, after = oldcontent.split(s)
  1877. from pypackrat import PyPackratSyntaxParser
  1878. from makepackrat import TreeOptimizer, ParserBuilder
  1879. p = PyPackratSyntaxParser(syntax)
  1880. t = p.file()
  1881. t = t.visit(TreeOptimizer())
  1882. visitor = ParserBuilder()
  1883. t.visit(visitor)
  1884. code = visitor.get_code()
  1885. content = """\
  1886. %s\
  1887. %s
  1888. from pypy.rlib.parsing.pypackrat import PackratParser, Status
  1889. from pypy.rlib.parsing.pypackrat import BacktrackException
  1890. from pypy.rlib.parsing import regex
  1891. import operator
  1892. %s
  1893. class RegexParser(PackratParser):
  1894. def __init__(self, stream):
  1895. self.init_parser(stream)
  1896. forbidden = dict.fromkeys(("__weakref__ __doc__ "
  1897. "__dict__ __module__").split())
  1898. initthere = "__init__" in RegexParser.__dict__
  1899. for key, value in Parser.__dict__.iteritems():
  1900. if key not in RegexParser.__dict__ and key not in forbidden:
  1901. setattr(RegexParser, key, value)
  1902. RegexParser.init_parser = Parser.__init__.im_func
  1903. %s
  1904. %s\
  1905. """ % (pre, s, code, s, after)
  1906. print content
  1907. f.write(content)