Started codegen work for a few languages, more interpretation changes, and overall work
This commit is contained in:
parent
eef6a218f8
commit
fc6686a2a1
|
@ -19,7 +19,7 @@ semver = "1.0.18"
|
|||
blake3 = "1.4.1"
|
||||
lz4_flex = "0.11.1"
|
||||
lex = "0.6.0"
|
||||
snafu = "0.7.5"
|
||||
snafu = { version = "0.7.5", features = ["guide"] }
|
||||
strum_macros = "0.25.1"
|
||||
strum = { version = "0.25.0", features = ["derive"] }
|
||||
downcast-rs = "1.2.0"
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
// Crate Uses
|
||||
use crate::schema::ir::diff::Differ;
|
||||
use crate::schema::ir::frozen::unit::FrozenUnit;
|
||||
use crate::schema::ir::compiler::interpreted::primitive::KindValue;
|
||||
use crate::schema::ir::compiler::interpreted::kind_search::KindValue;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
@ -61,7 +61,7 @@ impl Differ for Document {
|
|||
if left_value_display != right_value_display {
|
||||
self.minor_changes.push(
|
||||
format!(
|
||||
"Constant '{}' default value changed from '{}' to '{}'", name,
|
||||
"Constant '{}' default value changed from '{:?}' to '{:?}'", name,
|
||||
left_value_display, right_value_display
|
||||
)
|
||||
)
|
||||
|
|
3
core/src/codegen/lua/lib.lua
Normal file
3
core/src/codegen/lua/lib.lua
Normal file
|
@ -0,0 +1,3 @@
|
|||
|
||||
-- https://andrejs-cainikovs.blogspot.com/2009/05/lua-constants.html
|
||||
-- http://lua-users.org/wiki/ReadOnlyTables
|
52
core/src/codegen/luau/luau.rs
Normal file
52
core/src/codegen/luau/luau.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
// Standard Uses
|
||||
|
||||
// Crate Uses
|
||||
use crate::schema::ir::frozen::unit::FrozenUnit;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
pub(crate) fn frozen_to_code(units: &Vec<FrozenUnit>) -> String {
|
||||
let mut code = String::new();
|
||||
|
||||
let mut code = "// Generated code with Comline compiler and code generator\n\n"
|
||||
.to_owned();
|
||||
|
||||
for unit in units { code += &*unit_to_code(&unit); }
|
||||
|
||||
code
|
||||
}
|
||||
|
||||
fn unit_to_code(frozen_unit: &FrozenUnit) -> String {
|
||||
let mut code = String::new();
|
||||
|
||||
use FrozenUnit::*;
|
||||
match frozen_unit {
|
||||
Namespace(_) => {}
|
||||
Import(_) => {}
|
||||
Constant { docstring, name, kind_value } => {
|
||||
let (kind_name, kind_value) = kind_value.name_and_value();
|
||||
code += &*format!("const {name}: {}", kind_name);
|
||||
|
||||
if let Some(value) = kind_value {
|
||||
code += &*format!(" = {value}");
|
||||
}
|
||||
|
||||
code += ";\n";
|
||||
}
|
||||
Property { .. } => {}
|
||||
Parameter { .. } => {}
|
||||
ExpressionBlock { .. } => {}
|
||||
Enum { .. } => {}
|
||||
EnumVariant(_) => {}
|
||||
Settings { .. } => {}
|
||||
Struct { .. } => {}
|
||||
Protocol { .. } => {}
|
||||
Function { .. } => {}
|
||||
Error { .. } => {}
|
||||
Validator { .. } => {}
|
||||
_ => panic!("Reached forbidden or unimplemented node")
|
||||
}
|
||||
|
||||
code
|
||||
}
|
21
core/src/codegen/luau/mod.rs
Normal file
21
core/src/codegen/luau/mod.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
// https://luau-lang.org/
|
||||
|
||||
// Relative Modules
|
||||
pub mod luau;
|
||||
|
||||
// Standard Uses
|
||||
use std::collections::HashMap;
|
||||
|
||||
// Crate Uses
|
||||
use crate::codegen::VersionGenerators;
|
||||
|
||||
// External Uses
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
|
||||
pub(crate) static GENERATORS: VersionGenerators = Lazy::new(|| {
|
||||
HashMap::from([
|
||||
("Luau", luau::frozen_to_code as _)
|
||||
])
|
||||
});
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
// Relative Modules
|
||||
pub mod rust;
|
||||
pub(crate) mod rust;
|
||||
pub(crate) mod luau;
|
||||
pub(crate) mod python;
|
||||
|
||||
// Standard Uses
|
||||
use std::collections::HashMap;
|
||||
|
@ -11,18 +13,25 @@ use crate::schema::ir::frozen::unit::FrozenUnit;
|
|||
use once_cell::sync::Lazy;
|
||||
|
||||
|
||||
pub type VersionGenerators = Lazy<HashMap<&'static str, GeneratorFn>>;
|
||||
pub type GeneratorFn = fn(&Vec<FrozenUnit>) -> String;
|
||||
pub type Generator = (GeneratorFn, &'static str);
|
||||
|
||||
static GENERATORS: Lazy<HashMap<&str, GeneratorFn>> = Lazy::new(|| {
|
||||
static LANG_GENERATORS: Lazy<HashMap<&str, (&VersionGenerators, &str)>> = Lazy::new(|| {
|
||||
HashMap::from([
|
||||
("rust", rust::frozen_unit_to_code as _),
|
||||
("rust", (&rust::GENERATORS, "rs")),
|
||||
|
||||
("luau", (&luau::GENERATORS, "luau")),
|
||||
("python", (&python::GENERATORS, "py"))
|
||||
])
|
||||
});
|
||||
|
||||
pub fn find_generator(name: &str) -> Option<&GeneratorFn> {
|
||||
if !GENERATORS.contains_key(name) {
|
||||
return None
|
||||
}
|
||||
pub fn find_generator(name: &str, version: &str) -> Option<(&'static GeneratorFn, &'static str)> {
|
||||
if let Some((lang_generator, extension)) = LANG_GENERATORS.get(name) {
|
||||
if let Some(version_generator) = lang_generator.get(version) {
|
||||
return Some((version_generator, extension))
|
||||
}
|
||||
};
|
||||
|
||||
GENERATORS.get(name)
|
||||
None
|
||||
}
|
||||
|
|
151
core/src/codegen/python/_3_11_0.rs
Normal file
151
core/src/codegen/python/_3_11_0.rs
Normal file
|
@ -0,0 +1,151 @@
|
|||
// Standard Uses
|
||||
|
||||
// Crate Uses
|
||||
use crate::schema::ir::frozen::unit::{FrozenArgument, FrozenUnit};
|
||||
use crate::schema::ir::compiler::interpreted::kind_search::KindValue;
|
||||
|
||||
// External Uses
|
||||
use eyre::anyhow;
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn frozen_to_code(units: &Vec<FrozenUnit>) -> String {
|
||||
let mut code = "\
|
||||
# Generated code with Comline compiler and code generator\n\n"
|
||||
.to_owned();
|
||||
|
||||
code += "\
|
||||
import typing\n\
|
||||
import ctypes\n\
|
||||
\n\n\
|
||||
";
|
||||
|
||||
for unit in units { code += &*unit_to_code(&unit); }
|
||||
|
||||
code
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn unit_to_code(frozen_unit: &FrozenUnit) -> String {
|
||||
let mut code = String::new();
|
||||
|
||||
use FrozenUnit::*;
|
||||
match frozen_unit {
|
||||
Namespace(_) => {}
|
||||
Import(_) => {}
|
||||
Constant { docstring, name, kind_value } => {
|
||||
let (kind_name, default_value) = kind_value.name_and_value();
|
||||
|
||||
let Ok(kind_py) = from_frozen_kind_to_python_kind(kind_value) else {
|
||||
panic!("No appropriate kind was found for '{kind_name}'")
|
||||
};
|
||||
|
||||
// Name and kind
|
||||
code += &*format!("{name}: typing.Final[{kind_py}]");
|
||||
|
||||
// Value
|
||||
if let Some(value) = default_value {
|
||||
code += &*format!(" = {value}");
|
||||
}
|
||||
|
||||
code += "\n\n";
|
||||
}
|
||||
Property { .. } => {}
|
||||
Parameter { .. } => {}
|
||||
ExpressionBlock { .. } => {}
|
||||
Enum { .. } => {}
|
||||
EnumVariant(_) => {}
|
||||
Settings { .. } => {}
|
||||
Struct { .. } => {}
|
||||
Protocol {
|
||||
docstring, parameters,
|
||||
name, functions
|
||||
} => {
|
||||
// Async
|
||||
// TODO: Can we signify assyncness in python protocols, or if its that important, ABCs?
|
||||
// Associated methods already can be marked "async"
|
||||
|
||||
// Class Start
|
||||
code += &*format!("class {name}(typing.Protocol):\n");
|
||||
|
||||
// Functions
|
||||
for function in functions {
|
||||
code += &*unit_to_code(function);
|
||||
}
|
||||
|
||||
// Class End
|
||||
code += "\n";
|
||||
}
|
||||
Function {
|
||||
docstring, name, synchronous,
|
||||
arguments, returns,
|
||||
throws
|
||||
} => {
|
||||
// Async definition
|
||||
if !synchronous { code += "async "; }
|
||||
|
||||
// Function Start
|
||||
code += &*format!("\tdef {name}(");
|
||||
|
||||
// Arguments
|
||||
code += "self";
|
||||
|
||||
let mut idx = 0;
|
||||
while idx != arguments.len() {
|
||||
let FrozenArgument { name, kind }
|
||||
= arguments.get(idx).unwrap();
|
||||
|
||||
let (kind_name, kind_value) = kind.name_and_value();
|
||||
code += &*format!("{}: {:?}", name, kind_name);
|
||||
|
||||
if idx != arguments.len() { code += ", " }
|
||||
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
// Arguments End
|
||||
code += "):\n\t\t...\n\n";
|
||||
|
||||
// Returns
|
||||
// code += format!("-> {}", return)
|
||||
|
||||
// Function End
|
||||
}
|
||||
Error { .. } => {}
|
||||
Validator { .. } => {}
|
||||
_ => panic!("Reached forbidden or unimplemented node")
|
||||
}
|
||||
|
||||
code
|
||||
}
|
||||
|
||||
fn from_frozen_kind_to_python_kind(kind_value: &KindValue) -> eyre::Result<String> {
|
||||
let kind = match kind_value {
|
||||
KindValue::Primitive(primitive) => {
|
||||
let name = primitive.name();
|
||||
from_frozen_primitive_to_ctypes(name)
|
||||
.ok_or_else(|| anyhow!("No primitive equivalent found for {name}"))?
|
||||
}
|
||||
KindValue::EnumVariant(_, _) => {
|
||||
todo!()
|
||||
}
|
||||
KindValue::Union(_) => {
|
||||
todo!()
|
||||
}
|
||||
KindValue::Namespaced(_, _) => {
|
||||
todo!()
|
||||
}
|
||||
};
|
||||
|
||||
Ok(kind)
|
||||
}
|
||||
|
||||
fn from_frozen_primitive_to_ctypes(primitive: &str) -> Option<String> {
|
||||
let kind = match primitive {
|
||||
"u8" => "c_uint8",
|
||||
"u16" => "c_uint16",
|
||||
_ => { return None }
|
||||
};
|
||||
|
||||
Some(format!("ctypes.{kind}"))
|
||||
}
|
19
core/src/codegen/python/mod.rs
Normal file
19
core/src/codegen/python/mod.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Relative Modules
|
||||
pub(crate) mod _3_11_0;
|
||||
|
||||
// Standard Uses
|
||||
use std::collections::HashMap;
|
||||
|
||||
// Crate Uses
|
||||
use crate::codegen::VersionGenerators;
|
||||
|
||||
// External Uses
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
|
||||
pub(crate) static GENERATORS: VersionGenerators = Lazy::new(|| {
|
||||
HashMap::from([
|
||||
("3.11.0", _3_11_0::frozen_to_code as _)
|
||||
])
|
||||
});
|
||||
|
|
@ -1,102 +0,0 @@
|
|||
// Standard Uses
|
||||
|
||||
// Crate Uses
|
||||
use crate::schema::ir::frozen::unit::FrozenUnit;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn frozen_unit_to_code(units: &Vec<FrozenUnit>) -> String {
|
||||
|
||||
"".to_owned()
|
||||
}
|
||||
|
||||
/*
|
||||
fn unit_to_code(frozen_unit: FrozenUnit) -> String {
|
||||
let mut code = String::new();
|
||||
|
||||
use FrozenUnit::*;
|
||||
match frozen_unit {
|
||||
Tag(_) => {}
|
||||
Namespace(_) => {}
|
||||
Import(_) => {}
|
||||
Constant { .. } => {}
|
||||
Property { .. } => {}
|
||||
Parameter { .. } => {}
|
||||
ExpressionBlock { .. } => {}
|
||||
Enum { .. } => {}
|
||||
EnumVariant(_) => {}
|
||||
Settings { .. } => {}
|
||||
Struct { .. } => {}
|
||||
Protocol { .. } => {}
|
||||
Function { .. } => {}
|
||||
Error { .. } => {}
|
||||
Validator { .. } => {}
|
||||
_ => panic!("Reached forbidden or unimplemented node")
|
||||
}
|
||||
|
||||
code
|
||||
}
|
||||
|
||||
fn protocol_to_code(unit: FrozenUnit) -> String {
|
||||
let FrozenUnit::Protocol {
|
||||
docstring, parameters, name, functions
|
||||
} = unit else { panic!() };
|
||||
|
||||
let mut code = String::new();
|
||||
|
||||
// Trait Start
|
||||
// TODO: Maybe only include async_trait if at least one of the functions need it
|
||||
code.push_str("#[async_trait]");
|
||||
code.push_str(&*format!("pub trait {} {{", name));
|
||||
|
||||
// Functions
|
||||
for function in functions {
|
||||
let FrozenUnit::Function {
|
||||
docstring, name, synchronous, direction,
|
||||
arguments, returns, throws
|
||||
} = function else { panic!() };
|
||||
|
||||
let mut fn_code = String::new();
|
||||
|
||||
// Async definition (with async_trait)
|
||||
if !synchronous {
|
||||
fn_code.push_str("async ");
|
||||
}
|
||||
|
||||
// Fn
|
||||
fn_code.push_str(&*format!("fn {} (", name));
|
||||
|
||||
// Arguments
|
||||
let mut idx = 0;
|
||||
while idx != arguments.len() {
|
||||
let FrozenUnit::Parameter {
|
||||
name, default_value
|
||||
} = arguments.get(idx).unwrap();
|
||||
|
||||
if argument.id.is_none() {
|
||||
fn_code.push_str(&*format!("arg{}: {:?}", idx, argument.type_));
|
||||
if idx != arguments.len() { fn_code.push_str(", ") }
|
||||
} else {
|
||||
fn_code.push_str(
|
||||
&*format!("{}: {:?}", argument.id.clone().unwrap(), argument.type_)
|
||||
);
|
||||
if idx != arguments.len() { fn_code.push_str(", ") }
|
||||
}
|
||||
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
// Returns
|
||||
|
||||
code.push_str(&*fn_code)
|
||||
}
|
||||
|
||||
// Trait End
|
||||
code.push_str("}}");
|
||||
|
||||
code.to_owned()
|
||||
}
|
||||
*/
|
||||
|
169
core/src/codegen/rust/_1_7_0.rs
Normal file
169
core/src/codegen/rust/_1_7_0.rs
Normal file
|
@ -0,0 +1,169 @@
|
|||
// Standard Uses
|
||||
|
||||
// Crate Uses
|
||||
use crate::schema::ir::frozen::unit::{FrozenArgument, FrozenUnit};
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn frozen_to_code(units: &Vec<FrozenUnit>) -> String {
|
||||
let mut code = "// Generated code with Comline compiler and code generator\n\n"
|
||||
.to_owned();
|
||||
|
||||
for unit in units { code += &*unit_to_code(&unit); }
|
||||
|
||||
code
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn unit_to_code(frozen_unit: &FrozenUnit) -> String {
|
||||
let mut code = String::new();
|
||||
|
||||
use FrozenUnit::*;
|
||||
match frozen_unit {
|
||||
Namespace(_) => {}
|
||||
Import(_) => {}
|
||||
Constant { docstring, name, kind_value } => {
|
||||
let (kind_name, kind_value) = kind_value.name_and_value();
|
||||
code += &*format!("pub const {name}: {}", kind_name);
|
||||
|
||||
if let Some(value) = kind_value {
|
||||
code += &*format!(" = {value}");
|
||||
}
|
||||
|
||||
code += ";\n\n";
|
||||
}
|
||||
Property { .. } => {}
|
||||
Parameter { .. } => {}
|
||||
ExpressionBlock { .. } => {}
|
||||
Enum { .. } => {}
|
||||
EnumVariant(_) => {}
|
||||
Settings { .. } => {}
|
||||
Struct { .. } => {}
|
||||
Protocol {
|
||||
docstring, parameters,
|
||||
name, functions
|
||||
} => {
|
||||
// Async
|
||||
// TODO: Only include async_trait if at least one of the functions needs it
|
||||
// code += "#[async_trait]\n";
|
||||
|
||||
// Trait Start
|
||||
code += &*format!("pub trait {} {{\n", name);
|
||||
|
||||
// Functions
|
||||
for function in functions {
|
||||
code += &*unit_to_code(function);
|
||||
}
|
||||
|
||||
// Trait End
|
||||
code += "\n}\n\n";
|
||||
}
|
||||
Function {
|
||||
docstring, name, synchronous,
|
||||
arguments, returns,
|
||||
throws
|
||||
} => {
|
||||
code += "\t";
|
||||
|
||||
// Async definition (with async_trait)
|
||||
if !synchronous { code += "async "; }
|
||||
|
||||
// Function Start
|
||||
code += &*format!("fn {} (", name);
|
||||
code += "&self";
|
||||
|
||||
// Arguments
|
||||
let mut idx = 0;
|
||||
while idx != arguments.len() {
|
||||
let FrozenArgument { name, kind }
|
||||
= arguments.get(idx).unwrap();
|
||||
|
||||
let (kind_name, kind_value) = kind.name_and_value();
|
||||
code += &*format!("{}: {:?}", name, kind_name);
|
||||
|
||||
if idx != arguments.len() { code += ", " }
|
||||
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
// Arguments End
|
||||
code += ")";
|
||||
|
||||
// Returns
|
||||
// code += format!("-> {}", return)
|
||||
|
||||
// Function End
|
||||
code += ";\n";
|
||||
}
|
||||
Error { .. } => {}
|
||||
Validator { .. } => {}
|
||||
_ => panic!("Reached forbidden or unimplemented node")
|
||||
}
|
||||
|
||||
code
|
||||
}
|
||||
|
||||
/*
|
||||
fn protocol_to_code(unit: FrozenUnit) -> String {
|
||||
let FrozenUnit::Protocol {
|
||||
docstring, parameters, name, functions
|
||||
} = unit else { panic!() };
|
||||
|
||||
let mut code = String::new();
|
||||
|
||||
// Trait Start
|
||||
// TODO: Maybe only include async_trait if at least one of the functions need it
|
||||
code.push_str("#[async_trait]");
|
||||
code.push_str(&*format!("pub trait {} {{", name));
|
||||
|
||||
// Functions
|
||||
for function in functions {
|
||||
let FrozenUnit::Function {
|
||||
docstring, name, synchronous, direction,
|
||||
arguments, returns, throws
|
||||
} = function else { panic!() };
|
||||
|
||||
let mut fn_code = String::new();
|
||||
|
||||
// Async definition (with async_trait)
|
||||
if !synchronous {
|
||||
fn_code.push_str("async ");
|
||||
}
|
||||
|
||||
// Fn
|
||||
fn_code.push_str(&*format!("fn {} (", name));
|
||||
|
||||
// Arguments
|
||||
let mut idx = 0;
|
||||
while idx != arguments.len() {
|
||||
let FrozenUnit::Parameter {
|
||||
name, default_value
|
||||
} = arguments.get(idx).unwrap();
|
||||
|
||||
if argument.id.is_none() {
|
||||
fn_code.push_str(&*format!("arg{}: {:?}", idx, argument.type_));
|
||||
if idx != arguments.len() { fn_code.push_str(", ") }
|
||||
} else {
|
||||
fn_code.push_str(
|
||||
&*format!("{}: {:?}", argument.id.clone().unwrap(), argument.type_)
|
||||
);
|
||||
if idx != arguments.len() { fn_code.push_str(", ") }
|
||||
}
|
||||
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
// Returns
|
||||
|
||||
code.push_str(&*fn_code)
|
||||
}
|
||||
|
||||
// Trait End
|
||||
code.push_str("}}");
|
||||
|
||||
code.to_owned()
|
||||
}
|
||||
*/
|
||||
|
19
core/src/codegen/rust/mod.rs
Normal file
19
core/src/codegen/rust/mod.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Relative Modules
|
||||
pub(crate) mod _1_7_0;
|
||||
|
||||
// Standard Uses
|
||||
use std::collections::HashMap;
|
||||
|
||||
// Crate Uses
|
||||
use crate::codegen::VersionGenerators;
|
||||
|
||||
// External Uses
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
|
||||
pub(crate) static GENERATORS: VersionGenerators = Lazy::new(|| {
|
||||
HashMap::from([
|
||||
("1.70.0", _1_7_0::frozen_to_code as _)
|
||||
])
|
||||
});
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
#![deny(rust_2018_idioms)]
|
||||
|
||||
// Extern
|
||||
extern crate pest;
|
||||
#[macro_use]
|
||||
extern crate pest_derive;
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate rmp_serde as rmps;
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use std::cell::RefCell;
|
||||
// Standard Uses
|
||||
use std::path::Path;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
// Crate Uses
|
||||
use crate::codegen;
|
||||
|
@ -54,7 +54,9 @@ pub fn build(path: &Path) -> Result<()> {
|
|||
|
||||
let unit = idl::parser_new::from_path(concrete_path)?;
|
||||
|
||||
let context = ir::context::SchemaContext::with_project_and_main(unit);
|
||||
let context = ir::context::SchemaContext::with_project_and_main(
|
||||
unit, concrete_path.file_stem().unwrap().to_str().unwrap().to_owned()
|
||||
);
|
||||
|
||||
compiled_project.add_schema_context(
|
||||
Rc::new(RefCell::new(context))
|
||||
|
@ -63,13 +65,14 @@ pub fn build(path: &Path) -> Result<()> {
|
|||
|
||||
compiler::interpret::interpret_context(&mut compiled_project)?;
|
||||
|
||||
generate_code_for_targets(&mut compiled_project, &frozen)?;
|
||||
generate_code_for_targets(&mut compiled_project, &frozen, path)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn generate_code_for_targets<'a>(
|
||||
compiled_project: &mut ProjectContext<'a>, frozen_units: &'a Vec<FrozenUnit>
|
||||
compiled_project: &'a mut ProjectContext<'a>, frozen_units: &'a Vec<FrozenUnit>,
|
||||
base_path: &Path
|
||||
) -> Result<()> {
|
||||
for item in frozen_units.iter() {
|
||||
match item {
|
||||
|
@ -85,18 +88,20 @@ pub fn generate_code_for_targets<'a>(
|
|||
};
|
||||
|
||||
let path = resolve_path_query(&details.generation_path, args).unwrap();
|
||||
let path = Path::new(&path);
|
||||
let path = base_path.join(path);
|
||||
|
||||
let generator = codegen::find_generator(name);
|
||||
|
||||
if generator.is_none() {
|
||||
let Some((gen_fn, extension)) =
|
||||
codegen::find_generator(name, version) else
|
||||
{
|
||||
panic!(
|
||||
"No generator found for language named {} with version {}",
|
||||
"No generator found for language named '{}' with version '{}'",
|
||||
name, version
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
generate_code_for_context(&compiled_project, generator, &path)?;
|
||||
generate_code_for_context(
|
||||
&compiled_project, gen_fn, extension, &path
|
||||
)?;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
|
@ -125,19 +130,23 @@ pub fn resolve_path_query(query: &Option<String>, args: Args) -> Result<String,
|
|||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn generate_code_for_context(
|
||||
context: &ProjectContext, generator: Option<&GeneratorFn>,
|
||||
pub fn generate_code_for_context<'a>(
|
||||
context: &ProjectContext<'a>,
|
||||
generator: &GeneratorFn, extension: &str,
|
||||
target_path: &Path
|
||||
) -> Result<()> {
|
||||
std::fs::create_dir_all(target_path)?;
|
||||
|
||||
for schema_context in context.schema_contexts.iter() {
|
||||
let schema_ctx = schema_context.borrow();
|
||||
let frozen_schema = schema_ctx.frozen_schema.as_ref().unwrap();
|
||||
let file_path = target_path.join(
|
||||
format!("{}.{}", &schema_ctx.name, extension)
|
||||
);
|
||||
|
||||
let mut code = "Generated code with Comline compiler".to_owned();
|
||||
let code = &*generator(frozen_schema);
|
||||
|
||||
code += &*generator.unwrap()(frozen_schema);
|
||||
|
||||
std::fs::write(target_path, code).unwrap();
|
||||
std::fs::write(file_path, code).unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -49,7 +49,7 @@ pub fn parse_source(source: String, name: String) -> Result<SourcedWhole> {
|
|||
Ok((codemap, units))
|
||||
}
|
||||
|
||||
pub fn parse_inner(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
pub fn parse_inner(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
match pair.as_rule() {
|
||||
Rule::congregation => {
|
||||
let span = pair.as_span();
|
||||
|
@ -96,7 +96,7 @@ pub fn parse_inner(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit>
|
|||
|
||||
|
||||
#[allow(unused)]
|
||||
fn parse_assignment(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<AssignmentUnit> {
|
||||
fn parse_assignment(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<AssignmentUnit> {
|
||||
let unit = match pair.as_rule() {
|
||||
Rule::number => {
|
||||
Ok(AssignmentUnit::Number(pair.as_str().parse().unwrap()))
|
||||
|
@ -156,7 +156,7 @@ fn parse_assignment(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<AssignmentU
|
|||
}
|
||||
|
||||
|
||||
fn parse_list_item(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<ListItem> {
|
||||
fn parse_list_item(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<ListItem> {
|
||||
let span = pair.as_span();
|
||||
let item_span = file.insert_span(span.start(), span.end());
|
||||
|
||||
|
|
|
@ -1,32 +1,29 @@
|
|||
// Standard Uses
|
||||
|
||||
use std::rc::Rc;
|
||||
// Crate Uses
|
||||
use crate::project::ir::context::ProjectContext;
|
||||
use crate::schema::ir::compiler::interpreter::meta_stage;
|
||||
use crate::schema::ir::compiler::interpreter::{meta_stage, object_stage};
|
||||
|
||||
// External Uses
|
||||
use eyre::{anyhow, Result};
|
||||
|
||||
|
||||
pub fn interpret_context<'a>(
|
||||
// project_context: &'a RefCell<ProjectContext<'a>>,
|
||||
project_context: &'a ProjectContext<'a>,
|
||||
) -> Result<()> {
|
||||
let len = project_context.schema_contexts.len();
|
||||
|
||||
for i in 0..len {
|
||||
pub fn interpret_context(project_context: &ProjectContext<'_>) -> Result<()> {
|
||||
for schema_context in project_context.schema_contexts.iter() {
|
||||
meta_stage::compile_schema_metadata(
|
||||
&project_context.schema_contexts[i],
|
||||
Rc::clone(&schema_context),
|
||||
project_context
|
||||
).map_err(|e| anyhow!("{}", e))?;
|
||||
}
|
||||
|
||||
/*
|
||||
for schema_context in project_context.schema_contexts.iter() {
|
||||
interpreter::interpret_context(schema_context, project_context)
|
||||
.map_err(|e| anyhow!("{}", e))?;
|
||||
object_stage::compile_schema(
|
||||
Rc::clone(&schema_context),
|
||||
project_context
|
||||
).map_err(|e| anyhow!("{}", e))?;
|
||||
}
|
||||
*/
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -11,16 +11,13 @@ use snafu::Snafu;
|
|||
#[derive(Debug, Snafu)]
|
||||
#[snafu(visibility(pub(crate)))]
|
||||
pub enum CompileError {
|
||||
// #[strum(props(Id="2"))]
|
||||
#[snafu(display("Found namespace '{target}' when its already declared in '{origin}'"))]
|
||||
Namespace { origin: String, target: String },
|
||||
Namespace { origin: String, target: String } = 0,
|
||||
|
||||
// #[strum(props(Id="2"))]
|
||||
#[snafu(display("Spec version was not assigned'"))]
|
||||
// SpecVersionNotTold { source: Box<dyn std::error::Error + Send + Sync> }
|
||||
SpecVersionNotTold { source: io::Error },
|
||||
|
||||
// #[strum(props(Id="2"))]
|
||||
#[snafu(display("Schema paths list is empty, at least one schema is required'"))]
|
||||
// SpecVersionNotTold { source: Box<dyn std::error::Error + Send + Sync> }
|
||||
SchemaPathsEmpty,
|
||||
|
@ -36,4 +33,4 @@ impl From<CompileError> for Error {
|
|||
}
|
||||
*/
|
||||
|
||||
pub type CompileResult<T, E = CompileError> = std::result::Result<T, E>;
|
||||
pub type CompileResult<T, E = CompileError> = Result<T, E>;
|
||||
|
|
|
@ -50,7 +50,7 @@ impl<'a> ProjectContext<'a> {
|
|||
)
|
||||
}
|
||||
|
||||
pub(crate) fn add_relative_project_context(mut self, context: Rc<ProjectContext>) {
|
||||
pub(crate) fn add_relative_project_context(mut self, context: Rc<ProjectContext<'a>>) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ impl<'a> ProjectContext<'a> {
|
|||
}
|
||||
|
||||
pub(crate) fn find_schema_by_import(&self, import: &str)
|
||||
-> Option<&'a Rc<RefCell<SchemaContext>>>
|
||||
-> Option<&Rc<RefCell<SchemaContext<'a>>>>
|
||||
{
|
||||
for schema_context in self.schema_contexts.iter() {
|
||||
let units = &schema_context.borrow().schema.1;
|
||||
|
@ -79,6 +79,23 @@ impl<'a> ProjectContext<'a> {
|
|||
None
|
||||
}
|
||||
|
||||
/*
|
||||
pub(crate) fn find_schema_by_import(&self, import: &str)
|
||||
-> Option<&Rc<RefCell<SchemaContext<'a>>>>
|
||||
{
|
||||
for schema_context in self.schema_contexts.iter() {
|
||||
let schema_ctx = schema_context.borrow();
|
||||
let state = schema_ctx.compile_state.borrow();
|
||||
|
||||
if let Some(state) = &state.namespace {
|
||||
return Some(schema_context)
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
pub(crate) fn find_whole_unit_by_import(&self, import: &str) -> Option<&WholeUnit> {
|
||||
if self.include_stdlib {
|
||||
|
@ -97,7 +114,9 @@ impl<'a> ProjectContext<'a> {
|
|||
}
|
||||
*/
|
||||
|
||||
pub(crate) fn find_relative_project_context(&self, import: &str) -> Option<&ProjectContext> {
|
||||
pub(crate) fn find_relative_project_context(&self, import: &str)
|
||||
-> Option<&ProjectContext<'a>>
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ pub trait Differ: Downcast + Debug {
|
|||
// fn on_assignment_changed(&self, old: AssignmentUnit, new: AssignmentUnit);
|
||||
|
||||
#[allow(unused)]
|
||||
fn differ(
|
||||
&self, previous: &Vec<FrozenUnit>, next: &FrozenWhole,
|
||||
fn differ<'a>(
|
||||
&self, previous: &Vec<FrozenUnit>, next: &FrozenWhole<'a>,
|
||||
document_gen: bool, auto_version: bool
|
||||
) {
|
||||
// differ(previous, next, document_gen, auto_version)
|
||||
|
|
|
@ -9,7 +9,7 @@ use eyre::Result;
|
|||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn from_frozen_origin(path: &Path) -> Option<FrozenWhole> {
|
||||
pub fn from_frozen_origin(path: &Path) -> Option<FrozenWhole<'_>> {
|
||||
// let context = from_frozen_objects(&project_path);
|
||||
|
||||
todo!()
|
||||
|
|
|
@ -47,7 +47,7 @@ pub struct PublishTarget {
|
|||
}
|
||||
|
||||
pub fn schema_paths(units: &Vec<FrozenUnit>)
|
||||
-> FilterMap<Iter<FrozenUnit>, fn(&FrozenUnit) -> Option<&str>>
|
||||
-> FilterMap<Iter<'_, FrozenUnit>, fn(&FrozenUnit) -> Option<&str>>
|
||||
{
|
||||
units.iter().filter_map(|unit| {
|
||||
match unit {
|
||||
|
|
|
@ -2,18 +2,17 @@
|
|||
|
||||
// Crate Uses
|
||||
use crate::project::idl::ast::{AssignmentUnit, ASTUnit, DictKeyValue, ListItem};
|
||||
use crate::project::ir::compiler::report::CompileError;
|
||||
use crate::project::ir::context::ProjectContext;
|
||||
use crate::project::ir::frozen::{FrozenUnit, FrozenWhole, LanguageDetails, PublishTarget};
|
||||
use crate::report::ReportDetails;
|
||||
use crate::utils::codemap::Span;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn interpret_node_into_frozen(context: &ProjectContext, node: &ASTUnit)
|
||||
-> Result<Vec<FrozenUnit>, ReportDetails<CompileError>>
|
||||
pub fn interpret_node_into_frozen<'a>(
|
||||
context: &ProjectContext<'a>, node: &ASTUnit
|
||||
) -> Result<Vec<FrozenUnit>, Box<dyn snafu::Error>>
|
||||
{
|
||||
// use crate::project::idl::ast::ASTUnit::*;
|
||||
match node {
|
||||
|
@ -27,9 +26,9 @@ pub fn interpret_node_into_frozen(context: &ProjectContext, node: &ASTUnit)
|
|||
}
|
||||
}
|
||||
|
||||
pub fn interpret_assignment(
|
||||
context: &ProjectContext, name: &(Span, String), node: &(Span, AssignmentUnit)
|
||||
) -> Result<Vec<FrozenUnit>, ReportDetails<CompileError>> {
|
||||
pub fn interpret_assignment<'a>(
|
||||
context: &ProjectContext<'a>, name: &(Span, String), node: &(Span, AssignmentUnit)
|
||||
) -> Result<Vec<FrozenUnit>, Box<dyn snafu::Error>> {
|
||||
let result = match &*name.1 {
|
||||
"specification_version" => {
|
||||
let AssignmentUnit::Number(version) = &node.1 else {
|
||||
|
@ -84,7 +83,7 @@ pub fn interpret_assignment(
|
|||
}
|
||||
|
||||
fn interpret_assignment_code_generation(items: &Vec<DictKeyValue>)
|
||||
-> Result<Vec<FrozenUnit>, ReportDetails<CompileError>>
|
||||
-> Result<Vec<FrozenUnit>, Box<dyn snafu::Error>>
|
||||
{
|
||||
let mut languages = vec![];
|
||||
|
||||
|
@ -144,7 +143,7 @@ fn interpret_assignment_code_generation(items: &Vec<DictKeyValue>)
|
|||
}
|
||||
|
||||
fn interpret_assigment_publish_targets(items: &Vec<DictKeyValue>)
|
||||
-> Result<Vec<FrozenUnit>, ReportDetails<CompileError>>
|
||||
-> Result<Vec<FrozenUnit>, Box<dyn snafu::Error>>
|
||||
{
|
||||
let mut targets = vec![];
|
||||
|
||||
|
@ -204,7 +203,7 @@ fn interpret_assigment_publish_targets(items: &Vec<DictKeyValue>)
|
|||
#[allow(unused)]
|
||||
pub fn into_frozen_whole<'a>(
|
||||
context: &'a ProjectContext<'a>, interpreted: Vec<FrozenUnit>
|
||||
) -> Result<FrozenWhole, ReportDetails<CompileError>>
|
||||
) -> Result<FrozenWhole<'a>, Box<dyn snafu::Error>>
|
||||
{
|
||||
todo!()
|
||||
// Ok((Rc::from(context), interpreted))
|
||||
|
|
|
@ -3,16 +3,14 @@
|
|||
// Crate Uses
|
||||
use crate::project::ir::context::ProjectContext;
|
||||
use crate::project::ir::frozen::FrozenUnit;
|
||||
use crate::project::ir::compiler::report::CompileError;
|
||||
use crate::project::ir::interpreter::freezing;
|
||||
use crate::report::ReportDetails;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn interpret_context<'a>(mut context: &ProjectContext)
|
||||
-> Result<Vec<FrozenUnit>, ReportDetails<CompileError>>
|
||||
pub fn interpret_context<'a>(mut context: &ProjectContext<'a>)
|
||||
-> Result<Vec<FrozenUnit>, Box<dyn snafu::Error>>
|
||||
{
|
||||
let mut interpreted = vec![];
|
||||
|
||||
|
|
|
@ -36,7 +36,6 @@ impl<'a> Compile for Interpreter<'a> {
|
|||
let frozen = interpret::interpret_context(&context)
|
||||
.map_err(|e| anyhow!("{:?}", e))?;
|
||||
|
||||
// Ok((RefCell::new(context), frozen))
|
||||
Ok((context, frozen))
|
||||
}
|
||||
|
||||
|
|
|
@ -1,20 +1,15 @@
|
|||
// Standard Uses
|
||||
|
||||
// Crate Uses
|
||||
use crate::schema::ir::compiler::report::CompileWarning;
|
||||
use crate::project::idl::ast::AssignmentUnit;
|
||||
use crate::project::ir::compiler::report::CompileResult;
|
||||
use crate::project::ir::frozen::Dependency;
|
||||
use crate::report::ReportDetails;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
/*
|
||||
#[allow(unused)]
|
||||
pub fn report_errors(
|
||||
spec_version: Option<u32>, schema_paths: Option<Vec<String>>,
|
||||
dependencies: Option<Vec<Dependency>>, assignments: Vec<AssignmentUnit>
|
||||
) -> CompileResult<()> {
|
||||
) -> Result<(), dyn snafu::Error> {
|
||||
// let mut errors = vec![];
|
||||
|
||||
// let spec_version = spec_version.context(report::SpecVersionNotToldSnafu);
|
||||
|
@ -48,3 +43,4 @@ pub fn report_warnings(
|
|||
|
||||
warnings
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -1,32 +1,35 @@
|
|||
// Standard Uses
|
||||
use std::marker::PhantomData;
|
||||
use std::cell::Ref;
|
||||
use std::ops::Range;
|
||||
|
||||
// Local Uses
|
||||
// Crate Uses
|
||||
use crate::schema::ir::context::SchemaContext;
|
||||
use crate::schema::ir::compiler::report::CompileError;
|
||||
use crate::utils::codemap::Span;
|
||||
|
||||
// External Uses
|
||||
use ariadne::Color;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ReportDetails<T> {
|
||||
// pub id: u16,
|
||||
// pub message: String,
|
||||
pub(crate) phantom: PhantomData<T>, // pub kind: T,
|
||||
pub span: Option<Span>
|
||||
pub struct ReportDetails {
|
||||
pub line: Range<usize>,
|
||||
pub pos: Range<usize>
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
impl<T> ReportDetails<T> {
|
||||
fn code(&self) -> u8 {
|
||||
0
|
||||
impl ReportDetails {
|
||||
pub(crate) fn fetch(
|
||||
schema_context: &Ref<'_, SchemaContext<'_>>, span: &Span
|
||||
) -> Option<Self> {
|
||||
let pos = schema_context.schema.0.files().first()
|
||||
.unwrap().range_of(*span).unwrap();
|
||||
|
||||
Some(Self { line: Default::default(), pos })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
#[allow(unused)]
|
||||
pub fn report_errors(context: SchemaContext, errors: Vec<ReportDetails<CompileError>>) {
|
||||
pub fn report_errors<'a>(context: SchemaContext<'_>, errors: Vec<ReportDetails<CompileError>>) {
|
||||
let out = Color::Fixed(81);
|
||||
|
||||
/*
|
||||
|
@ -47,8 +50,9 @@ pub fn report_errors(context: SchemaContext, errors: Vec<ReportDetails<CompileEr
|
|||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn report_warnings(context: SchemaContext, warnings: Vec<ReportDetails<CompileError>>) {
|
||||
pub fn report_warnings(context: SchemaContext<'_>, warnings: Vec<ReportDetails<CompileError>>) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
|
|
@ -79,7 +79,9 @@ pub enum ASTUnit {
|
|||
throws: Vec<(Span, String)>
|
||||
},
|
||||
Argument {
|
||||
name: Option<(Span, String)>, // TODO: Having optional names might not be a good thing
|
||||
// TODO: Having optional names might not be a good thing, think about it
|
||||
// name: Option<(Span, String)>,
|
||||
name: (Span, String),
|
||||
kind: (Span, String)
|
||||
},
|
||||
Error {
|
||||
|
@ -107,6 +109,7 @@ pub enum ASTUnit {
|
|||
}
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn namespace(units: &Vec<SpannedUnit>) -> &String {
|
||||
let mut namespace: Option<&String> = None;
|
||||
|
||||
|
|
|
@ -134,7 +134,7 @@ protocol = {
|
|||
~ "{" ~ WS? ~ function* ~ WS? ~ "}"
|
||||
}
|
||||
function = {
|
||||
WS? ~ property*
|
||||
WS? ~ docstring? ~ property*
|
||||
~ (index ~ WS? ~ "#" ~ WS?)?
|
||||
~ (asynchronous ~ WS?)?
|
||||
~ (direction ~ WS?)?
|
||||
|
@ -143,6 +143,7 @@ function = {
|
|||
~ (WS? ~ "->" ~ WS? ~ returns+)?
|
||||
// ~ (WS? ~ ":" ~ WS? ~ parameter+)?
|
||||
~ (WS? ~ "!" ~ WS? ~ throws)?
|
||||
~ ";"
|
||||
}
|
||||
direction = { "client" | "server" }
|
||||
|
||||
|
@ -153,15 +154,15 @@ argument = {
|
|||
~ WS?
|
||||
}
|
||||
returns = { ","? ~ WS? ~ (kind) ~ WS? }
|
||||
parameter = {
|
||||
WS? ~ id ~ WS? ~ "=" ~ WS? ~ value ~ WS?
|
||||
}
|
||||
throws = {
|
||||
function_call
|
||||
}
|
||||
|
||||
|
||||
// Common Rules
|
||||
parameter = {
|
||||
WS? ~ id ~ WS? ~ "=" ~ WS? ~ value ~ WS?
|
||||
}
|
||||
property = {
|
||||
WS? ~ "@"
|
||||
~ WS? ~ property_domain
|
||||
|
|
|
@ -175,7 +175,7 @@ pub fn parse_inner(pairs: Pair<Rule>) -> Result<ASTUnit> {
|
|||
}
|
||||
*/
|
||||
|
||||
pub fn to_docstring(pair: Pair<Rule>, file: &Arc<FileMap>) -> SpannedUnit {
|
||||
pub fn to_docstring(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> SpannedUnit {
|
||||
let pair = pair.into_inner().next().unwrap();
|
||||
|
||||
match pair.as_rule() {
|
||||
|
@ -238,7 +238,7 @@ pub fn to_parameters(mut pairs: Pairs<Rule>) -> Vec<Parameter> {
|
|||
}
|
||||
*/
|
||||
|
||||
pub fn to_value_other(pair: Pair<Rule>) -> Option<String> {
|
||||
pub fn to_value_other(pair: Pair<'_, Rule>) -> Option<String> {
|
||||
let inner = pair.into_inner().next().unwrap();
|
||||
|
||||
match inner.as_rule() {
|
||||
|
@ -252,7 +252,7 @@ pub fn to_value_other(pair: Pair<Rule>) -> Option<String> {
|
|||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn to_field(pair: Pair<Rule>, file: &Arc<FileMap>) -> ASTUnit {
|
||||
pub fn to_field(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> ASTUnit {
|
||||
let pairs = pair.into_inner();
|
||||
|
||||
let mut docstring = vec![];
|
||||
|
@ -298,7 +298,7 @@ pub fn to_property(pair: Pair<Rule>, file: &Arc<FileMap>) -> ASTUnit {
|
|||
*/
|
||||
|
||||
#[allow(unused)]
|
||||
fn to_function(pair: Pair<Rule>) -> ASTUnit {
|
||||
fn to_function(pair: Pair<'_, Rule>) -> ASTUnit {
|
||||
let inner = pair.into_inner();
|
||||
|
||||
let mut synchronous = true;
|
||||
|
@ -370,7 +370,7 @@ fn to_argument(pairs: Pairs<Rule>) -> Argument {
|
|||
}
|
||||
*/
|
||||
|
||||
pub fn to_expression_block(pair: Pair<Rule>) -> ASTUnit {
|
||||
pub fn to_expression_block(pair: Pair<'_, Rule>) -> ASTUnit {
|
||||
let inner = pair.into_inner().next().unwrap();
|
||||
|
||||
// let mut expression = vec![];
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
// Standard Uses
|
||||
use std::path::Path;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
// Local Uses
|
||||
use crate::utils::codemap::{CodeMap, FileMap};
|
||||
use crate::schema::idl::ast::unit::{ASTUnit, SourcedWhole, SpannedUnit};
|
||||
use crate::schema::idl::ast::unit::{ASTUnit, SourcedWholeRc, SpannedUnit};
|
||||
|
||||
// External Uses
|
||||
use eyre::{bail, Result};
|
||||
|
@ -18,7 +19,7 @@ pub struct SchemaParser;
|
|||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn from_path(path: &Path) -> Result<SourcedWhole> {
|
||||
pub fn from_path(path: &Path) -> Result<SourcedWholeRc> {
|
||||
if !path.exists() { bail!("Path doesn't exist: {:?}", path) }
|
||||
let source = std::fs::read_to_string(path).unwrap();
|
||||
|
||||
|
@ -30,7 +31,7 @@ pub fn from_path(path: &Path) -> Result<SourcedWhole> {
|
|||
sourced_whole
|
||||
}
|
||||
|
||||
pub fn parse_source(source: String, name: String) -> Result<SourcedWhole> {
|
||||
pub fn parse_source(source: String, name: String) -> Result<SourcedWholeRc> {
|
||||
let mut codemap = CodeMap::new();
|
||||
let file = codemap.insert_file(name, source.clone());
|
||||
|
||||
|
@ -39,7 +40,7 @@ pub fn parse_source(source: String, name: String) -> Result<SourcedWhole> {
|
|||
|
||||
for pair in pairs {
|
||||
if let Ok(unit) = parse_inner(pair, &file) {
|
||||
units.push(unit)
|
||||
units.push(Rc::new(unit))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,7 +48,7 @@ pub fn parse_source(source: String, name: String) -> Result<SourcedWhole> {
|
|||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn parse_inner(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
pub fn parse_inner(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
match pair.as_rule() {
|
||||
Rule::namespace => {
|
||||
let span = pair.as_span();
|
||||
|
@ -261,7 +262,7 @@ pub fn parse_inner(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit>
|
|||
}
|
||||
|
||||
|
||||
pub fn to_docstring(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
pub fn to_docstring(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
let pair = pair.into_inner().next().unwrap();
|
||||
|
||||
match pair.as_rule() {
|
||||
|
@ -299,7 +300,7 @@ pub fn to_docstring(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit
|
|||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn to_parameter(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
pub fn to_parameter(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
let pair_span = pair.as_span();
|
||||
let unit_span = file.insert_span(pair_span.start(), pair_span.end());
|
||||
let mut inner = pair.into_inner();
|
||||
|
@ -323,7 +324,7 @@ pub fn to_parameter(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit
|
|||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn to_field(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
pub fn to_field(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
let pair_span = pair.as_span();
|
||||
let unit_span = file.insert_span(pair_span.start(), pair_span.end());
|
||||
let mut inner = pair.into_inner();
|
||||
|
@ -354,7 +355,7 @@ pub fn to_field(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
|||
}))
|
||||
}
|
||||
|
||||
pub fn to_property(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
pub fn to_property(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
let pair_span = pair.as_span();
|
||||
let unit_span = file.insert_span(pair_span.start(), pair_span.end());
|
||||
let inner = pair.into_inner().next().unwrap();
|
||||
|
@ -380,7 +381,7 @@ pub fn to_property(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit>
|
|||
|
||||
|
||||
#[allow(unused)]
|
||||
fn to_enum_variant(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
fn to_enum_variant(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
let pair_span = pair.as_span();
|
||||
let unit_span = file.insert_span(pair_span.start(), pair_span.end());
|
||||
let inner = pair.into_inner();
|
||||
|
@ -405,7 +406,7 @@ fn to_enum_variant(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit>
|
|||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn to_function(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
fn to_function(pair: Pair<'_, Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
||||
let pair_span = pair.as_span();
|
||||
let unit_span = file.insert_span(pair_span.start(), pair_span.end());
|
||||
let inner = pair.into_inner();
|
||||
|
@ -436,10 +437,10 @@ fn to_function(pair: Pair<Rule>, file: &Arc<FileMap>) -> Result<SpannedUnit> {
|
|||
let kind_span = kind_pair.as_span();
|
||||
|
||||
arguments.push((unit_span, ASTUnit::Argument {
|
||||
name: Some((
|
||||
name: (
|
||||
file.insert_span(name_span.start(), name_span.end()),
|
||||
name_pair.as_str().to_owned()
|
||||
)),
|
||||
),
|
||||
kind: (
|
||||
file.insert_span(kind_span.start(), kind_span.end()),
|
||||
kind_pair.as_str().to_owned()
|
||||
|
|
|
@ -1,296 +0,0 @@
|
|||
// Standard Uses
|
||||
use std::cell::RefMut;
|
||||
|
||||
// Local Uses
|
||||
use crate::schema::idl::ast::unit::{ASTUnit, SpannedUnit};
|
||||
use crate::schema::ir::context::SchemaContext;
|
||||
use crate::schema::ir::compiler::interpreted::primitive::KindValue;
|
||||
use crate::schema::ir::compiler::report::CompileError;
|
||||
use crate::schema::ir::frozen::unit::FrozenUnit;
|
||||
use crate::project::ir::context::ProjectContext;
|
||||
use crate::report::ReportDetails;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn interpret_context<'a>(
|
||||
schema_context: &'a mut SchemaContext<'a>,
|
||||
project_context: &'a RefMut<ProjectContext<'a>>
|
||||
) -> Result<(), ReportDetails<CompileError>> {
|
||||
let mut interpreted: Vec<FrozenUnit> = vec![];
|
||||
|
||||
for i in 0..schema_context.schema.1.len() {
|
||||
let spanned_unit = &schema_context.schema.1[i];
|
||||
|
||||
use crate::schema::idl::ast::unit::ASTUnit::*;
|
||||
match &spanned_unit.1 {
|
||||
Namespace(s, n) => {
|
||||
let name = n.clone();
|
||||
let Some(ctx)
|
||||
= project_context.find_schema_by_import(&n) else
|
||||
{
|
||||
return Err(ReportDetails {
|
||||
// kind: "Namespace".to_string(),
|
||||
/*
|
||||
message: format!(
|
||||
"Namespace {} used in another schema: {} and {},\n \
|
||||
only one schema per namespace is allowed",
|
||||
n, context.main.0.filename(), unit_namespace.unwrap().0.filename()
|
||||
),
|
||||
start: 0, end: 0,
|
||||
*/
|
||||
phantom: Default::default(),
|
||||
span: None,
|
||||
})
|
||||
};
|
||||
|
||||
if let Some(name) = &schema_context.compile_state.borrow_mut().namespace {
|
||||
panic!("Namespace {} was already previously set", name)
|
||||
} else {
|
||||
schema_context.compile_state.borrow_mut().namespace = Some(name)
|
||||
}
|
||||
}
|
||||
Docstring {..} => {
|
||||
todo!()
|
||||
}
|
||||
Import(s, i) => {
|
||||
/*
|
||||
let Some(ctx)
|
||||
= project_context.find_schema_by_import(i) else {
|
||||
panic!("No import")
|
||||
};
|
||||
*/
|
||||
}
|
||||
// Docstring { .. } => {}
|
||||
Constant {
|
||||
docstring, name,
|
||||
kind, default_value
|
||||
} => {
|
||||
/*
|
||||
let kind_value = primitive::to_kind_value(
|
||||
schema_context, kind, default_value
|
||||
);
|
||||
*/
|
||||
}
|
||||
Property { .. } => {}
|
||||
Parameter { .. } => {}
|
||||
ExpressionBlock { .. } => {}
|
||||
Enum { .. } => {}
|
||||
EnumVariant { .. } => {}
|
||||
Settings { .. } => {}
|
||||
Struct { .. } => {}
|
||||
Protocol { .. } => {}
|
||||
Function { .. } => {}
|
||||
Argument { .. } => {}
|
||||
Error { .. } => {}
|
||||
Validator { .. } => {}
|
||||
Field { .. } => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Ok(context.unwrap().compile_state.to_frozen())
|
||||
Ok(())
|
||||
|
||||
/*
|
||||
for (unit, span) in &context.config.1 {
|
||||
use crate::schema::idl::ast::unit::ASTUnit::*;
|
||||
match unit {
|
||||
ASTUnit::Namespace(n) => {
|
||||
let unit_namespace = context.find_whole_unit_by_import(n);
|
||||
|
||||
/*
|
||||
if unit_namespace.is_some() {
|
||||
return Err(ReportDetails {
|
||||
kind: "Namespace".to_string(),
|
||||
message: format!(
|
||||
"Namespace {} used in another schema: {} and {},\n \
|
||||
only one schema per namespace is allowed",
|
||||
n, context.main.0.filename(), unit_namespace.unwrap().0.filename()
|
||||
),
|
||||
start: 0, end: 0,
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
||||
interpreted.push(FrozenUnit::Namespace(n.clone()));
|
||||
},
|
||||
Import(_) => {
|
||||
let import = interpret_node(&context, unit)?;
|
||||
interpreted.push(import);
|
||||
}
|
||||
Constant { .. } => {
|
||||
let constant = interpret_node(&context, unit)?;
|
||||
interpreted.push(constant);
|
||||
}
|
||||
Enum { .. } => {
|
||||
let r#enum = interpret_node(&context, unit)?;
|
||||
interpreted.push(r#enum);
|
||||
}
|
||||
/*
|
||||
Unit::Settings { .. } => {}
|
||||
Unit::Struct { .. } => {}
|
||||
Unit::Protocol { .. } => {}
|
||||
Unit::Error { .. } => {}
|
||||
*/
|
||||
|
||||
Validator { .. } => {
|
||||
let validator = interpret_node(&context, unit)?;
|
||||
interpreted.push(validator);
|
||||
}
|
||||
/*
|
||||
//r => panic!("Left to impl: {:?}", r)
|
||||
*/
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn interpret_node<'a>(
|
||||
schema_context: &'a SchemaContext<'a>,
|
||||
project_context: &'a ProjectContext<'a>,
|
||||
node: &ASTUnit
|
||||
)
|
||||
-> Result<FrozenUnit, ReportDetails<CompileError>>
|
||||
{
|
||||
use crate::schema::idl::ast::unit::ASTUnit::*;
|
||||
match node {
|
||||
Namespace(_, n) => {
|
||||
let mut found: Option<&SchemaContext> = None;
|
||||
|
||||
/*
|
||||
for relative_ctx in schema_ctx.borrow().schema_contexts.iter() {
|
||||
if unit::namespace(&relative_ctx.schema.1) == n {
|
||||
/*
|
||||
if found.is_some() {
|
||||
return Err(ReportDetails {
|
||||
kind: "namespace".to_string(),
|
||||
message: format!(
|
||||
"Found namespace {} when its already declared in {}",
|
||||
&n, &relative_ctx.main.0.filename()
|
||||
),
|
||||
start: 0, end: 0,
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
||||
found = Some(relative_ctx)
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
Import(_, i) => {
|
||||
let relative_unit = project_context.find_schema_by_import(&i);
|
||||
|
||||
/*
|
||||
if relative_unit.is_none() {
|
||||
let relative_unit = relative_unit.unwrap();
|
||||
|
||||
return Err(ReportDetails {
|
||||
kind: "import".to_string(),
|
||||
message: format!("Could not find namespace of {}", relative_unit.0.filename()),
|
||||
start: 0, end: 0,
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
||||
return Ok(FrozenUnit::Import(i.clone()))
|
||||
},
|
||||
Constant {
|
||||
name: (_, name), kind: (_, kind),
|
||||
default_value, ..
|
||||
} => {
|
||||
/*
|
||||
let kind_value = primitive::to_kind_value(kind, default_value);
|
||||
|
||||
return Ok(FrozenUnit::Constant {
|
||||
docstring: None,
|
||||
name: name.clone(), kind_value
|
||||
})
|
||||
*/
|
||||
}
|
||||
Enum { name, variants, .. } => {
|
||||
let mut frozen_variants: Vec<FrozenUnit> = vec![];
|
||||
|
||||
for variant in variants {
|
||||
pub(crate) fn to_variant(variant: &ASTUnit) -> KindValue {
|
||||
match variant {
|
||||
EnumVariant { name, kind } => {
|
||||
if kind.is_none() {
|
||||
return KindValue::EnumVariant(
|
||||
name.1.clone(),None
|
||||
)
|
||||
}
|
||||
|
||||
return KindValue::EnumVariant(
|
||||
name.1.clone(), None
|
||||
)
|
||||
},
|
||||
_ => panic!("Should not be here")
|
||||
}
|
||||
}
|
||||
|
||||
frozen_variants.push(FrozenUnit::EnumVariant(
|
||||
to_variant(&variant.1)
|
||||
));
|
||||
}
|
||||
|
||||
return Ok(FrozenUnit::Enum {
|
||||
docstring: None,
|
||||
name: name.1.clone(),
|
||||
variants: frozen_variants
|
||||
})
|
||||
}
|
||||
/*
|
||||
EnumVariant { .. } => {}
|
||||
Settings { .. } => {}
|
||||
Struct { .. } => {}
|
||||
Protocol { .. } => {}
|
||||
Function { .. } => {}
|
||||
Error { .. } => {}
|
||||
*/
|
||||
#[allow(unused)]
|
||||
Validator {
|
||||
docstring, properties,
|
||||
name, expression_block
|
||||
} => {
|
||||
let properties = to_properties(properties);
|
||||
let expr_block = Box::new(FrozenUnit::ExpressionBlock { function_calls: vec![] });
|
||||
|
||||
|
||||
return Ok(FrozenUnit::Validator {
|
||||
docstring: Some("".to_owned()), // TODO: Docstrings should likely be a vector of units
|
||||
properties: vec![],
|
||||
name: name.1.clone(),
|
||||
expression_block: expr_block
|
||||
})
|
||||
}
|
||||
/*
|
||||
Field { .. } => {}
|
||||
Parameter { .. } => {}
|
||||
Property { .. } => {}
|
||||
ExpressionBlock { .. } => {}
|
||||
*/
|
||||
missing => unimplemented!("Missing implementation for node '{:?}'", missing)
|
||||
}
|
||||
|
||||
panic!()
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn to_properties(nodes: &Vec<SpannedUnit>) -> Vec<FrozenUnit> {
|
||||
let properties = vec![];
|
||||
|
||||
for node in nodes {
|
||||
|
||||
}
|
||||
|
||||
properties
|
||||
}
|
||||
|
||||
pub fn into_frozen_unit() -> FrozenUnit {
|
||||
todo!()
|
||||
}
|
|
@ -1,21 +1,28 @@
|
|||
// Standard Uses
|
||||
use std::cell::Ref;
|
||||
|
||||
// Local Uses
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// External Uses
|
||||
use strum_macros::EnumProperty;
|
||||
use crate::schema::ir::context::SchemaContext;
|
||||
use crate::schema::ir::compiler::report::CompileError;
|
||||
use crate::schema::ir::compiler::report;
|
||||
use crate::project::ir::context::ProjectContext;
|
||||
use crate::report::ReportDetails;
|
||||
use crate::utils::codemap::Span;
|
||||
|
||||
// External Uses
|
||||
use strum_macros::EnumProperty;
|
||||
use snafu::ResultExt;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||
#[derive(EnumProperty)]
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
#[derive(Deserialize, Serialize, EnumProperty)]
|
||||
#[repr(u8)]
|
||||
pub enum Primitive {
|
||||
#[strum(props(Name="bool", Description="boolean, 1 byte"))]
|
||||
Boolean(Option<bool>) = 0,
|
||||
|
||||
#[strum(props(Name="u8", Description="unsigned 1 byte, 8 bits"))]
|
||||
U8(Option<u8>) = 0,
|
||||
U8(Option<u8>),
|
||||
|
||||
#[strum(props(Name="u16", Description="unsigned 2 bytes, 16 bits"))]
|
||||
U16(Option<u16>),
|
||||
|
@ -53,6 +60,9 @@ pub enum Primitive {
|
|||
impl Primitive {
|
||||
pub fn value_str(&self) -> Option<String> {
|
||||
match self {
|
||||
Primitive::Boolean(b) => {
|
||||
if let Some(b) = b { Some(b.to_string()) } else { None }
|
||||
}
|
||||
Primitive::U8(u) => {
|
||||
if let Some(u) = u { Some(u.to_string()) } else { None }
|
||||
}
|
||||
|
@ -92,6 +102,11 @@ impl Primitive {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
use strum::EnumProperty;
|
||||
self.get_str("Name").unwrap()
|
||||
}
|
||||
|
||||
pub fn name_description(&self) -> String {
|
||||
use strum::EnumProperty;
|
||||
format!("{}({})", self.get_str("Name").unwrap(), self.get_str("Description").unwrap())
|
||||
|
@ -113,14 +128,14 @@ pub enum KindValue {
|
|||
Primitive(Primitive),
|
||||
EnumVariant(String, Option<Box<KindValue>>),
|
||||
Union(Vec<KindValue>),
|
||||
Namespaced(String)
|
||||
Namespaced(String, Option<Box<KindValue>>)
|
||||
}
|
||||
|
||||
impl KindValue {
|
||||
pub fn name_and_value(&self) -> (String, String) {
|
||||
pub fn name_and_value(&self) -> (String, Option<String>) {
|
||||
match self {
|
||||
KindValue::Primitive(primitive) => {
|
||||
return (primitive.name_description(), primitive.value_str().unwrap())
|
||||
return (primitive.name().to_owned(), Some(primitive.value_str().unwrap()))
|
||||
}
|
||||
KindValue::EnumVariant(name, value) => {
|
||||
let value = value.as_ref().unwrap();
|
||||
|
@ -131,37 +146,121 @@ impl KindValue {
|
|||
todo!()
|
||||
}
|
||||
#[allow(unused)]
|
||||
KindValue::Namespaced(namespace) => {
|
||||
KindValue::Namespaced(namespace, ..) => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn to_kind_value(
|
||||
schema_context: &Rc<RefCell<SchemaContext>>,
|
||||
pub fn resolve_kind_value(
|
||||
schema_context: &Ref<'_, SchemaContext<'_>>, project_context: &'_ ProjectContext<'_>,
|
||||
kind: &(Span, String), value: &Option<(Span, String)>
|
||||
) -> KindValue {
|
||||
) -> Result<KindValue, Box<dyn snafu::Error>> {
|
||||
if value.is_none() {
|
||||
return to_kind_only(kind)
|
||||
if let Some(kind) = to_kind_only(schema_context, project_context, kind) {
|
||||
return Ok(kind)
|
||||
}
|
||||
}
|
||||
|
||||
KindValue::Primitive(
|
||||
to_primitive(schema_context, kind, value.clone().unwrap().1).unwrap()
|
||||
)
|
||||
}
|
||||
if let Some(kind_value) = to_kind_value(
|
||||
schema_context, kind, &value.as_ref().unwrap()
|
||||
) {
|
||||
return Ok(kind_value)
|
||||
};
|
||||
|
||||
fn to_kind_only(kind: &(Span, String)) -> KindValue {
|
||||
match kind {
|
||||
&_ => panic!("Cant")
|
||||
}
|
||||
Err(CompileError::TypeNotFound { name: kind.1.clone() })
|
||||
.context(report::CompileSnafu {
|
||||
details: ReportDetails::fetch(schema_context, &kind.0).unwrap()
|
||||
})?
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn to_primitive(
|
||||
schema_context: &Rc<RefCell<SchemaContext>>,
|
||||
kind: &(Span, String), value: String
|
||||
pub(crate) fn to_kind_value(
|
||||
schema_context: &Ref<'_, SchemaContext<'_>>,
|
||||
kind: &(Span, String), value: &(Span, String)
|
||||
) -> Option<KindValue> {
|
||||
if let Some(primitive) = to_primitive_kind_value(kind, &value.1) {
|
||||
return Some(KindValue::Primitive(primitive))
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn to_kind_only(
|
||||
schema_context: &Ref<'_, SchemaContext<'_>>, project_context: &'_ ProjectContext<'_>,
|
||||
kind: &(Span, String)
|
||||
) -> Option<KindValue> {
|
||||
if let Some(primitive) = to_primitive_kind_only(kind) {
|
||||
return Some(KindValue::Primitive(primitive))
|
||||
}
|
||||
|
||||
if let Some(namespaced) = to_namespaced_kind_only(schema_context, kind) {
|
||||
return Some(namespaced)
|
||||
}
|
||||
|
||||
if let Some(union) = to_union_kind_only(schema_context, kind) {
|
||||
return Some(union)
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
fn to_namespaced_kind_only(
|
||||
schema_context: &Ref<'_, SchemaContext<'_>>, kind: &(Span, String)
|
||||
) -> Option<KindValue> {
|
||||
let state = schema_context.compile_state.borrow();
|
||||
|
||||
for (_, structure) in state.structures.iter() {
|
||||
if structure.name.1 == kind.1 {
|
||||
return Some(KindValue::Namespaced(
|
||||
structure.name.1.clone(), None
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
for (_, constant) in state.consts.iter() {
|
||||
if constant.name.1 == kind.1 {
|
||||
return Some(KindValue::Namespaced(
|
||||
constant.name.1.clone(), None
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn to_union_kind_only(
|
||||
schema_context: &Ref<'_, SchemaContext<'_>>, kind: &(Span, String)
|
||||
) -> Option<KindValue> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn to_primitive_kind_only(kind: &(Span, String)) -> Option<Primitive> {
|
||||
use self::Primitive::*;
|
||||
match &*kind.1 {
|
||||
"u8" => Some(U8(None)),
|
||||
"u16" => Some(U16(None)),
|
||||
"u32" => Some(U32(None)),
|
||||
"u64" => Some(U64(None)),
|
||||
"u128" => Some(U128(None)),
|
||||
|
||||
"s8" => Some(S8(None)),
|
||||
"s16" => Some(S16(None)),
|
||||
"s32" => Some(S32(None)),
|
||||
"s64" => Some(S64(None)),
|
||||
"s128" => Some(S128(None)),
|
||||
|
||||
"str" => Some(String(None)),
|
||||
"bool" => Some(Boolean(None)),
|
||||
_ => { None }
|
||||
}
|
||||
}
|
||||
|
||||
fn to_primitive_kind_value<'a>(
|
||||
kind: &(Span, String), value: &str
|
||||
) -> Option<Primitive> {
|
||||
use self::Primitive::*;
|
||||
let kv = match &*kind.1 {
|
||||
|
@ -178,6 +277,7 @@ fn to_primitive(
|
|||
"s128" => S128(Some(value.parse().unwrap())),
|
||||
|
||||
"str" => String(Some(value.to_owned())),
|
||||
"bool" => Boolean(Some(value.parse().unwrap())),
|
||||
_ => { panic!("Got unknown type '{:#}'", kind.1) }
|
||||
};
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
// Relative Modules
|
||||
pub mod sanitizer;
|
||||
pub mod serialization;
|
||||
pub mod primitive;
|
||||
pub mod interpreter;
|
||||
pub mod kind_search;
|
||||
|
||||
|
|
|
@ -7,6 +7,6 @@ use crate::schema::ir::context::SchemaContext;
|
|||
|
||||
|
||||
#[allow(unused)]
|
||||
fn sanitize_context_units(context: &SchemaContext) {
|
||||
fn sanitize_context_units(context: &SchemaContext<'_>) {
|
||||
todo!()
|
||||
}
|
||||
|
|
|
@ -1,20 +1,23 @@
|
|||
// Standard Uses
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use crate::project::ir::context::ProjectContext;
|
||||
use snafu::ResultExt;
|
||||
|
||||
// Crate Uses
|
||||
use crate::report::ReportDetails;
|
||||
use crate::schema::ir::compiler::report::CompileError;
|
||||
use crate::schema::ir::context::SchemaContext;
|
||||
use crate::project::ir::context::ProjectContext;
|
||||
use crate::schema::ir::compiler::interpreter::semi_frozen;
|
||||
use crate::schema::ir::compiler::report;
|
||||
use crate::schema::ir::compiler::report::CompileError;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
pub fn compile_schema_metadata<'a>(
|
||||
schema_context: &Rc<RefCell<SchemaContext>>,
|
||||
project_context: &'a ProjectContext<'a>
|
||||
) -> Result<(), ReportDetails<CompileError>> {
|
||||
schema_context: Rc<RefCell<SchemaContext<'a>>>,
|
||||
project_context: &ProjectContext<'a>
|
||||
) -> Result<(), Box<dyn snafu::Error>> {
|
||||
let schema_ctx = schema_context.borrow();
|
||||
|
||||
for i in 0..schema_ctx.schema.1.len() {
|
||||
|
@ -22,24 +25,14 @@ pub fn compile_schema_metadata<'a>(
|
|||
|
||||
use crate::schema::idl::ast::unit::ASTUnit::*;
|
||||
match &spanned_unit.1 {
|
||||
Namespace(_, n) => {
|
||||
|
||||
Namespace(span, n) => {
|
||||
let name = n.clone();
|
||||
let Some(_) = project_context.find_schema_by_import(&n) else
|
||||
{
|
||||
return Err(ReportDetails {
|
||||
// kind: "Namespace".to_string(),
|
||||
/*
|
||||
message: format!(
|
||||
"Namespace {} used in another schema: {} and {},\n \
|
||||
only one schema per namespace is allowed",
|
||||
n, context.main.0.filename(), unit_namespace.unwrap().0.filename()
|
||||
),
|
||||
start: 0, end: 0,
|
||||
*/
|
||||
phantom: Default::default(),
|
||||
span: None,
|
||||
})
|
||||
let Some(_) = project_context.find_schema_by_import(&n) else {
|
||||
Err(CompileError::NamespaceCollision {
|
||||
origin: "aa".to_string(), target: "aaa".to_string()
|
||||
}).context(report::CompileSnafu {
|
||||
details: ReportDetails::fetch(&schema_ctx, span).unwrap()
|
||||
})?
|
||||
};
|
||||
|
||||
let mut compile_state = schema_ctx.compile_state.borrow_mut();
|
||||
|
@ -52,23 +45,33 @@ pub fn compile_schema_metadata<'a>(
|
|||
Docstring {..} => {
|
||||
todo!()
|
||||
}
|
||||
Import(_, _) => {
|
||||
/*
|
||||
let Some(ctx)
|
||||
= project_context.find_schema_by_import(i) else {
|
||||
panic!("No import")
|
||||
};
|
||||
*/
|
||||
Import(span, import) => {
|
||||
let spanned = Rc::clone(&spanned_unit);
|
||||
|
||||
schema_ctx.compile_state.borrow_mut().imports.entry(spanned).or_insert(
|
||||
semi_frozen::Import {
|
||||
namespace: (*span, import.clone()),
|
||||
// alias: alias.clone
|
||||
}
|
||||
);
|
||||
}
|
||||
// Docstring { .. } => {}
|
||||
#[allow(unused)]
|
||||
Constant { name, ..} => {
|
||||
/*
|
||||
let spanned_unit = Rc::clone(&spanned_unit);
|
||||
|
||||
schema_ctx.compile_state.borrow_mut().consts
|
||||
.entry(&spanned_unit).or_insert(
|
||||
SemiFrozenUnit::Constant { name: name.clone() }
|
||||
.entry(spanned_unit).or_insert(
|
||||
semi_frozen::Constant { name: name.clone() }
|
||||
);
|
||||
}
|
||||
Struct { name, .. } => {
|
||||
let spanned_unit = Rc::clone(&spanned_unit);
|
||||
|
||||
schema_ctx.compile_state.borrow_mut().structures
|
||||
.entry(spanned_unit).or_insert(
|
||||
semi_frozen::Structure { name: name.clone() }
|
||||
);
|
||||
*/
|
||||
}
|
||||
Property { .. } => {}
|
||||
Parameter { .. } => {}
|
||||
|
@ -76,8 +79,14 @@ pub fn compile_schema_metadata<'a>(
|
|||
Enum { .. } => {}
|
||||
EnumVariant { .. } => {}
|
||||
Settings { .. } => {}
|
||||
Struct { .. } => {}
|
||||
Protocol { .. } => {}
|
||||
Protocol { name, .. } => {
|
||||
let spanned_unit = Rc::clone(&spanned_unit);
|
||||
|
||||
schema_ctx.compile_state.borrow_mut().protocols
|
||||
.entry(spanned_unit).or_insert(
|
||||
semi_frozen::Protocol { name: name.clone() }
|
||||
);
|
||||
}
|
||||
Function { .. } => {}
|
||||
Argument { .. } => {}
|
||||
Error { .. } => {}
|
||||
|
|
|
@ -1,18 +1,343 @@
|
|||
// Standard Uses
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use std::cell::{Ref, RefCell};
|
||||
|
||||
// Crate Uses
|
||||
use crate::report::ReportDetails;
|
||||
use crate::schema::ir::compiler::report::CompileError;
|
||||
use crate::schema::ir::context::SchemaContext;
|
||||
use crate::schema::idl::ast::unit::{ASTUnit, SpannedUnit};
|
||||
use crate::schema::ir::compiler::interpreted::kind_search;
|
||||
use crate::schema::ir::compiler::interpreted::kind_search::{KindValue};
|
||||
use crate::schema::ir::frozen::unit::{FrozenArgument, FrozenUnit};
|
||||
use crate::project::ir::context::ProjectContext;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn compile_schema_metadata(schema_context: &Rc<RefCell<SchemaContext>>
|
||||
) -> Result<(), ReportDetails<CompileError>>
|
||||
pub fn compile_schema<'a>(
|
||||
schema_context: Rc<RefCell<SchemaContext<'a>>>,
|
||||
project_context: &ProjectContext<'a>
|
||||
) -> Result<(), Box<dyn snafu::Error>> {
|
||||
let schema_ctx = schema_context.borrow();
|
||||
let mut interpreted: Vec<FrozenUnit> = vec![];
|
||||
|
||||
for spanned_unit in &schema_ctx.schema.1 {
|
||||
use crate::schema::idl::ast::unit::ASTUnit::*;
|
||||
match &spanned_unit.1 {
|
||||
Namespace(..) => {}
|
||||
Docstring {..} => {
|
||||
todo!()
|
||||
}
|
||||
Import(s, i) => {
|
||||
let Some(import_ctx)
|
||||
= project_context.find_schema_by_import(i) else {
|
||||
panic!("No schema found by import '{}'", i)
|
||||
};
|
||||
}
|
||||
// Docstring { .. } => {}
|
||||
Constant {
|
||||
docstring, name,
|
||||
kind, default_value
|
||||
} => {
|
||||
let state = schema_ctx.compile_state.borrow();
|
||||
|
||||
let kind_value = kind_search::resolve_kind_value(
|
||||
&schema_ctx, project_context, kind, default_value
|
||||
)?;
|
||||
|
||||
interpreted.push(FrozenUnit::Constant {
|
||||
docstring: None,
|
||||
name: name.1.clone(),
|
||||
kind_value,
|
||||
});
|
||||
}
|
||||
Property { .. } => {}
|
||||
Parameter { .. } => {}
|
||||
ExpressionBlock { .. } => {}
|
||||
Enum { .. } => {}
|
||||
EnumVariant { .. } => {}
|
||||
Settings { .. } => {}
|
||||
Struct { .. } => {}
|
||||
Protocol {
|
||||
docstring, parameters,
|
||||
name, functions
|
||||
} => {
|
||||
let state = schema_ctx.compile_state.borrow();
|
||||
|
||||
let mut params = vec![];
|
||||
let mut funcs = vec![];
|
||||
for function in functions {
|
||||
funcs.push(to_function_frozen(
|
||||
&schema_ctx, &project_context, function
|
||||
)?);
|
||||
}
|
||||
|
||||
interpreted.push(FrozenUnit::Protocol {
|
||||
docstring: "".to_string(),
|
||||
parameters: params,
|
||||
name: name.1.clone(),
|
||||
functions: funcs,
|
||||
});
|
||||
}
|
||||
Function { name, .. } => {
|
||||
}
|
||||
Argument { .. } => {}
|
||||
Error { .. } => {}
|
||||
Validator { .. } => {}
|
||||
Field { .. } => {}
|
||||
}
|
||||
}
|
||||
|
||||
drop(schema_ctx);
|
||||
let mut schema_ctx = RefCell::borrow_mut(&schema_context);
|
||||
schema_ctx.frozen_schema = Some(interpreted);
|
||||
|
||||
Ok(())
|
||||
|
||||
/*
|
||||
for (unit, span) in &context.config.1 {
|
||||
use crate::schema::idl::ast::unit::ASTUnit::*;
|
||||
match unit {
|
||||
ASTUnit::Namespace(n) => {
|
||||
let unit_namespace = context.find_whole_unit_by_import(n);
|
||||
|
||||
/*
|
||||
if unit_namespace.is_some() {
|
||||
return Err(ReportDetails {
|
||||
kind: "Namespace".to_string(),
|
||||
message: format!(
|
||||
"Namespace {} used in another schema: {} and {},\n \
|
||||
only one schema per namespace is allowed",
|
||||
n, context.main.0.filename(), unit_namespace.unwrap().0.filename()
|
||||
),
|
||||
start: 0, end: 0,
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
||||
interpreted.push(FrozenUnit::Namespace(n.clone()));
|
||||
},
|
||||
Import(_) => {
|
||||
let import = interpret_node(&context, unit)?;
|
||||
interpreted.push(import);
|
||||
}
|
||||
Constant { .. } => {
|
||||
let constant = interpret_node(&context, unit)?;
|
||||
interpreted.push(constant);
|
||||
}
|
||||
Enum { .. } => {
|
||||
let r#enum = interpret_node(&context, unit)?;
|
||||
interpreted.push(r#enum);
|
||||
}
|
||||
/*
|
||||
Unit::Settings { .. } => {}
|
||||
Unit::Struct { .. } => {}
|
||||
Unit::Protocol { .. } => {}
|
||||
Unit::Error { .. } => {}
|
||||
*/
|
||||
|
||||
Validator { .. } => {
|
||||
let validator = interpret_node(&context, unit)?;
|
||||
interpreted.push(validator);
|
||||
}
|
||||
/*
|
||||
//r => panic!("Left to impl: {:?}", r)
|
||||
*/
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
fn to_function_frozen(
|
||||
schema_ctx: &Ref<'_, SchemaContext<'_>>, project_context: &'_ ProjectContext<'_>,
|
||||
spanned_unit: &SpannedUnit
|
||||
) -> Result<FrozenUnit, Box<dyn snafu::Error>> {
|
||||
#[allow(unused)]
|
||||
let ASTUnit::Function {
|
||||
docstring, parameters, name,
|
||||
asynchronous,
|
||||
arguments, returns, throws
|
||||
} = &spanned_unit.1 else { panic!() };
|
||||
let sync = || { if let Some(_) = asynchronous { false } else { true } };
|
||||
|
||||
let mut args = vec![];
|
||||
let mut rets = vec![];
|
||||
|
||||
for (_, unit) in arguments {
|
||||
let ASTUnit::Argument { name, kind } = unit else { panic!() };
|
||||
|
||||
let Some(kind) = kind_search::to_kind_only(
|
||||
&schema_ctx, project_context, kind
|
||||
) else { panic!() };
|
||||
|
||||
args.push(FrozenArgument {
|
||||
name: name.1.clone(),
|
||||
kind
|
||||
});
|
||||
}
|
||||
|
||||
for ret in returns {
|
||||
let Some(kind) = kind_search::to_kind_only(
|
||||
&schema_ctx, project_context, ret
|
||||
) else { panic!() };
|
||||
|
||||
rets.push(kind);
|
||||
}
|
||||
|
||||
Ok(FrozenUnit::Function {
|
||||
docstring: "".to_string(),
|
||||
name: name.1.clone(),
|
||||
synchronous: sync(),
|
||||
// direction: Box::new(()),
|
||||
arguments: args,
|
||||
returns: rets,
|
||||
throws: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn interpret_node<'a>(
|
||||
schema_context: &'a SchemaContext<'a>,
|
||||
project_context: &'a ProjectContext<'a>,
|
||||
node: &ASTUnit
|
||||
) -> Result<FrozenUnit, Box<dyn snafu::Error>>
|
||||
{
|
||||
use crate::schema::idl::ast::unit::ASTUnit::*;
|
||||
match node {
|
||||
Namespace(_, n) => {
|
||||
let mut found: Option<&SchemaContext<'a>> = None;
|
||||
|
||||
/*
|
||||
for relative_ctx in schema_ctx.borrow().schema_contexts.iter() {
|
||||
if unit::namespace(&relative_ctx.schema.1) == n {
|
||||
/*
|
||||
if found.is_some() {
|
||||
return Err(ReportDetails {
|
||||
kind: "namespace".to_string(),
|
||||
message: format!(
|
||||
"Found namespace {} when its already declared in {}",
|
||||
&n, &relative_ctx.main.0.filename()
|
||||
),
|
||||
start: 0, end: 0,
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
||||
found = Some(relative_ctx)
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
Import(_, i) => {
|
||||
let relative_unit = project_context.find_schema_by_import(&i);
|
||||
|
||||
/*
|
||||
if relative_unit.is_none() {
|
||||
let relative_unit = relative_unit.unwrap();
|
||||
|
||||
return Err(ReportDetails {
|
||||
kind: "import".to_string(),
|
||||
message: format!("Could not find namespace of {}", relative_unit.0.filename()),
|
||||
start: 0, end: 0,
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
||||
return Ok(FrozenUnit::Import(i.clone()))
|
||||
},
|
||||
Constant {
|
||||
name: (_, name), kind: (_, kind),
|
||||
default_value, ..
|
||||
} => {
|
||||
/*
|
||||
let kind_value = primitive::to_kind_value(kind, default_value);
|
||||
|
||||
return Ok(FrozenUnit::Constant {
|
||||
docstring: None,
|
||||
name: name.clone(), kind_value
|
||||
})
|
||||
*/
|
||||
}
|
||||
Enum { name, variants, .. } => {
|
||||
let mut frozen_variants: Vec<FrozenUnit> = vec![];
|
||||
|
||||
for variant in variants {
|
||||
pub(crate) fn to_variant(variant: &ASTUnit) -> KindValue {
|
||||
match variant {
|
||||
EnumVariant { name, kind } => {
|
||||
if kind.is_none() {
|
||||
return KindValue::EnumVariant(
|
||||
name.1.clone(),None
|
||||
)
|
||||
}
|
||||
|
||||
return KindValue::EnumVariant(
|
||||
name.1.clone(), None
|
||||
)
|
||||
},
|
||||
_ => panic!("Should not be here")
|
||||
}
|
||||
}
|
||||
|
||||
frozen_variants.push(FrozenUnit::EnumVariant(
|
||||
to_variant(&variant.1)
|
||||
));
|
||||
}
|
||||
|
||||
return Ok(FrozenUnit::Enum {
|
||||
docstring: None,
|
||||
name: name.1.clone(),
|
||||
variants: frozen_variants
|
||||
})
|
||||
}
|
||||
/*
|
||||
EnumVariant { .. } => {}
|
||||
Settings { .. } => {}
|
||||
Struct { .. } => {}
|
||||
Protocol { .. } => {}
|
||||
Function { .. } => {}
|
||||
Error { .. } => {}
|
||||
*/
|
||||
#[allow(unused)]
|
||||
Validator {
|
||||
docstring, properties,
|
||||
name, expression_block
|
||||
} => {
|
||||
let properties = to_properties(properties);
|
||||
let expr_block = Box::new(FrozenUnit::ExpressionBlock { function_calls: vec![] });
|
||||
|
||||
|
||||
return Ok(FrozenUnit::Validator {
|
||||
docstring: Some("".to_owned()), // TODO: Docstrings should likely be a vector of units
|
||||
properties: vec![],
|
||||
name: name.1.clone(),
|
||||
expression_block: expr_block
|
||||
})
|
||||
}
|
||||
/*
|
||||
Field { .. } => {}
|
||||
Parameter { .. } => {}
|
||||
Property { .. } => {}
|
||||
ExpressionBlock { .. } => {}
|
||||
*/
|
||||
missing => unimplemented!("Missing implementation for node '{:?}'", missing)
|
||||
}
|
||||
|
||||
panic!()
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn to_properties(nodes: &Vec<SpannedUnit>) -> Vec<FrozenUnit> {
|
||||
let properties = vec![];
|
||||
|
||||
for node in nodes {
|
||||
|
||||
}
|
||||
|
||||
properties
|
||||
}
|
||||
|
||||
pub fn into_frozen_unit() -> FrozenUnit {
|
||||
todo!()
|
||||
}
|
|
@ -6,10 +6,28 @@ use crate::utils::codemap::Span;
|
|||
// External Uses
|
||||
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum SemiFrozenUnit {
|
||||
Constant {
|
||||
name: (Span, String)
|
||||
},
|
||||
pub struct Import {
|
||||
pub(crate) namespace: (Span, String),
|
||||
// TODO: Alias of imports for schemas, objects, etc, is needed for namespace conflicts
|
||||
// pub(crate) alias: (Span, String)
|
||||
}
|
||||
|
||||
// TODO: All of these structures might be able to be replace with a single one, since
|
||||
// a lot of them might just need to have a name and span to the objects
|
||||
#[derive(Clone)]
|
||||
pub struct Structure {
|
||||
pub(crate) name: (Span, String)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Constant {
|
||||
pub(crate) name: (Span, String)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Protocol {
|
||||
pub(crate) name: (Span, String)
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ pub mod report;
|
|||
|
||||
// Local Uses
|
||||
use crate::schema::idl::parser_new;
|
||||
use crate::schema::idl::ast::unit::{ASTUnit, SourcedWhole};
|
||||
use crate::schema::idl::ast::unit::{ASTUnit, SourcedWholeRc};
|
||||
|
||||
// External Uses
|
||||
|
||||
|
@ -29,5 +29,5 @@ pub trait Compile {
|
|||
}
|
||||
|
||||
|
||||
fn from_sourced_whole(sourced: SourcedWhole) -> Self::Output;
|
||||
fn from_sourced_whole(sourced: SourcedWholeRc) -> Self::Output;
|
||||
}
|
||||
|
|
|
@ -1,47 +1,63 @@
|
|||
// Standard Uses
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
// Crate Uses
|
||||
use crate::report::ReportDetails;
|
||||
|
||||
// External Uses
|
||||
use snafu::Snafu;
|
||||
use strum::EnumProperty;
|
||||
use strum_macros::FromRepr;
|
||||
|
||||
|
||||
// TODO: repr(u16) is here so we could turn a enum variant instance into its corresponding number
|
||||
// however i didn't find how to do it without having to match pattern every each one of
|
||||
// the variants and return the number by casting the variant, so strum props were used.
|
||||
// If in the future we know of a better solution, it should be applied
|
||||
#[repr(u16)]
|
||||
#[derive(EnumProperty)]
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum CompileError {
|
||||
#[strum(props(Id="1"))]
|
||||
#[snafu(display("Found namespace '{target}' when its already declared in '{origin}'"))]
|
||||
Namespace { source: std::io::Error, origin: String, target: String } = 1,
|
||||
#[snafu(display(
|
||||
"Found namespace '{target}' when its already declared in '{origin}'"
|
||||
))]
|
||||
NamespaceCollision { origin: String, target: String } = 0,
|
||||
|
||||
#[snafu(display("No type found by name '{name}'"))]
|
||||
TypeNotFound { name: String },
|
||||
}
|
||||
|
||||
impl CompileError {
|
||||
pub fn code(&self) -> u16 {
|
||||
self.get_str("Id").expect("Variant does not have a 'Id' property").parse().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ReportDetails<CompileError> {
|
||||
#[allow(unused)]
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "TODO: Display needs implementation here")
|
||||
pub(crate) fn code(&self) -> u16 {
|
||||
unsafe { *<*const _>::from(self).cast::<u16>() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO: Same case as the enum `CompileError` about the repr
|
||||
#[derive(EnumProperty, FromRepr)]
|
||||
#[repr(u16)]
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum CompileWarning {
|
||||
#[strum(props(Id="1"))]
|
||||
#[snafu(display("No dependencies were assigned, this will automatically add Comline's langlib'"))]
|
||||
DependenciesNotFilled
|
||||
}
|
||||
|
||||
impl CompileWarning {
|
||||
pub(crate) fn code(&self) -> u16 {
|
||||
unsafe { *<*const _>::from(self).cast::<u16>() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
#[snafu(visibility(pub(crate)))]
|
||||
pub enum Report {
|
||||
#[snafu(display(
|
||||
"Error[E0{}]: {source}\nAt line {}:{} (position {}:{})",
|
||||
source.code(), details.line.start, details.line.end, details.pos.start, details.pos.end
|
||||
))]
|
||||
CompileErrorError {
|
||||
source: CompileError,
|
||||
details: ReportDetails,
|
||||
},
|
||||
|
||||
#[snafu(display(
|
||||
"Warning[W0{}]: {source}\nAt line {}:{} (position {}:{})",
|
||||
source.code(), details.line.start, details.line.end, details.pos.start, details.pos.end
|
||||
))]
|
||||
CompileWarningError {
|
||||
source: CompileWarning,
|
||||
details: ReportDetails,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,22 +1,28 @@
|
|||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
|
||||
// Local Uses
|
||||
use crate::schema::idl::ast::unit::{SourcedWhole, SpannedUnit};
|
||||
use crate::schema::ir::compiler::interpreter::semi_frozen::SemiFrozenUnit;
|
||||
use crate::schema::idl::ast::unit::{SourcedWholeRc, SpannedUnit};
|
||||
use crate::schema::ir::compiler::interpreter::semi_frozen;
|
||||
use crate::schema::ir::frozen::unit::FrozenUnit;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct CompileState<'a> {
|
||||
pub struct CompileState<'e> {
|
||||
pub namespace: Option<String>,
|
||||
pub consts: HashMap<&'a SpannedUnit, SemiFrozenUnit>
|
||||
pub imports: HashMap<Rc<SpannedUnit>, semi_frozen::Import>,
|
||||
pub consts: HashMap<Rc<SpannedUnit>, semi_frozen::Constant>,
|
||||
pub structures: HashMap<Rc<SpannedUnit>, semi_frozen::Structure>,
|
||||
pub protocols: HashMap<Rc<SpannedUnit>, semi_frozen::Protocol>,
|
||||
pub phantom: PhantomData<&'e ()>,
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
impl<'a> CompileState<'_> {
|
||||
impl<'e> CompileState<'_> {
|
||||
pub(crate) fn to_frozen(&self) -> Vec<FrozenUnit> {
|
||||
let mut interpreted = vec![];
|
||||
|
||||
|
@ -27,7 +33,8 @@ impl<'a> CompileState<'_> {
|
|||
}
|
||||
|
||||
pub struct SchemaContext<'a> {
|
||||
pub schema: SourcedWhole,
|
||||
pub name: String,
|
||||
pub schema: SourcedWholeRc,
|
||||
pub frozen_schema: Option<Vec<FrozenUnit>>,
|
||||
// pub project_context: Option<&'a RefCell<ProjectContext<'a>>>,
|
||||
// pub project_context: Option<&'a mut ProjectContext<'a>>,
|
||||
|
@ -37,8 +44,9 @@ pub struct SchemaContext<'a> {
|
|||
|
||||
#[allow(unused)]
|
||||
impl<'a> SchemaContext<'a> {
|
||||
pub fn with_main(schema: SourcedWhole) -> Self{
|
||||
pub fn with_main(schema: SourcedWholeRc, name: String) -> Self{
|
||||
Self {
|
||||
name,
|
||||
schema,
|
||||
frozen_schema: None,
|
||||
// project_context: None,
|
||||
|
@ -47,10 +55,12 @@ impl<'a> SchemaContext<'a> {
|
|||
}
|
||||
|
||||
pub fn with_project_and_main(
|
||||
schema: SourcedWhole,
|
||||
schema: SourcedWholeRc,
|
||||
// project: &'a RefCell<ProjectContext<'a>>
|
||||
name: String
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
schema,
|
||||
frozen_schema: None,
|
||||
// project_context: Some(project),
|
||||
|
@ -58,15 +68,6 @@ impl<'a> SchemaContext<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn with_main_no_std(schema: SourcedWhole) -> Self{
|
||||
Self {
|
||||
schema,
|
||||
frozen_schema: None,
|
||||
// project_context: None,
|
||||
compile_state: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn sanitize_units(self) {
|
||||
todo!()
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::fmt::Debug;
|
|||
|
||||
// Crate Uses
|
||||
use crate::schema::ir::frozen::unit::{FrozenUnit, FrozenWhole};
|
||||
use crate::schema::ir::compiler::interpreted::primitive::KindValue;
|
||||
use crate::schema::ir::compiler::interpreted::kind_search::KindValue;
|
||||
|
||||
// External Uses
|
||||
use downcast_rs::{Downcast, impl_downcast};
|
||||
|
@ -18,8 +18,8 @@ pub trait Differ: Downcast + Debug {
|
|||
&mut self, name: &str, left_kind_value: &KindValue, right_kind_value: &KindValue
|
||||
);
|
||||
|
||||
fn differ(
|
||||
&self, previous: &Vec<FrozenUnit>, next: &FrozenWhole,
|
||||
fn differ<'a>(
|
||||
&self, previous: &Vec<FrozenUnit>, next: &FrozenWhole<'a>,
|
||||
document_gen: bool, auto_version: bool
|
||||
) {
|
||||
todo!()
|
||||
|
|
|
@ -2,13 +2,15 @@
|
|||
|
||||
// Crate Uses
|
||||
use crate::schema::ir::context::SchemaContext;
|
||||
use crate::schema::ir::compiler::interpreted::primitive::KindValue;
|
||||
use crate::schema::ir::compiler::interpreted::kind_search::KindValue;
|
||||
|
||||
// External Uses
|
||||
|
||||
|
||||
pub type FrozenWhole<'a> = (SchemaContext<'a>, Vec<FrozenUnit>);
|
||||
|
||||
|
||||
// TODO: A lot of string instances could be &'a str, boxes could also get ditched, etc
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum FrozenUnit {
|
||||
|
@ -60,9 +62,9 @@ pub enum FrozenUnit {
|
|||
docstring: String,
|
||||
name: String,
|
||||
synchronous: bool,
|
||||
direction: Box<FrozenUnit>,
|
||||
arguments: Vec<FrozenUnit>,
|
||||
returns: Vec<FrozenUnit>,
|
||||
// direction: Box<FrozenUnit>,
|
||||
arguments: Vec<FrozenArgument>,
|
||||
returns: Vec<KindValue>,
|
||||
throws: Vec<FrozenUnit>
|
||||
},
|
||||
Error {
|
||||
|
@ -86,3 +88,10 @@ pub enum FrozenUnit {
|
|||
kind_value: KindValue,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct FrozenArgument {
|
||||
pub name: String,
|
||||
pub kind: KindValue
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use comline::schema::ir::frozen::unit::FrozenUnit;
|
|||
// External Uses
|
||||
use once_cell::sync::Lazy;
|
||||
use comline::autodoc;
|
||||
use comline::schema::ir::compiler::interpreted::primitive::{KindValue, Primitive};
|
||||
use comline::schema::ir::compiler::interpreted::kind_search::{KindValue, Primitive};
|
||||
|
||||
|
||||
static PREVIOUS_FROZEN_UNITS: Lazy<Vec<FrozenUnit>> = Lazy::new(||
|
||||
|
|
|
@ -5,8 +5,8 @@ use std::path::Path;
|
|||
|
||||
// External Uses
|
||||
use comline::schema::{idl, ir};
|
||||
use comline::schema::idl::constants::SCHEMA_EXTENSION;
|
||||
use comline::schema::idl::ast::unit::ASTUnit;
|
||||
use comline::schema::idl::constants::SCHEMA_EXTENSION;
|
||||
use comline::utils::codemap::Span;
|
||||
|
||||
|
||||
|
@ -193,7 +193,9 @@ fn compile_unit() {
|
|||
let path = Path::new(path);
|
||||
let unit = idl::parser_new::from_path(path).unwrap();
|
||||
|
||||
let context = ir::context::SchemaContext::with_main(unit);
|
||||
let context = ir::context::SchemaContext::with_main(
|
||||
unit, path.file_stem().unwrap().to_str().unwrap().to_owned()
|
||||
);
|
||||
|
||||
/*
|
||||
let frozen_unit = interpreter::interpret_context(context).unwrap();
|
||||
|
|
|
@ -3,10 +3,12 @@ specification_version = 1
|
|||
|
||||
schema_paths = [
|
||||
"ping.ids"
|
||||
"health.ids"
|
||||
]
|
||||
|
||||
code_generation = {
|
||||
languages = {
|
||||
python#3.11.0 = { package_versions=[all] }
|
||||
rust#1.70.0 = { package_versions=[all] }
|
||||
}
|
||||
}
|
||||
|
|
14
core/tests/package/test/generated/python/3.11.0/health.py
Normal file
14
core/tests/package/test/generated/python/3.11.0/health.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
# Generated code with Comline compiler and code generator
|
||||
|
||||
import typing
|
||||
import ctypes
|
||||
|
||||
|
||||
class HealthCheck(typing.Protocol):
|
||||
def alive(self):
|
||||
...
|
||||
|
||||
def capabilities(self):
|
||||
...
|
||||
|
||||
|
16
core/tests/package/test/generated/python/3.11.0/ping.py
Normal file
16
core/tests/package/test/generated/python/3.11.0/ping.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
# Generated code with Comline compiler and code generator
|
||||
|
||||
import typing
|
||||
import ctypes
|
||||
|
||||
|
||||
LOW_PING_RATE: typing.Final[ctypes.c_uint16] = 20
|
||||
|
||||
class Ping(typing.Protocol):
|
||||
def ping(self):
|
||||
...
|
||||
|
||||
def ping_limit(self):
|
||||
...
|
||||
|
||||
|
8
core/tests/package/test/generated/rust/1.70.0/health.rs
Normal file
8
core/tests/package/test/generated/rust/1.70.0/health.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
// Generated code with Comline compiler and code generator
|
||||
|
||||
pub trait HealthCheck {
|
||||
fn alive (&self);
|
||||
fn capabilities (&self);
|
||||
|
||||
}
|
||||
|
10
core/tests/package/test/generated/rust/1.70.0/ping.rs
Normal file
10
core/tests/package/test/generated/rust/1.70.0/ping.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
// Generated code with Comline compiler and code generator
|
||||
|
||||
pub const LOW_PING_RATE: u16 = 20;
|
||||
|
||||
pub trait Ping {
|
||||
fn ping (&self);
|
||||
fn ping_limit (&self);
|
||||
|
||||
}
|
||||
|
|
@ -2,13 +2,13 @@
|
|||
namespace test::idl::health
|
||||
|
||||
struct Capabilities {
|
||||
names: str[]
|
||||
names: str
|
||||
}
|
||||
|
||||
/// Health check an address
|
||||
@provider=Any
|
||||
protocol HealthCheck {
|
||||
function alive() -> bool
|
||||
function capabilities() -> Capabilities
|
||||
function alive() -> bool;
|
||||
function capabilities() -> Capabilities;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
// Ping Schema
|
||||
namespace tests::idl::ping
|
||||
namespace test::idl::ping
|
||||
|
||||
const LOW_PING_RATE:u16 = 20;
|
||||
import test::idl::health
|
||||
|
||||
const LOW_PING_RATE: u16 = 20
|
||||
|
||||
|
||||
/// Ping another address
|
||||
@provider=Any
|
||||
protocol Ping {
|
||||
function ping() -> bool
|
||||
function ping() -> bool;
|
||||
|
||||
@timeout_ms=1000
|
||||
function ping_limit() -> bool
|
||||
function ping_limit() -> bool;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue