Advertisement
Guest User

Untitled

a guest
Mar 7th, 2017
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 8.75 KB | None | 0 0
  1. 5:08:12 PM   (Connected)
  2. 5:08:12 PM  -NickServ-  This nickname is registered. Please choose a different nickname, or identify via /msg NickServ identify <password>.
  3. 5:08:18 PM   #django :Cannot join channel (+r) - you need to be identified with services.
  4. 5:08:18 PM   (You joined the channel)
  5. 5:08:18 PM   The topic is: »ö« Welcome to Perl 6! | https://perl6.org/ | evalbot usage: 'p6: say 3;' or rakudo:,  or /msg camelia p6: ... | irclog: http://irc.perl6.org or http://colabti.org/irclogger/irclogger_logs/perl6 | UTF-8 is our friend!
  6. 5:08:18 PM   Topic set by moritz!~moritz@tina.perlgeek.de on Tue Dec 22 2015 20:12:58 GMT+0200 (FLE Standard Time).
  7. 5:08:24 PM  >NickServ<  identify s33f3rcross
  8. 5:08:25 PM  -NickServ-  You are now identified for poska.
  9. 5:08:59 PM  poska  hey guys
  10. 5:09:32 PM  poska  how do you use variables inside rule definitions in a grammar?
  11. 5:10:07 PM  poska  e.g. rule foo { \d+ {$!my-special-variable} }
  12. 5:10:18 PM  poska  this doesn't seem to work
  13. 5:10:57 PM   Cabanossi has quit: Ping timeout: 258 seconds.
  14. 5:10:58 PM  smls  [Coke]: .[1; 2] is not a slice, it indexes a singular element. No reasoin to expect .{"a"; "b"}  to be different.
  15. 5:11:59 PM  smls  [Coke]: .[1, 2] would be a slice, as would .[1; 2,] - i.e. when the subscript for at least one dimension is an Iterable.
  16. 5:13:04 PM  IOninja  poska: what are you trying to accomplish?
  17. 5:13:05 PM   Cabanossi joined the channel.
  18. 5:13:40 PM  IOninja  private attributes won't work, since ever rule is a separate instance of Cursor or something like that
  19. 5:13:53 PM  poska  i have a rule which must repeat a fixed amount of times
  20. 5:14:02 PM  poska  and with each parse call, this amount differs
  21. 5:14:13 PM  IOninja  OK
  22. 5:14:23 PM  IOninja  Well, you can use dynamic variables to pass that information
  23. 5:14:51 PM  poska  could you share a working snippet of this?
  24. 5:15:13 PM  IOninja  m: my $*FOO = 3; say grammar { token TOP { $<foo>=.**{$*FOO} .+ } }.parse: 'abcdefghij'
  25. 5:15:13 PM  camelia  rakudo-moar 76a59c: OUTPUT: «「abcdefghij」␤ foo => 「abc」␤»
  26. 5:15:16 PM  IOninja  m: my $*FOO = 10; say grammar { token TOP { $<foo>=.**{$*FOO} .+ } }.parse: 'abcdefghij'
  27. 5:15:17 PM  camelia  rakudo-moar 76a59c: OUTPUT: «Nil␤»
  28. 5:15:20 PM  IOninja  m: my $*FOO = 10; say grammar { token TOP { $<foo>=.**{$*FOO} .+ } }.parse: 'abcdefghijddassdadas'
  29. 5:15:20 PM  camelia  rakudo-moar 76a59c: OUTPUT: «「abcdefghijddassdadas」␤ foo => 「abcdefghij」␤»
  30. 5:16:25 PM  poska  nice! thank you
  31. 5:16:32 PM  IOninja  m: say grammar { token TOP { $<n>=\d+ {my $*FOO = +$<n>} <bar> .+ }; token bar { .**{$*FOO} } }.parse: '3abcdefg'
  32. 5:16:33 PM  camelia  rakudo-moar 76a59c: OUTPUT: «Dynamic variable $*FOO not found␤  in regex bar at <tmp> line 1␤  in regex TOP at <tmp> line 1␤  in block <unit> at <tmp> line 1␤␤Actually thrown at:␤  in regex bar at <tmp> line 1␤  in regex TOP at <tmp> line 1␤  in block <unit> at <tmp>…»
  33. 5:16:44 PM  IOninja  m: say grammar { token TOP { $<n>=\d+ :my $*FOO = +$<n>; <bar> .+ }; token bar { .**{$*FOO} } }.parse: '3abcdefg'
  34. 5:16:44 PM  camelia  rakudo-moar 76a59c: OUTPUT: «Use of Nil in numeric context␤  in regex TOP at <tmp> line 1␤「3abcdefg」␤ n =>3」␤ bar => 「」␤»
  35. 5:16:57 PM  IOninja  m: say grammar { token TOP { $<n>=\d+ {} :my $*FOO = +$<n>; <bar> .+ }; token bar { .**{$*FOO} } }.parse: '3abcdefg'
  36. 5:16:57 PM  camelia  rakudo-moar 76a59c: OUTPUT: «「3abcdefg」␤ n =>3」␤ bar => 「abc」␤»
  37. 5:17:00 PM  IOninja  m: say grammar { token TOP { $<n>=\d+ {} :my $*FOO = +$<n>; <bar> .+ }; token bar { .**{$*FOO} } }.parse: '5abcdefg'
  38. 5:17:00 PM  camelia  rakudo-moar 76a59c: OUTPUT: «「5abcdefg」␤ n =>5」␤ bar => 「abcde」␤»
  39. 5:17:06 PM  IOninja  It's also available in Actions
  40. 5:17:11 PM  naxieAlDle  m: my $x = ‘hello’; say ‘hello’ ~~ /<{$x}>/
  41. 5:17:12 PM  camelia  rakudo-moar 76a59c: OUTPUT: «「hello」␤»
  42. 5:17:26 PM  IOninja  (and the empty block {} is there to update the $/ variable so that $<n> capture would be in it)
  43. 5:17:45 PM  smls  poska: You could maybe also pass it down as a parameter:   rule n-times ($n) { . ** {$n} };
  44. 5:18:07 PM  smls  Call it like  <n-times(5)>
  45. 5:18:08 PM  poska  i didn't know you can pass parameters to rules
  46. 5:18:34 PM  naxieAlDle  there are some limitations to this, if I recall correctly
  47. 5:18:42 PM  naxieAlDle  namely you cannot have parameterized protos or something
  48. 5:19:26 PM  poska  are dynamic variables accessed globally?
  49. 5:19:44 PM  IOninja  poska: they're accessed in dynamic scope
  50. 5:20:33 PM  poska  is it possible to pass a parameter to the grammar on construction and then expose it as a dynamic variable?
  51. 5:21:17 PM  moritz  yes, you could override the parse method in the grammar to set the dynamic variable when parsing beings
  52. 5:21:20 PM  moritz  *begins
  53. 5:21:22 PM  IOninja  you can use dynamic variables in token parameters: e.g.  token foo ($*FOO) {... } and then it'll be available
  54. 5:22:05 PM  poska  okay, still need to wrap this in my head :D i'm quite new to p6
  55. 5:22:09 PM  IOninja  You can use :$args name param, can't you? No need to override anything
  56. 5:22:30 PM  IOninja  m: say grammar { token TOP ($*FOO) { $<foo>=.**{$*FOO} .+ } }.parse: 'abcdefghij', :args[3]
  57. 5:22:30 PM  camelia  rakudo-moar 76a59c: OUTPUT: «「abcdefghij」␤ foo => 「abc」␤»
  58. 5:22:32 PM  IOninja  yup
  59. 5:24:02 PM   lukaramu_ has quit: Read error: Connection reset by peer.
  60. 5:28:12 PM   perlpilot has quit: Remote host closed the connection.
  61. 5:31:15 PM  smls  m: grammar A { token TOP ($n) { <foo($n)> .* };  token foo ($n) { . ** {$n} } };  say A.parse("Hello world", args => \(4))
  62. 5:31:15 PM  camelia  rakudo-moar e2db7b: OUTPUT: «「Hello world」␤ foo => 「Hell」␤»
  63. 5:31:30 PM  smls  poska: This ^^ is what I meant, with passing it down as a parameter.
  64. 5:32:27 PM  poska  can you explain what does args => \(4) do?
  65. 5:32:52 PM  smls  'args' is a named argument accepted by the .parse method
  66. 5:33:09 PM  smls  it takes a Capture of arguments to pass to rule TOP
  67. 5:33:26 PM  [Coke]  m: say (\(4)).perl
  68. 5:33:26 PM  camelia  rakudo-moar e2db7b: OUTPUT: «\(4)␤»
  69. 5:33:44 PM  smls  \( )  constructs a Capture, which is a collection of positional and/or named arguments that can be passed to a function or method
  70. 5:34:22 PM  poska  so it's sort of a function signature?
  71. 5:34:41 PM  smls  it's something that can be *passed* to a signature
  72. 5:35:04 PM  smls  a.k.a. an "argument list"
  73. 5:37:07 PM  poska  ok, this integrates perfectly! thanks
  74. 5:37:56 PM  poska  in the python world i imagine this as foo(*(1, 2, 3)), when the function naturally accepts foo(1, 2, 3)
  75. 5:38:56 PM   perlpilot joined the channel.
  76. 5:40:02 PM  IOninja  There's no need to make it a capture
  77. 5:40:49 PM  smls  poska: In Perl6, | takes the role of Python's * in that case
  78. 5:40:53 PM  smls  m: sub a ($x, $y, :$foo) { say $x; say $y; say $foo };  my $args = \(1, 2, foo => 42);  a |$args
  79. 5:40:53 PM  camelia  rakudo-moar e2db7b: OUTPUT: «1242␤»
  80. 5:41:06 PM  IOninja  m: grammar A { token TOP ($n) { <foo($n)> .* };  token foo ($n) { . ** {$n} } };  say A.parse("Hello world", :args[4])
  81. 5:41:06 PM  camelia  rakudo-moar e2db7b: OUTPUT: «「Hello world」␤ foo => 「Hell」␤»
  82. 5:41:10 PM  IOninja  m: 4.Capture
  83. 5:41:10 PM  camelia  rakudo-moar e2db7b: ( no output )
  84. 5:41:14 PM  IOninja  m: |4.Capture
  85. 5:41:15 PM  camelia  rakudo-moar e2db7b: ( no output )
  86. 5:41:29 PM  IOninja  Wonder why it complains about :4args ...
  87. 5:41:37 PM  IOninja  m: dd 4.Capture
  88. 5:41:37 PM  camelia  rakudo-moar e2db7b: OUTPUT: «\()␤»
  89. 5:41:41 PM  IOninja  m: dd |4.Capture
  90. 5:41:42 PM  camelia  rakudo-moar e2db7b: OUTPUT: «block <unit>␤»
  91. 5:41:47 PM  IOninja  m: dd [|4.Capture]
  92. 5:41:47 PM  camelia  rakudo-moar e2db7b: OUTPUT: «[]␤»
  93. 5:41:54 PM  IOninja  That's why :(
  94. 5:42:11 PM   itaipu joined the channel.
  95. 5:43:58 PM   cdg_ has quit: Remote host closed the connection.
  96. 5:44:24 PM   cdg joined the channel.
  97. 5:46:33 PM  IOninja  Well, never mind. Seems .Capture is kinda busted, so makes sense to pass Capture to :args
  98. 5:47:51 PM   lizmat has quit: Quit: So long, and thanks for all the Fish.
  99. 5:48:38 PM   khw joined the channel.
  100. 5:49:19 PM  IOninja  Oh, wait, it's the slipping of a capture that's busted...
  101. 5:49:22 PM  IOninja  m: dd [ [:42x].Capture ]
  102. 5:49:22 PM  camelia  rakudo-moar e2db7b: OUTPUT: «[\(:x(42))]␤»
  103. 5:49:24 PM  IOninja  m: dd [ |[:42x].Capture ]
  104. 5:49:25 PM  camelia  rakudo-moar e2db7b: OUTPUT: «[]␤»
  105. 5:50:14 PM  IOninja  m: grammar A { token TOP (:$n) { <foo($n)> .* };  token foo ($n) { . ** {$n} } };  say A.parse("Hello world", args => \(:42n) )
  106. 5:50:15 PM  camelia  rakudo-moar e2db7b: OUTPUT: «Nil␤»
  107. 5:50:22 PM  IOninja  m: grammar A { token TOP (:$n) { <foo($n)> .* };  token foo ($n) { . ** {$n} } };  say A.parse("Hello world", args => \(:2n) )
  108. 5:50:22 PM  camelia  rakudo-moar e2db7b: OUTPUT: «「Hello world」␤ foo => 「He」␤»
  109. 5:50:34 PM   IOninja moves this to #perl6-dev
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement