use std::os::raw::c_int;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::Arc;
use fig_core::kit::ast::{AST, NodeId, SerializeOptions as CoreSerializeOptions};
use fig_core::kit::convert;
use fig_core::kit::language::{self as core_language, Language as CoreLanguage};
use fig_core::kit::manifest;
use fig_core::kit::table::{self, Kind as RowKind, Table};
use fig_core::registry::{self, RegisterOptions};
use crate::{Capabilities, Error, ExtKind, Format, LanguageFailure, Span};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Description {
pub name: String,
pub caps: Capabilities,
pub max_mapping_depth: Option<u8>,
pub lossless: Option<NativeKinds>,
pub syntax: Option<Syntax>,
pub dialects: Vec<Dialect>,
pub samples: Vec<String>,
pub renderers: Renderers,
pub streams: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StreamDocument {
pub start: usize,
pub end: usize,
pub explicit: bool,
}
impl Description {
pub fn new(name: &str) -> Self {
Description {
name: name.to_owned(),
caps: Capabilities::default(),
dialects: vec![Dialect::new(name)],
..Default::default()
}
}
}
impl Default for Capabilities {
fn default() -> Self {
Capabilities::new(true, false, false)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Dialect {
pub name: String,
pub extensions: Vec<String>,
pub splice: Splice,
pub empty_doc_seed: Option<String>,
pub syntax: Option<Syntax>,
pub prints: Option<Prints>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Prints {
pub holds: NativeKinds,
pub typed_scalars: bool,
pub null_at_root: bool,
pub root_container: bool,
pub char_as: String,
pub number_special_as: String,
pub comments: CommentsPrinted,
}
impl Prints {
pub fn new(holds: NativeKinds) -> Self {
Prints {
holds,
typed_scalars: true,
null_at_root: false,
root_container: false,
char_as: "string".to_owned(),
number_special_as: "string".to_owned(),
comments: CommentsPrinted::Always,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum CommentsPrinted {
#[default]
Always,
Pretty,
Never,
}
impl Dialect {
pub fn new(name: &str) -> Self {
Dialect {
name: name.to_owned(),
extensions: Vec::new(),
splice: Splice::Literal,
empty_doc_seed: None,
syntax: None,
prints: None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Splice {
#[default]
Literal,
Escaped,
Raw,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct NativeKinds {
pub null: bool,
pub offset_datetime: bool,
pub local_datetime: bool,
pub local_date: bool,
pub local_time: bool,
pub enum_literal: bool,
pub char_literal: bool,
pub number_special: bool,
pub plist_date: bool,
pub data: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Renderers {
pub value: bool,
pub entry: bool,
pub item: bool,
pub tail: bool,
pub key: bool,
pub key_name: bool,
pub string: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Syntax {
pub comments: Comments,
pub kv_sep: Option<String>,
pub flow_kv_sep_from_siblings: bool,
pub flow_map_pad: String,
pub key_sigil: Option<u8>,
pub empty_map_literal: Option<String>,
pub block_seq_editable: bool,
pub flow_containers: bool,
pub indent_unit: String,
pub seq_item_marker: String,
pub closed_containers: Option<ClosedContainers>,
pub single_line_block_mapping: bool,
pub bare_document_mapping: bool,
pub flow_map_open: String,
pub flow_map_close: String,
pub structural_indent: bool,
pub section_noun: Option<String>,
pub section_header: Option<SectionHeader>,
pub merge_key: Option<String>,
pub flow_entry_sep: String,
pub flow_root: bool,
pub flow_maps_only: bool,
pub line_prefix_marks: String,
pub flow_map_kv_sep: String,
pub flow_map_entry_sep: String,
pub line_continuation: Option<u8>,
pub empty_seq_literal: Option<String>,
pub opener_line_comment: bool,
}
impl Default for Syntax {
fn default() -> Self {
Syntax {
comments: Comments::hash(),
kv_sep: None,
flow_kv_sep_from_siblings: false,
flow_map_pad: String::new(),
key_sigil: None,
empty_map_literal: None,
block_seq_editable: true,
flow_containers: true,
indent_unit: " ".to_owned(),
seq_item_marker: "- ".to_owned(),
closed_containers: None,
single_line_block_mapping: false,
bare_document_mapping: true,
flow_map_open: "{".to_owned(),
flow_map_close: "}".to_owned(),
structural_indent: false,
section_noun: None,
section_header: None,
merge_key: None,
flow_entry_sep: ",".to_owned(),
flow_root: false,
flow_maps_only: false,
line_prefix_marks: String::new(),
flow_map_kv_sep: String::new(),
flow_map_entry_sep: String::new(),
line_continuation: None,
empty_seq_literal: None,
opener_line_comment: false,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Comments {
pub line: Option<CommentDelimiter>,
pub trailing: Option<CommentDelimiter>,
pub block: Option<CommentDelimiter>,
}
impl Comments {
pub fn new(line: Option<CommentDelimiter>, trailing: Option<CommentDelimiter>) -> Self {
Comments {
line,
trailing,
block: None,
}
}
pub fn hash() -> Self {
Comments::new(
Some(CommentDelimiter::open("#")),
Some(CommentDelimiter::open("#")),
)
}
pub fn slashes() -> Self {
Comments::new(
Some(CommentDelimiter::open("//")),
Some(CommentDelimiter::open("//")),
)
}
pub fn none() -> Self {
Comments::new(None, None)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CommentDelimiter {
pub open: String,
pub close: String,
pub forbidden: Option<String>,
}
impl CommentDelimiter {
pub fn open(open: &str) -> Self {
CommentDelimiter {
open: open.to_owned(),
close: String::new(),
forbidden: None,
}
}
pub fn pair(open: &str, close: &str) -> Self {
CommentDelimiter {
open: open.to_owned(),
close: close.to_owned(),
forbidden: None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SectionHeader {
pub open: String,
pub close: String,
pub seq_open: Option<String>,
pub seq_close: Option<String>,
pub sep: String,
pub skip_index: bool,
}
impl SectionHeader {
pub fn new(open: &str, close: &str, sep: &str) -> Self {
SectionHeader {
open: open.to_owned(),
close: close.to_owned(),
seq_open: None,
seq_close: None,
sep: sep.to_owned(),
skip_index: true,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ClosedContainers {
pub map_open: String,
pub map_close: String,
pub seq_open: String,
pub seq_close: String,
}
impl ClosedContainers {
pub fn new(map_open: &str, map_close: &str, seq_open: &str, seq_close: &str) -> Self {
ClosedContainers {
map_open: map_open.to_owned(),
map_close: map_close.to_owned(),
seq_open: seq_open.to_owned(),
seq_close: seq_close.to_owned(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct NodeTable {
pub rows: Vec<NodeRow>,
pub regions: Vec<RegionRow>,
pub mentions: Vec<MentionRow>,
pub comments: Vec<CommentRow>,
pub directives: Vec<DirectiveRow>,
pub diagnostics: Vec<DiagnosticRow>,
}
impl NodeTable {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, row: NodeRow) -> u32 {
self.rows.push(row);
(self.rows.len() - 1) as u32
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NodeRow {
pub kind: NodeKind,
pub ext_kind: Option<ExtKind>,
pub parent: Option<u32>,
pub span: Option<Span>,
pub text: Option<String>,
pub anchor: Option<String>,
pub anchor_span: Option<Span>,
pub tag: Option<String>,
pub tag_span: Option<Span>,
pub marker: Option<Span>,
pub sep: Option<Span>,
pub item_sep: Option<String>,
pub open: bool,
}
impl NodeRow {
pub fn new(kind: NodeKind, parent: Option<u32>, span: Span) -> Self {
NodeRow {
kind,
ext_kind: None,
parent,
span: Some(span),
text: None,
anchor: None,
anchor_span: None,
tag: None,
tag_span: None,
marker: None,
sep: None,
item_sep: None,
open: false,
}
}
pub fn with_text(mut self, text: &str) -> Self {
self.text = Some(text.to_owned());
self
}
pub fn with_sep(mut self, sep: Span) -> Self {
self.sep = Some(sep);
self
}
pub fn with_marker(mut self, marker: Span) -> Self {
self.marker = Some(marker);
self
}
pub fn with_item_sep(mut self, sep: &str, open: bool) -> Self {
self.item_sep = Some(sep.to_owned());
self.open = open;
self
}
pub fn with_anchor(mut self, name: &str, span: Span) -> Self {
self.anchor = Some(name.to_owned());
self.anchor_span = Some(span);
self
}
pub fn with_tag(mut self, tag: &str, span: Span) -> Self {
self.tag = Some(tag.to_owned());
self.tag_span = Some(span);
self
}
pub fn with_ext_kind(mut self, kind: ExtKind) -> Self {
self.ext_kind = Some(kind);
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum NodeKind {
Null,
Bool,
Int,
Float,
String,
Sequence,
Mapping,
KeyValue,
Alias,
}
impl NodeKind {
pub(crate) fn to_row(self) -> u8 {
match self {
NodeKind::Null => RowKind::NULL,
NodeKind::Bool => RowKind::FALSE,
NodeKind::Int => RowKind::INT,
NodeKind::Float => RowKind::FLOAT,
NodeKind::String => RowKind::STRING,
NodeKind::Sequence => RowKind::SEQUENCE,
NodeKind::Mapping => RowKind::MAPPING,
NodeKind::KeyValue => RowKind::KEYVALUE,
NodeKind::Alias => RowKind::ALIAS,
}
}
pub(crate) fn from_row(v: u8) -> Option<Self> {
Some(match v {
RowKind::NULL => NodeKind::Null,
RowKind::FALSE | RowKind::TRUE => NodeKind::Bool,
RowKind::INT => NodeKind::Int,
RowKind::FLOAT => NodeKind::Float,
RowKind::STRING => NodeKind::String,
RowKind::SEQUENCE => NodeKind::Sequence,
RowKind::MAPPING => NodeKind::Mapping,
RowKind::KEYVALUE => NodeKind::KeyValue,
RowKind::ALIAS => NodeKind::Alias,
_ => return None,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RegionRow {
pub node: u32,
pub span: Span,
}
impl RegionRow {
pub fn new(node: u32, span: Span) -> Self {
RegionRow { node, span }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MentionRow {
pub node: u32,
pub span: Span,
pub kind: MentionKind,
}
impl MentionRow {
pub fn new(node: u32, span: Span, kind: MentionKind) -> Self {
MentionRow { node, span, kind }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MentionKind {
Header,
Entry,
Index,
Append,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CommentRow {
pub node: u32,
pub slot: CommentSlot,
pub style: CommentForm,
pub text: String,
}
impl CommentRow {
pub fn new(node: u32, slot: CommentSlot, style: CommentForm, text: impl Into<String>) -> Self {
CommentRow {
node,
slot,
style,
text: text.into(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DirectiveRow {
pub handle: String,
pub prefix: String,
}
impl DirectiveRow {
pub fn new(handle: impl Into<String>, prefix: impl Into<String>) -> Self {
DirectiveRow {
handle: handle.into(),
prefix: prefix.into(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DiagnosticRow {
pub severity: Severity,
pub span: Span,
pub message: String,
pub label: String,
}
impl DiagnosticRow {
pub fn new(
severity: Severity,
span: Span,
message: impl Into<String>,
label: impl Into<String>,
) -> Self {
DiagnosticRow {
severity,
span,
message: message.into(),
label: label.into(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Severity {
Error,
Warning,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CommentSlot {
Leading,
Trailing,
Dangling,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CommentForm {
Line,
Block,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PrintOptions {
pub pretty: bool,
pub strip_comments: bool,
pub indent: u8,
pub width: u16,
pub splice: bool,
pub flow: bool,
}
impl Default for PrintOptions {
fn default() -> Self {
PrintOptions {
pretty: true,
strip_comments: false,
indent: 2,
width: 80,
splice: false,
flow: false,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LanguageError {
pub message: String,
pub byte_offset: Option<usize>,
}
impl LanguageError {
pub fn new(message: impl Into<String>) -> Self {
LanguageError {
message: message.into(),
byte_offset: None,
}
}
pub fn at(message: impl Into<String>, byte_offset: usize) -> Self {
LanguageError {
message: message.into(),
byte_offset: Some(byte_offset),
}
}
}
impl std::fmt::Display for LanguageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.byte_offset {
Some(off) => write!(f, "{} (byte offset {off})", self.message),
None => f.write_str(&self.message),
}
}
}
impl std::error::Error for LanguageError {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Renderer {
Value,
Entry,
Item,
Tail,
Key,
KeyName,
String,
}
impl Renderer {
pub fn name(self) -> &'static str {
match self {
Renderer::Value => "value",
Renderer::Entry => "entry",
Renderer::Item => "item",
Renderer::Tail => "tail",
Renderer::Key => "key",
Renderer::KeyName => "key_name",
Renderer::String => "string",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Literal {
Null,
Bool,
Int,
Float,
Datetime,
#[default]
String,
}
impl Literal {
pub fn name(self) -> &'static str {
match self {
Literal::Null => "null",
Literal::Bool => "bool",
Literal::Int => "int",
Literal::Float => "float",
Literal::Datetime => "datetime",
Literal::String => "string",
}
}
pub fn from_name(name: &str) -> Option<Literal> {
Some(match name {
"null" => Literal::Null,
"bool" => Literal::Bool,
"int" => Literal::Int,
"float" => Literal::Float,
"datetime" => Literal::Datetime,
"string" => Literal::String,
_ => return None,
})
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RenderArgs<'a> {
pub dialect: &'a str,
pub indent: &'a [u8],
pub key: &'a [u8],
pub value: &'a [u8],
pub literal: Literal,
pub old_key: &'a [u8],
pub parent_key: &'a [u8],
pub parent_tag: &'a [u8],
pub old_value: &'a [u8],
}
impl<'a> RenderArgs<'a> {
pub fn dialect(mut self, dialect: &'a str) -> Self {
self.dialect = dialect;
self
}
pub fn indent(mut self, indent: &'a [u8]) -> Self {
self.indent = indent;
self
}
pub fn key(mut self, key: &'a [u8]) -> Self {
self.key = key;
self
}
pub fn value(mut self, value: &'a [u8]) -> Self {
self.value = value;
self
}
pub fn literal(mut self, literal: Literal) -> Self {
self.literal = literal;
self
}
pub fn old_key(mut self, old_key: &'a [u8]) -> Self {
self.old_key = old_key;
self
}
pub fn parent_key(mut self, parent_key: &'a [u8]) -> Self {
self.parent_key = parent_key;
self
}
pub fn parent_tag(mut self, parent_tag: &'a [u8]) -> Self {
self.parent_tag = parent_tag;
self
}
pub fn old_value(mut self, old_value: &'a [u8]) -> Self {
self.old_value = old_value;
self
}
}
pub trait Language: Send + Sync + 'static {
fn describe(&self) -> Description;
fn parse(&self, dialect: &str, input: &[u8]) -> Result<NodeTable, LanguageError>;
fn print(
&self,
dialect: &str,
table: &NodeTable,
options: &PrintOptions,
) -> Result<Vec<u8>, LanguageError> {
let _ = (dialect, table, options);
Err(LanguageError::new("this language does not print"))
}
fn render(&self, which: Renderer, args: RenderArgs<'_>) -> Result<Vec<u8>, LanguageError> {
let _ = args;
Err(LanguageError::new(format!(
"this language does not render {}",
which.name()
)))
}
fn split_stream(
&self,
dialect: &str,
input: &[u8],
) -> Result<Vec<StreamDocument>, LanguageError> {
let _ = (dialect, input);
Err(LanguageError::new(
"this language has no multi-document streams",
))
}
}
pub fn register(lang: impl Language) -> Result<Vec<Format>, Error> {
let desc = lang.describe();
let dialect_names: Vec<String> = desc.dialects.iter().map(|d| d.name.clone()).collect();
let adapter = Registration::new(Box::new(lang), desc)?;
registration(adapter.register())?;
dialect_names
.iter()
.map(|name| Format::by_name(name).ok_or(Error::Internal))
.collect()
}
fn registration(r: fig_core::Result<i32>) -> Result<Format, Error> {
let err = match r {
Ok(abi) => return Ok(Format(abi)),
Err(err) => err,
};
let (status, message) = match err.name() {
"OutOfMemory" => (crate::capi::Status::OutOfMemory, b"out of memory".to_vec()),
"InvalidLanguage" | "HarnessFailed" | "NameTaken" => (
crate::capi::Status::InvalidArgument,
registry::last_refusal(),
),
_ => (
crate::capi::Status::UnsupportedOperation,
b"the language registry cannot take another registration".to_vec(),
),
};
let message = String::from_utf8_lossy(&message[..message.len().min(255)]).into_owned();
if message.is_empty() {
Error::from_status(status)?;
return Err(Error::Internal);
}
Err(Error::Language(LanguageFailure {
message,
byte_offset: None,
}))
}
pub fn register_core(lang: &'static dyn CoreLanguage) -> Result<Format, Error> {
static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _held = LOCK.lock().unwrap_or_else(|p| p.into_inner());
registration(registry::register_bundled(registry::global(), lang))
}
struct Registration {
lang: AssertUnwindSafe<Box<dyn Language>>,
name: &'static str,
caps: manifest::Caps,
syntax: Option<manifest::Syntax>,
dialects: &'static [manifest::DialectInfo],
dialect_syntax: Vec<Option<manifest::Syntax>>,
dialect_extensions: Vec<Vec<&'static str>>,
extensions: &'static [&'static str],
samples: &'static [&'static [u8]],
renderers: Renderers,
streams: bool,
}
fn leak_str(s: &str) -> &'static str {
Box::leak(s.to_owned().into_boxed_str())
}
fn leak_bytes(s: &str) -> &'static [u8] {
leak_str(s).as_bytes()
}
fn checked(s: &str) -> Result<&str, Error> {
match s.find('\0') {
None => Ok(s),
Some(at) => Err(Error::Language(LanguageFailure {
message: format!("a declared string contains a NUL byte: {s:?}"),
byte_offset: Some(at),
})),
}
}
fn str_of(s: &str) -> Result<&'static str, Error> {
Ok(leak_str(checked(s)?))
}
fn bytes_of(s: &str) -> Result<&'static [u8], Error> {
Ok(leak_bytes(checked(s)?))
}
fn opt_bytes(s: Option<&str>) -> Result<Option<&'static [u8]>, Error> {
s.map(bytes_of).transpose()
}
fn native_kinds(l: &NativeKinds) -> manifest::NativeKinds {
manifest::NativeKinds {
null: l.null,
offset_datetime: l.offset_datetime,
local_datetime: l.local_datetime,
local_date: l.local_date,
local_time: l.local_time,
enum_literal: l.enum_literal,
char_literal: l.char_literal,
number_special: l.number_special,
plist_date: l.plist_date,
data: l.data,
}
}
fn delimiter(d: Option<&CommentDelimiter>) -> Result<Option<manifest::CommentDelimiter>, Error> {
let Some(d) = d else { return Ok(None) };
Ok(Some(manifest::CommentDelimiter {
open: bytes_of(&d.open)?,
close: bytes_of(&d.close)?,
forbidden: opt_bytes(d.forbidden.as_deref())?,
}))
}
fn syntax_of(s: &Syntax) -> Result<manifest::Syntax, Error> {
Ok(manifest::Syntax {
comments: manifest::Comments {
line: delimiter(s.comments.line.as_ref())?,
trailing: delimiter(s.comments.trailing.as_ref())?,
block: delimiter(s.comments.block.as_ref())?,
},
kv_sep: opt_bytes(s.kv_sep.as_deref())?,
flow_kv_sep_from_siblings: s.flow_kv_sep_from_siblings,
flow_map_pad: bytes_of(&s.flow_map_pad)?,
key_sigil: s.key_sigil.filter(|&b| b != 0),
empty_map_literal: opt_bytes(s.empty_map_literal.as_deref())?,
block_seq_editable: s.block_seq_editable,
flow_containers: s.flow_containers,
indent_unit: bytes_of(&s.indent_unit)?,
seq_item_marker: bytes_of(&s.seq_item_marker)?,
closed_containers: match &s.closed_containers {
Some(c) => Some(manifest::ClosedContainers {
map: manifest::Delimiters {
open: bytes_of(&c.map_open)?,
close: bytes_of(&c.map_close)?,
},
seq: manifest::Delimiters {
open: bytes_of(&c.seq_open)?,
close: bytes_of(&c.seq_close)?,
},
}),
None => None,
},
single_line_block_mapping: s.single_line_block_mapping,
bare_document_mapping: s.bare_document_mapping,
flow_map_open: bytes_of(&s.flow_map_open)?,
flow_map_close: bytes_of(&s.flow_map_close)?,
structural_indent: s.structural_indent,
section_noun: opt_bytes(s.section_noun.as_deref())?,
section_header: match &s.section_header {
Some(h) => Some(manifest::SectionHeader {
open: bytes_of(&h.open)?,
close: bytes_of(&h.close)?,
seq_open: opt_bytes(h.seq_open.as_deref())?,
seq_close: opt_bytes(h.seq_close.as_deref())?,
sep: bytes_of(&h.sep)?,
skip_index: h.skip_index,
}),
None => None,
},
merge_key: opt_bytes(s.merge_key.as_deref())?,
flow_entry_sep: bytes_of(&s.flow_entry_sep)?,
flow_root: s.flow_root,
flow_maps_only: s.flow_maps_only,
line_prefix_marks: bytes_of(&s.line_prefix_marks)?,
flow_map_kv_sep: bytes_of(&s.flow_map_kv_sep)?,
flow_map_entry_sep: bytes_of(&s.flow_map_entry_sep)?,
line_continuation: s.line_continuation.filter(|&b| b != 0),
empty_seq_literal: opt_bytes(s.empty_seq_literal.as_deref())?,
opener_line_comment: s.opener_line_comment,
})
}
fn prints_of(p: &Prints) -> Result<manifest::Prints, Error> {
Ok(manifest::Prints {
holds: native_kinds(&p.holds),
typed_scalars: p.typed_scalars,
null_at_root: p.null_at_root,
root_container: p.root_container,
char_as: str_of(&p.char_as)?,
number_special_as: str_of(&p.number_special_as)?,
comments: match p.comments {
CommentsPrinted::Always => manifest::PrintsComments::Always,
CommentsPrinted::Pretty => manifest::PrintsComments::Pretty,
CommentsPrinted::Never => manifest::PrintsComments::Never,
},
})
}
impl Registration {
fn new(lang: Box<dyn Language>, desc: Description) -> Result<Self, Error> {
let name = str_of(&desc.name)?;
let caps = manifest::Caps {
read: desc.caps.read,
edit: desc.caps.edit,
serialize: desc.caps.serialize,
references: desc.caps.references,
lossless: desc.lossless.as_ref().map(native_kinds),
max_mapping_depth: desc.max_mapping_depth,
};
let syntax = desc.syntax.as_ref().map(syntax_of).transpose()?;
let mut dialects = Vec::with_capacity(desc.dialects.len());
let mut dialect_syntax = Vec::with_capacity(desc.dialects.len());
let mut dialect_extensions = Vec::with_capacity(desc.dialects.len());
let mut extensions = Vec::new();
for (index, d) in desc.dialects.iter().enumerate() {
let exts: Vec<&'static str> = d
.extensions
.iter()
.map(|e| str_of(e))
.collect::<Result<_, _>>()?;
let splice = match d.splice {
Splice::Literal => manifest::SpliceStyle::Literal,
Splice::Escaped => manifest::SpliceStyle::Escaped,
Splice::Raw => manifest::SpliceStyle::Raw,
};
dialects.push(manifest::DialectInfo {
dialect: index as manifest::Dialect,
prints: d.prints.as_ref().map(prints_of).transpose()?,
..manifest::DialectInfo::new(
str_of(&d.name)?,
splice,
opt_bytes(d.empty_doc_seed.as_deref())?,
)
});
dialect_syntax.push(d.syntax.as_ref().map(syntax_of).transpose()?);
extensions.extend(exts.iter().copied());
dialect_extensions.push(exts);
}
let samples: Vec<&'static [u8]> = desc.samples.iter().map(|s| leak_bytes(s)).collect();
Ok(Registration {
lang: AssertUnwindSafe(lang),
name,
caps,
syntax,
dialects: Box::leak(dialects.into_boxed_slice()),
dialect_syntax,
dialect_extensions,
extensions: Box::leak(extensions.into_boxed_slice()),
samples: Box::leak(samples.into_boxed_slice()),
renderers: desc.renderers,
streams: desc.streams,
})
}
fn register(self) -> fig_core::Result<i32> {
registry::clear_refusal();
let name = self.name;
if !name.is_empty()
&& self.caps.read
&& !self.dialects.is_empty()
&& self.caps.edit
&& self.syntax.is_none()
{
registry::refuse(&format!(
"'{name}' declares the edit capability and must supply syntax"
));
return Err(fig_core::err!(InvalidLanguage));
}
registry::global().register_arc(Arc::new(self), RegisterOptions::default())
}
fn dialect_name(&self, d: manifest::Dialect) -> &'static str {
self.dialects.get(d as usize).map_or(self.name, |r| r.name)
}
fn has(&self, which: manifest::Renderer) -> bool {
let r = &self.renderers;
match which {
manifest::Renderer::Value => r.value,
manifest::Renderer::Entry => r.entry,
manifest::Renderer::Item => r.item,
manifest::Renderer::Tail => r.tail,
manifest::Renderer::Key => r.key,
manifest::Renderer::KeyName => r.key_name,
manifest::Renderer::String => r.string,
}
}
fn print_from(
&self,
dialect: manifest::Dialect,
ast: &AST,
root: NodeId,
options: &CoreSerializeOptions,
out: &mut Vec<u8>,
) -> fig_core::Result<()> {
if !self.caps.serialize {
return Err(fig_core::err!(FormatDisabled));
}
let mut built = convert::document_to_table(ast, root)?;
if options.strip_comments {
built.comments.clear();
}
let print_options = PrintOptions {
pretty: options.pretty,
strip_comments: options.strip_comments,
indent: options.indent,
width: options.width,
splice: options.splice,
flow: options.flow,
};
let dialect = self.dialect_name(dialect);
let result = catch_unwind(AssertUnwindSafe(|| {
let table = table_from_view(&built.view())?;
self.lang.print(dialect, &table, &print_options)
}));
match result {
Ok(Ok(bytes)) => {
out.extend_from_slice(&bytes);
Ok(())
}
Ok(Err(_)) | Err(_) => Err(fig_core::err!(RuntimePrintFailed)),
}
}
}
fn panic_message(p: Box<dyn std::any::Any + Send>) -> LanguageError {
let msg = p
.downcast_ref::<&str>()
.map(|s| s.to_string())
.or_else(|| p.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "panic".to_owned());
LanguageError::new(format!("the language panicked: {msg}"))
}
impl CoreLanguage for Registration {
fn name(&self) -> &str {
self.name
}
fn extensions(&self) -> &[&'static str] {
self.extensions
}
fn dialect_extensions(&self, row: usize) -> Option<Vec<&'static str>> {
Some(
self.dialect_extensions
.get(row)
.cloned()
.unwrap_or_default(),
)
}
fn caps(&self) -> manifest::Caps {
self.caps
}
fn dialects(&self) -> &[manifest::DialectInfo] {
self.dialects
}
fn samples(&self) -> &[&'static [u8]] {
self.samples
}
fn syntax(&self, dialect: manifest::Dialect) -> manifest::Syntax {
if let Some(Some(s)) = self.dialect_syntax.get(dialect as usize) {
return *s;
}
self.syntax
.unwrap_or(manifest::Syntax::new(manifest::Comments::NONE, None, None))
}
fn is_runtime(&self) -> bool {
true
}
fn parse_table(
&self,
input: &[u8],
dialect: manifest::Dialect,
out: &mut Table,
) -> fig_core::Result<()> {
let dialect = self.dialect_name(dialect);
let result = catch_unwind(AssertUnwindSafe(|| {
let table = self.lang.parse(dialect, input)?;
fill_table(out, &table)
}));
let e = match result {
Ok(Ok(())) => return Ok(()),
Ok(Err(e)) => e,
Err(p) => panic_message(p),
};
core_language::set_parse_error(e.message.as_bytes(), e.byte_offset.unwrap_or(0));
Err(fig_core::err!(RuntimeParseFailed))
}
fn print(
&self,
dialect: manifest::Dialect,
ast: &AST,
options: &CoreSerializeOptions,
out: &mut Vec<u8>,
) -> fig_core::Result<()> {
self.print_from(dialect, ast, ast.root, options, out)
}
fn print_node(
&self,
dialect: manifest::Dialect,
ast: &AST,
node: NodeId,
options: &CoreSerializeOptions,
out: &mut Vec<u8>,
) -> fig_core::Result<()> {
self.print_from(dialect, ast, node, options, out)
}
fn render(
&self,
which: manifest::Renderer,
dialect: manifest::Dialect,
out: &mut Vec<u8>,
r: &manifest::RenderRequest<'_>,
) -> Option<fig_core::Result<()>> {
if !self.has(which) {
return None;
}
let renderer = match which {
manifest::Renderer::Value => Renderer::Value,
manifest::Renderer::Entry => Renderer::Entry,
manifest::Renderer::Item => Renderer::Item,
manifest::Renderer::Tail => Renderer::Tail,
manifest::Renderer::Key => Renderer::Key,
manifest::Renderer::KeyName => Renderer::KeyName,
manifest::Renderer::String => Renderer::String,
};
let args = RenderArgs {
dialect: self.dialect_name(dialect),
indent: r.indent,
key: r.key,
value: r.value,
literal: Literal::from_name(r.literal.name()).unwrap_or_default(),
old_key: r.old_key,
parent_key: r.parent_key,
parent_tag: r.parent_tag,
old_value: r.old_value,
};
let result = match catch_unwind(AssertUnwindSafe(|| self.lang.render(renderer, args))) {
Ok(result) => result,
Err(p) => Err(panic_message(p)),
};
Some(match result {
Ok(bytes) => {
out.extend_from_slice(&bytes);
Ok(())
}
Err(e) => {
let bytes = &e.message.as_bytes()[..e.message.len().min(255)];
let text = String::from_utf8_lossy(bytes);
registry::refuse(if text.is_empty() {
"the renderer declined"
} else {
&text
});
Err(fig_core::err!(RendererRefused))
}
})
}
fn render_value(
&self,
d: manifest::Dialect,
out: &mut Vec<u8>,
r: &manifest::RenderRequest<'_>,
) -> Option<fig_core::Result<()>> {
self.render(manifest::Renderer::Value, d, out, r)
}
fn render_entry(
&self,
d: manifest::Dialect,
out: &mut Vec<u8>,
r: &manifest::RenderRequest<'_>,
) -> Option<fig_core::Result<()>> {
self.render(manifest::Renderer::Entry, d, out, r)
}
fn render_item(
&self,
d: manifest::Dialect,
out: &mut Vec<u8>,
r: &manifest::RenderRequest<'_>,
) -> Option<fig_core::Result<()>> {
self.render(manifest::Renderer::Item, d, out, r)
}
fn render_tail(
&self,
d: manifest::Dialect,
out: &mut Vec<u8>,
r: &manifest::RenderRequest<'_>,
) -> Option<fig_core::Result<()>> {
self.render(manifest::Renderer::Tail, d, out, r)
}
fn render_key(
&self,
d: manifest::Dialect,
out: &mut Vec<u8>,
r: &manifest::RenderRequest<'_>,
) -> Option<fig_core::Result<()>> {
self.render(manifest::Renderer::Key, d, out, r)
}
fn render_key_name(
&self,
d: manifest::Dialect,
out: &mut Vec<u8>,
r: &manifest::RenderRequest<'_>,
) -> Option<fig_core::Result<()>> {
self.render(manifest::Renderer::KeyName, d, out, r)
}
fn render_string(
&self,
d: manifest::Dialect,
out: &mut Vec<u8>,
r: &manifest::RenderRequest<'_>,
) -> Option<fig_core::Result<()>> {
self.render(manifest::Renderer::String, d, out, r)
}
fn has_renderer(&self, _dialect: manifest::Dialect, which: manifest::Renderer) -> bool {
self.has(which)
}
fn split_stream(
&self,
input: &[u8],
dialect: manifest::Dialect,
) -> Option<fig_core::Result<Vec<manifest::StreamDocument>>> {
if !self.streams {
return None;
}
let dialect = self.dialect_name(dialect);
let result = catch_unwind(AssertUnwindSafe(|| self.lang.split_stream(dialect, input)));
Some(match result {
Ok(Ok(docs)) => Ok(docs
.into_iter()
.map(|d| manifest::StreamDocument {
start: d.start,
end: d.end,
explicit: d.explicit,
})
.collect()),
Ok(Err(_)) | Err(_) => Err(fig_core::err!(RuntimeParseFailed)),
})
}
fn has_split_stream(&self) -> bool {
self.streams
}
}
fn fill_table(t: &mut Table, table: &NodeTable) -> Result<(), LanguageError> {
let oom = || LanguageError::new("out of memory filling the node table");
let refused = || LanguageError::new("fig refused a node table fact");
let span = |s: Option<Span>| -> (u32, u32) {
match s {
Some(s) => (s.start as u32, s.end as u32),
None => (table::NONE, table::NONE),
}
};
fn text(t: &mut Table, s: &str) -> Result<table::Text, LanguageError> {
t.add_string(s.as_bytes())
.map_err(|_| LanguageError::new("out of memory filling the node table"))
}
fn opt_text(t: &mut Table, s: &Option<String>) -> Result<table::Text, LanguageError> {
match s {
Some(s) => text(t, s),
None => Ok(table::Text::ABSENT),
}
}
let mut rows = Vec::with_capacity(table.rows.len());
for r in &table.rows {
let (start, end) = span(r.span);
let (kind, row_text) = match (r.ext_kind, r.kind) {
(Some(k), _) => (RowKind::EXT + k.to_c() as u8, opt_text(t, &r.text)?),
(None, NodeKind::Bool) => match r.text.as_deref() {
Some("true") => (RowKind::TRUE, table::Text::ABSENT),
Some("false") => (RowKind::FALSE, table::Text::ABSENT),
_ => return Err(LanguageError::new("a bool row's text is not true or false")),
},
(None, k) => (k.to_row(), opt_text(t, &r.text)?),
};
rows.push(table::Row {
kind,
flags: if r.open { table::ROW_OPEN } else { 0 },
parent: r.parent.unwrap_or(table::NONE),
start,
end,
text: row_text,
first_child: table::NONE,
next_sibling: table::NONE,
});
}
if !rows.is_empty() {
let dest = t.add_rows(rows.len()).map_err(|_| oom())?;
dest.copy_from_slice(&rows);
}
let row_count = t.rows.len() as u32;
let on_row = |node: u32| {
if node < row_count {
Ok(())
} else {
Err(refused())
}
};
for (i, r) in table.rows.iter().enumerate() {
let node = i as u32;
if let Some(name) = &r.anchor {
let (start, end) = span(r.anchor_span);
let name = text(t, name)?;
on_row(node)?;
t.anchors.push(table::Anchor {
node,
start,
end,
name,
});
}
if let Some(tag) = &r.tag {
let (start, end) = span(r.tag_span);
let tag = text(t, tag)?;
on_row(node)?;
t.tags.push(table::Tag {
node,
start,
end,
text: tag,
resolved: table::TAG_UNSTATED,
agrees: 1,
});
}
if let Some(m) = r.marker {
on_row(node)?;
t.markers.push(table::SpanFact {
node,
start: m.start as u32,
end: m.end as u32,
});
}
if let Some(m) = r.sep {
on_row(node)?;
t.seps.push(table::SpanFact {
node,
start: m.start as u32,
end: m.end as u32,
});
}
if let Some(is) = &r.item_sep {
let is = text(t, is)?;
on_row(node)?;
t.item_seps.push(table::ItemSep { node, text: is });
}
}
for r in &table.regions {
on_row(r.node)?;
t.regions.push(table::SpanFact {
node: r.node,
start: r.span.start as u32,
end: r.span.end as u32,
});
}
for m in &table.mentions {
let kind = match m.kind {
MentionKind::Header => table::MENTION_HEADER,
MentionKind::Entry => table::MENTION_ENTRY,
MentionKind::Index => table::MENTION_INDEX,
MentionKind::Append => table::MENTION_APPEND,
};
on_row(m.node)?;
t.mentions.push(table::Mention {
node: m.node,
start: m.span.start as u32,
end: m.span.end as u32,
kind,
});
}
for c in &table.comments {
let slot = match c.slot {
CommentSlot::Leading => table::COMMENT_LEADING,
CommentSlot::Trailing => table::COMMENT_TRAILING,
CommentSlot::Dangling => table::COMMENT_DANGLING,
};
let style = match c.style {
CommentForm::Line => table::COMMENT_LINE,
CommentForm::Block => table::COMMENT_BLOCK,
};
let body = text(t, &c.text)?;
on_row(c.node)?;
t.comments.push(table::Comment {
node: c.node,
slot,
style,
text: body,
});
}
for d in &table.directives {
let handle = text(t, &d.handle)?;
let prefix = text(t, &d.prefix)?;
t.directives.push(table::Directive { handle, prefix });
}
for d in &table.diagnostics {
let severity = match d.severity {
Severity::Error => table::DIAGNOSTIC_ERROR,
Severity::Warning => table::DIAGNOSTIC_WARNING,
};
let message = text(t, &d.message)?;
let label = text(t, &d.label)?;
t.diagnostics.push(table::Diagnostic {
severity,
start: d.span.start as u32,
end: d.span.end as u32,
message,
label,
});
}
Ok(())
}
pub(crate) fn table_from_view(v: &table::View<'_>) -> Result<NodeTable, LanguageError> {
let text_of = |t: table::Text| -> Result<Option<String>, LanguageError> {
if t.is_absent() {
return Ok(None);
}
let bytes = v
.text_of(t)
.ok_or_else(|| LanguageError::new("a table text is out of range"))?;
String::from_utf8(bytes.to_vec())
.map(Some)
.map_err(|_| LanguageError::new("a table string is not UTF-8"))
};
let span_of = |start: u32, end: u32| -> Option<Span> {
if start == table::NONE {
None
} else {
Some(Span {
start: start as usize,
end: end as usize,
})
}
};
let mut out = NodeTable::new();
for r in v.rows {
let (kind, ext_kind, text) = if r.kind >= RowKind::EXT {
let k = ExtKind::from_c((r.kind - RowKind::EXT) as c_int)
.ok_or_else(|| LanguageError::new("unknown extended kind"))?;
let kind = if k == ExtKind::CharLiteral {
NodeKind::Int
} else {
NodeKind::String
};
(kind, Some(k), text_of(r.text)?)
} else {
match r.kind {
RowKind::FALSE => (NodeKind::Bool, None, Some("false".to_owned())),
RowKind::TRUE => (NodeKind::Bool, None, Some("true".to_owned())),
k => (
NodeKind::from_row(k).ok_or_else(|| LanguageError::new("unknown node kind"))?,
None,
text_of(r.text)?,
),
}
};
out.rows.push(NodeRow {
kind,
ext_kind,
parent: if r.parent == table::NONE {
None
} else {
Some(r.parent)
},
span: span_of(r.start, r.end),
text,
anchor: None,
anchor_span: None,
tag: None,
tag_span: None,
marker: None,
sep: None,
item_sep: None,
open: r.flags & table::ROW_OPEN != 0,
});
}
let row = |out: &NodeTable, node: u32| -> Result<usize, LanguageError> {
let i = node as usize;
if i < out.rows.len() {
Ok(i)
} else {
Err(LanguageError::new("a side fact names no row"))
}
};
for a in v.anchors {
let i = row(&out, a.node)?;
out.rows[i].anchor = text_of(a.name)?;
out.rows[i].anchor_span = span_of(a.start, a.end);
}
for t in v.tags {
let i = row(&out, t.node)?;
out.rows[i].tag = text_of(t.text)?;
out.rows[i].tag_span = span_of(t.start, t.end);
}
for m in v.markers {
let i = row(&out, m.node)?;
out.rows[i].marker = span_of(m.start, m.end);
}
for m in v.seps {
let i = row(&out, m.node)?;
out.rows[i].sep = span_of(m.start, m.end);
}
for m in v.item_seps {
let i = row(&out, m.node)?;
out.rows[i].item_sep = text_of(m.text)?;
}
for r in v.regions {
out.regions.push(RegionRow {
node: r.node,
span: Span {
start: r.start as usize,
end: r.end as usize,
},
});
}
for m in v.mentions {
out.mentions.push(MentionRow {
node: m.node,
span: span_of(m.start, m.end)
.ok_or_else(|| LanguageError::new("a mention has no span"))?,
kind: match m.kind {
table::MENTION_ENTRY => MentionKind::Entry,
table::MENTION_INDEX => MentionKind::Index,
table::MENTION_APPEND => MentionKind::Append,
_ => MentionKind::Header,
},
});
}
for c in v.comments {
out.comments.push(CommentRow {
node: c.node,
slot: match c.slot {
table::COMMENT_TRAILING => CommentSlot::Trailing,
table::COMMENT_DANGLING => CommentSlot::Dangling,
_ => CommentSlot::Leading,
},
style: if c.style == table::COMMENT_BLOCK {
CommentForm::Block
} else {
CommentForm::Line
},
text: text_of(c.text)?.unwrap_or_default(),
});
}
for d in v.directives {
out.directives.push(DirectiveRow {
handle: text_of(d.handle)?
.ok_or_else(|| LanguageError::new("a directive has no handle"))?,
prefix: text_of(d.prefix)?
.ok_or_else(|| LanguageError::new("a directive has no prefix"))?,
});
}
Ok(out)
}