Skip to main content

Module parser

Module parser 

Source
Expand description

Parser for AgentScript source code.

This module provides a complete parser that converts AgentScript source into a typed Abstract Syntax Tree (AgentFile).

§Architecture

The parser uses a two-phase approach:

  1. Lexical analysis - Source → Tokens (via crate::lexer)
  2. Parsing - Tokens → AST (via chumsky combinators)

§Usage

use busbar_sf_agentscript::parser::parse;

let source = r#"
config:
   agent_name: "MyAgent"

topic main:
   description: "Main topic"
"#;

match parse(source) {
    Ok(agent) => {
        println!("Parsed {} topics", agent.topics.len());
    }
    Err(errors) => {
        for err in errors {
            eprintln!("{}", err);
        }
    }
}

§Error Handling

Use parse_with_errors() for partial parsing that returns both the result and any errors encountered:

use busbar_sf_agentscript::parser::parse_with_errors;

let source = "config:\n   agent_name: \"Test\"";
let (result, errors) = parse_with_errors(source);

if let Some(agent) = result {
    println!("Parsed successfully");
}
for err in errors {
    eprintln!("Warning: {}", err);
}

§Module Structure

The parser is split into submodules for each block type:

  • config - Config block parsing
  • variables - Variable declarations
  • system - System instructions and messages
  • topics - Topic and start_agent blocks
  • actions - Action definitions
  • reasoning - Reasoning blocks
  • expressions - Expression parsing
  • instructions - Static and dynamic instructions

Functions§

parse
Parse an AgentScript file from source code.
parse_with_errors
Parse an AgentScript file from source with full error reporting.
parse_with_structured_errors
Parse an AgentScript file and return structured errors with span information.
parse_with_structured_errors_all
Parse an AgentScript file and return structured errors with span information.

Type Aliases§

Span
Token span type (from lexer).