Skip to main content

meta_language/translation/
mod.rs

1//! Translation between JavaScript, Rust, Lean and Rocq through a checked
2//! portable core.
3//!
4//! Each frontend reads a source program into the surface AST,
5//! the checker resolves names, types and the exact operator semantics, and
6//! each emitter writes a target program together with its mapping, the
7//! encodings it chose and the assumptions it relies on.
8//!
9//! Mirrors `js/src/translation/`; both runtimes produce the same surface
10//! trees, checked programs, target texts and contracts.
11
12use serde::{Deserialize, Serialize};
13
14pub mod aborts;
15pub(crate) mod array_binding_rules;
16pub(crate) mod array_callback_rules;
17pub(crate) mod array_classification_rules;
18pub(crate) mod array_from_rules;
19pub(crate) mod array_value_rules;
20pub(crate) mod ascii_text_predicate_rules;
21pub(crate) mod boolean_declaration_value_rules;
22pub(crate) mod borrowed_call_interface;
23pub(crate) mod borrowed_call_interface_rules;
24pub(crate) mod callable_emission_rules;
25pub(crate) mod callable_source_rules;
26pub mod check;
27pub(crate) mod collection_construction_rules;
28pub mod decimal;
29pub(crate) mod declaration_dependency_rules;
30pub mod diagnostics;
31pub(crate) mod embedded_region_rules;
32pub mod emit_common;
33pub mod emit_javascript;
34pub mod emit_lean;
35pub mod emit_rocq;
36pub mod emit_rust;
37pub(crate) mod external_emission_rules;
38pub(crate) mod frontend_rules;
39pub(crate) mod grammar_attribute_order_rules;
40pub(crate) mod grammar_lexer_rules;
41pub(crate) mod grammar_rule_composition_rules;
42pub(crate) mod grammar_selection_field_range_rules;
43pub(crate) mod grammar_selection_included_range_rules;
44pub(crate) mod grammar_selection_mapping_rules;
45pub(crate) mod grammar_selection_rules;
46pub(crate) mod guarded_private_array_write_rules;
47pub mod ir;
48pub mod javascript;
49mod javascript_line_terminator_rules;
50pub(crate) mod javascript_record_parameter_rules;
51pub(crate) mod javascript_tag_context_rules;
52pub mod js_number;
53pub mod lean;
54pub mod lean_root_names;
55pub mod lexer;
56pub(crate) mod link_network_binary_buffer_rules;
57pub(crate) mod network_projection_rules;
58pub(crate) mod nullable_type_syntax_rules;
59mod number_predicate_rules;
60pub(crate) mod optional_inference_rules;
61pub(crate) mod optional_value_rules;
62pub mod output;
63pub(crate) mod owned_array_append_rules;
64pub(crate) mod owned_local_state;
65pub(crate) mod parameter_default_rules;
66pub(crate) mod partial_callable_rules;
67pub(crate) mod present_type;
68mod primitive_method_rules;
69pub mod primitive_value_rules;
70pub mod product_emission_rules;
71pub(crate) mod program_number_projection_rules;
72mod program_string_representation_rules;
73pub mod proof;
74pub(crate) mod record_emission_rules;
75pub(crate) mod record_type_syntax_rules;
76pub(crate) mod record_value_rules;
77pub(crate) mod reference_identity_rules;
78pub mod rocq;
79pub(crate) mod runtime_shell;
80pub(crate) mod runtime_shell_rules;
81pub mod rust;
82pub(crate) mod rust_character_rules;
83mod rust_const_branch_rules;
84pub(crate) mod rust_diagnostic_attribute_rules;
85pub(crate) mod rust_literal_match_rules;
86pub(crate) mod scalar_loop_rules;
87pub(crate) mod scoped_binding_emission;
88pub(crate) mod scoped_binding_emission_rules;
89pub(crate) mod self_translation_layout_rules;
90pub(crate) mod source_const_functions;
91pub(crate) mod source_diagnostic_attributes;
92pub(crate) mod source_empty_options;
93pub(crate) mod source_header_emission;
94pub(crate) mod source_header_emission_rules;
95pub mod source_loops;
96pub(crate) mod source_primitive_owners;
97pub(crate) mod source_pure_unit_calls;
98pub(crate) mod source_signature_rules;
99pub mod source_signatures;
100pub(crate) mod source_text_byte_rules;
101pub(crate) mod source_text_bytes;
102pub(crate) mod source_type_namespaces;
103pub(crate) mod source_unit_clone_derivations;
104mod string_order_rules;
105pub mod structured_type_rules;
106pub mod surface;
107pub mod surface_raw_metadata_rules;
108pub mod surface_reference_analysis_rules;
109pub(crate) mod surface_reference_facts;
110pub(crate) mod type_namespace_rules;
111pub(crate) mod type_syntax_rules;
112pub(crate) mod typed_program_link_rules;
113pub mod types;
114
115/// A source range in UTF-16 code units, as JavaScript string offsets count.
116#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
117pub struct Span {
118    pub start: usize,
119    pub end: usize,
120}
121
122impl Span {
123    #[must_use]
124    pub const fn new(start: usize, end: usize) -> Self {
125        Self { start, end }
126    }
127}
128
129/// The four languages of the translation matrix.
130#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
131pub enum Language {
132    JavaScript,
133    Rust,
134    Lean,
135    Rocq,
136}
137
138impl Language {
139    pub const ALL: [Self; 4] = [Self::JavaScript, Self::Rust, Self::Lean, Self::Rocq];
140
141    #[must_use]
142    pub const fn as_str(self) -> &'static str {
143        match self {
144            Self::JavaScript => "JavaScript",
145            Self::Rust => "Rust",
146            Self::Lean => "Lean",
147            Self::Rocq => "Rocq",
148        }
149    }
150
151    #[must_use]
152    pub fn parse(name: &str) -> Option<Self> {
153        Self::ALL
154            .into_iter()
155            .find(|language| language.as_str() == name)
156    }
157}