Skip to main content

logicaffeine_language/analysis/
discovery.rs

1//! Discovery pass for type and policy extraction.
2//!
3//! Runs before main parsing to scan tokens for type and policy definitions.
4//! Populates [`TypeRegistry`] and [`PolicyRegistry`] for use during parsing.
5//!
6//! # Discovery Targets
7//!
8//! | Block | Pattern | Result |
9//! |-------|---------|--------|
10//! | `## Definition` | "A Stack is a generic collection." | `TypeDef::Generic` |
11//! | `## Definition` | "A User is a structure." | `TypeDef::Struct` |
12//! | `## Definition` | "A Shape is an enum." | `TypeDef::Enum` |
13//! | `## Policy` | "A user can publish if they are admin." | `CapabilityDef` |
14//!
15//! # Key Function
16//!
17//! [`DiscoveryPass::run`] - Execute the discovery pass and return registries.
18
19use super::policy::{CapabilityDef, PolicyCondition, PolicyRegistry, PredicateDef};
20use super::registry::{FieldDef, FieldType, TypeDef, TypeRegistry, VariantDef};
21use crate::token::{BlockType, Span, Token, TokenType};
22use logicaffeine_base::{Interner, Symbol};
23
24/// Result of running the discovery pass
25pub struct DiscoveryResult {
26    pub types: TypeRegistry,
27    pub policies: PolicyRegistry,
28}
29
30/// Discovery pass that scans tokens before main parsing to build a TypeRegistry.
31///
32/// This pass looks for type definitions in `## Definition` blocks:
33/// - "A Stack is a generic collection." → Generic type
34/// - "A User is a structure." → Struct type
35/// - "A Shape is an enum." → Enum type
36///
37/// Phase 50: Also scans `## Policy` blocks for security predicates and capabilities.
38pub struct DiscoveryPass<'a> {
39    tokens: &'a [Token],
40    pos: usize,
41    interner: &'a mut Interner,
42}
43
44impl<'a> DiscoveryPass<'a> {
45    pub fn new(tokens: &'a [Token], interner: &'a mut Interner) -> Self {
46        Self {
47            tokens,
48            pos: 0,
49            interner,
50        }
51    }
52
53    /// Run discovery pass, returning populated TypeRegistry
54    /// (Backward compatible - returns only TypeRegistry)
55    pub fn run(&mut self) -> TypeRegistry {
56        self.run_full().types
57    }
58
59    /// Phase 50: Run discovery pass, returning both TypeRegistry and PolicyRegistry
60    pub fn run_full(&mut self) -> DiscoveryResult {
61        let mut type_registry = TypeRegistry::with_primitives(self.interner);
62        let mut policy_registry = PolicyRegistry::new();
63        let mut view_variants: Vec<VariantDef> = Vec::new();
64
65        while self.pos < self.tokens.len() {
66            // Look for Definition blocks
67            if self.check_block_header(BlockType::Definition) {
68                self.advance(); // consume ## Definition
69                self.scan_definition_block(&mut type_registry);
70            } else if self.check_block_header(BlockType::TypeDef) {
71                // Inline type definition: ## A Point has: or ## A Color is one of:
72                // The article is part of the block header, so don't skip it
73                self.advance(); // consume ## A/An
74                self.parse_type_definition_inline(&mut type_registry);
75            } else if self.check_block_header(BlockType::Policy) {
76                // Phase 50: Security policy definitions
77                self.advance(); // consume ## Policy
78                self.scan_policy_block(&mut policy_registry);
79            } else if self.check_block_header(BlockType::Session) {
80                // `## Session` declares a RECORD, in the same words a record is declared in, and
81                // it is registered HERE, from the tokens, so the parse that follows knows it. A
82                // `## Store`'s record is not: the lowering types it by the rule that builds it and
83                // registers it into the parser's own registry (`lgx_lower::stores`), which is the
84                // registry the engines read once the parse is done (`Parser::into_type_registry`).
85                self.advance(); // consume ## Session
86                self.scan_session_block(&mut type_registry);
87            } else if self.check_block_header(BlockType::Function) {
88                // A `-> View` template declares a variant of the program's `__View`: a page's
89                // lowered code holds a View as that DATA (`parser::lgx_lower::views`), and the
90                // enum is registered HERE for the reason `## Session`'s record is — see that comment.
91                self.advance(); // consume ## To
92                if let Some(variant) = self.scan_view_template() {
93                    view_variants.push(variant);
94                }
95            } else if self.check_block_header(BlockType::Requires) {
96                // Requires blocks contain dependency metadata, not type definitions.
97                // Skip to next block header.
98                self.advance(); // consume ## Requires
99                while self.pos < self.tokens.len() {
100                    if matches!(
101                        self.tokens.get(self.pos),
102                        Some(Token {
103                            kind: TokenType::BlockHeader { .. },
104                            ..
105                        })
106                    ) {
107                        break;
108                    }
109                    self.advance();
110                }
111            } else {
112                self.advance();
113            }
114        }
115
116        if !view_variants.is_empty() {
117            let none = self
118                .interner
119                .intern(crate::parser::lgx_lower::empty_view_variant_name());
120            view_variants.insert(
121                0,
122                VariantDef {
123                    name: none,
124                    fields: Vec::new(),
125                },
126            );
127            type_registry.register(
128                self.interner
129                    .intern(crate::parser::lgx_lower::view_type_name()),
130                TypeDef::Enum {
131                    variants: view_variants,
132                    generics: Vec::new(),
133                    is_portable: false,
134                    is_shared: false,
135                },
136            );
137        }
138
139        DiscoveryResult {
140            types: type_registry,
141            policies: policy_registry,
142        }
143    }
144
145    /// `## To f (p: T) and (q: U) -> View:` with a markup body — the variant `__View_f` carrying
146    /// one field per parameter, a `View` parameter as the enum itself. Read the way the parser
147    /// reads a function header (name, `(name: Type)` groups joined by `and` or a preposition or
148    /// separated by commas, `-> View`, `:`); anything else is not a template and declares nothing.
149    /// The lowering refuses, by the template's name, any template this reading disagrees with.
150    fn scan_view_template(&mut self) -> Option<VariantDef> {
151        let name = self.header_word()?;
152        let mut fields = Vec::new();
153        while self.check_lparen() {
154            self.advance();
155            if self.check_rparen() {
156                self.advance();
157                break;
158            }
159            loop {
160                let param = self.header_word()?;
161                if !self.check_colon() {
162                    return None;
163                }
164                self.advance();
165                let ty = match self.consume_field_type() {
166                    FieldType::Primitive(t) | FieldType::Named(t)
167                        if self.interner.resolve(t) == "View" =>
168                    {
169                        FieldType::Named(
170                            self.interner
171                                .intern(crate::parser::lgx_lower::view_type_name()),
172                        )
173                    }
174                    other => other,
175                };
176                fields.push(FieldDef {
177                    name: param,
178                    ty,
179                    is_public: true,
180                });
181                if self.check_comma() {
182                    self.advance();
183                    continue;
184                }
185                break;
186            }
187            if !self.check_rparen() {
188                return None;
189            }
190            self.advance();
191            if self.check_and()
192                || matches!(
193                    self.peek(),
194                    Some(Token {
195                        kind: TokenType::Preposition(_) | TokenType::From,
196                        ..
197                    })
198                )
199            {
200                self.advance();
201            }
202        }
203        if !matches!(
204            self.peek(),
205            Some(Token {
206                kind: TokenType::Arrow,
207                ..
208            })
209        ) {
210            return None;
211        }
212        self.advance();
213        if !self.check_word("View") {
214            return None;
215        }
216        self.advance();
217        if !self.check_colon() {
218            return None;
219        }
220        self.advance();
221        let markup = matches!(
222            self.peek(),
223            Some(Token {
224                kind: TokenType::Indent,
225                ..
226            })
227        ) && matches!(
228            self.tokens.get(self.pos + 1),
229            Some(Token {
230                kind: TokenType::RawBlock {
231                    kind: crate::token::RawKind::View,
232                    ..
233                },
234                ..
235            })
236        );
237        if !markup {
238            return None;
239        }
240        let variant = crate::parser::lgx_lower::view_variant_name(self.interner.resolve(name));
241        Some(VariantDef {
242            name: self.interner.intern(&variant),
243            fields,
244        })
245    }
246
247    /// A name in a function header, as the parser's `expect_identifier` reads it: a word's own
248    /// symbol, or its lexeme.
249    fn header_word(&mut self) -> Option<Symbol> {
250        let token = self.peek()?;
251        let sym = match &token.kind {
252            TokenType::Noun(s) | TokenType::ProperName(s) | TokenType::Adjective(s) => *s,
253            TokenType::Colon
254            | TokenType::LParen
255            | TokenType::RParen
256            | TokenType::Arrow
257            | TokenType::Newline
258            | TokenType::Indent
259            | TokenType::Dedent
260            | TokenType::EOF
261            | TokenType::BlockHeader { .. } => return None,
262            _ => token.lexeme,
263        };
264        self.advance();
265        Some(sym)
266    }
267
268    /// `## Session` — the fields a visitor carries between requests, registered as `Session`.
269    ///
270    /// The field syntax is a record's, so it is read by a record's own field scanner rather than
271    /// a second one written beside it: two scanners for one syntax is how the two drift.
272    fn scan_session_block(&mut self, registry: &mut TypeRegistry) {
273        let name = self.interner.intern("Session");
274        let mut spans = Vec::new();
275        let mut fields = self.parse_struct_fields_with_params(&[], Some(&mut spans));
276        if !fields.is_empty() {
277            // ★ EVERY SESSION CARRIES A CSRF NONCE, declared or not. It is added HERE, with the
278            // author's own fields, so it rides in the same cookie and through the same payload
279            // encoder — there is no second place for it to be forgotten. See
280            // `SESSION_NONCE_FIELD` for why the token is bound to a nonce rather than to the
281            // session's contents.
282            fields.push(crate::analysis::registry::FieldDef {
283                name: self
284                    .interner
285                    .intern(crate::parser::lgx_lower::SESSION_NONCE_FIELD),
286                ty: crate::analysis::registry::FieldType::Primitive(self.interner.intern("Text")),
287                is_public: true,
288            });
289            for (field, span) in spans {
290                registry.set_field_span(name, field, span);
291            }
292            registry.register(
293                name,
294                TypeDef::Struct {
295                    fields,
296                    generics: Vec::new(),
297                    is_portable: false,
298                    is_shared: false,
299                },
300            );
301        }
302    }
303
304    fn check_block_header(&self, expected: BlockType) -> bool {
305        matches!(
306            self.tokens.get(self.pos),
307            Some(Token { kind: TokenType::BlockHeader { block_type }, .. })
308            if *block_type == expected
309        )
310    }
311
312    fn scan_definition_block(&mut self, registry: &mut TypeRegistry) {
313        // Scan until next block header or EOF
314        while self.pos < self.tokens.len() {
315            if matches!(
316                self.peek(),
317                Some(Token {
318                    kind: TokenType::BlockHeader { .. },
319                    ..
320                })
321            ) {
322                break;
323            }
324
325            // Look for "A [Name] is a..." pattern
326            if self.check_article() {
327                self.try_parse_type_definition(registry);
328            } else {
329                self.advance();
330            }
331        }
332    }
333
334    /// Phase 50: Scan policy block for predicate and capability definitions
335    /// Patterns:
336    /// - "A User is admin if the user's role equals \"admin\"."
337    /// - "A User can publish the Document if the user is admin OR the user equals the document's owner."
338    fn scan_policy_block(&mut self, registry: &mut PolicyRegistry) {
339        while self.pos < self.tokens.len() {
340            if matches!(
341                self.peek(),
342                Some(Token {
343                    kind: TokenType::BlockHeader { .. },
344                    ..
345                })
346            ) {
347                break;
348            }
349
350            // Skip newlines and indentation
351            if self.check_newline() || self.check_indent() || self.check_dedent() {
352                self.advance();
353                continue;
354            }
355
356            // Look for "A [Type] is [predicate] if..." or "A [Type] can [action] ..."
357            if self.check_article() {
358                self.try_parse_policy_definition(registry);
359            } else {
360                self.advance();
361            }
362        }
363    }
364
365    /// Phase 50: Parse a policy definition
366    fn try_parse_policy_definition(&mut self, registry: &mut PolicyRegistry) {
367        self.advance(); // consume article
368
369        // Get subject type name (e.g., "User")
370        let subject_type = match self.consume_noun_or_proper() {
371            Some(sym) => sym,
372            None => return,
373        };
374
375        // Determine if predicate ("is admin") or capability ("can publish")
376        if self.check_copula() {
377            // "A User is admin if..."
378            self.advance(); // consume "is"
379
380            // Get predicate name (e.g., "admin")
381            let predicate_name = match self.consume_noun_or_proper() {
382                Some(sym) => sym,
383                None => return,
384            };
385
386            // Expect "if"
387            if !self.check_word("if") {
388                self.skip_to_period();
389                return;
390            }
391            self.advance(); // consume "if"
392
393            // Handle multi-line condition (colon followed by indented lines)
394            if self.check_colon() {
395                self.advance();
396            }
397            if self.check_newline() {
398                self.advance();
399            }
400            if self.check_indent() {
401                self.advance();
402            }
403
404            // Parse condition
405            let condition = self.parse_policy_condition();
406
407            registry.register_predicate(PredicateDef {
408                subject_type,
409                predicate_name,
410                condition,
411            });
412
413            self.skip_to_period();
414        } else if self.check_word("can") {
415            // "A User can publish the Document if..."
416            self.advance(); // consume "can"
417
418            // Get action name (e.g., "publish", "edit"). For a verb action take its
419            // LEMMA — that is exactly what the `Check ... can <verb>` site resolves
420            // (parser), so the registered and looked-up symbols agree. Using
421            // `consume_noun_or_proper` first would capture the verb LEXEME instead and
422            // every capability check would miss ("No capability 'Edit' defined").
423            let action = match self.peek() {
424                Some(Token {
425                    kind: TokenType::Verb { lemma, .. },
426                    ..
427                }) => {
428                    let sym = *lemma;
429                    self.advance();
430                    sym
431                }
432                _ => match self.consume_noun_or_proper() {
433                    Some(sym) => sym,
434                    None => return,
435                },
436            };
437
438            // Skip "the" article if present
439            if self.check_article() {
440                self.advance();
441            }
442
443            // Get object type (e.g., "Document")
444            let object_type = match self.consume_noun_or_proper() {
445                Some(sym) => sym,
446                None => return,
447            };
448
449            // Expect "if"
450            if !self.check_word("if") {
451                self.skip_to_period();
452                return;
453            }
454            self.advance(); // consume "if"
455
456            // Parse condition (may include colon for multi-line)
457            if self.check_colon() {
458                self.advance();
459            }
460            if self.check_newline() {
461                self.advance();
462            }
463            if self.check_indent() {
464                self.advance();
465            }
466
467            let condition = self.parse_policy_condition();
468
469            registry.register_capability(CapabilityDef {
470                subject_type,
471                action,
472                object_type,
473                condition,
474            });
475
476            // Skip to end of definition (may span multiple lines)
477            self.skip_policy_definition();
478        } else {
479            self.skip_to_period();
480        }
481    }
482
483    /// Phase 50: Parse a policy condition
484    /// Handles: field comparisons, predicate references, and OR/AND combinators
485    fn parse_policy_condition(&mut self) -> PolicyCondition {
486        // Left-fold the FULL n-ary chain. Returning after the first connective
487        // silently dropped every conjunct/disjunct past the second atom, making
488        // an AND-policy over-permissive and an OR-policy over-restrictive.
489        let mut acc = self.parse_atomic_condition();
490
491        // Check for OR/AND combinators
492        loop {
493            // Skip newlines between conditions
494            while self.check_newline() {
495                self.advance();
496            }
497
498            // Handle ", AND" or ", OR" patterns
499            if self.check_comma() {
500                self.advance(); // consume comma
501                                // Skip whitespace after comma
502                while self.check_newline() {
503                    self.advance();
504                }
505            }
506
507            if self.check_word("AND") {
508                self.advance();
509                // Skip newlines after AND
510                while self.check_newline() {
511                    self.advance();
512                }
513                let right = self.parse_atomic_condition();
514                acc = PolicyCondition::And(Box::new(acc), Box::new(right));
515            } else if self.check_word("OR") {
516                self.advance();
517                // Skip newlines after OR
518                while self.check_newline() {
519                    self.advance();
520                }
521                let right = self.parse_atomic_condition();
522                acc = PolicyCondition::Or(Box::new(acc), Box::new(right));
523            } else {
524                break;
525            }
526        }
527
528        acc
529    }
530
531    /// Phase 50: Parse an atomic condition
532    fn parse_atomic_condition(&mut self) -> PolicyCondition {
533        // Skip "The" article if present
534        if self.check_article() {
535            self.advance();
536        }
537
538        // Get the subject reference (e.g., "user" or "user's role")
539        let subject_ref = match self.consume_noun_or_proper() {
540            Some(sym) => sym,
541            None => {
542                return PolicyCondition::FieldEquals {
543                    field: self.interner.intern("unknown"),
544                    value: self.interner.intern("unknown"),
545                    is_string_literal: false,
546                }
547            }
548        };
549
550        // Check if it's a field access ("'s role") or a predicate ("is admin")
551        if self.check_possessive() {
552            self.advance(); // consume "'s"
553
554            // Get field name
555            let field = match self.consume_noun_or_proper() {
556                Some(sym) => sym,
557                None => {
558                    return PolicyCondition::FieldEquals {
559                        field: self.interner.intern("unknown"),
560                        value: self.interner.intern("unknown"),
561                        is_string_literal: false,
562                    }
563                }
564            };
565
566            // Expect "equals"
567            if self.check_word("equals") {
568                self.advance();
569
570                // First try an object-field RHS: `the document's owner`, giving a
571                // cross-field comparison `self.<field> == object.<obj_field>`. Only a
572                // literal/identifier RHS falls through to `FieldEquals`.
573                let checkpoint = self.pos;
574                if self.check_article() {
575                    self.advance();
576                }
577                if let Some(obj_ref) = self.consume_noun_or_proper() {
578                    if self.check_possessive() {
579                        self.advance(); // consume "'s"
580                        if let Some(obj_field) = self.consume_noun_or_proper() {
581                            return PolicyCondition::SubjectFieldEqualsObjectField {
582                                subject_field: field,
583                                object: obj_ref,
584                                object_field: obj_field,
585                            };
586                        }
587                    }
588                }
589                // Not an object-field reference — rewind and take a plain value.
590                self.pos = checkpoint;
591                let (value, is_string_literal) = self.consume_value();
592
593                return PolicyCondition::FieldEquals {
594                    field,
595                    value,
596                    is_string_literal,
597                };
598            }
599        } else if self.check_copula() {
600            // "user is admin"
601            self.advance(); // consume "is"
602
603            // Get predicate name
604            let predicate = match self.consume_noun_or_proper() {
605                Some(sym) => sym,
606                None => {
607                    return PolicyCondition::FieldEquals {
608                        field: self.interner.intern("unknown"),
609                        value: self.interner.intern("unknown"),
610                        is_string_literal: false,
611                    }
612                }
613            };
614
615            return PolicyCondition::Predicate {
616                subject: subject_ref,
617                predicate,
618            };
619        } else if self.check_word("equals") {
620            // "user equals the document's owner"
621            self.advance(); // consume "equals"
622
623            // Skip "the" if present
624            if self.check_article() {
625                self.advance();
626            }
627
628            // Check for object field reference: "document's owner"
629            if let Some(obj_ref) = self.consume_noun_or_proper() {
630                if self.check_possessive() {
631                    self.advance(); // consume "'s"
632                    if let Some(field) = self.consume_noun_or_proper() {
633                        return PolicyCondition::ObjectFieldEquals {
634                            subject: subject_ref,
635                            object: obj_ref,
636                            field,
637                        };
638                    }
639                }
640            }
641        }
642
643        // Fallback: unknown condition
644        PolicyCondition::FieldEquals {
645            field: self.interner.intern("unknown"),
646            value: self.interner.intern("unknown"),
647            is_string_literal: false,
648        }
649    }
650
651    /// Consume a value (string literal or identifier), returning the symbol and whether it was a string literal
652    fn consume_value(&mut self) -> (Symbol, bool) {
653        if let Some(Token {
654            kind: TokenType::StringLiteral(sym),
655            ..
656        }) = self.peek()
657        {
658            let s = *sym;
659            self.advance();
660            (s, true)
661        } else if let Some(sym) = self.consume_noun_or_proper() {
662            (sym, false)
663        } else {
664            (self.interner.intern("unknown"), false)
665        }
666    }
667
668    /// Check for possessive marker ('s)
669    fn check_possessive(&self) -> bool {
670        matches!(
671            self.peek(),
672            Some(Token {
673                kind: TokenType::Possessive,
674                ..
675            })
676        )
677    }
678
679    /// Skip to end of a multi-line policy definition
680    fn skip_policy_definition(&mut self) {
681        let mut depth = 0;
682        while self.pos < self.tokens.len() {
683            if self.check_indent() {
684                depth += 1;
685            } else if self.check_dedent() {
686                if depth == 0 {
687                    break;
688                }
689                depth -= 1;
690            }
691            if self.check_period() && depth == 0 {
692                self.advance();
693                break;
694            }
695            if matches!(
696                self.peek(),
697                Some(Token {
698                    kind: TokenType::BlockHeader { .. },
699                    ..
700                })
701            ) {
702                break;
703            }
704            self.advance();
705        }
706    }
707
708    /// Parse inline type definition where article was part of block header (## A Point has:)
709    fn parse_type_definition_inline(&mut self, registry: &mut TypeRegistry) {
710        // Don't skip article - it was part of the block header
711        self.parse_type_definition_body(registry);
712    }
713
714    fn try_parse_type_definition(&mut self, registry: &mut TypeRegistry) {
715        self.advance(); // skip article
716        self.parse_type_definition_body(registry);
717    }
718
719    fn parse_type_definition_body(&mut self, registry: &mut TypeRegistry) {
720        // Phase 47/49: Check for pre-type modifiers: "A portable Config has:" or "A shared Config has:"
721        let mut is_portable = false;
722        let mut is_shared = false;
723        loop {
724            if self.check_portable() {
725                is_portable = true;
726                self.advance();
727            } else if self.check_shared() {
728                is_shared = true;
729                self.advance();
730            } else {
731                break;
732            }
733        }
734
735        if let Some(name_sym) = self.consume_noun_or_proper() {
736            // Phase 34: Check for "of [T]" which indicates user-defined generic
737            let type_params = if self.check_preposition("of") {
738                self.advance(); // consume "of"
739                self.parse_type_params()
740            } else {
741                vec![]
742            };
743            if self.check_copula() {
744                let copula_pos = self.pos;
745                self.advance(); // consume is/are
746
747                // Check for modifiers in any order (e.g., "is Shared and Portable and")
748                loop {
749                    if self.check_portable() {
750                        self.advance(); // consume "Portable"
751                        is_portable = true;
752                        if self.check_word("and") {
753                            self.advance(); // consume "and"
754                        }
755                    } else if self.check_shared() {
756                        self.advance(); // consume "Shared"
757                        is_shared = true;
758                        if self.check_word("and") {
759                            self.advance(); // consume "and"
760                        }
761                    } else {
762                        break;
763                    }
764                }
765
766                // If no modifiers were found, restore position
767                if !is_portable && !is_shared {
768                    self.pos = copula_pos;
769                }
770            }
771
772            // Phase 31/34: Check for "has:" which indicates struct with fields
773            // Pattern: "A Point has:" or "A Box of [T] has:" or "A Message is Portable and has:"
774            if self.check_word("has") {
775                self.advance(); // consume "has"
776                if self.check_colon() {
777                    self.advance(); // consume ":"
778                                    // Skip newline if present
779                    if self.check_newline() {
780                        self.advance();
781                    }
782                    if self.check_indent() {
783                        self.advance(); // consume INDENT
784                        let fields = self.parse_struct_fields_with_params(&type_params, None);
785                        registry.register(
786                            name_sym,
787                            TypeDef::Struct {
788                                fields,
789                                generics: type_params,
790                                is_portable,
791                                is_shared,
792                            },
793                        );
794                        return;
795                    }
796                }
797            }
798
799            // Check for "is either:" or "is one of:" pattern (Phase 33/34: Sum types with variants)
800            if self.check_copula() {
801                self.advance(); // consume is/are
802
803                // Phase 33: Check for "either:" or "one of:" pattern
804                let is_enum_pattern = if self.check_either() {
805                    self.advance(); // consume "either"
806                    true
807                } else if self.check_word("one") {
808                    self.advance(); // consume "one"
809                    if self.check_word("of") {
810                        self.advance(); // consume "of"
811                        true
812                    } else {
813                        false
814                    }
815                } else {
816                    false
817                };
818
819                if is_enum_pattern {
820                    if self.check_colon() {
821                        self.advance(); // consume ":"
822                                        // Skip newline if present
823                        if self.check_newline() {
824                            self.advance();
825                        }
826                        if self.check_indent() {
827                            self.advance(); // consume INDENT
828                            let variants = self.parse_enum_variants_with_params(&type_params);
829                            registry.register(
830                                name_sym,
831                                TypeDef::Enum {
832                                    variants,
833                                    generics: type_params,
834                                    is_portable,
835                                    is_shared,
836                                },
837                            );
838                            return;
839                        }
840                    }
841                }
842
843                if self.check_article() {
844                    self.advance(); // consume a/an
845
846                    // Look for type indicators
847                    if self.check_word("generic") {
848                        // `A Map of [K] and [V] is a generic collection.` declares its
849                        // arity in the `of [..]` list — honor it (2 here) so the parser
850                        // reads `Map of Int to Int` as two params. A bare `A Stack is a
851                        // generic collection.` names no params and stays single-arity.
852                        let param_count = if type_params.is_empty() {
853                            1
854                        } else {
855                            type_params.len()
856                        };
857                        registry.register(name_sym, TypeDef::Generic { param_count });
858                        self.skip_to_period();
859                    } else if self.check_word("record")
860                        || self.check_word("struct")
861                        || self.check_word("structure")
862                    {
863                        registry.register(
864                            name_sym,
865                            TypeDef::Struct {
866                                fields: vec![],
867                                generics: vec![],
868                                is_portable: false,
869                                is_shared: false,
870                            },
871                        );
872                        self.skip_to_period();
873                    } else if self.check_word("sum")
874                        || self.check_word("enum")
875                        || self.check_word("choice")
876                    {
877                        registry.register(
878                            name_sym,
879                            TypeDef::Enum {
880                                variants: vec![],
881                                generics: vec![],
882                                is_portable: false,
883                                is_shared: false,
884                            },
885                        );
886                        self.skip_to_period();
887                    }
888                }
889            } else if !type_params.is_empty() {
890                // "A Stack of [Things] is..." - old generic syntax, still supported
891                registry.register(
892                    name_sym,
893                    TypeDef::Generic {
894                        param_count: type_params.len(),
895                    },
896                );
897                self.skip_to_period();
898            }
899        }
900    }
901
902    /// Phase 33/34: Parse enum variants in "is either:" block
903    /// Each variant: "A VariantName." or "A VariantName with a field, which is Type."
904    /// or concise: "A VariantName (field: Type)."
905    fn parse_enum_variants_with_params(&mut self, type_params: &[Symbol]) -> Vec<VariantDef> {
906        let mut variants = Vec::new();
907
908        while self.pos < self.tokens.len() {
909            // Exit on dedent or next block
910            if self.check_dedent() {
911                self.advance();
912                break;
913            }
914            if matches!(
915                self.peek(),
916                Some(Token {
917                    kind: TokenType::BlockHeader { .. },
918                    ..
919                })
920            ) {
921                break;
922            }
923
924            // Skip newlines between variants
925            if self.check_newline() {
926                self.advance();
927                continue;
928            }
929
930            // Parse variant: "A VariantName [with fields | (field: Type)]." or bare "VariantName."
931            // Optionally consume article (a/an) if present
932            if self.check_article() {
933                self.advance(); // consume "A"/"An"
934            }
935
936            // Try to parse variant name (noun or proper name) — registered by
937            // RAW spelling: under the dynamic lexicon a capitalized variant
938            // like `Ok` classifies as a common word whose canonical symbol is
939            // the lowercased lemma, and a lemma-keyed registry would never
940            // match the `When Ok (v):` arm's raw-lexeme lookup.
941            let raw_lexeme = self.peek().map(|t| t.lexeme);
942            if self.consume_noun_or_proper().is_some() {
943                let variant_name = raw_lexeme.expect("peeked before consuming");
944                // Check for payload fields
945                let fields = if self.check_word("with") {
946                    // Natural syntax: "A Circle with a radius, which is Int."
947                    self.parse_variant_fields_natural_with_params(type_params)
948                } else if self.check_lparen() {
949                    // Concise syntax: "A Circle (radius: Int)."
950                    self.parse_variant_fields_concise_with_params(type_params)
951                } else {
952                    // Unit variant: "A Point." or "Point."
953                    vec![]
954                };
955
956                variants.push(VariantDef {
957                    name: variant_name,
958                    fields,
959                });
960
961                // Consume period
962                if self.check_period() {
963                    self.advance();
964                }
965            } else {
966                self.advance(); // skip malformed token
967            }
968        }
969
970        variants
971    }
972
973    /// Parse variant fields in natural syntax.
974    /// Supports multiple syntaxes:
975    /// - "with a radius, which is Int." (verbose natural)
976    /// - "with radius Int" (concise natural - no article/comma)
977    fn parse_variant_fields_natural_with_params(
978        &mut self,
979        type_params: &[Symbol],
980    ) -> Vec<FieldDef> {
981        let mut fields = Vec::new();
982
983        // "with" has already been detected, consume it
984        self.advance();
985
986        loop {
987            if self.check_article() && !self.article_is_variant_field_name(type_params) {
988                self.advance();
989            }
990
991            if let Some(field_name) = self.consume_field_name() {
992                // Support multiple type annotation patterns:
993                // 1. ", which is Type" (verbose)
994                // 2. " Type" (concise - just a type name after field name)
995                let ty = if self.check_comma() {
996                    self.advance(); // consume ","
997                                    // Consume "which"
998                    if self.check_word("which") {
999                        self.advance();
1000                    }
1001                    // Consume "is"
1002                    if self.check_copula() {
1003                        self.advance();
1004                    }
1005                    self.consume_field_type_with_params(type_params)
1006                } else if self.check_colon() {
1007                    self.advance(); // consume ":"
1008                    self.consume_field_type_with_params(type_params)
1009                } else {
1010                    // Concise syntax: "radius Int" - type immediately follows field name
1011                    self.consume_field_type_with_params(type_params)
1012                };
1013
1014                fields.push(FieldDef {
1015                    name: field_name,
1016                    ty,
1017                    is_public: true, // Variant fields are always public
1018                });
1019
1020                // Check for "and" to continue: "and height Int"
1021                // May have comma before "and"
1022                if self.check_comma() {
1023                    self.advance(); // consume comma before "and"
1024                }
1025                if self.check_word("and") {
1026                    self.advance();
1027                    continue;
1028                }
1029            }
1030            break;
1031        }
1032
1033        fields
1034    }
1035
1036    /// Parse variant fields in concise syntax: "(radius: Int)" or "(width: Int, height: Int)"
1037    fn parse_variant_fields_concise_with_params(
1038        &mut self,
1039        type_params: &[Symbol],
1040    ) -> Vec<FieldDef> {
1041        let mut fields = Vec::new();
1042
1043        // Consume "("
1044        self.advance();
1045
1046        loop {
1047            // The word before `:` is the field's name.
1048            let field_name = if self.is_name_separator_at(self.pos + 1) {
1049                self.consume_field_name()
1050            } else {
1051                self.consume_noun_or_proper()
1052            };
1053            if let Some(field_name) = field_name {
1054                // Expect ": Type" pattern
1055                let ty = if self.check_colon() {
1056                    self.advance(); // consume ":"
1057                    self.consume_field_type_with_params(type_params)
1058                } else {
1059                    FieldType::Primitive(self.interner.intern("Unknown"))
1060                };
1061
1062                fields.push(FieldDef {
1063                    name: field_name,
1064                    ty,
1065                    is_public: true, // Variant fields are always public
1066                });
1067
1068                // Check for "," to continue
1069                if self.check_comma() {
1070                    self.advance();
1071                    continue;
1072                }
1073            }
1074            break;
1075        }
1076
1077        // Consume ")"
1078        if self.check_rparen() {
1079            self.advance();
1080        }
1081
1082        fields
1083    }
1084
1085    /// Parse struct fields in "has:" block
1086    /// Each field: "a [public] name, which is Type."
1087    ///
1088    /// `spans`, when given, collects each field's own name token — a check that must report AT a
1089    /// particular field (`## Session`'s field-kind refusal) reads it back from
1090    /// `TypeRegistry::field_span`; an ordinary struct has no such check and passes `None`.
1091    fn parse_struct_fields_with_params(
1092        &mut self,
1093        type_params: &[Symbol],
1094        mut spans: Option<&mut Vec<(Symbol, Span)>>,
1095    ) -> Vec<FieldDef> {
1096        let mut fields = Vec::new();
1097
1098        while self.pos < self.tokens.len() {
1099            // Exit on dedent or next block
1100            if self.check_dedent() {
1101                self.advance();
1102                break;
1103            }
1104            if matches!(
1105                self.peek(),
1106                Some(Token {
1107                    kind: TokenType::BlockHeader { .. },
1108                    ..
1109                })
1110            ) {
1111                break;
1112            }
1113
1114            // Skip newlines between fields
1115            if self.check_newline() {
1116                self.advance();
1117                continue;
1118            }
1119
1120            // Parse field: "a [public] name, which is Type." or "name: Type." (no article). An
1121            // article directly before the separator is the field's name (`a: Int.`).
1122            let has_article = self.check_article() && !self.is_name_separator_at(self.pos + 1);
1123            if has_article {
1124                self.advance(); // consume "a"/"an"
1125            }
1126
1127            // Check for "public" modifier — unless `public` is the field's own name (`public: Bool.`)
1128            let has_public_keyword =
1129                if self.check_word("public") && !self.is_name_separator_at(self.pos + 1) {
1130                    self.advance();
1131                    true
1132                } else {
1133                    false
1134                };
1135            // Visibility determined later based on syntax used
1136            let mut is_public = has_public_keyword;
1137
1138            // A field's name is known by its position (`consume_field_name`): after an article, or
1139            // directly before the `:` of `name: Type` / the `,` of `name, which is Type`.
1140            let name_span = self.peek().map(|t| t.span).unwrap_or_default();
1141            let names_by_position = has_article || self.is_name_separator_at(self.pos + 1);
1142            let field_name = if names_by_position {
1143                self.consume_field_name()
1144            } else {
1145                self.consume_noun_or_proper()
1146            };
1147            if let Some(field_name) = field_name {
1148                // Three syntaxes, each public by default:
1149                // 1. "name: Type." (concise)
1150                // 2. "name, which is Type." (natural)
1151                // 3. "a name (Type)." (parenthesized)
1152                let ty = if self.check_colon() {
1153                    // Concise syntax: "x: Int" - public by default
1154                    is_public = true;
1155                    self.advance(); // consume ":"
1156                    self.consume_field_type_with_params(type_params)
1157                } else if self.check_comma() {
1158                    // Natural syntax: "name, which is Type" - also public by default
1159                    is_public = true;
1160                    self.advance(); // consume ","
1161                                    // Consume "which"
1162                    if self.check_word("which") {
1163                        self.advance();
1164                    }
1165                    // Consume "is"
1166                    if self.check_copula() {
1167                        self.advance();
1168                    }
1169                    self.consume_field_type_with_params(type_params)
1170                } else if has_article && self.check_lparen() {
1171                    is_public = true;
1172                    self.consume_field_type_with_params(type_params)
1173                } else if !has_article {
1174                    // No colon and no article - this wasn't a field, skip
1175                    continue;
1176                } else {
1177                    // Fallback: unknown type
1178                    FieldType::Primitive(self.interner.intern("Unknown"))
1179                };
1180
1181                if let Some(spans) = spans.as_deref_mut() {
1182                    spans.push((field_name, name_span));
1183                }
1184                fields.push(FieldDef {
1185                    name: field_name,
1186                    ty,
1187                    is_public,
1188                });
1189
1190                // Consume period
1191                if self.check_period() {
1192                    self.advance();
1193                }
1194            } else if !has_article {
1195                // Didn't have article and couldn't get field name - skip this token
1196                self.advance();
1197            }
1198        }
1199
1200        fields
1201    }
1202
1203    /// Parse a field type reference
1204    fn consume_field_type(&mut self) -> FieldType {
1205        if self.check_lparen() {
1206            return self.consume_parenthesized_field_type(&[]);
1207        }
1208
1209        // Skip article if present (e.g., "a Tally" -> "Tally")
1210        if self.check_article() {
1211            self.advance();
1212        }
1213
1214        if let Some(name) = self.consume_noun_or_proper() {
1215            let name_str = self.interner.resolve(name);
1216
1217            // Phase 49c: Check for bias/algorithm modifier on SharedSet: "SharedSet (AddWins) of T"
1218            let modified_name = if name_str == "SharedSet" || name_str == "ORSet" {
1219                if self.check_lparen() {
1220                    self.advance(); // consume "("
1221                    let modifier = if self.check_removewins() {
1222                        self.advance(); // consume "RemoveWins"
1223                        Some("SharedSet_RemoveWins")
1224                    } else if self.check_addwins() {
1225                        self.advance(); // consume "AddWins"
1226                        Some("SharedSet_AddWins")
1227                    } else {
1228                        None
1229                    };
1230                    if self.check_rparen() {
1231                        self.advance(); // consume ")"
1232                    }
1233                    modifier.map(|m| self.interner.intern(m))
1234                } else {
1235                    None
1236                }
1237            } else if name_str == "SharedSequence" {
1238                // Phase 49c: Check for algorithm modifier on SharedSequence: "SharedSequence (YATA) of T"
1239                if self.check_lparen() {
1240                    self.advance(); // consume "("
1241                    let modifier = if self.check_yata() {
1242                        self.advance(); // consume "YATA"
1243                        Some("SharedSequence_YATA")
1244                    } else {
1245                        None
1246                    };
1247                    if self.check_rparen() {
1248                        self.advance(); // consume ")"
1249                    }
1250                    modifier.map(|m| self.interner.intern(m))
1251                } else {
1252                    None
1253                }
1254            } else {
1255                None
1256            };
1257
1258            // Use modified name if we found a modifier, otherwise use original
1259            let final_name = modified_name.unwrap_or(name);
1260            let final_name_str = self.interner.resolve(final_name);
1261
1262            // Phase 49c: Handle "SharedMap from K to V" / "ORMap from K to V" syntax
1263            if (final_name_str == "SharedMap" || final_name_str == "ORMap") && self.check_from() {
1264                self.advance(); // consume "from"
1265                let key_type = self.consume_field_type();
1266                // Expect "to" (can be TokenType::To or preposition)
1267                if self.check_to() {
1268                    self.advance(); // consume "to"
1269                }
1270                let value_type = self.consume_field_type();
1271                return FieldType::Generic {
1272                    base: final_name,
1273                    params: vec![key_type, value_type],
1274                };
1275            }
1276
1277            // Check for generic: "List of Int", "Seq of Text", "Map of K to V"
1278            if self.check_preposition("of") {
1279                // How many parameters this generic takes, from the ONE table the type-expression
1280                // parser reads — so `Result of Int and E` binds BOTH here, as it does everywhere
1281                // else. A field whose declared type silently loses a parameter takes its whole
1282                // record out of the layout tables, and nothing says so.
1283                let arity = crate::analysis::registry::builtin_generic_param_count(final_name_str)
1284                    .unwrap_or(1);
1285
1286                self.advance();
1287                let mut params = Vec::with_capacity(arity);
1288                params.push(self.consume_field_type());
1289                // Later parameters follow a separator — `and` (`Result of T and E`), `to`
1290                // (`Map of K to V`) or a comma. A generic written with fewer than its arity keeps
1291                // what was actually given rather than consuming the sentence past its end.
1292                while params.len() < arity {
1293                    if self.check_and() || self.check_to() || self.check_comma() {
1294                        self.advance();
1295                        params.push(self.consume_field_type());
1296                    } else {
1297                        break;
1298                    }
1299                }
1300                return FieldType::Generic {
1301                    base: final_name,
1302                    params,
1303                };
1304            }
1305
1306            // Phase 49b: "Divergent T" syntax (no "of" required)
1307            if final_name_str == "Divergent" {
1308                // Next token should be the inner type
1309                let param = self.consume_field_type();
1310                return FieldType::Generic {
1311                    base: final_name,
1312                    params: vec![param],
1313                };
1314            }
1315
1316            // Check if primitive
1317            match final_name_str {
1318                "Int" | "Nat" | "Text" | "Bool" | "Real" | "Unit" | "Word8" | "Word16"
1319                | "Word32" | "Word64" | "Lanes8Word32" | "Lanes4Word32" | "Lanes16Word8"
1320                | "Lanes4Word64" | "Lanes16Word16" => FieldType::Primitive(final_name),
1321                _ => FieldType::Named(final_name),
1322            }
1323        } else {
1324            FieldType::Primitive(self.interner.intern("Unknown"))
1325        }
1326    }
1327
1328    // Helper methods
1329    fn peek(&self) -> Option<&Token> {
1330        self.tokens.get(self.pos)
1331    }
1332
1333    fn advance(&mut self) {
1334        if self.pos < self.tokens.len() {
1335            self.pos += 1;
1336        }
1337    }
1338
1339    fn check_article(&self) -> bool {
1340        match self.peek() {
1341            Some(Token {
1342                kind: TokenType::Article(_),
1343                ..
1344            }) => true,
1345            // Also accept ProperName("A") / ProperName("An") which can occur at line starts
1346            Some(Token {
1347                kind: TokenType::ProperName(sym),
1348                ..
1349            }) => {
1350                let text = self.interner.resolve(*sym);
1351                text.eq_ignore_ascii_case("a") || text.eq_ignore_ascii_case("an")
1352            }
1353            _ => false,
1354        }
1355    }
1356
1357    fn check_copula(&self) -> bool {
1358        match self.peek() {
1359            Some(Token {
1360                kind: TokenType::Is | TokenType::Are,
1361                ..
1362            }) => true,
1363            // Also match "is" when tokenized as a verb (common in declarative mode)
1364            Some(Token {
1365                kind: TokenType::Verb { lemma, .. },
1366                ..
1367            }) => {
1368                let word = self.interner.resolve(*lemma).to_lowercase();
1369                word == "is" || word == "are"
1370            }
1371            _ => false,
1372        }
1373    }
1374
1375    fn check_preposition(&self, word: &str) -> bool {
1376        if let Some(Token {
1377            kind: TokenType::Preposition(sym),
1378            ..
1379        }) = self.peek()
1380        {
1381            self.interner.resolve(*sym) == word
1382        } else {
1383            false
1384        }
1385    }
1386
1387    /// Take the word here as a field's name. A field's name is known by its POSITION, never by the
1388    /// part of speech the lexer gave the word: `first`/`second` are ordinals to it, `who` an
1389    /// interrogative, `a` an article, and each is an ordinary field name where a name stands. Any
1390    /// identifier-shaped word is accepted (the symbol [`Self::consume_noun_or_proper`] would give,
1391    /// where it accepts the word).
1392    fn consume_field_name(&mut self) -> Option<Symbol> {
1393        if let Some(sym) = self.consume_noun_or_proper() {
1394            return Some(sym);
1395        }
1396        let t = self.peek()?;
1397        let lexeme = self.interner.resolve(t.lexeme);
1398        let identifier = lexeme.chars().next().is_some_and(|c| c.is_alphabetic())
1399            && lexeme.chars().all(|c| c.is_alphanumeric() || c == '_');
1400        if !identifier {
1401            return None;
1402        }
1403        let sym = t.lexeme;
1404        self.advance();
1405        Some(sym)
1406    }
1407
1408    /// Whether token `i` separates a field's name from its type: the `:` of `name: Type`, or the
1409    /// `,` of `name, which is Type` — never the `,` before `and` in a variant's field list.
1410    fn is_name_separator_at(&self, i: usize) -> bool {
1411        match self.tokens.get(i).map(|t| &t.kind) {
1412            Some(TokenType::Colon) => true,
1413            Some(TokenType::Comma) => !self
1414                .tokens
1415                .get(i + 1)
1416                .is_some_and(|t| self.interner.resolve(t.lexeme).eq_ignore_ascii_case("and")),
1417            _ => false,
1418        }
1419    }
1420
1421    /// In a variant's `with` list, whether the article here is the field's own NAME (`with a Int
1422    /// and b Int`, `with a Seq of Int`) rather than the article before one (`with a radius Int`,
1423    /// `with a radius, which is Int`). A separator right after it makes it the name; one after the
1424    /// next word makes it an article. Otherwise both readings are tried against the type grammar:
1425    /// the article is the name only when skipping it leaves no field (a name, then a type ending at
1426    /// the list's boundary) and taking it as the name does.
1427    fn article_is_variant_field_name(&mut self, type_params: &[Symbol]) -> bool {
1428        let at = self.pos;
1429        if self.is_name_separator_at(at + 1) {
1430            return true;
1431        }
1432        if self.is_name_separator_at(at + 2) {
1433            return false;
1434        }
1435        !self.juxtaposed_field_parses_from(at + 1, type_params)
1436            && self.juxtaposed_field_parses_from(at, type_params)
1437    }
1438
1439    /// Whether a field written `name Type` parses from token `start` and ends at the boundary of a
1440    /// variant's field list (`and`, `,`, `.`, `)`, or the line's end). The position is restored.
1441    fn juxtaposed_field_parses_from(&mut self, start: usize, type_params: &[Symbol]) -> bool {
1442        let saved = self.pos;
1443        self.pos = start;
1444        let parses = self.consume_field_name().is_some() && {
1445            let type_start = self.pos;
1446            let _ = self.consume_field_type_with_params(type_params);
1447            self.pos > type_start
1448                && (self.check_word("and")
1449                    || self.check_comma()
1450                    || self.check_period()
1451                    || self.check_rparen()
1452                    || self.check_newline()
1453                    || self.check_dedent()
1454                    || matches!(
1455                        self.peek(),
1456                        None | Some(Token {
1457                            kind: TokenType::EOF,
1458                            ..
1459                        })
1460                    ))
1461        };
1462        self.pos = saved;
1463        parses
1464    }
1465
1466    fn consume_noun_or_proper(&mut self) -> Option<Symbol> {
1467        let t = self.peek()?;
1468        match &t.kind {
1469            TokenType::Noun(s) | TokenType::ProperName(s) => {
1470                let sym = *s;
1471                self.advance();
1472                Some(sym)
1473            }
1474            // Phase 31: Also accept Adjective as identifier (for field names like "x")
1475            TokenType::Adjective(s) => {
1476                let sym = *s;
1477                self.advance();
1478                Some(sym)
1479            }
1480            // ★ Accept a PRONOUN as a field name, by its lexeme. `an i, which is Int.` and
1481            // `an it, which is Option of Inner.` are ordinary declarations — `i` for an index and
1482            // `it` for an item are among the most natural short field names there are — and both
1483            // used to VANISH. With the article consumed and the name unaccepted, neither branch
1484            // of the field loop ran: the token was skipped on the next pass, the rest of the line
1485            // parsed as nothing, and the record simply did not have that field. No error, at
1486            // declaration OR at construction (`a new Row with it 4` set nothing), so the first
1487            // sign was a value missing from the far end of a wire.
1488            TokenType::Pronoun { .. } => {
1489                let sym = t.lexeme;
1490                self.advance();
1491                Some(sym)
1492            }
1493            // Temporal adverbs are names in a declaration too ("First", "next"), just as after
1494            // "a new"; dropping one silently hides its record layout from the static backends.
1495            TokenType::Adverb(_) | TokenType::TemporalAdverb(_) => {
1496                let sym = t.lexeme;
1497                self.advance();
1498                Some(sym)
1499            }
1500            // Phase 47: Accept Performative as type name (for agent messages like "Command")
1501            TokenType::Performative(s) => {
1502                let sym = *s;
1503                self.advance();
1504                Some(sym)
1505            }
1506            // Phase 34: Accept special tokens as identifiers using their lexeme
1507            TokenType::Items | TokenType::Some => {
1508                let sym = t.lexeme;
1509                self.advance();
1510                Some(sym)
1511            }
1512            // Phase 49/50: Accept Verb tokens as identifiers
1513            // - Uppercase verbs like "Setting" are type names
1514            // - Lowercase verbs like "trusted", "privileged" are predicate names
1515            // Use lexeme to preserve the original word (not lemma which strips suffixes)
1516            TokenType::Verb { .. } => {
1517                let sym = t.lexeme;
1518                self.advance();
1519                Some(sym)
1520            }
1521            // Phase 49b: Accept CRDT type tokens as type names
1522            TokenType::Tally => {
1523                self.advance();
1524                Some(self.interner.intern("Tally"))
1525            }
1526            TokenType::SharedSet => {
1527                self.advance();
1528                Some(self.interner.intern("SharedSet"))
1529            }
1530            TokenType::SharedSequence => {
1531                self.advance();
1532                Some(self.interner.intern("SharedSequence"))
1533            }
1534            TokenType::CollaborativeSequence => {
1535                self.advance();
1536                Some(self.interner.intern("CollaborativeSequence"))
1537            }
1538            TokenType::SharedMap => {
1539                self.advance();
1540                Some(self.interner.intern("SharedMap"))
1541            }
1542            TokenType::Divergent => {
1543                self.advance();
1544                Some(self.interner.intern("Divergent"))
1545            }
1546            // Phase 49: Accept Ambiguous tokens (e.g., "name" could be verb or noun)
1547            // Use lexeme to get the original word
1548            TokenType::Ambiguous { .. } => {
1549                let sym = t.lexeme;
1550                self.advance();
1551                Some(sym)
1552            }
1553            // Escape hatch keyword can be a type/identifier name
1554            TokenType::Escape => {
1555                let sym = t.lexeme;
1556                self.advance();
1557                Some(sym)
1558            }
1559            // Phrasal verb particles can be identifiers (out, up, down, etc.)
1560            TokenType::Particle(_) => {
1561                let sym = t.lexeme;
1562                self.advance();
1563                Some(sym)
1564            }
1565            // Prepositions can be identifiers in code context (from, into, etc.)
1566            TokenType::Preposition(_) => {
1567                let sym = t.lexeme;
1568                self.advance();
1569                Some(sym)
1570            }
1571            // Phase 103: Accept Focus tokens as identifiers (e.g., "Just" for Maybe variants)
1572            TokenType::Focus(_) => {
1573                let sym = t.lexeme;
1574                self.advance();
1575                Some(sym)
1576            }
1577            // Phase 103: Accept Nothing token as identifier (for Maybe/Option variants)
1578            TokenType::Nothing => {
1579                let sym = t.lexeme;
1580                self.advance();
1581                Some(sym)
1582            }
1583            // Phase 103: Accept Article tokens as type parameter names (L, R, A, etc.)
1584            TokenType::Article(_) => {
1585                let sym = t.lexeme;
1586                self.advance();
1587                Some(sym)
1588            }
1589            // Phase 103: Accept Either token as type name (for Either type definition)
1590            TokenType::Either => {
1591                let sym = t.lexeme;
1592                self.advance();
1593                Some(sym)
1594            }
1595            // Calendar unit tokens can be type/variant/field names (Day, Week, Month, Year)
1596            TokenType::CalendarUnit(_) => {
1597                let sym = t.lexeme;
1598                self.advance();
1599                Some(sym)
1600            }
1601            _ => None,
1602        }
1603    }
1604
1605    fn check_word(&self, word: &str) -> bool {
1606        if let Some(token) = self.peek() {
1607            // Check against the lexeme of the token
1608            self.interner
1609                .resolve(token.lexeme)
1610                .eq_ignore_ascii_case(word)
1611        } else {
1612            false
1613        }
1614    }
1615
1616    fn skip_to_period(&mut self) {
1617        while self.pos < self.tokens.len() {
1618            if matches!(
1619                self.peek(),
1620                Some(Token {
1621                    kind: TokenType::Period,
1622                    ..
1623                })
1624            ) {
1625                self.advance();
1626                break;
1627            }
1628            self.advance();
1629        }
1630    }
1631
1632    fn check_colon(&self) -> bool {
1633        matches!(
1634            self.peek(),
1635            Some(Token {
1636                kind: TokenType::Colon,
1637                ..
1638            })
1639        )
1640    }
1641
1642    fn check_newline(&self) -> bool {
1643        matches!(
1644            self.peek(),
1645            Some(Token {
1646                kind: TokenType::Newline,
1647                ..
1648            })
1649        )
1650    }
1651
1652    fn check_indent(&self) -> bool {
1653        matches!(
1654            self.peek(),
1655            Some(Token {
1656                kind: TokenType::Indent,
1657                ..
1658            })
1659        )
1660    }
1661
1662    fn check_dedent(&self) -> bool {
1663        matches!(
1664            self.peek(),
1665            Some(Token {
1666                kind: TokenType::Dedent,
1667                ..
1668            })
1669        )
1670    }
1671
1672    fn check_comma(&self) -> bool {
1673        matches!(
1674            self.peek(),
1675            Some(Token {
1676                kind: TokenType::Comma,
1677                ..
1678            })
1679        )
1680    }
1681
1682    /// "and" — the separator between a multi-parameter generic's type arguments
1683    /// (`Result of T and E`, `Pair of A and B`, `Triple of A and B and C`).
1684    fn check_and(&self) -> bool {
1685        matches!(
1686            self.peek(),
1687            Some(Token {
1688                kind: TokenType::And,
1689                ..
1690            })
1691        )
1692    }
1693
1694    fn check_period(&self) -> bool {
1695        matches!(
1696            self.peek(),
1697            Some(Token {
1698                kind: TokenType::Period,
1699                ..
1700            })
1701        )
1702    }
1703
1704    fn check_either(&self) -> bool {
1705        matches!(
1706            self.peek(),
1707            Some(Token {
1708                kind: TokenType::Either,
1709                ..
1710            })
1711        )
1712    }
1713
1714    fn check_lparen(&self) -> bool {
1715        matches!(
1716            self.peek(),
1717            Some(Token {
1718                kind: TokenType::LParen,
1719                ..
1720            })
1721        )
1722    }
1723
1724    fn check_rparen(&self) -> bool {
1725        matches!(
1726            self.peek(),
1727            Some(Token {
1728                kind: TokenType::RParen,
1729                ..
1730            })
1731        )
1732    }
1733
1734    /// Phase 49c: Check for AddWins token
1735    fn check_addwins(&self) -> bool {
1736        matches!(
1737            self.peek(),
1738            Some(Token {
1739                kind: TokenType::AddWins,
1740                ..
1741            })
1742        )
1743    }
1744
1745    /// Phase 49c: Check for RemoveWins token
1746    fn check_removewins(&self) -> bool {
1747        matches!(
1748            self.peek(),
1749            Some(Token {
1750                kind: TokenType::RemoveWins,
1751                ..
1752            })
1753        )
1754    }
1755
1756    /// Phase 49c: Check for YATA token
1757    fn check_yata(&self) -> bool {
1758        matches!(
1759            self.peek(),
1760            Some(Token {
1761                kind: TokenType::YATA,
1762                ..
1763            })
1764        )
1765    }
1766
1767    /// Phase 49c: Check for "to" (either TokenType::To or preposition "to")
1768    fn check_to(&self) -> bool {
1769        match self.peek() {
1770            Some(Token {
1771                kind: TokenType::To,
1772                ..
1773            }) => true,
1774            Some(Token {
1775                kind: TokenType::Preposition(sym),
1776                ..
1777            }) => self.interner.resolve(*sym) == "to",
1778            _ => false,
1779        }
1780    }
1781
1782    /// Phase 49c: Check for "from" (either TokenType::From or preposition "from")
1783    fn check_from(&self) -> bool {
1784        match self.peek() {
1785            Some(Token {
1786                kind: TokenType::From,
1787                ..
1788            }) => true,
1789            Some(Token {
1790                kind: TokenType::Preposition(sym),
1791                ..
1792            }) => self.interner.resolve(*sym) == "from",
1793            _ => false,
1794        }
1795    }
1796
1797    /// Phase 47: Check for Portable token
1798    fn check_portable(&self) -> bool {
1799        matches!(
1800            self.peek(),
1801            Some(Token {
1802                kind: TokenType::Portable,
1803                ..
1804            })
1805        )
1806    }
1807
1808    /// Phase 49: Check for Shared token
1809    fn check_shared(&self) -> bool {
1810        matches!(
1811            self.peek(),
1812            Some(Token {
1813                kind: TokenType::Shared,
1814                ..
1815            })
1816        )
1817    }
1818
1819    // Phase 34: Bracket checks for type parameters
1820    fn check_lbracket(&self) -> bool {
1821        matches!(
1822            self.peek(),
1823            Some(Token {
1824                kind: TokenType::LBracket,
1825                ..
1826            })
1827        )
1828    }
1829
1830    fn check_rbracket(&self) -> bool {
1831        matches!(
1832            self.peek(),
1833            Some(Token {
1834                kind: TokenType::RBracket,
1835                ..
1836            })
1837        )
1838    }
1839
1840    /// Phase 34: Parse type parameters in brackets: "[T]" or "[A] and [B]"
1841    fn parse_type_params(&mut self) -> Vec<Symbol> {
1842        let mut params = Vec::new();
1843
1844        loop {
1845            if self.check_lbracket() {
1846                self.advance(); // consume [
1847                if let Some(param) = self.consume_noun_or_proper() {
1848                    params.push(param);
1849                }
1850                if self.check_rbracket() {
1851                    self.advance(); // consume ]
1852                }
1853            }
1854
1855            // Check for "and" separator for multi-param generics
1856            if self.check_word("and") {
1857                self.advance();
1858                continue;
1859            }
1860            break;
1861        }
1862        params
1863    }
1864
1865    /// A parenthesized field type, the `(` next: one type in parentheses is that type (`Seq of (Seq
1866    /// of Int)`), a comma list a tuple (`(Int, Text)`), and `name: Type` entries a named tuple
1867    /// (`(quotient: Int, remainder: Int)`). An entry's name is known by its position, before `:`.
1868    fn consume_parenthesized_field_type(&mut self, type_params: &[Symbol]) -> FieldType {
1869        self.advance(); // consume "("
1870        let mut entries: Vec<(Option<Symbol>, FieldType)> = Vec::new();
1871        loop {
1872            let named = matches!(
1873                self.tokens.get(self.pos + 1),
1874                Some(Token {
1875                    kind: TokenType::Colon,
1876                    ..
1877                })
1878            ) && !self.check_lparen();
1879            let name = if named {
1880                let name = self.peek().map(|t| t.lexeme);
1881                self.advance(); // the entry's name
1882                self.advance(); // consume ":"
1883                name
1884            } else {
1885                None
1886            };
1887            let ty = self.consume_field_type_with_params(type_params);
1888            entries.push((name, ty));
1889            if self.check_comma() {
1890                self.advance();
1891                continue;
1892            }
1893            break;
1894        }
1895        if self.check_rparen() {
1896            self.advance(); // consume ")"
1897        }
1898        if entries.iter().all(|(name, _)| name.is_some()) {
1899            return FieldType::Record(
1900                entries
1901                    .into_iter()
1902                    .map(|(n, t)| (n.unwrap_or_default(), t))
1903                    .collect(),
1904            );
1905        }
1906        let mut types: Vec<FieldType> = entries.into_iter().map(|(_, t)| t).collect();
1907        if types.len() == 1 {
1908            return types
1909                .pop()
1910                .unwrap_or(FieldType::Primitive(self.interner.intern("Unknown")));
1911        }
1912        FieldType::Tuple(types)
1913    }
1914
1915    /// Phase 34: Parse a field type reference, recognizing type parameters
1916    fn consume_field_type_with_params(&mut self, type_params: &[Symbol]) -> FieldType {
1917        if self.check_lparen() {
1918            return self.consume_parenthesized_field_type(type_params);
1919        }
1920
1921        // Phase 34: Single-letter type params like "A" may be tokenized as Article
1922        // Check for Article that matches a type param first
1923        if let Some(Token {
1924            kind: TokenType::Article(_),
1925            lexeme,
1926            ..
1927        }) = self.peek()
1928        {
1929            let text = self.interner.resolve(*lexeme);
1930            // Find matching type param by name (case-insensitive for single letters)
1931            for &param_sym in type_params {
1932                let param_name = self.interner.resolve(param_sym);
1933                if text.eq_ignore_ascii_case(param_name) {
1934                    self.advance(); // consume the article token
1935                    return FieldType::TypeParam(param_sym);
1936                }
1937            }
1938            // Article didn't match a type param, skip it (e.g., "a Tally" -> "Tally")
1939            self.advance();
1940        }
1941
1942        if let Some(name) = self.consume_noun_or_proper() {
1943            // Check if this is a type parameter reference
1944            if type_params.contains(&name) {
1945                return FieldType::TypeParam(name);
1946            }
1947
1948            let name_str = self.interner.resolve(name);
1949
1950            // Phase 49c: Check for bias/algorithm modifier on SharedSet: "SharedSet (AddWins) of T"
1951            let modified_name = if name_str == "SharedSet" || name_str == "ORSet" {
1952                if self.check_lparen() {
1953                    self.advance(); // consume "("
1954                    let modifier = if self.check_removewins() {
1955                        self.advance(); // consume "RemoveWins"
1956                        Some("SharedSet_RemoveWins")
1957                    } else if self.check_addwins() {
1958                        self.advance(); // consume "AddWins"
1959                        Some("SharedSet_AddWins")
1960                    } else {
1961                        None
1962                    };
1963                    if self.check_rparen() {
1964                        self.advance(); // consume ")"
1965                    }
1966                    modifier.map(|m| self.interner.intern(m))
1967                } else {
1968                    None
1969                }
1970            } else if name_str == "SharedSequence" {
1971                // Phase 49c: Check for algorithm modifier on SharedSequence: "SharedSequence (YATA) of T"
1972                if self.check_lparen() {
1973                    self.advance(); // consume "("
1974                    let modifier = if self.check_yata() {
1975                        self.advance(); // consume "YATA"
1976                        Some("SharedSequence_YATA")
1977                    } else {
1978                        None
1979                    };
1980                    if self.check_rparen() {
1981                        self.advance(); // consume ")"
1982                    }
1983                    modifier.map(|m| self.interner.intern(m))
1984                } else {
1985                    None
1986                }
1987            } else {
1988                None
1989            };
1990
1991            // Use modified name if we found a modifier, otherwise use original
1992            let final_name = modified_name.unwrap_or(name);
1993            let final_name_str = self.interner.resolve(final_name);
1994
1995            // Phase 49c: Handle "SharedMap from K to V" / "ORMap from K to V" syntax
1996            if (final_name_str == "SharedMap" || final_name_str == "ORMap") && self.check_from() {
1997                self.advance(); // consume "from"
1998                let key_type = self.consume_field_type_with_params(type_params);
1999                // Expect "to" (can be TokenType::To or preposition)
2000                if self.check_to() {
2001                    self.advance(); // consume "to"
2002                }
2003                let value_type = self.consume_field_type_with_params(type_params);
2004                return FieldType::Generic {
2005                    base: final_name,
2006                    params: vec![key_type, value_type],
2007                };
2008            }
2009
2010            // Check for generic: "List of Int", "Seq of Text", "List of T", "Map of K to V"
2011            if self.check_preposition("of") {
2012                // How many parameters this generic takes, from the ONE table the type-expression
2013                // parser reads — so `Result of Int and E` binds BOTH here, as it does everywhere
2014                // else. A field whose declared type silently loses a parameter takes its whole
2015                // record out of the layout tables, and nothing says so.
2016                let arity = crate::analysis::registry::builtin_generic_param_count(final_name_str)
2017                    .unwrap_or(1);
2018
2019                self.advance();
2020                let mut params = Vec::with_capacity(arity);
2021                params.push(self.consume_field_type_with_params(type_params));
2022                // Later parameters follow a separator — `and` (`Result of T and E`), `to`
2023                // (`Map of K to V`) or a comma. A generic written with fewer than its arity keeps
2024                // what was actually given rather than consuming the sentence past its end.
2025                while params.len() < arity {
2026                    if self.check_and() || self.check_to() || self.check_comma() {
2027                        self.advance();
2028                        params.push(self.consume_field_type_with_params(type_params));
2029                    } else {
2030                        break;
2031                    }
2032                }
2033                return FieldType::Generic {
2034                    base: final_name,
2035                    params,
2036                };
2037            }
2038
2039            // Phase 49b: "Divergent T" syntax (no "of" required)
2040            if final_name_str == "Divergent" {
2041                // Next token should be the inner type
2042                let param = self.consume_field_type_with_params(type_params);
2043                return FieldType::Generic {
2044                    base: final_name,
2045                    params: vec![param],
2046                };
2047            }
2048
2049            // Check if primitive
2050            match final_name_str {
2051                "Int" | "Nat" | "Text" | "Bool" | "Real" | "Unit" | "Word8" | "Word16"
2052                | "Word32" | "Word64" | "Lanes8Word32" | "Lanes4Word32" | "Lanes16Word8"
2053                | "Lanes4Word64" | "Lanes16Word16" => FieldType::Primitive(final_name),
2054                _ => FieldType::Named(final_name),
2055            }
2056        } else {
2057            FieldType::Primitive(self.interner.intern("Unknown"))
2058        }
2059    }
2060}
2061
2062// Note: discover_with_imports is defined in the main crate since it needs
2063// access to the project::Loader which is part of the compile system.
2064
2065#[cfg(test)]
2066#[path = "../../../logicaffeine_tests/unit/logicaffeine_language/analysis/discovery/tests.rs"]
2067mod tests;