Files
orchid/orchid-api/src/parser.rs
2025-09-16 22:54:22 +02:00

103 lines
3.1 KiB
Rust

use core::ops::Range;
use std::num::NonZeroU64;
use orchid_api_derive::{Coding, Hierarchy};
use orchid_api_traits::Request;
use crate::{
Expression, ExtHostReq, HostExtReq, OrcResult, SourceRange, SysId, TStr, TStrv, TokenTree,
};
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Coding)]
pub struct ParsId(pub NonZeroU64);
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Coding)]
pub struct ParsedConstId(pub NonZeroU64);
/// Parse a single source line. Return values can be modules, constants, or
/// token sequences for re-parsing. These re-parsed token sequences can also
/// represent raw language items such as modules, imports, and const. This is
/// how we enable generating imports without forcing import syntax to affect API
/// versioning
#[derive(Clone, Debug, Coding, Hierarchy)]
#[extends(HostExtReq)]
pub struct ParseLine {
pub sys: SysId,
/// The immediately enclosing module path
pub module: TStrv,
/// The root module path for the snipppet of source code, prefix of
/// [ParseLine#module]
pub src: TStrv,
pub comments: Vec<Comment>,
pub exported: bool,
pub idx: u16,
pub line: Vec<TokenTree>,
}
impl Request for ParseLine {
type Response = OrcResult<Vec<ParsedLine>>;
}
#[derive(Clone, Debug, Coding)]
pub struct ParsedLine {
pub comments: Vec<Comment>,
pub source_range: SourceRange,
pub kind: ParsedLineKind,
}
#[derive(Clone, Debug, Coding)]
pub enum ParsedLineKind {
Recursive(Vec<TokenTree>),
Member(ParsedMember),
}
#[derive(Clone, Debug, Coding)]
pub struct ParsedMember {
pub name: TStr,
pub exported: bool,
pub kind: ParsedMemberKind,
}
#[derive(Clone, Debug, Coding)]
pub enum ParsedMemberKind {
Constant(ParsedConstId),
Module { lines: Vec<ParsedLine>, use_prelude: bool },
}
/// Obtain the value of a parsed constant. This is guaranteed to be called after
/// the last [ParseLine] but before any [crate::AtomReq]. As such, in principle
/// the macro engine could run here.
#[derive(Clone, Debug, Coding, Hierarchy)]
#[extends(HostExtReq)]
pub struct FetchParsedConst(pub SysId, pub ParsedConstId);
impl Request for FetchParsedConst {
type Response = Expression;
}
#[derive(Clone, Debug, Coding)]
pub struct Comment {
pub text: TStr,
pub range: Range<u32>,
}
/// Resolve relative names from the perspective of a constant. This can only be
/// called during a [FetchParsedConst] call, but it can be called for a
/// different [ParsedConstId] from the one in [FetchParsedConst].
///
/// Each name is either resolved to a valid name or a potential error error.
/// The error is not raised by the interpreter itself, as names may have a
/// primary meaning such as a local binding which can be overridden by specific
/// true names such as those triggering macro keywords. It is not recommended to
/// define syntax that can break by defining arbitrary constants, as line
/// parsers can define new ones at will.
#[derive(Clone, Debug, Coding, Hierarchy)]
#[extends(ExtHostReq)]
pub struct ResolveNames {
pub sys: SysId,
pub constid: ParsedConstId,
pub names: Vec<TStrv>,
}
impl Request for ResolveNames {
type Response = Vec<OrcResult<TStrv>>;
}