Created
June 15, 2018 09:10
-
-
Save joseoliv/ab7530f9a414d59392d2cd7c7c89f280 to your computer and use it in GitHub Desktop.
Metaobject regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package metaobjectTest | |
@doc{* | |
usage: | |
String@regex(re) | |
regex checks if the variable holds only strings that match the | |
regular expression re. | |
*} | |
object Regex | |
func run { | |
var String@regex("a*b[A-Z]") ab; | |
var String s; | |
ab = "bB"; // ok | |
ab = "aaaaabC"; // ok | |
// ab = "a26"; // compile-time error | |
var Boolean ok = true; | |
s = "bB"; | |
{ | |
ab = s; | |
} catch: { (: ExceptionStr e :) ok = false }; | |
assert ok; | |
ok = true; | |
s = """aaaaabC"""; | |
{ | |
ab = s; | |
} catch: { (: ExceptionStr e :) ok = false; }; | |
assert ok; | |
ok = false; | |
s = "abAZ"; | |
{ | |
ab = s; // fails at runtime | |
} catch: { (: ExceptionStr e :) ok = true; }; | |
assert ok; | |
ok = false; | |
s = "aaaa"; | |
{ | |
ab = s; // fails at runtime | |
} catch: { (: ExceptionStr e :) ok = true }; | |
assert ok; | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment