axon-frontend 0.3.0

AXON compiler frontend — lexer, parser, AST, epistemic type system, type checker, IR generator, compile-time checker. Zero runtime dependencies (no tokio/axum/sqlx/reqwest); consumable by any tool that analyses AXON source without running it. v0.3.0 closes Fase 14 (lossless lexing): the lexer emits comment tokens (`LineComment`, `BlockComment`, `DocLineComment`, `DocBlockComment`, `InnerDocLineComment`, `InnerDocBlockComment`) and the parser materialises them as `Trivia` attached to every Declaration variant via `leading_trivia` / `trailing_trivia` fields — the API surface that powers `axon fmt`, axon-lsp's formatter and hover docstring rendering, and any rustdoc-style documentation generator. Back-compatible: existing callers see empty trivia tuples until they opt in. v0.2.0 added Fase 13 typed channels (channel/emit/publish/discover) + `channel_analysis` module for LSP integration.
Documentation
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! AXON token types and keyword lookup table.
//! Direct port of axon/compiler/tokens.py.

/// A single token produced by the AXON lexer.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Token {
    pub ttype: TokenType,
    pub value: String,
    pub line: u32,
    pub column: u32,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TokenType {
    // ── Keywords ──────────────────────────────────────────────────
    Persona, Context, Intent, Flow, Reason, Anchor, Validate, Refine,
    Memory, Tool, Probe, Weave, Step, Type, Import, Run, If, Else,
    Use, Remember, Recall,
    // Epistemic
    Know, Believe, Speculate, Doubt,
    // Parallel / yielding
    Par, Consolidate, Hibernate, Deliberate, Consensus,
    // Creative synthesis
    Forge,
    // OTS
    Ots, Teleology, HomotopySearch, LinearConstraints, LossFunction,
    // Streaming & effects
    Stream, OnChunk, OnComplete, Effects, Pure, Network,
    // Agent
    Agent, Goal, Tools, Budget, Strategy, OnStuck,
    // Shield
    Shield, Scan, OnBreach, Severity, Allow, Deny, Sandbox, Quarantine, Redact,
    // PIX
    Pix, Navigate, Drill, Trail,
    // Psyche
    Psyche, Dimensions, Manifold, Quantum, Inference,
    // MDN
    Corpus, Corroborate, EdgeFilter,
    // Data Science
    Dataspace, Ingest, Focus, Associate, Aggregate, Explore,
    // EMCP
    Mcp, Taint,
    // Mandate
    Mandate, Constraint, Kp, Ki, Kd, Tolerance, MaxSteps, OnViolation,
    // Daemon
    Daemon, Listen, BudgetPerEvent,
    // Compute
    Compute, Logic,
    // Lambda Data
    Lambda, Ontology, Certainty, TemporalFrame, Provenance, Derivation,
    // AxonStore
    AxonStore, Schema, Persist, Retrieve, Mutate, Purge, Transact,
    // AxonEndpoint
    AxonEndpoint,
    // I/O Cognitivo (§λ-L-E Fase 1 — Resources)
    Resource, Fabric, Manifest, Observe,
    // Control Cognitivo (§λ-L-E Fase 3)
    Reconcile, Lease, Ensemble,
    // Topology & Session (§λ-L-E Fase 4 — π-calculus)
    Topology, Session, Send, Receive, Loop, End,
    // Immune System (§λ-L-E Fase 5)
    Immune, Reflex, Heal,
    // UI Cognitiva (§λ-L-E Fase 9 — 100% .axon apps)
    Component, View,
    // Mobile Typed Channels (§λ-L-E Fase 13 — π-calc mobility)
    Channel, Emit, Publish, Discover,
    // Modifiers
    As, Within, ConstrainedBy, OnFailure, OutputTo, Effort,
    // Contextual
    For, In, Into, Against, About, From, Where, Let, Return, Or,
    // Field keywords
    Given, Ask, Output,

    // ── Literals ──────────────────────────────────────────────────
    StringLit, Integer, Float, Bool, Duration, Identifier,

    // ── Symbols ───────────────────────────────────────────────────
    LBrace, RBrace, LParen, RParen, LBracket, RBracket,
    Colon, Comma, Dot, Arrow, DotDot, Question, At,
    Lt, Gt, Lte, Gte, Eq, Neq, Assign,
    Plus, Minus, Star, Slash,

    // ── Special ───────────────────────────────────────────────────
    Eof,

    // ── Trivia (Fase 14.a — lossless lexing) ──────────────────────
    // Comment tokens emitted by the lexer instead of being silently
    // stripped. The parser collects them into a parallel `Trivia`
    // array indexed by effective-token position and attaches them to
    // AST nodes as `leading_trivia` / `trailing_trivia` (Roslyn
    // convention). This is what enables LSP hover with docstrings,
    // round-trip-preserving formatters, and rustdoc-style doc
    // generators downstream.
    //
    // Doc-comment heuristic (mirrors the Python lexer):
    //   //   regular line comment
    //   ///  outer doc line comment   (documents the next item)
    //   //!  inner doc line comment   (documents the enclosing item — Fase 14.c)
    //   /*   regular block comment
    //   /**  outer doc block comment  (documents the next item)
    //   /*!  inner doc block comment  (documents the enclosing item — Fase 14.c)
    // ////` (4+ slashes) and `/**/` (empty block) stay regular.
    LineComment,            // //  regular line comment
    BlockComment,           // /* */ regular block comment
    DocLineComment,         // ///  outer doc line comment
    DocBlockComment,        // /** */ outer doc block comment
    InnerDocLineComment,    // //!  inner doc line comment (Fase 14.c)
    InnerDocBlockComment,   // /*! */ inner doc block comment (Fase 14.c)
}

/// Comment trivia attached to AST nodes (Fase 14.a).
///
/// Parallel of the Python `Trivia` dataclass in `axon/compiler/ast_nodes.py`.
/// Each AST node carries a `leading_trivia` slice (comments preceding
/// the node's first token) and a `trailing_trivia` slice (comments on
/// the same line as the node's last token).
#[derive(Debug, Clone, PartialEq)]
pub struct Trivia {
    pub kind: TriviaKind,
    pub text: String,
    pub line: u32,
    pub column: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TriviaKind {
    Line,
    Block,
    DocLine,
    DocBlock,
    /// Inner doc line comment `//!` — documents the *enclosing* module
    /// or file rather than the next sibling. Fase 14.c.
    InnerDocLine,
    /// Inner doc block comment `/*! … */` — same convention as
    /// `InnerDocLine`. Fase 14.c.
    InnerDocBlock,
}

impl Trivia {
    /// `true` iff this trivia is any kind of doc comment — outer
    /// (`///`, `/** */`) or inner (`//!`, `/*! */`).
    pub fn is_doc(&self) -> bool {
        matches!(
            self.kind,
            TriviaKind::DocLine
                | TriviaKind::DocBlock
                | TriviaKind::InnerDocLine
                | TriviaKind::InnerDocBlock,
        )
    }

    /// `true` iff this trivia is an *inner* doc comment (`//!` or
    /// `/*! … */`). Inner doc comments document the enclosing item.
    pub fn is_inner_doc(&self) -> bool {
        matches!(
            self.kind,
            TriviaKind::InnerDocLine | TriviaKind::InnerDocBlock,
        )
    }

    /// Body of the comment with marker prefixes/suffixes removed.
    /// Useful for LSP hover rendering.
    pub fn stripped_text(&self) -> &str {
        match self.kind {
            TriviaKind::DocLine => self.text.strip_prefix("///").unwrap_or(&self.text),
            TriviaKind::InnerDocLine => self.text.strip_prefix("//!").unwrap_or(&self.text),
            TriviaKind::Line => self.text.strip_prefix("//").unwrap_or(&self.text),
            TriviaKind::DocBlock => {
                let s = self.text.strip_prefix("/**").unwrap_or(&self.text);
                s.strip_suffix("*/").unwrap_or(s)
            }
            TriviaKind::InnerDocBlock => {
                let s = self.text.strip_prefix("/*!").unwrap_or(&self.text);
                s.strip_suffix("*/").unwrap_or(s)
            }
            TriviaKind::Block => {
                let s = self.text.strip_prefix("/*").unwrap_or(&self.text);
                s.strip_suffix("*/").unwrap_or(s)
            }
        }
    }
}

pub fn keyword_type(word: &str) -> TokenType {
    match word {
        "persona"          => TokenType::Persona,
        "context"          => TokenType::Context,
        "intent"           => TokenType::Intent,
        "flow"             => TokenType::Flow,
        "reason"           => TokenType::Reason,
        "anchor"           => TokenType::Anchor,
        "validate"         => TokenType::Validate,
        "refine"           => TokenType::Refine,
        "memory"           => TokenType::Memory,
        "tool"             => TokenType::Tool,
        "probe"            => TokenType::Probe,
        "weave"            => TokenType::Weave,
        "step"             => TokenType::Step,
        "type"             => TokenType::Type,
        "import"           => TokenType::Import,
        "run"              => TokenType::Run,
        "if"               => TokenType::If,
        "else"             => TokenType::Else,
        "use"              => TokenType::Use,
        "remember"         => TokenType::Remember,
        "recall"           => TokenType::Recall,
        "know"             => TokenType::Know,
        "believe"          => TokenType::Believe,
        "speculate"        => TokenType::Speculate,
        "doubt"            => TokenType::Doubt,
        "par"              => TokenType::Par,
        "consolidate"      => TokenType::Consolidate,
        "hibernate"        => TokenType::Hibernate,
        "deliberate"       => TokenType::Deliberate,
        "consensus"        => TokenType::Consensus,
        "forge"            => TokenType::Forge,
        "ots"              => TokenType::Ots,
        "teleology"        => TokenType::Teleology,
        "homotopy_search"  => TokenType::HomotopySearch,
        "linear_constraints" => TokenType::LinearConstraints,
        "loss_function"    => TokenType::LossFunction,
        "stream"           => TokenType::Stream,
        "on_chunk"         => TokenType::OnChunk,
        "on_complete"      => TokenType::OnComplete,
        "effects"          => TokenType::Effects,
        "pure"             => TokenType::Pure,
        "network"          => TokenType::Network,
        "agent"            => TokenType::Agent,
        "goal"             => TokenType::Goal,
        "tools"            => TokenType::Tools,
        "budget"           => TokenType::Budget,
        "strategy"         => TokenType::Strategy,
        "on_stuck"         => TokenType::OnStuck,
        "shield"           => TokenType::Shield,
        "scan"             => TokenType::Scan,
        "on_breach"        => TokenType::OnBreach,
        "severity"         => TokenType::Severity,
        "allow"            => TokenType::Allow,
        "deny"             => TokenType::Deny,
        "sandbox"          => TokenType::Sandbox,
        "quarantine"       => TokenType::Quarantine,
        "redact"           => TokenType::Redact,
        "pix"              => TokenType::Pix,
        "navigate"         => TokenType::Navigate,
        "drill"            => TokenType::Drill,
        "trail"            => TokenType::Trail,
        "psyche"           => TokenType::Psyche,
        "dimensions"       => TokenType::Dimensions,
        "manifold"         => TokenType::Manifold,
        "quantum"          => TokenType::Quantum,
        "inference"        => TokenType::Inference,
        "corpus"           => TokenType::Corpus,
        "corroborate"      => TokenType::Corroborate,
        "edge_filter"      => TokenType::EdgeFilter,
        "dataspace"        => TokenType::Dataspace,
        "ingest"           => TokenType::Ingest,
        "focus"            => TokenType::Focus,
        "associate"        => TokenType::Associate,
        "aggregate"        => TokenType::Aggregate,
        "explore"          => TokenType::Explore,
        "mcp"              => TokenType::Mcp,
        "taint"            => TokenType::Taint,
        "mandate"          => TokenType::Mandate,
        "constraint"       => TokenType::Constraint,
        "kp"               => TokenType::Kp,
        "ki"               => TokenType::Ki,
        "kd"               => TokenType::Kd,
        "tolerance"        => TokenType::Tolerance,
        "max_steps"        => TokenType::MaxSteps,
        "on_violation"     => TokenType::OnViolation,
        "daemon"           => TokenType::Daemon,
        "listen"           => TokenType::Listen,
        "budget_per_event" => TokenType::BudgetPerEvent,
        "compute"          => TokenType::Compute,
        "logic"            => TokenType::Logic,
        "lambda"           => TokenType::Lambda,
        "ontology"         => TokenType::Ontology,
        "certainty"        => TokenType::Certainty,
        "temporal_frame"   => TokenType::TemporalFrame,
        "provenance"       => TokenType::Provenance,
        "derivation"       => TokenType::Derivation,
        "axonstore"        => TokenType::AxonStore,
        "schema"           => TokenType::Schema,
        "persist"          => TokenType::Persist,
        "retrieve"         => TokenType::Retrieve,
        "mutate"           => TokenType::Mutate,
        "purge"            => TokenType::Purge,
        "transact"         => TokenType::Transact,
        "axonendpoint" | "axpoint" => TokenType::AxonEndpoint,
        // I/O Cognitivo (§λ-L-E Fase 1 — Resources)
        "resource"         => TokenType::Resource,
        "fabric"           => TokenType::Fabric,
        "manifest"         => TokenType::Manifest,
        "observe"          => TokenType::Observe,
        // Control Cognitivo (§λ-L-E Fase 3)
        "reconcile"        => TokenType::Reconcile,
        "lease"            => TokenType::Lease,
        "ensemble"         => TokenType::Ensemble,
        // Topology & Session (§λ-L-E Fase 4 — π-calculus)
        "topology"         => TokenType::Topology,
        "session"          => TokenType::Session,
        "send"             => TokenType::Send,
        "receive"          => TokenType::Receive,
        "loop"             => TokenType::Loop,
        "end"              => TokenType::End,
        // Immune System (§λ-L-E Fase 5)
        "immune"           => TokenType::Immune,
        "reflex"           => TokenType::Reflex,
        "heal"             => TokenType::Heal,
        // UI Cognitiva (§λ-L-E Fase 9)
        "component"        => TokenType::Component,
        "view"             => TokenType::View,
        // Mobile Typed Channels (§λ-L-E Fase 13)
        "channel"          => TokenType::Channel,
        "emit"             => TokenType::Emit,
        "publish"          => TokenType::Publish,
        "discover"         => TokenType::Discover,
        "as"               => TokenType::As,
        "within"           => TokenType::Within,
        "constrained_by"   => TokenType::ConstrainedBy,
        "on_failure"       => TokenType::OnFailure,
        "output_to"        => TokenType::OutputTo,
        "effort"           => TokenType::Effort,
        "for"              => TokenType::For,
        "in"               => TokenType::In,
        "into"             => TokenType::Into,
        "against"          => TokenType::Against,
        "about"            => TokenType::About,
        "from"             => TokenType::From,
        "where"            => TokenType::Where,
        "let"              => TokenType::Let,
        "return"           => TokenType::Return,
        "or"               => TokenType::Or,
        "given"            => TokenType::Given,
        "ask"              => TokenType::Ask,
        "output"           => TokenType::Output,
        "true" | "false"   => TokenType::Bool,
        _                  => TokenType::Identifier,
    }
}

/// Returns true for keywords that open a top-level declaration.
/// Used by the structural declaration counter (depth-0 scan).
pub fn is_declaration_keyword(tt: &TokenType) -> bool {
    matches!(
        tt,
        TokenType::Persona
            | TokenType::Flow
            | TokenType::Anchor
            | TokenType::Context
            | TokenType::Intent
            | TokenType::Type
            | TokenType::Memory
            | TokenType::Tool
            | TokenType::Probe
            | TokenType::Weave
            | TokenType::Agent
            | TokenType::Shield
            | TokenType::Pix
            | TokenType::Psyche
            | TokenType::Corpus
            | TokenType::Dataspace
            | TokenType::Mcp
            | TokenType::Daemon
            | TokenType::Compute
            | TokenType::Mandate
            | TokenType::Lambda
            | TokenType::AxonStore
            | TokenType::AxonEndpoint
            | TokenType::Import
            | TokenType::Run
            // §λ-L-E Fase 1–5 — I/O cognitivo, control, topology, immune
            | TokenType::Resource
            | TokenType::Fabric
            | TokenType::Manifest
            | TokenType::Observe
            | TokenType::Reconcile
            | TokenType::Lease
            | TokenType::Ensemble
            | TokenType::Topology
            | TokenType::Session
            | TokenType::Immune
            | TokenType::Reflex
            | TokenType::Heal
            // §λ-L-E Fase 9 — UI cognitiva
            | TokenType::Component
            | TokenType::View
            // §λ-L-E Fase 13 — Mobile typed channels
            | TokenType::Channel
    )
}

#[cfg(test)]
mod tests_lang_extensions {
    //! Regression tests for the Fase 1–5 keyword additions.
    use super::*;

    fn check(word: &str, expected: TokenType) {
        assert_eq!(keyword_type(word), expected, "keyword '{word}'");
    }

    #[test]
    fn fase1_io_cognitivo_keywords() {
        check("resource", TokenType::Resource);
        check("fabric",   TokenType::Fabric);
        check("manifest", TokenType::Manifest);
        check("observe",  TokenType::Observe);
    }

    #[test]
    fn fase3_control_keywords() {
        check("reconcile", TokenType::Reconcile);
        check("lease",     TokenType::Lease);
        check("ensemble",  TokenType::Ensemble);
    }

    #[test]
    fn fase4_topology_and_session_keywords() {
        check("topology", TokenType::Topology);
        check("session",  TokenType::Session);
        check("send",     TokenType::Send);
        check("receive",  TokenType::Receive);
        check("loop",     TokenType::Loop);
        check("end",      TokenType::End);
    }

    #[test]
    fn fase5_immune_keywords() {
        check("immune", TokenType::Immune);
        check("reflex", TokenType::Reflex);
        check("heal",   TokenType::Heal);
    }

    #[test]
    fn new_decl_keywords_are_declaration_level() {
        for tt in [
            TokenType::Resource, TokenType::Fabric, TokenType::Manifest, TokenType::Observe,
            TokenType::Reconcile, TokenType::Lease, TokenType::Ensemble,
            TokenType::Topology, TokenType::Session,
            TokenType::Immune, TokenType::Reflex, TokenType::Heal,
        ] {
            assert!(is_declaration_keyword(&tt), "{tt:?} must be a top-level decl");
        }
    }

    #[test]
    fn session_body_keywords_are_not_declaration_level() {
        for tt in [TokenType::Send, TokenType::Receive, TokenType::Loop, TokenType::End] {
            assert!(
                !is_declaration_keyword(&tt),
                "{tt:?} is a session-body step, not a top-level decl"
            );
        }
    }

    #[test]
    fn unknown_words_fall_back_to_identifier() {
        assert_eq!(keyword_type("frobnicate"), TokenType::Identifier);
        assert_eq!(keyword_type("PatientRecord"), TokenType::Identifier);
    }

    // §λ-L-E Fase 13 — Mobile typed channels.

    #[test]
    fn fase13_channel_keywords() {
        check("channel",  TokenType::Channel);
        check("emit",     TokenType::Emit);
        check("publish",  TokenType::Publish);
        check("discover", TokenType::Discover);
    }

    #[test]
    fn fase13_channel_is_top_level_decl() {
        assert!(is_declaration_keyword(&TokenType::Channel),
            "channel must be a top-level decl");
    }

    #[test]
    fn fase13_emit_publish_discover_are_flow_steps_not_decls() {
        for tt in [TokenType::Emit, TokenType::Publish, TokenType::Discover] {
            assert!(
                !is_declaration_keyword(&tt),
                "{tt:?} is a flow-step reduction, not a top-level decl"
            );
        }
    }
}