Dynamic Typing: Regular Strings

Felix supports subtyping of strings by regular expressions.

The typing can be enforced at run time, and uses a fast deterministic finite state automaton with complexity linear in the string length.

Note regular string types themselves are static types known to the compiler.

Regular expressions can be formed using the below combinators.
They can also be named using a regexp definition.

Regular Combinators
Name
Sample Type annotation
Kleene closure
letter *
Alternation letter | digit
Catenation
digit digit digit
Option
letter ?
One or More
letter +


Charsets
Name
Sample Type annotation
Any
_
Not newline .
One of ["abc" "de" 30]
Any but [^"abcde"]
Range ["a"-"z"]

Example Program




include "std";
regexp lower = ["abcdefghijklmnopqrstuvwxyz"];
regexp upper = ["A"-"Z"];
regexp digit = ["0123456789"];
regexp alpha = lower | upper | "_";
regexp id = alpha (alpha | digit) *;

print
  regmatch "identifier" with
  | digit+ => "Number" 
| id =>  "Identifier"  endmatch
;
endl;