1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use super::token_de_info::*;
use crate::common;
use quote::quote;
pub fn impl_token_deserialize_macro_enum_value(ast: &syn::DeriveInput) -> proc_macro2::TokenStream {
let name = &ast.ident;
let fields = common::get_struct_enum_fields(ast);
let mut expected_values = vec![];
let mut errors = quote! {};
let convert_match_items = {
let mut parse_gen = quote! {};
for (_i, field) in fields.iter().enumerate() {
let ident = field.ident.as_ref().unwrap();
let (token_info, token_err) = get_token_de_info(field);
errors = quote! {
#errors
#token_err
};
let token_ref = token_info.token;
expected_values.push(token_ref.clone().unwrap());
parse_gen = quote! {
#parse_gen
#token_ref => Ok(Self::#ident),
};
// Alias
if let Some(alias) = token_info.alias {
expected_values.push(alias.clone());
parse_gen = quote! {
#parse_gen
#alias => {
diagnostics.add_message(
DMExtraInfo {
range: arg.node.get_range(),
message_template_data: hash_map! {
"alias_name" => format!("`{}`", #alias),
"suggested_name" => format!("`{}`", #token_ref),
},
},
"alias",
);
Ok(Self::#ident)
},
};
}
}
parse_gen
};
let expected_values_string = expected_values
.iter()
.map(|item| format!("`{}`", item))
.collect::<Vec<String>>()
.join(", ");
// This is the same as `td_reference.rs`
quote! {
#errors
/// Deserialize a token with following pattern: `[REF:ENUM]`
impl TokenDeserialize for #name {
#[allow(unused_variables)]
fn deserialize_tokens(
mut cursor: &mut df_ls_syntax_analysis::TreeCursor,
source: &str,
mut diagnostics: &mut df_ls_diagnostics::DiagnosticsInfo,
) -> Result<Box<Self>, ()> {
use df_ls_syntax_analysis::{TryFromArgumentGroup, Token};
let mut token = match Token::deserialize_tokens(&mut cursor, source, &mut diagnostics){
Ok(token) => token,
Err(err) => {
// When token could not be parsed correctly.
// Token could not be parsed, so we can consume it.
// Because this will always fail.
Token::consume_token(&mut cursor)?;
return Err(err);
}
};
Token::consume_token(&mut cursor)?;
// Token Name
token.check_token_name(source, &mut diagnostics, true)?;
// Arg 0
let result = Self::try_from_argument_group(&mut token, source, &mut diagnostics, true);
// Just check if all tokens are consumed if no other errors are present
if result.is_ok() {
token.check_all_arg_consumed(source, &mut diagnostics, true)?;
} else {
// In case of an error, mark other arguments as unchecked.
df_ls_syntax_analysis::mark_rest_of_token_as_unchecked(diagnostics, &token);
}
Ok(Box::new(result?))
}
fn deserialize_general_token(
_cursor: &mut df_ls_syntax_analysis::TreeCursor,
_source: &str,
_diagnostics: &mut df_ls_diagnostics::DiagnosticsInfo,
new_self: Box<Self>,
) -> (df_ls_syntax_analysis::LoopControl, Box<Self>) {
(df_ls_syntax_analysis::LoopControl::DoNothing, new_self)
}
fn get_vec_loopcontrol() -> df_ls_syntax_analysis::LoopControl {
df_ls_syntax_analysis::LoopControl::DoNothing
}
fn get_allowed_tokens() -> Option<Vec<String>> {
// Should not be set, see #61
None
}
}
// ------------------------- Convert a group of arguments to Self -----------------------
impl df_ls_syntax_analysis::TryFromArgumentGroup for #name {
fn try_from_argument_group(
token: &mut df_ls_syntax_analysis::Token,
source: &str,
mut diagnostics: &mut df_ls_diagnostics::DiagnosticsInfo,
add_diagnostics_on_err: bool,
) -> Result<Self, ()> {
use df_ls_syntax_analysis::TryFromArgument;
token.check_as_required_argument(source, &mut diagnostics, add_diagnostics_on_err)?;
let arg = token.get_current_arg_opt();
let result = Self::try_from_argument(arg, source, &mut diagnostics, add_diagnostics_on_err);
token.consume_argument();
result
}
}
// -------------------------Convert one argument to Self -----------------------
impl df_ls_syntax_analysis::TryFromArgument for #name {
fn try_from_argument(
arg_opt: Option<&df_ls_syntax_analysis::Argument>,
_source: &str,
diagnostics: &mut df_ls_diagnostics::DiagnosticsInfo,
add_diagnostics_on_err: bool,
) -> Result<Self, ()> {
use df_ls_syntax_analysis::{TokenArgument};
use df_ls_diagnostics::{hash_map, DMExtraInfo};
if let Some(arg) = arg_opt {
match &arg.value {
TokenArgument::TVReference(v) => match v.as_ref() {
#convert_match_items
found_value => {
if add_diagnostics_on_err {
diagnostics.add_message(
DMExtraInfo {
range: arg.node.get_range(),
message_template_data: hash_map! {
"found_value" => format!("`{}`", found_value),
"valid_values" => #expected_values_string.to_owned(),
},
},
"wrong_enum_value",
);
}
Err(())
},
},
_ => {
if add_diagnostics_on_err {
diagnostics.add_message(
DMExtraInfo {
range: arg.node.get_range(),
message_template_data: hash_map! {
"expected_parameters" => Self::expected_argument_types(),
"found_parameters" => arg.value.argument_to_token_type_name(),
},
},
"wrong_arg_type",
);
}
Err(())
},
}
} else {
Err(())
}
}
fn expected_argument_types() -> String {
"Reference".to_owned()
}
}
}
}