-
Notifications
You must be signed in to change notification settings - Fork 7
Rewrote this repository in Rust #55
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
Open
SOF3
wants to merge
3
commits into
ansuz:master
Choose a base branch
from
SOF3:riir-this-repo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| name: CI | ||
| on: | ||
| - push | ||
| - pull_request | ||
| jobs: | ||
| build: | ||
| name: Rust clippy and unit tests | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| rustup_channel: | ||
| - stable | ||
| - beta | ||
| - nightly | ||
| dev_flag: | ||
| - "" | ||
| - "--release" | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: rustup install | ||
| run: | | ||
| rustup toolchain update --no-self-update ${{ matrix.rustup_channel }} | ||
| rustup default ${{ matrix.rustup_channel }} | ||
| - name: install just | ||
| run: cargo install just | ||
| - run: just build | ||
| - name: unit tests | ||
| run: just test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| build: clean build-macro | ||
| #!/usr/bin/env bash | ||
| LIB_PATH=$(echo -n target/debug/deps/libriir_macro-*.so) | ||
| rustc \ | ||
| --crate-name riir \ | ||
| --edition=2018 \ | ||
| README.md \ | ||
| --crate-type bin \ | ||
| --out-dir target/debug \ | ||
| -L dependency=target/debug/deps \ | ||
| --extern riir_macro=$LIB_PATH | ||
| clean: | ||
| #!/usr/bin/env bash | ||
| rm target/debug/deps/libriir_macro-*.so || true | ||
| build-macro: | ||
| cd riir-macro && cargo build --target-dir=../target $( (rustc --version | grep nightly >/dev/null) && echo --features span) | ||
|
|
||
| test: | ||
| cd riir-macro && cargo test --target-dir=../target $( (rustc --version | grep nightly >/dev/null) && echo --features span) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "riir-macro" | ||
| version = "0.1.0" | ||
| edition = "2018" | ||
|
|
||
| [lib] | ||
| proc-macro = true | ||
|
|
||
| [dependencies] | ||
| proc-macro2 = "1.0.18" | ||
| quote = "1.0.6" | ||
|
|
||
| [features] | ||
| span = ["proc-macro2/span-locations"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| #![cfg_attr(feature = "span", feature(proc_macro_span))] | ||
|
|
||
| use proc_macro2::{Delimiter, Span, TokenStream, TokenTree}; | ||
| use quote::quote; | ||
|
|
||
| #[proc_macro] | ||
| pub fn print_literally(ts: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
| let string = print_ts(ts.into()); | ||
| let ts = quote! { | ||
| fn main() { | ||
| print!(#string); | ||
| } | ||
| }; | ||
| ts.into() | ||
| } | ||
|
|
||
| fn print_ts(ts: TokenStream) -> String { | ||
| let mut output = String::new(); | ||
|
|
||
| #[cfg_attr(feature = "span", allow(unused_mut, unused_variables))] | ||
| let mut was_hash = false; | ||
| #[cfg_attr(not(feature = "span"), allow(unused_mut, unused_variables))] | ||
| let mut last_span: Option<Span> = None; | ||
|
|
||
| for tt in ts { | ||
| #[cfg(feature = "span")] | ||
| { | ||
| if let Some(last_span) = last_span { | ||
| let end = last_span.end(); | ||
| let start = tt.span().start(); | ||
| let end_col = if start.line > end.line { | ||
| let lines = start.line - end.line; | ||
| output.extend((0..lines).map(|_| '\n')); | ||
| 0 | ||
| } else { | ||
| end.column | ||
| }; | ||
| let space_size = start.column as isize - end_col as isize; | ||
| output.extend((0..space_size).map(|_| ' ')); | ||
| } | ||
| } | ||
| #[cfg_attr(not(feature = "span"), allow(unused_assignments))] | ||
| { | ||
| last_span = Some(tt.span()); | ||
| } | ||
| match tt { | ||
| TokenTree::Ident(ident) => { | ||
| #[cfg_attr(not(feature = "span"), allow(unused_mut))] | ||
| let mut string = ident.to_string(); | ||
| #[cfg(feature = "span")] | ||
| { | ||
| fill_space(ident.span(), &mut string); | ||
| } | ||
| #[cfg(not(feature = "span"))] | ||
| { | ||
| string.push(' '); | ||
| } | ||
| output.push_str(&string); | ||
| } | ||
| TokenTree::Punct(punct) => { | ||
| let mut string = punct.as_char().to_string(); | ||
| #[cfg(feature = "span")] | ||
| { | ||
| fill_space(punct.span(), &mut string); | ||
| } | ||
| #[cfg(not(feature = "span"))] | ||
| { | ||
| use proc_macro2::Spacing; | ||
|
|
||
| if punct.as_char() == '#' { | ||
| if !was_hash { | ||
| string = format!("\n{}", string); | ||
| } | ||
| was_hash = true; | ||
| } | ||
| if punct.spacing() == Spacing::Joint { | ||
| string.push(' '); | ||
| } | ||
| } | ||
| output.push_str(&string); | ||
| } | ||
| TokenTree::Literal(lit) => { | ||
| #[cfg_attr(not(feature = "span"), allow(unused_mut))] | ||
| let mut string = lit.to_string(); | ||
| #[cfg(feature = "span")] | ||
| { | ||
| fill_space(lit.span(), &mut string); | ||
| } | ||
| output.push_str(&string); | ||
| } | ||
| TokenTree::Group(group) => { | ||
| let mut string = String::new(); | ||
| let (l, r) = match group.delimiter() { | ||
| Delimiter::Parenthesis => ("(", ")"), | ||
| Delimiter::Bracket => ("[", "]"), | ||
| Delimiter::Brace => ("{", "}"), | ||
| Delimiter::None => ("", ""), | ||
| }; | ||
| string.push_str(l); | ||
| string.push_str(&print_ts(group.stream())); | ||
| string.push_str(r); | ||
| #[cfg(feature = "span")] | ||
| { | ||
| fill_space(group.span(), &mut string); | ||
| } | ||
| output.push_str(&string); | ||
| } | ||
| } | ||
| } | ||
| output | ||
| } | ||
|
|
||
| #[cfg(feature = "span")] | ||
| fn fill_space(span: Span, string: &mut String) { | ||
| let start_col = if span.end().line > span.start().line { | ||
| let lines = span.end().line - span.start().line; | ||
| string.extend((0..lines).map(|_| '\n')); | ||
| -1 | ||
| } else { | ||
| (span.start().column + string.len()) as isize | ||
| }; | ||
| let space_size = span.end().column as isize - start_col; | ||
| string.extend((0..space_size).map(|_| ' ')); | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[test] | ||
| fn test() { | ||
| use std::{fs, path::PathBuf}; | ||
|
|
||
| let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test.txt"); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test file was made external instead of using |
||
| let content = fs::read_to_string(path).unwrap(); | ||
| let actual = print_ts(content.parse().unwrap()); | ||
| #[cfg(feature = "span")] | ||
| { | ||
| assert_eq!(actual, content.trim()); | ||
| } | ||
| #[cfg(not(feature = "span"))] | ||
| { | ||
| // TODO write unit tests for non-span-sensitive code | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| a bc | ||
| ## de f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| nightly |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTTPS links have
//which prevents the()from being paired, causing errors at the lexing stage. They have been migrated to footnote style which does not wrap URLs inside brackets.