Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add file!() and line!() macros #168

Merged
merged 2 commits into from
Nov 24, 2020
Merged

add file!() and line!() macros #168

merged 2 commits into from
Nov 24, 2020

Conversation

tgolsson
Copy link
Contributor

Hello!

From discussion on Discord - this adds the file!() and line!() macros, which transform into string and number literals representing the current filename and line number (1-indexed). I tried to follow the pattern from format!(), but it seems a bit awkward being unable to implement these in runestick, instead feature-gating it in rune-modules.

➜ cat test.rn
pub fn main() {
   println(`Hello from ${file!()}:${line!()}`);
}

➜ target/release/rune test.rn
Hello from test.rn:2
== () (25.346µs)

Copy link
Collaborator

@udoprog udoprog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job. Only the comment about using self.source.path() needs to be addressed for now. Especially nice touch re-using the assembly impl for LitStr and LitNumber!

fn expand_line_macro(&mut self, ast: &mut ast::MacroCall) -> Result<BuiltInMacro, ParseError> {
let (l, _) = self
.source
.position_to_utf16cu_line_char(ast.open.span.start.into_usize())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, not that it matters (line count should be the same), but it would be nice with an utf8 version of this as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How'd that work - I can't see any information about the actual encoding of the incoming source? This should probably use the unicode variant though, not utf16.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utf16cu stands for UTF-16 code units, the position function does some work to re-encode each character to utf-16 in order to find the column. An equivalent version for UTF-8 would simply rely on the emitted chars instead.

See: https://github.com/rune-rs/rune/blob/master/crates/runestick/src/source.rs#L135

Not a blocker though, since the line will be the same.

@udoprog
Copy link
Collaborator

udoprog commented Nov 24, 2020

All blockers are resolved. 🚀

@udoprog udoprog merged commit 10046e7 into rune-rs:main Nov 24, 2020
@udoprog udoprog added changelog Issue has been added to the changelog enhancement New feature or request labels Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog Issue has been added to the changelog enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants