ethanpailes / remake

Apache License 2.0
4 stars 1 forks source link

Macro-based Regex instantiation (tracking issue) #6

Open tchebb opened 6 years ago

tchebb commented 6 years ago

Would allow the syntax tree to be parsed at compile-time and make the DSL a bit more ergonomic.

#zerocostabstractions

tekknolagi commented 6 years ago

So you would do something like:

remake! {
    let digit = \d
    let block = repeat(digit, 3)
    sep(repeat(block, 4), '.')
}

And it would return a RegExp AST?

ethanpailes commented 6 years ago

I don't know much about proc macros, but assuming the remake tokens are a subset of Rust's tokens I think this should be as simple as putting them through a translation shim to emit some src/lex.rs:Tokens.

@tekknolagi, I can think of four possible return types Result<Remake, remake::Error>, Result<Regex, remake::Error>, Remake, and Regex. If we didn't return Results we would want to just cause the build to fail with a nice error message. I think that use case might be common enough that it would be worth doing and just saying to use the Remake::new constructor if you want more control.

tekknolagi commented 6 years ago

I think returning a Remake or failing build is reasonable. There's no point deferring checks to run-time in a language that can do them at compile-time.

tekknolagi commented 6 years ago

Here is an example: https://maud.lambda.xyz/

ethanpailes commented 6 years ago

Cool! If you want to start looking into the proc macro API, this could be a good feature to sink your teeth into. I think you would mostly just have to take the token stream that rust gives you and translate it into something that impls Iterator<Item=Spanned<Token<'input>, usize, InternalError>> just like src/lex.rs:Lexer