1. my class Grammar is Match {
  2. method parse(\target, :$rule, :$args, Mu :$actions) is raw {
  3. nqp::stmts(
  4. (my $grammar := self.new(:orig(target), |%_).set_actions($actions)),
  5. nqp::decont(nqp::getlexcaller('$/') =
  6. nqp::if(
  7. (my $cursor := nqp::if(
  8. $rule,
  9. nqp::if(
  10. $args,
  11. $grammar."$rule"(|$args.Capture),
  12. $grammar."$rule"()
  13. ),
  14. nqp::if(
  15. $args,
  16. $grammar.TOP(|$args.Capture),
  17. $grammar.TOP()
  18. ),
  19. )),
  20. nqp::stmts(
  21. (my $match := $cursor.MATCH),
  22. nqp::while(
  23. $match && nqp::isne_i(
  24. nqp::getattr_i(($match := $cursor.MATCH),Match,'$!pos'),
  25. target.chars
  26. ),
  27. $match := ($cursor := $cursor.'!cursor_next'()).MATCH
  28. ),
  29. $match || Nil
  30. ),
  31. Nil
  32. )
  33. )
  34. )
  35. }
  36. method subparse(\target, :$rule, :$args, Mu :$actions) is raw {
  37. nqp::stmts(
  38. (my $grammar := self.new(:orig(target), |%_).set_actions($actions)),
  39. nqp::decont(nqp::getlexcaller('$/') =
  40. nqp::if(
  41. $rule,
  42. nqp::if(
  43. $args,
  44. $grammar."$rule"(|$args.Capture).MATCH,
  45. $grammar."$rule"().MATCH,
  46. ),
  47. nqp::if(
  48. $args,
  49. $grammar.TOP(|$args.Capture).MATCH,
  50. $grammar.TOP().MATCH
  51. ),
  52. )
  53. )
  54. )
  55. }
  56. method parsefile(Str(Cool) $filename, :$enc) is raw {
  57. nqp::decont(nqp::getlexcaller('$/') = nqp::if(
  58. nqp::elems(nqp::getattr(%_,Map,'$!storage')),
  59. self.parse($filename.IO.slurp(:$enc), |%_),
  60. self.parse($filename.IO.slurp(:$enc))
  61. ))
  62. }
  63. }