summaryrefslogtreecommitdiffstats
path: root/third_party/rust/weedle2/src/argument.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/weedle2/src/argument.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/weedle2/src/argument.rs')
-rw-r--r--third_party/rust/weedle2/src/argument.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/third_party/rust/weedle2/src/argument.rs b/third_party/rust/weedle2/src/argument.rs
new file mode 100644
index 0000000000..8c0e085f20
--- /dev/null
+++ b/third_party/rust/weedle2/src/argument.rs
@@ -0,0 +1,76 @@
+use crate::attribute::ExtendedAttributeList;
+use crate::common::{Default, Identifier, Punctuated};
+use crate::types::{AttributedType, Type};
+
+/// Parses a list of argument. Ex: `double v1, double v2, double v3, optional double alpha`
+pub type ArgumentList<'a> = Punctuated<Argument<'a>, term!(,)>;
+
+ast_types! {
+ /// Parses an argument. Ex: `double v1|double... v1s`
+ enum Argument<'a> {
+ /// Parses `[attributes]? optional? attributedtype identifier ( = default )?`
+ ///
+ /// Note: `= default` is only allowed if `optional` is present
+ Single(struct SingleArgument<'a> {
+ attributes: Option<ExtendedAttributeList<'a>>,
+ optional: Option<term!(optional)>,
+ type_: AttributedType<'a>,
+ identifier: Identifier<'a>,
+ default: Option<Default<'a>> = nom::combinator::map(
+ nom::combinator::cond(optional.is_some(), weedle!(Option<Default<'a>>)),
+ |default| default.unwrap_or(None)
+ ),
+ }),
+ /// Parses `[attributes]? type... identifier`
+ Variadic(struct VariadicArgument<'a> {
+ attributes: Option<ExtendedAttributeList<'a>>,
+ type_: Type<'a>,
+ ellipsis: term!(...),
+ identifier: Identifier<'a>,
+ }),
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use crate::literal::{DecLit, DefaultValue, IntegerLit};
+ use crate::Parse;
+
+ test!(should_parse_single_argument { "short a" =>
+ "";
+ SingleArgument;
+ attributes.is_none();
+ optional.is_none();
+ identifier.0 == "a";
+ default.is_none();
+ });
+
+ test!(should_parse_variadic_argument { "short... a" =>
+ "";
+ VariadicArgument;
+ attributes.is_none();
+ identifier.0 == "a";
+ });
+
+ test!(should_parse_optional_single_argument { "optional short a" =>
+ "";
+ SingleArgument;
+ attributes.is_none();
+ optional.is_some();
+ identifier.0 == "a";
+ default.is_none();
+ });
+
+ test!(should_parse_optional_single_argument_with_default { "optional short a = 5" =>
+ "";
+ SingleArgument;
+ attributes.is_none();
+ optional.is_some();
+ identifier.0 == "a";
+ default == Some(Default {
+ assign: term!(=),
+ value: DefaultValue::Integer(IntegerLit::Dec(DecLit("5"))),
+ });
+ });
+}