Skip to main content

oak_dockerfile/lexer/
token_type.rs

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