phenotyper 0.3.0

Core compiler library for the Phenotyper structural artifact definition language
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// SPDX-License-Identifier: Apache-2.0
//! Lexer module — tokenization and source extraction for Phenotyper v1.
//!
//! The lexer converts raw source text (`.pht` or extracted from `.md`) into a
//! stream of [`SpannedToken`]s. It handles:
//! - All v1 keywords and primitive type names (lexically reserved)
//! - Identifiers, string literals with escape sequences
//! - Structural punctuation and operators
//! - Line and block comments (discarded)
//! - Whitespace (discarded)

pub mod source_map;
mod token;

pub use source_map::{SourceBlock, SourceMap, extract_pht_blocks};
pub use token::{Keyword, Span, SpannedToken, Token};

use crate::diagnostic::{Diagnostic, Severity};

/// Lex an input string into a sequence of spanned tokens.
///
/// Comments and whitespace are consumed but not emitted.
/// Returns `(tokens, diagnostics)`. If diagnostics contain errors,
/// the token stream may be incomplete.
pub fn lex(source: &str, file: &str) -> (Vec<SpannedToken>, Vec<Diagnostic>) {
    let mut lexer = LexerState::new(source, file);
    let mut tokens = Vec::new();
    let mut diagnostics = Vec::new();

    loop {
        lexer.skip_whitespace();
        if lexer.is_eof() {
            tokens.push(lexer.make_token(Token::Eof, 0));
            break;
        }

        match lexer.next_token() {
            Ok(Some(tok)) => tokens.push(tok),
            Ok(None) => { /* comment or whitespace, already consumed */ }
            Err(diag) => {
                diagnostics.push(diag);
                // Skip the offending character and continue.
                lexer.advance();
            }
        }
    }

    (tokens, diagnostics)
}

// ---------------------------------------------------------------------------
// Internal lexer state
// ---------------------------------------------------------------------------

struct LexerState<'a> {
    source: &'a str,
    file: String,
    pos: usize,
    line: usize,
    col: usize,
}

impl<'a> LexerState<'a> {
    fn new(source: &'a str, file: &str) -> Self {
        Self {
            source,
            file: file.to_string(),
            pos: 0,
            line: 1,
            col: 1,
        }
    }

    fn is_eof(&self) -> bool {
        self.pos >= self.source.len()
    }

    fn peek(&self) -> Option<char> {
        self.source[self.pos..].chars().next()
    }

    fn peek_next(&self) -> Option<char> {
        let mut chars = self.source[self.pos..].chars();
        chars.next();
        chars.next()
    }

    fn advance(&mut self) -> Option<char> {
        let ch = self.peek()?;
        self.pos += ch.len_utf8();
        if ch == '\n' {
            self.line += 1;
            self.col = 1;
        } else {
            self.col += 1;
        }
        Some(ch)
    }

    fn skip_whitespace(&mut self) {
        while let Some(ch) = self.peek() {
            if ch.is_ascii_whitespace() {
                self.advance();
            } else {
                break;
            }
        }
    }

    fn make_token(&self, kind: Token, len: usize) -> SpannedToken {
        // `len` is the number of source characters consumed for this token.
        // The span end column points to the last character of the token.
        let end_col = if len > 0 { self.col - 1 } else { self.col };
        SpannedToken {
            token: kind,
            span: Span {
                file: self.file.clone(),
                start_line: self.line,
                start_col: if len > 0 {
                    self.col.saturating_sub(len)
                } else {
                    self.col
                },
                end_line: self.line,
                end_col,
            },
        }
    }

    fn make_span_from(&self, start_line: usize, start_col: usize) -> Span {
        Span {
            file: self.file.clone(),
            start_line,
            start_col,
            end_line: self.line,
            end_col: if self.col > 1 { self.col - 1 } else { 1 },
        }
    }

    fn error_at(&self, message: impl Into<String>) -> Diagnostic {
        Diagnostic {
            severity: Severity::Error,
            summary: message.into(),
            file: self.file.clone(),
            line: self.line,
            col: self.col,
            explanation: None,
            suggestion: None,
        }
    }

    /// Try to consume the next token.
    /// Returns:
    /// - `Ok(Some(token))` for a real token
    /// - `Ok(None)` if a comment was consumed (caller should continue)
    /// - `Err(diagnostic)` on error
    fn next_token(&mut self) -> Result<Option<SpannedToken>, Diagnostic> {
        let start_line = self.line;
        let start_col = self.col;
        let ch = self.peek().unwrap();

        match ch {
            // --- String literals ---
            '"' => self.lex_string_literal(start_line, start_col).map(Some),

            // --- Two-character lookahead: comments and slash ---
            '/' => {
                match self.peek_next() {
                    Some('/') => {
                        self.lex_line_comment();
                        Ok(None)
                    }
                    Some('*') => {
                        self.lex_block_comment()?;
                        Ok(None)
                    }
                    _ => {
                        // Slash for namespace paths
                        self.advance();
                        Ok(Some(SpannedToken {
                            token: Token::Slash,
                            span: self.make_span_from(start_line, start_col),
                        }))
                    }
                }
            }

            // --- Single-character tokens ---
            ':' => self.single_char_token(Token::Colon, start_line, start_col),
            ';' => self.single_char_token(Token::Semicolon, start_line, start_col),
            ',' => self.single_char_token(Token::Comma, start_line, start_col),
            '{' => self.single_char_token(Token::LeftBrace, start_line, start_col),
            '}' => self.single_char_token(Token::RightBrace, start_line, start_col),
            '[' => self.single_char_token(Token::LeftBracket, start_line, start_col),
            ']' => self.single_char_token(Token::RightBracket, start_line, start_col),
            '(' => self.single_char_token(Token::LeftParen, start_line, start_col),
            ')' => self.single_char_token(Token::RightParen, start_line, start_col),
            '@' => self.single_char_token(Token::At, start_line, start_col),
            '+' => self.single_char_token(Token::Plus, start_line, start_col),
            '*' => self.single_char_token(Token::Star, start_line, start_col),

            // --- Identifiers and keywords ---
            c if is_ident_start(c) => Ok(Some(self.lex_identifier(start_line, start_col))),

            // --- Unknown character ---
            _ => Err(self.error_at(format!("unexpected character: '{ch}'"))),
        }
    }

    fn single_char_token(
        &mut self,
        kind: Token,
        start_line: usize,
        start_col: usize,
    ) -> Result<Option<SpannedToken>, Diagnostic> {
        self.advance();
        Ok(Some(SpannedToken {
            token: kind,
            span: self.make_span_from(start_line, start_col),
        }))
    }

    fn lex_identifier(&mut self, start_line: usize, start_col: usize) -> SpannedToken {
        let start_pos = self.pos;
        while let Some(ch) = self.peek() {
            if is_ident_continue(ch) {
                self.advance();
            } else {
                break;
            }
        }
        let text = &self.source[start_pos..self.pos];
        let token = match classify_keyword(text) {
            Some(kw) => Token::Keyword(kw),
            None => Token::Identifier(text.to_string()),
        };
        SpannedToken {
            token,
            span: self.make_span_from(start_line, start_col),
        }
    }

    fn lex_string_literal(
        &mut self,
        start_line: usize,
        start_col: usize,
    ) -> Result<SpannedToken, Diagnostic> {
        // Consume opening quote
        self.advance();

        let mut value = String::new();

        loop {
            match self.peek() {
                None => {
                    return Err(Diagnostic {
                        severity: Severity::Error,
                        summary: "unterminated string literal".to_string(),
                        file: self.file.clone(),
                        line: start_line,
                        col: start_col,
                        explanation: Some(
                            "string literal was opened here but never closed".to_string(),
                        ),
                        suggestion: Some("add a closing '\"'".to_string()),
                    });
                }
                Some('"') => {
                    self.advance(); // consume closing quote
                    return Ok(SpannedToken {
                        token: Token::StringLiteral(value),
                        span: self.make_span_from(start_line, start_col),
                    });
                }
                Some('\\') => {
                    self.advance(); // consume backslash
                    match self.peek() {
                        Some('"') => {
                            value.push('"');
                            self.advance();
                        }
                        Some('\\') => {
                            value.push('\\');
                            self.advance();
                        }
                        Some('n') => {
                            value.push('\n');
                            self.advance();
                        }
                        Some('r') => {
                            value.push('\r');
                            self.advance();
                        }
                        Some('t') => {
                            value.push('\t');
                            self.advance();
                        }
                        Some(c) => {
                            return Err(self.error_at(format!("unknown escape sequence: '\\{c}'")));
                        }
                        None => {
                            return Err(
                                self.error_at("unterminated escape sequence at end of input")
                            );
                        }
                    }
                }
                Some('\n') => {
                    return Err(Diagnostic {
                        severity: Severity::Error,
                        summary: "unterminated string literal (newline in string)".to_string(),
                        file: self.file.clone(),
                        line: start_line,
                        col: start_col,
                        explanation: Some("string literals cannot span multiple lines".to_string()),
                        suggestion: Some(
                            "use \\n for newlines, or close the string before the line break"
                                .to_string(),
                        ),
                    });
                }
                Some(ch) => {
                    value.push(ch);
                    self.advance();
                }
            }
        }
    }

    fn lex_line_comment(&mut self) {
        // Consume the two slashes
        self.advance();
        self.advance();
        // Consume until newline or EOF
        while let Some(ch) = self.peek() {
            if ch == '\n' {
                self.advance();
                break;
            }
            self.advance();
        }
    }

    fn lex_block_comment(&mut self) -> Result<(), Diagnostic> {
        let start_line = self.line;
        let start_col = self.col;
        // Consume /*
        self.advance();
        self.advance();

        loop {
            match self.peek() {
                None => {
                    return Err(Diagnostic {
                        severity: Severity::Error,
                        summary: "unterminated block comment".to_string(),
                        file: self.file.clone(),
                        line: start_line,
                        col: start_col,
                        explanation: Some(
                            "block comment was opened here but never closed".to_string(),
                        ),
                        suggestion: Some("add a closing '*/'".to_string()),
                    });
                }
                Some('*') => {
                    if self.peek_next() == Some('/') {
                        self.advance(); // consume *
                        self.advance(); // consume /
                        return Ok(());
                    }
                    self.advance();
                }
                _ => {
                    self.advance();
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Character classification
// ---------------------------------------------------------------------------

fn is_ident_start(ch: char) -> bool {
    ch.is_ascii_alphabetic() || ch == '_'
}

fn is_ident_continue(ch: char) -> bool {
    ch.is_ascii_alphanumeric() || ch == '_'
}

/// Classify an identifier text as a keyword, or return `None` for a plain identifier.
fn classify_keyword(text: &str) -> Option<Keyword> {
    match text {
        // Language keywords
        "namespace" => Some(Keyword::Namespace),
        "uses" => Some(Keyword::Uses),
        "type" => Some(Keyword::Type),
        "plural" => Some(Keyword::Plural),
        "required" => Some(Keyword::Required),
        "optional" => Some(Keyword::Optional),
        // Primitive type names (lexically reserved per DEC-007)
        "string" => Some(Keyword::String),
        "int64" => Some(Keyword::Int64),
        "real64" => Some(Keyword::Real64),
        "bool" => Some(Keyword::Bool),
        "date" => Some(Keyword::Date),
        "time" => Some(Keyword::Time),
        "datetime" => Some(Keyword::DateTime),
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests;