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
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <hello@stalw.art>
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/
use crate::compiler::{
grammar::{instruction::CompilerState, Capability, Comparator},
lexer::{tokenizer::TokenInfo, word::Word, Token},
CompileError, ErrorType, Value, VariableType,
};
use crate::compiler::grammar::{test::Test, MatchType};
/*
Usage: hasflag [MATCH-TYPE] [COMPARATOR]
[<variable-list: string-list>]
<list-of-flags: string-list>
*/
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
any(test, feature = "serde"),
derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub(crate) struct TestHasFlag {
pub comparator: Comparator,
pub match_type: MatchType,
pub variable_list: Vec<VariableType>,
pub flags: Vec<Value>,
pub is_not: bool,
}
impl CompilerState<'_> {
pub(crate) fn parse_test_hasflag(&mut self) -> Result<Test, CompileError> {
let mut match_type = MatchType::Is;
let mut comparator = Comparator::AsciiCaseMap;
let mut is_local = false;
let mut maybe_variables;
loop {
let token_info = self.tokens.unwrap_next()?;
match token_info.token {
Token::Tag(
word @ (Word::Is
| Word::Contains
| Word::Matches
| Word::Value
| Word::Count
| Word::Regex),
) => {
self.validate_argument(
1,
match word {
Word::Value | Word::Count => Capability::Relational.into(),
Word::Regex => Capability::Regex.into(),
Word::List => Capability::ExtLists.into(),
_ => None,
},
token_info.line_num,
token_info.line_pos,
)?;
match_type = self.parse_match_type(word)?;
}
Token::Tag(Word::Comparator) => {
self.validate_argument(2, None, token_info.line_num, token_info.line_pos)?;
comparator = self.parse_comparator()?;
}
Token::Tag(Word::Local) => {
is_local = true;
}
_ => {
maybe_variables = self.parse_strings_token(token_info)?;
break;
}
}
}
match self.tokens.peek() {
Some(Ok(TokenInfo {
token: Token::StringConstant(_) | Token::StringVariable(_) | Token::BracketOpen,
line_num,
line_pos,
})) => {
if !maybe_variables.is_empty() {
let line_num = *line_num;
let line_pos = *line_pos;
let mut variable_list = Vec::with_capacity(maybe_variables.len());
for variable in maybe_variables {
match variable {
Value::Text(var_name) => {
variable_list.push(
self.register_variable(var_name.to_string(), is_local)
.map_err(|error_type| CompileError {
line_num,
line_pos,
error_type,
})?,
);
}
_ => {
return Err(self
.tokens
.unwrap_next()?
.custom(ErrorType::ExpectedConstantString))
}
}
}
let mut flags = self.parse_strings(false)?;
self.validate_match(&match_type, &mut flags)?;
Ok(Test::HasFlag(TestHasFlag {
comparator,
match_type,
variable_list,
flags,
is_not: false,
}))
} else {
Err(self
.tokens
.unwrap_next()?
.custom(ErrorType::ExpectedConstantString))
}
}
_ => {
self.validate_match(&match_type, &mut maybe_variables)?;
Ok(Test::HasFlag(TestHasFlag {
comparator,
match_type,
variable_list: Vec::new(),
flags: maybe_variables,
is_not: false,
}))
}
}
}
}