raku

Raku: effect of capture markers is lost “higher up”

断了今生、忘了曾经 提交于 2020-08-27 07:28:39
问题 The following Raku script: #!/usr/bin/env raku use v6.d; grammar MyGrammar { rule TOP { <keyword> '=' <value> } token keyword { \w+ } token value { <strvalue> | <numvalue> } token strvalue { '"' <( <-["]>* )> '"' } token numvalue { '-'? \d+ [ '.' \d* ]? } } say MyGrammar.parse('foo = 42'); say MyGrammar.parse('bar = "Hello, World!"'); has the following output: 「foo = 42」 keyword => 「foo」 value => 「42」 numvalue => 「42」 「bar = "Hello, World!"」 keyword => 「bar」 value => 「"Hello, World!"」

Perl6 Regex Match Num

青春壹個敷衍的年華 提交于 2020-08-27 06:50:18
问题 I would like to match any Num from part of a text string. So far, this (stolen from from https://docs.perl6.org/language/regexes.html#Best_practices_and_gotchas) does the job... my token sign { <[+-]> } my token decimal { \d+ } my token exponent { 'e' <sign>? <decimal> } my regex float { <sign>? <decimal>? '.' <decimal> <exponent>? } my regex int { <sign>? <decimal> } my regex num { <float>? <int>? } $str ~~ s/( <num>? \s*) ( .* )/$1/; This seems like a lot of (error prone) reinvention of the

How to pass hash-like object which does associative role to a constructor expecting a hash?

霸气de小男生 提交于 2020-08-24 07:43:44
问题 I am experimenting with customized hashes. The following is trying to implement a simpler lookup for config-like hashes: use v6; class X::Config::KeyNotFound is Exception { method message() { "Key not found!"; } } # A hash that allows for nested lookup using a '.' to separate keys. # (This means that keys themselves cannot contain a dot) # For example: # # %h = Config.new(%(a => %(b => 1))); # my $foo = %h<a.b>; # <-- $foo = 1 # class Config does Associative[Cool,Str] { has %.hash; multi

How to pass hash-like object which does associative role to a constructor expecting a hash?

瘦欲@ 提交于 2020-08-24 07:42:30
问题 I am experimenting with customized hashes. The following is trying to implement a simpler lookup for config-like hashes: use v6; class X::Config::KeyNotFound is Exception { method message() { "Key not found!"; } } # A hash that allows for nested lookup using a '.' to separate keys. # (This means that keys themselves cannot contain a dot) # For example: # # %h = Config.new(%(a => %(b => 1))); # my $foo = %h<a.b>; # <-- $foo = 1 # class Config does Associative[Cool,Str] { has %.hash; multi

How to pass hash-like object which does associative role to a constructor expecting a hash?

℡╲_俬逩灬. 提交于 2020-08-24 07:41:04
问题 I am experimenting with customized hashes. The following is trying to implement a simpler lookup for config-like hashes: use v6; class X::Config::KeyNotFound is Exception { method message() { "Key not found!"; } } # A hash that allows for nested lookup using a '.' to separate keys. # (This means that keys themselves cannot contain a dot) # For example: # # %h = Config.new(%(a => %(b => 1))); # my $foo = %h<a.b>; # <-- $foo = 1 # class Config does Associative[Cool,Str] { has %.hash; multi

How to mock a class method when unittesting in Raku

痴心易碎 提交于 2020-08-06 10:26:31
问题 Suppose I have a class like this: class MyClass { method data-is-valid { return self!get-data ~~ m{^From}; } method !get-data { return 'From Internet'; } } where !get-data method gets some data from Internet. Is it possible to mock that method so that it returns my own hardcoded data so I can test the module without connecting to the Internet? Ideally, the solution should not modify the definition of the class in any way. NOTE: A similar question exists regarding unittesting subroutines of

How to mock a class method when unittesting in Raku

南楼画角 提交于 2020-08-06 10:24:17
问题 Suppose I have a class like this: class MyClass { method data-is-valid { return self!get-data ~~ m{^From}; } method !get-data { return 'From Internet'; } } where !get-data method gets some data from Internet. Is it possible to mock that method so that it returns my own hardcoded data so I can test the module without connecting to the Internet? Ideally, the solution should not modify the definition of the class in any way. NOTE: A similar question exists regarding unittesting subroutines of

How to mock a class method when unittesting in Raku

社会主义新天地 提交于 2020-08-06 10:22:17
问题 Suppose I have a class like this: class MyClass { method data-is-valid { return self!get-data ~~ m{^From}; } method !get-data { return 'From Internet'; } } where !get-data method gets some data from Internet. Is it possible to mock that method so that it returns my own hardcoded data so I can test the module without connecting to the Internet? Ideally, the solution should not modify the definition of the class in any way. NOTE: A similar question exists regarding unittesting subroutines of

What's the real difference between a token and a rule?

醉酒当歌 提交于 2020-08-03 14:20:14
问题 I was drawn to Raku due to its built-in grammars and figured I'd play around with it and write a simple email address parser, only problem: I couldn't get it to work. I tried countless iterations before landing on something that actually works, and I'm struggling to understand why. All it boiled down to, was changing token to rule . Here's my example code: grammar Email { token TOP { <name> '@' [<subdomain> '.']* <domain> '.' <tld> } token name { \w+ ['.' \w+]* } token domain { \w+ } token