Skip to content

Instantly share code, notes, and snippets.

@joseoliv
Created June 15, 2018 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseoliv/ab7530f9a414d59392d2cd7c7c89f280 to your computer and use it in GitHub Desktop.
Save joseoliv/ab7530f9a414d59392d2cd7c7c89f280 to your computer and use it in GitHub Desktop.
Metaobject regex
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