Skip to content

Instantly share code, notes, and snippets.

@Kimundi
Last active December 17, 2015 04:49
Show Gist options
  • Save Kimundi/5553204 to your computer and use it in GitHub Desktop.
Save Kimundi/5553204 to your computer and use it in GitHub Desktop.

Syntax extension

The re!() syntax extension accepts an string literal and optional flags, and returns an type T implementing CompiledRegularExpression:

re!("re_string_literal"[, options, ...]) -> T 

The parse_re!() syntax extension just accepts an string literal and returns an type ReAst representing the regular expressions AST.

parse_re!("re_string_literal") -> ReAst

Both would be compiletime syntax checked, with nice error messages underlining spans of the string literal. re!() would compile the RE at compiletime, and exposing that to usercode as a specialized type and implementation of CompiledRegularExpression.

parse_re!() would basically just expand to an constructor expression for the ReAst type.

All this would also be duplicated as modules, types and functions in the re module for runtime compilation. Usage of CompiledRegularExpression for both compiletime and runtime compiled REs would ensure compatibilty and code reuse on the user side, and allows the option and flexibilty to plug in an custom runtime compiler and/or parser.

API

trait CompiledRegularExpression {

}

Notes

There would be no way to know what type re!() returns. The user would have to rely on inference, generics or trait objects to use it, wich however shouldn't really be a problem. The situation is similar to external Iterators and other OOP-like types in rust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment