Skip to main content

fatou_parser/parser/
options.rs

1//! Explicit, host-independent Julia syntax selection.
2
3/// A Julia syntax edition supported by this parser. Editions are separate from
4/// the runtime versions a package supports.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6pub enum SyntaxEdition {
7    Julia113,
8    Julia114,
9    Julia115,
10}
11
12impl SyntaxEdition {
13    pub const ALL: [Self; 3] = [Self::Julia113, Self::Julia114, Self::Julia115];
14    pub const OLDEST: Self = Self::Julia113;
15    pub const LATEST: Self = Self::Julia115;
16
17    /// Julia's `normalized_edition`: select the newest supported edition no
18    /// greater than the version, clamping at either end of the edition table.
19    pub const fn normalized(major: u32, minor: u32) -> Self {
20        if major > 1 || (major == 1 && minor >= 15) {
21            Self::Julia115
22        } else if major == 1 && minor >= 14 {
23            Self::Julia114
24        } else {
25            Self::Julia113
26        }
27    }
28}
29
30impl Default for SyntaxEdition {
31    fn default() -> Self {
32        Self::LATEST
33    }
34}
35
36impl std::fmt::Display for SyntaxEdition {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        f.write_str(match self {
39            Self::Julia113 => "1.13",
40            Self::Julia114 => "1.14",
41            Self::Julia115 => "1.15",
42        })
43    }
44}
45
46/// An explicit edition must name a supported edition. Project metadata instead
47/// normalizes a Julia version with [`SyntaxEdition::normalized`].
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub struct InvalidSyntaxEdition;
50
51impl std::fmt::Display for InvalidSyntaxEdition {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        f.write_str("expected syntax edition 1.13, 1.14, or 1.15")
54    }
55}
56impl std::error::Error for InvalidSyntaxEdition {}
57
58impl std::str::FromStr for SyntaxEdition {
59    type Err = InvalidSyntaxEdition;
60    fn from_str(value: &str) -> Result<Self, Self::Err> {
61        match value {
62            "1.13" => Ok(Self::Julia113),
63            "1.14" => Ok(Self::Julia114),
64            "1.15" => Ok(Self::Julia115),
65            _ => Err(InvalidSyntaxEdition),
66        }
67    }
68}
69
70/// Resolved parsing options. The parser never discovers projects or invokes Julia.
71#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
72pub struct ParseOptions {
73    pub syntax_edition: SyntaxEdition,
74}