|
| 1 | +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. |
| 2 | + |
| 3 | +use monch::*; |
| 4 | + |
| 5 | +use crate::CowVec; |
| 6 | +use crate::Partial; |
| 7 | +use crate::VersionBoundKind; |
| 8 | +use crate::VersionPreOrBuild; |
| 9 | +use crate::VersionRange; |
| 10 | +use crate::XRange; |
| 11 | + |
| 12 | +// logical-or ::= ( ' ' ) * '||' ( ' ' ) * |
| 13 | +pub fn logical_or(input: &str) -> ParseResult<&str> { |
| 14 | + delimited(skip_whitespace, tag("||"), skip_whitespace)(input) |
| 15 | +} |
| 16 | + |
| 17 | +// logical-and ::= ( ' ' ) * '&&' ( ' ' ) * |
| 18 | +pub fn logical_and(input: &str) -> ParseResult<&str> { |
| 19 | + delimited(skip_whitespace, tag("&&"), skip_whitespace)(input) |
| 20 | +} |
| 21 | + |
| 22 | +// partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )? |
| 23 | +pub fn partial<'a>( |
| 24 | + xr: impl Fn(&'a str) -> ParseResult<'a, XRange>, |
| 25 | +) -> impl Fn(&'a str) -> ParseResult<'a, Partial> { |
| 26 | + move |input| { |
| 27 | + let (input, major) = xr(input)?; |
| 28 | + let (input, maybe_minor) = maybe(preceded(ch('.'), &xr))(input)?; |
| 29 | + let (input, maybe_patch) = if maybe_minor.is_some() { |
| 30 | + maybe(preceded(ch('.'), &xr))(input)? |
| 31 | + } else { |
| 32 | + (input, None) |
| 33 | + }; |
| 34 | + let (input, qual) = if maybe_patch.is_some() { |
| 35 | + maybe(qualifier)(input)? |
| 36 | + } else { |
| 37 | + (input, None) |
| 38 | + }; |
| 39 | + let qual = qual.unwrap_or_default(); |
| 40 | + Ok(( |
| 41 | + input, |
| 42 | + Partial { |
| 43 | + major, |
| 44 | + minor: maybe_minor.unwrap_or(XRange::Wildcard), |
| 45 | + patch: maybe_patch.unwrap_or(XRange::Wildcard), |
| 46 | + pre: qual.pre, |
| 47 | + build: qual.build, |
| 48 | + }, |
| 49 | + )) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Debug, Clone, Default)] |
| 54 | +pub struct Qualifier { |
| 55 | + pub pre: CowVec<VersionPreOrBuild>, |
| 56 | + pub build: CowVec<VersionPreOrBuild>, |
| 57 | +} |
| 58 | + |
| 59 | +// qualifier ::= ( '-' pre )? ( '+' build )? |
| 60 | +pub fn qualifier(input: &str) -> ParseResult<Qualifier> { |
| 61 | + let (input, pre_parts) = maybe(pre)(input)?; |
| 62 | + let (input, build_parts) = maybe(build)(input)?; |
| 63 | + Ok(( |
| 64 | + input, |
| 65 | + Qualifier { |
| 66 | + pre: pre_parts.unwrap_or_default(), |
| 67 | + build: build_parts.unwrap_or_default(), |
| 68 | + }, |
| 69 | + )) |
| 70 | +} |
| 71 | + |
| 72 | +// pre ::= parts |
| 73 | +fn pre(input: &str) -> ParseResult<CowVec<VersionPreOrBuild>> { |
| 74 | + preceded(maybe(ch('-')), parts)(input) |
| 75 | +} |
| 76 | + |
| 77 | +// build ::= parts |
| 78 | +fn build(input: &str) -> ParseResult<CowVec<VersionPreOrBuild>> { |
| 79 | + preceded(ch('+'), parts)(input) |
| 80 | +} |
| 81 | + |
| 82 | +// parts ::= part ( '.' part ) * |
| 83 | +fn parts(input: &str) -> ParseResult<CowVec<VersionPreOrBuild>> { |
| 84 | + if_true( |
| 85 | + map(separated_list(part, ch('.')), |text| { |
| 86 | + text |
| 87 | + .into_iter() |
| 88 | + .map(VersionPreOrBuild::from_str) |
| 89 | + .collect::<CowVec<_>>() |
| 90 | + }), |
| 91 | + |items| !items.is_empty(), |
| 92 | + )(input) |
| 93 | +} |
| 94 | + |
| 95 | +// part ::= nr | [-0-9A-Za-z]+ |
| 96 | +fn part(input: &str) -> ParseResult<&str> { |
| 97 | + // nr is in the other set, so don't bother checking for it |
| 98 | + if_true( |
| 99 | + take_while(|c| c.is_ascii_alphanumeric() || c == '-'), |
| 100 | + |result| !result.is_empty(), |
| 101 | + )(input) |
| 102 | +} |
| 103 | + |
| 104 | +#[derive(Debug, Clone, Copy)] |
| 105 | +pub enum PrimitiveKind { |
| 106 | + GreaterThan, |
| 107 | + LessThan, |
| 108 | + GreaterThanOrEqual, |
| 109 | + LessThanOrEqual, |
| 110 | + Equal, |
| 111 | +} |
| 112 | + |
| 113 | +pub fn primitive_kind(input: &str) -> ParseResult<PrimitiveKind> { |
| 114 | + or5( |
| 115 | + map(tag(">="), |_| PrimitiveKind::GreaterThanOrEqual), |
| 116 | + map(tag("<="), |_| PrimitiveKind::LessThanOrEqual), |
| 117 | + map(ch('<'), |_| PrimitiveKind::LessThan), |
| 118 | + map(ch('>'), |_| PrimitiveKind::GreaterThan), |
| 119 | + map(ch('='), |_| PrimitiveKind::Equal), |
| 120 | + )(input) |
| 121 | +} |
| 122 | + |
| 123 | +#[derive(Debug, Clone)] |
| 124 | +pub struct Primitive { |
| 125 | + pub kind: PrimitiveKind, |
| 126 | + pub partial: Partial, |
| 127 | +} |
| 128 | + |
| 129 | +impl Primitive { |
| 130 | + pub fn into_version_range(self) -> VersionRange { |
| 131 | + let partial = self.partial; |
| 132 | + match self.kind { |
| 133 | + PrimitiveKind::Equal => partial.as_equal_range(), |
| 134 | + PrimitiveKind::GreaterThan => { |
| 135 | + partial.as_greater_than(VersionBoundKind::Exclusive) |
| 136 | + } |
| 137 | + PrimitiveKind::GreaterThanOrEqual => { |
| 138 | + partial.as_greater_than(VersionBoundKind::Inclusive) |
| 139 | + } |
| 140 | + PrimitiveKind::LessThan => { |
| 141 | + partial.as_less_than(VersionBoundKind::Exclusive) |
| 142 | + } |
| 143 | + PrimitiveKind::LessThanOrEqual => { |
| 144 | + partial.as_less_than(VersionBoundKind::Inclusive) |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +pub fn primitive<'a>( |
| 151 | + partial: impl Fn(&'a str) -> ParseResult<'a, Partial>, |
| 152 | +) -> impl Fn(&'a str) -> ParseResult<'a, Primitive> { |
| 153 | + move |input| { |
| 154 | + let (input, kind) = primitive_kind(input)?; |
| 155 | + let (input, _) = skip_whitespace(input)?; |
| 156 | + let (input, partial) = partial(input)?; |
| 157 | + Ok((input, Primitive { kind, partial })) |
| 158 | + } |
| 159 | +} |
0 commit comments