Skip to content

Commit a9c3855

Browse files
committed
start playing with macros for easier traces
1 parent 7a3915c commit a9c3855

16 files changed

Lines changed: 258 additions & 488 deletions

File tree

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ members = [
2020
"crates/vim9-lexer",
2121
"crates/vim9-parser",
2222
"crates/vim9-gen",
23+
"crates/macros",
2324
]

crates/macros/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "macros"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[lib]
9+
proc-macro = true
10+
11+
[dependencies]
12+
syn = "1.0"
13+
quote = "1.0"

crates/macros/src/lib.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
extern crate proc_macro;
2+
use proc_macro::TokenStream;
3+
use quote::quote;
4+
use syn::{parse_macro_input, visit_mut::VisitMut, ImplItem};
5+
6+
struct ReplaceQuestions {
7+
name: syn::Type,
8+
count: usize,
9+
}
10+
11+
impl VisitMut for ReplaceQuestions {
12+
fn visit_expr_mut(&mut self, i: &mut syn::Expr) {
13+
match i {
14+
syn::Expr::Try(try_expr) => {
15+
syn::visit_mut::visit_expr_try_mut(self, try_expr);
16+
17+
// let name = syn::LitStr::new(&self.name, self.span);
18+
let e = &try_expr.expr;
19+
// *i = syn::parse_quote! {
20+
// match #e {
21+
// ::core::result::Result::Ok(v) => v,
22+
// ::core::result::Result::Err(e) => {
23+
// panic!(#name);
24+
// // return Err(e.context("oh no"));
25+
// }
26+
// }
27+
// };
28+
29+
self.count += 1;
30+
let name = &self.name;
31+
// let count = &self.count;
32+
33+
*i = syn::parse_quote! {
34+
#e.expect(&format!("{}.{}", stringify!(#name), stringify!(#e)))
35+
}
36+
}
37+
_ => syn::visit_mut::visit_expr_mut(self, i),
38+
}
39+
}
40+
}
41+
42+
#[proc_macro_attribute]
43+
pub fn parse_context(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
44+
let mut input = parse_macro_input!(tokens as syn::ItemImpl);
45+
for mut item in input.items.iter_mut() {
46+
match &item {
47+
ImplItem::Const(_) => todo!(),
48+
ImplItem::Method(_) => {
49+
let mut replacer = ReplaceQuestions {
50+
name: *input.self_ty.clone(),
51+
count: 0,
52+
};
53+
54+
replacer.visit_impl_item_mut(&mut item);
55+
}
56+
ImplItem::Type(_) => todo!(),
57+
ImplItem::Macro(_) => todo!(),
58+
ImplItem::Verbatim(_) => todo!(),
59+
_ => todo!(),
60+
}
61+
}
62+
63+
TokenStream::from(quote! {
64+
#input
65+
})
66+
}

crates/vim9-gen/src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ use parser::{
77
self, new_parser, ArrayLiteral, AssignStatement, AugroupCommand,
88
AutocmdCommand, Body, BreakCommand, CallCommand, CallExpression,
99
ContinueCommand, DeclCommand, DefCommand, DeferCommand, DictAccess,
10-
DictLiteral, EchoCommand, ExecuteCommand, ElseCommand, ElseIfCommand, ExCommand,
11-
Expandable, ExportCommand, Expression, ForCommand, GroupedExpression,
12-
Heredoc, Identifier, IfCommand, ImportCommand, IndexExpression, IndexType,
13-
InfixExpression, InnerType, Lambda, Literal, MethodCall, MutationStatement,
14-
PrefixExpression, RawIdentifier, Register, ReturnCommand, ScopedIdentifier,
15-
SharedCommand, Signature, StatementCommand, Ternary, TryCommand, Type,
16-
UnpackIdentifier, UserCommand, VarCommand, Vim9ScriptCommand, VimBoolean,
17-
VimKey, VimNumber, VimOption, VimScope, VimString, WhileCommand,
10+
DictLiteral, EchoCommand, ElseCommand, ElseIfCommand, ExCommand,
11+
ExecuteCommand, Expandable, ExportCommand, Expression, ForCommand,
12+
GroupedExpression, Heredoc, Identifier, IfCommand, ImportCommand,
13+
IndexExpression, IndexType, InfixExpression, InnerType, Lambda, Literal,
14+
MethodCall, MutationStatement, PrefixExpression, RawIdentifier, Register,
15+
ReturnCommand, ScopedIdentifier, SharedCommand, Signature,
16+
StatementCommand, Ternary, TryCommand, Type, UnpackIdentifier, UserCommand,
17+
VarCommand, Vim9ScriptCommand, VimBoolean, VimKey, VimNumber, VimOption,
18+
VimScope, VimString, WhileCommand,
1819
};
1920

2021
// this word is missspelled
@@ -1275,7 +1276,7 @@ pub fn eval(program: parser::Program, is_test: bool) -> String {
12751276

12761277
pub fn generate(contents: &str, is_test: bool) -> String {
12771278
let lexer = Lexer::new(contents);
1278-
let mut parser = new_parser(lexer);
1279+
let parser = new_parser(&lexer);
12791280
let program = parser.parse_program();
12801281

12811282
let result = eval(program, is_test);

crates/vim9-gen/testdata/busted/shared.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ def Test_syn()
66
->split("\n")
77
->len())
88
enddef
9+

crates/vim9-gen/testdata/output/busted_indexing.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe("filename", function()
1010
local Test_index_with_prefix = nil
1111
local Test_string = nil
1212
-- vim9script
13+
1314
local l = { 1, 2, 3 }
1415

1516
it("Test_can_index", function()

crates/vim9-gen/testdata/output/vim9_gen__test__assign.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
22
source: crates/vim9-gen/src/lib.rs
3-
assertion_line: 1005
3+
assertion_line: 1361
44
expression: "generate(contents, false)"
55
---
66
local NVIM9 = require("vim9script")
77
local __VIM9_MODULE = {}
88
-- vim9script
9+
910
vim.g["my_var"] = NVIM9.ops["Plus"](1, 2)
1011
print(vim.g["my_var"])
1112
return __VIM9_MODULE

crates/vim9-gen/testdata/output/vim9_gen__test__cfilter.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
---
22
source: crates/vim9-gen/src/lib.rs
3-
assertion_line: 1251
3+
assertion_line: 1364
44
expression: "generate(contents, false)"
55
---
66
local NVIM9 = require("vim9script")
77
local __VIM9_MODULE = {}
88
local Qf_filter = nil
99
-- vim9script
10+
1011
-- # cfilter.vim: Plugin to filter entries from a quickfix/location list
1112
-- # Last Change: Jun 30, 2022
1213
-- # Maintainer: Yegappan Lakshmanan (yegappan AT yahoo DOT com)

crates/vim9-gen/testdata/output/vim9_gen__test__expr.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
22
source: crates/vim9-gen/src/lib.rs
3-
assertion_line: 1003
3+
assertion_line: 1359
44
expression: "generate(contents, false)"
55
---
66
local NVIM9 = require("vim9script")
77
local __VIM9_MODULE = {}
88
-- vim9script
9+
910
local x = NVIM9.ops["Plus"](1, 2)
1011
print(x)
1112
return __VIM9_MODULE

0 commit comments

Comments
 (0)