Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5:08:12 PM (Connected)
- 5:08:12 PM -NickServ- This nickname is registered. Please choose a different nickname, or identify via /msg NickServ identify <password>.
- 5:08:18 PM #django :Cannot join channel (+r) - you need to be identified with services.
- 5:08:18 PM (You joined the channel)
- 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!
- 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).
- 5:08:24 PM >NickServ< identify s33f3rcross
- 5:08:25 PM -NickServ- You are now identified for poska.
- 5:08:59 PM poska hey guys
- 5:09:32 PM poska how do you use variables inside rule definitions in a grammar?
- 5:10:07 PM poska e.g. rule foo { \d+ {$!my-special-variable} }
- 5:10:18 PM poska this doesn't seem to work
- 5:10:57 PM Cabanossi has quit: Ping timeout: 258 seconds.
- 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.
- 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.
- 5:13:04 PM IOninja poska: what are you trying to accomplish?
- 5:13:05 PM Cabanossi joined the channel.
- 5:13:40 PM IOninja private attributes won't work, since ever rule is a separate instance of Cursor or something like that
- 5:13:53 PM poska i have a rule which must repeat a fixed amount of times
- 5:14:02 PM poska and with each parse call, this amount differs
- 5:14:13 PM IOninja OK
- 5:14:23 PM IOninja Well, you can use dynamic variables to pass that information
- 5:14:51 PM poska could you share a working snippet of this?
- 5:15:13 PM IOninja m: my $*FOO = 3; say grammar { token TOP { $<foo>=.**{$*FOO} .+ } }.parse: 'abcdefghij'
- 5:15:13 PM camelia rakudo-moar 76a59c: OUTPUT: «「abcdefghij」 foo => 「abc」»
- 5:15:16 PM IOninja m: my $*FOO = 10; say grammar { token TOP { $<foo>=.**{$*FOO} .+ } }.parse: 'abcdefghij'
- 5:15:17 PM camelia rakudo-moar 76a59c: OUTPUT: «Nil»
- 5:15:20 PM IOninja m: my $*FOO = 10; say grammar { token TOP { $<foo>=.**{$*FOO} .+ } }.parse: 'abcdefghijddassdadas'
- 5:15:20 PM camelia rakudo-moar 76a59c: OUTPUT: «「abcdefghijddassdadas」 foo => 「abcdefghij」»
- 5:16:25 PM poska nice! thank you
- 5:16:32 PM IOninja m: say grammar { token TOP { $<n>=\d+ {my $*FOO = +$<n>} <bar> .+ }; token bar { .**{$*FOO} } }.parse: '3abcdefg'
- 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 1Actually thrown at: in regex bar at <tmp> line 1 in regex TOP at <tmp> line 1 in block <unit> at <tmp>…»
- 5:16:44 PM IOninja m: say grammar { token TOP { $<n>=\d+ :my $*FOO = +$<n>; <bar> .+ }; token bar { .**{$*FOO} } }.parse: '3abcdefg'
- 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 => 「」»
- 5:16:57 PM IOninja m: say grammar { token TOP { $<n>=\d+ {} :my $*FOO = +$<n>; <bar> .+ }; token bar { .**{$*FOO} } }.parse: '3abcdefg'
- 5:16:57 PM camelia rakudo-moar 76a59c: OUTPUT: «「3abcdefg」 n => 「3」 bar => 「abc」»
- 5:17:00 PM IOninja m: say grammar { token TOP { $<n>=\d+ {} :my $*FOO = +$<n>; <bar> .+ }; token bar { .**{$*FOO} } }.parse: '5abcdefg'
- 5:17:00 PM camelia rakudo-moar 76a59c: OUTPUT: «「5abcdefg」 n => 「5」 bar => 「abcde」»
- 5:17:06 PM IOninja It's also available in Actions
- 5:17:11 PM naxieAlDle m: my $x = ‘hello’; say ‘hello’ ~~ /<{$x}>/
- 5:17:12 PM camelia rakudo-moar 76a59c: OUTPUT: «「hello」»
- 5:17:26 PM IOninja (and the empty block {} is there to update the $/ variable so that $<n> capture would be in it)
- 5:17:45 PM smls poska: You could maybe also pass it down as a parameter: rule n-times ($n) { . ** {$n} };
- 5:18:07 PM smls Call it like <n-times(5)>
- 5:18:08 PM poska i didn't know you can pass parameters to rules
- 5:18:34 PM naxieAlDle there are some limitations to this, if I recall correctly
- 5:18:42 PM naxieAlDle namely you cannot have parameterized protos or something
- 5:19:26 PM poska are dynamic variables accessed globally?
- 5:19:44 PM IOninja poska: they're accessed in dynamic scope
- 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?
- 5:21:17 PM moritz yes, you could override the parse method in the grammar to set the dynamic variable when parsing beings
- 5:21:20 PM moritz *begins
- 5:21:22 PM IOninja you can use dynamic variables in token parameters: e.g. token foo ($*FOO) {... } and then it'll be available
- 5:22:05 PM poska okay, still need to wrap this in my head :D i'm quite new to p6
- 5:22:09 PM IOninja You can use :$args name param, can't you? No need to override anything
- 5:22:30 PM IOninja m: say grammar { token TOP ($*FOO) { $<foo>=.**{$*FOO} .+ } }.parse: 'abcdefghij', :args[3]
- 5:22:30 PM camelia rakudo-moar 76a59c: OUTPUT: «「abcdefghij」 foo => 「abc」»
- 5:22:32 PM IOninja yup
- 5:24:02 PM lukaramu_ has quit: Read error: Connection reset by peer.
- 5:28:12 PM perlpilot has quit: Remote host closed the connection.
- 5:31:15 PM smls m: grammar A { token TOP ($n) { <foo($n)> .* }; token foo ($n) { . ** {$n} } }; say A.parse("Hello world", args => \(4))
- 5:31:15 PM camelia rakudo-moar e2db7b: OUTPUT: «「Hello world」 foo => 「Hell」»
- 5:31:30 PM smls poska: This ^^ is what I meant, with passing it down as a parameter.
- 5:32:27 PM poska can you explain what does args => \(4) do?
- 5:32:52 PM smls 'args' is a named argument accepted by the .parse method
- 5:33:09 PM smls it takes a Capture of arguments to pass to rule TOP
- 5:33:26 PM [Coke] m: say (\(4)).perl
- 5:33:26 PM camelia rakudo-moar e2db7b: OUTPUT: «\(4)»
- 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
- 5:34:22 PM poska so it's sort of a function signature?
- 5:34:41 PM smls it's something that can be *passed* to a signature
- 5:35:04 PM smls a.k.a. an "argument list"
- 5:37:07 PM poska ok, this integrates perfectly! thanks
- 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)
- 5:38:56 PM perlpilot joined the channel.
- 5:40:02 PM IOninja There's no need to make it a capture
- 5:40:49 PM smls poska: In Perl6, | takes the role of Python's * in that case
- 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
- 5:40:53 PM camelia rakudo-moar e2db7b: OUTPUT: «1242»
- 5:41:06 PM IOninja m: grammar A { token TOP ($n) { <foo($n)> .* }; token foo ($n) { . ** {$n} } }; say A.parse("Hello world", :args[4])
- 5:41:06 PM camelia rakudo-moar e2db7b: OUTPUT: «「Hello world」 foo => 「Hell」»
- 5:41:10 PM IOninja m: 4.Capture
- 5:41:10 PM camelia rakudo-moar e2db7b: ( no output )
- 5:41:14 PM IOninja m: |4.Capture
- 5:41:15 PM camelia rakudo-moar e2db7b: ( no output )
- 5:41:29 PM IOninja Wonder why it complains about :4args ...
- 5:41:37 PM IOninja m: dd 4.Capture
- 5:41:37 PM camelia rakudo-moar e2db7b: OUTPUT: «\()»
- 5:41:41 PM IOninja m: dd |4.Capture
- 5:41:42 PM camelia rakudo-moar e2db7b: OUTPUT: «block <unit>»
- 5:41:47 PM IOninja m: dd [|4.Capture]
- 5:41:47 PM camelia rakudo-moar e2db7b: OUTPUT: «[]»
- 5:41:54 PM IOninja That's why :(
- 5:42:11 PM itaipu joined the channel.
- 5:43:58 PM cdg_ has quit: Remote host closed the connection.
- 5:44:24 PM cdg joined the channel.
- 5:46:33 PM IOninja Well, never mind. Seems .Capture is kinda busted, so makes sense to pass Capture to :args
- 5:47:51 PM lizmat has quit: Quit: So long, and thanks for all the Fish.
- 5:48:38 PM khw joined the channel.
- 5:49:19 PM IOninja Oh, wait, it's the slipping of a capture that's busted...
- 5:49:22 PM IOninja m: dd [ [:42x].Capture ]
- 5:49:22 PM camelia rakudo-moar e2db7b: OUTPUT: «[\(:x(42))]»
- 5:49:24 PM IOninja m: dd [ |[:42x].Capture ]
- 5:49:25 PM camelia rakudo-moar e2db7b: OUTPUT: «[]»
- 5:50:14 PM IOninja m: grammar A { token TOP (:$n) { <foo($n)> .* }; token foo ($n) { . ** {$n} } }; say A.parse("Hello world", args => \(:42n) )
- 5:50:15 PM camelia rakudo-moar e2db7b: OUTPUT: «Nil»
- 5:50:22 PM IOninja m: grammar A { token TOP (:$n) { <foo($n)> .* }; token foo ($n) { . ** {$n} } }; say A.parse("Hello world", args => \(:2n) )
- 5:50:22 PM camelia rakudo-moar e2db7b: OUTPUT: «「Hello world」 foo => 「He」»
- 5:50:34 PM IOninja moves this to #perl6-dev
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement