Skip to main content

oak_dockerfile/lexer/
token_type.rs

1use oak_core::{TokenType, UniversalTokenRole};
2
3// pub type DockerfileToken = Token<DockerfileTokenType>;
4
5/// Token types for the Dockerfile lexer.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub enum DockerfileTokenType {
9    /// An identifier (e.g., variable name, image name).
10    Identifier,
11    /// A string literal.
12    String,
13    /// A numeric literal.
14    Number,
15    /// Whitespace (spaces, tabs).
16    Whitespace,
17    /// A newline character.
18    Newline,
19    /// `FROM` instruction.
20    From,
21    /// `RUN` instruction.
22    Run,
23    /// `CMD` instruction.
24    Cmd,
25    /// `LABEL` instruction.
26    Label,
27    /// `MAINTAINER` instruction (deprecated).
28    Maintainer,
29    /// `EXPOSE` instruction.
30    Expose,
31    /// `ENV` instruction.
32    Env,
33    /// `ADD` instruction.
34    Add,
35    /// `COPY` instruction.
36    Copy,
37    /// `ENTRYPOINT` instruction.
38    Entrypoint,
39    /// `VOLUME` instruction.
40    Volume,
41    /// `USER` instruction.
42    User,
43    /// `WORKDIR` instruction.
44    Workdir,
45    /// `ARG` instruction.
46    Arg,
47    /// `ONBUILD` instruction.
48    Onbuild,
49    /// `STOPSIGNAL` instruction.
50    Stopsignal,
51    /// `HEALTHCHECK` instruction.
52    Healthcheck,
53    /// `SHELL` instruction.
54    Shell,
55    /// `AS` keyword (used in `FROM`).
56    As,
57    /// No-op or placeholder.
58    None,
59    /// `interval` keyword (in `HEALTHCHECK`).
60    Interval,
61    /// `timeout` keyword (in `HEALTHCHECK`).
62    Timeout,
63    /// `start-period` keyword (in `HEALTHCHECK`).
64    StartPeriod,
65    /// `retries` keyword (in `HEALTHCHECK`).
66    Retries,
67    /// Single equals sign `=`.
68    Equal,
69    /// Double equals sign `==`.
70    Equals,
71    /// Colon `:`.
72    Colon,
73    /// Comma `,`.
74    Comma,
75    /// Semicolon `;`.
76    Semicolon,
77    /// Dollar sign `$`.
78    Dollar,
79    /// Left bracket `[`.
80    LeftBracket,
81    /// Right bracket `]`.
82    RightBracket,
83    /// Left brace `{`.
84    LeftBrace,
85    /// Right brace `}`.
86    RightBrace,
87    /// Left parenthesis `(`.
88    LeftParen,
89    /// Right parenthesis `)`.
90    RightParen,
91    /// A comment starting with `#`.
92    Comment,
93    /// A file path.
94    Path,
95    /// The root of the Dockerfile.
96    Root,
97    /// A statement or instruction.
98    Statement,
99    /// An error token.
100    Error,
101    /// End of stream.
102    Eof,
103}
104
105impl DockerfileTokenType {
106    /// Returns `true` if this token type is a Dockerfile instruction.
107    pub fn is_instruction(&self) -> bool {
108        matches!(
109            self,
110            Self::From
111                | Self::Run
112                | Self::Cmd
113                | Self::Label
114                | Self::Maintainer
115                | Self::Expose
116                | Self::Env
117                | Self::Add
118                | Self::Copy
119                | Self::Entrypoint
120                | Self::Volume
121                | Self::User
122                | Self::Workdir
123                | Self::Arg
124                | Self::Onbuild
125                | Self::Stopsignal
126                | Self::Healthcheck
127                | Self::Shell
128        )
129    }
130
131    /// Returns `true` if this token type is a trivia token (whitespace, newline, or comment).
132    pub fn is_trivia(&self) -> bool {
133        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
134    }
135}
136
137impl TokenType for DockerfileTokenType {
138    type Role = UniversalTokenRole;
139    const END_OF_STREAM: Self = Self::Eof;
140
141    fn is_ignored(&self) -> bool {
142        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
143    }
144
145    fn role(&self) -> Self::Role {
146        match self {
147            Self::Whitespace => UniversalTokenRole::Whitespace,
148            Self::Newline => UniversalTokenRole::Whitespace,
149            Self::Comment => UniversalTokenRole::Comment,
150            Self::Eof => UniversalTokenRole::Eof,
151            Self::Error => UniversalTokenRole::Error,
152            _ => UniversalTokenRole::None,
153        }
154    }
155}