Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement plugin system and link builtin plugin #92

Merged
merged 5 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::comp::{CompDoc, Compiler, CompilerError};
use crate::lang::Preset;
use crate::metrics::CompilerMetrics;
use crate::pack::{self, PackedProject, PackerError, PackerResult, Resource, ValidUse};
use crate::plug::run_plugins;
use crate::plug::{run_plugins, PluginRuntime};

/// Output of the compiler API
#[derive(Debug, Clone)]
Expand All @@ -35,6 +35,7 @@ pub struct OkOutput {
#[derive(Default, Debug, Clone)]
pub struct CompilerMetadata {
pub presets: HashMap<String, Preset>,
pub plugins: Vec<PluginRuntime>,
pub default_icon_priority: i64,
}

Expand Down Expand Up @@ -96,7 +97,7 @@ pub async fn compile(root_resource: &Resource, setting: &Setting) -> CompilerOut
let ms = metrics.comp_done();
info!("comp phase done in {ms}ms");

let comp_doc = run_plugins(comp_doc);
let comp_doc = run_plugins(comp_doc, &comp_meta.plugins).await;
let ms = metrics.plug_done();
info!("plug phase done in {ms}ms");

Expand Down
5 changes: 5 additions & 0 deletions compiler-core/src/comp/comp_doc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use celerctypes::DocDiagnostic;
use celerctypes::{DocPoorText, RouteMetadata};
use serde::{Deserialize, Serialize};

Expand All @@ -21,6 +22,8 @@ pub struct CompDoc {
pub preface: Vec<Vec<DocPoorText>>,
/// The route
pub route: Vec<CompSection>,
/// Overall diagnostics (that don't apply to any line)
pub diagnostics: Vec<DocDiagnostic>,
}

impl Compiler {
Expand Down Expand Up @@ -50,6 +53,7 @@ impl Compiler {
project: self.project,
preface,
route: route_vec,
diagnostics: vec![],
},
self.meta,
))
Expand Down Expand Up @@ -118,6 +122,7 @@ impl Compiler {
route: vec![self.create_empty_section_for_error(errors).await],
project: self.project,
preface: vec![],
diagnostics: vec![],
},
self.meta,
)
Expand Down
1 change: 1 addition & 0 deletions compiler-core/src/comp/compiler_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl CompilerBuilder {
meta: CompilerMetadata {
presets: self.presets,
default_icon_priority: self.default_icon_priority,
..Default::default()
},
color: self.color,
coord: self.coord,
Expand Down
2 changes: 2 additions & 0 deletions compiler-core/src/comp/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub const NAME: &str = "name";
pub const NOTES: &str = "notes";
pub const PRESETS: &str = "presets";
pub const PRIORITY: &str = "priority";
pub const PLUGINS: &str = "plugins";
pub const ROUTE: &str = "route";
pub const SCALE: &str = "scale";
pub const SIZE: &str = "size";
Expand All @@ -43,4 +44,5 @@ pub const TRANSLATE: &str = "translate";
pub const USE: &str = "use";
pub const VERSION: &str = "version";
pub const WARP: &str = "warp";
pub const WITH: &str = "with";
pub const ZOOM_BOUNDS: &str = "zoom-bounds";
14 changes: 13 additions & 1 deletion compiler-core/src/exec/exec_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl CompDoc {
project: self.project,
preface: self.preface,
route: sections,
diagnostics: self.diagnostics,
})
}
}
Expand All @@ -27,10 +28,11 @@ impl CompDoc {
mod test {
use celerctypes::{
DocPoorText, ExecLine, ExecMapSection, ExecSection, GameCoord, MapLine, MapMetadata,
RouteMetadata,
RouteMetadata, DocDiagnostic,
};

use crate::comp::{CompLine, CompMovement, CompSection};
use crate::lang::parse_poor;

use super::*;

Expand All @@ -44,15 +46,25 @@ mod test {

let test_preface = vec![vec![DocPoorText::Text("test".to_string())]];

let test_diagnostics = vec![
DocDiagnostic {
msg: parse_poor("test msg"),
msg_type: "test".to_string(),
source: "test".to_string(),
}
];

let test_doc = CompDoc {
project: test_metadata.clone(),
preface: test_preface.clone(),
diagnostics: test_diagnostics.clone(),
..Default::default()
};

let exec_doc = test_doc.exec().await.unwrap();
assert_eq!(exec_doc.project, test_metadata);
assert_eq!(exec_doc.preface, test_preface);
assert_eq!(exec_doc.diagnostics, test_diagnostics);
}

#[tokio::test]
Expand Down
39 changes: 38 additions & 1 deletion compiler-core/src/lang/rich/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,27 @@ fn parse_tagexp(pt: &pt::TagExp) -> DocRichText {
arg.push_str(str);
}
for pt_unit in pt.m_arg.iter() {
append_unit_to_string(pt_unit, &mut arg);
append_unit_inside_tag_to_string(pt_unit, &mut arg);
}
DocRichText {
tag: Some(tag),
text: arg,
link: None,
}
}
fn append_unit_inside_tag_to_string(pt: &pt::UnitInsideTag, out: &mut String) {
match pt {
pt::UnitInsideTag::Unit(pt) => {
append_unit_to_string(pt, out);
}
pt::UnitInsideTag::UnitDotSymbol(_) => {
out.push('.');
}
pt::UnitInsideTag::UnitOpenParenSymbol(_) => {
out.push('(');
}
}
}

fn append_unit_to_string(pt: &pt::Unit, out: &mut String) {
match pt {
Expand Down Expand Up @@ -253,4 +266,28 @@ mod test {
}]
);
}

#[test]
fn test_dot_in_text() {
assert_eq!(
parse_rich(".tag([hello]continue.me)"),
vec![DocRichText {
tag: Some("tag".to_string()),
text: "[hello]continue.me".to_string(),
link: None
}]
);
}

#[test]
fn test_open_paren_in_text() {
assert_eq!(
parse_rich(".tag([hello]co(ntinue.me)"),
vec![DocRichText {
tag: Some("tag".to_string()),
text: "[hello]co(ntinue.me".to_string(),
link: None
}]
);
}
}
5 changes: 4 additions & 1 deletion compiler-core/src/lang/rich/rich.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ rule TagExp(
(Tag) tag: token Identifier,
_: token Symbol"(",
space: optional token Space,
(Arg) arg: optional Unit+,
(Arg) arg: optional UnitInsideTag+,
_: token Symbol")"
);

rule Unit = UnitId | UnitEscape;
rule UnitInsideTag = Unit | UnitDotSymbol | UnitOpenParenSymbol;
rule UnitId(t: token Identifier, s: optional token Space);
rule UnitEscape(t: token Escape, s: optional token Space);
rule UnitDotSymbol((Text)_: token Symbol".", s: optional token Space);
rule UnitOpenParenSymbol((Text)_: token Symbol"(", s: optional token Space);
rule Symbol((Text)t: token Symbol);
rule Space(t: token Space);
23 changes: 12 additions & 11 deletions compiler-core/src/pack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! The output of the packer is a [`RouteMetadata`](celerctypes::RouteMetadata)
//! and a json blob of the route.

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::BTreeMap;
use std::convert::Infallible;
Expand Down Expand Up @@ -36,7 +37,8 @@ use crate::lang::parse_poor;
#[cfg(feature = "wasm")]
use crate::util::WasmError;

#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[derive(Debug, Clone, PartialEq, thiserror::Error, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type", content="data")]
pub enum PackerError {
#[error("The project file (project.yaml) is missing or invalid.")]
InvalidProject,
Expand Down Expand Up @@ -71,6 +73,9 @@ pub enum PackerError {
#[error("Error when parsing structured data in file {0}: {1}")]
InvalidFormat(String, String),

#[error("Error when parsing file {0}: file is not UTF-8")]
InvalidUtf8(String),

#[error("")]
InvalidIcon,

Expand Down Expand Up @@ -106,6 +111,9 @@ pub enum PackerError {
)]
DuplicateMap(usize),

#[error("`{0}` is not a valid built-in plugin or reference to a plugin script")]
InvalidPlugin(String),

#[error("No map defined in project config")]
MissingMap,

Expand All @@ -114,10 +122,6 @@ pub enum PackerError {

#[error("{0}")]
NotImpl(String),

#[cfg(feature = "wasm")]
#[error("Wasm execution error: {0}")]
Wasm(#[from] WasmError),
}

impl PackerError {
Expand All @@ -130,11 +134,7 @@ impl PackerError {
}

pub fn is_cancel(&self) -> bool {
#[cfg(feature = "wasm")]
let x = matches!(self, Self::Wasm(WasmError::Cancel));
#[cfg(not(feature = "wasm"))]
let x = false;
x
false
}
}

Expand All @@ -150,7 +150,8 @@ pub type PackerResult<T> = Result<T, PackerError>;
///
/// This is used to expose errors to the compiler, so it can be displayed
/// using the diagnostics API
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum PackerValue {
Ok(Value),
Err(PackerError),
Expand Down
Loading
Loading