Skip to content

Commit

Permalink
ability to move text nodes and elements over
Browse files Browse the repository at this point in the history
  • Loading branch information
crcn committed Sep 12, 2023
1 parent 0251fd3 commit 675fb5c
Show file tree
Hide file tree
Showing 27 changed files with 375 additions and 169 deletions.
4 changes: 2 additions & 2 deletions libs/ast_serialize/src/pc.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::css::{serialize_decl_value, serialize_declarations};
use crate::docco::serialize_comment as serialize_doc_comment;
use paperclip_common::serialize_context::Context;
use paperclip_proto::ast::base as base_ast;
use paperclip_proto::ast::docco as docco_ast;
use paperclip_proto::ast::pc as ast;
use paperclip_proto::ast::{base as base_ast, shared};

pub fn serialize(document: &ast::Document) -> String {
let mut context = Context::new(0);
Expand Down Expand Up @@ -322,7 +322,7 @@ pub fn serialize_string(node: &base_ast::Str, context: &mut Context) {
}

pub fn serialize_number(_node: &base_ast::Num, _context: &mut Context) {}
pub fn serialize_reference(node: &ast::Reference, context: &mut Context) {
pub fn serialize_reference(node: &shared::Reference, context: &mut Context) {
context.add_buffer(node.path.join(".").as_str());
}

Expand Down
3 changes: 2 additions & 1 deletion libs/compiler-react/src/code_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use paperclip_evaluator::core::utils::get_style_namespace;
use paperclip_proto::ast::{
graph_ext::{Dependency, Graph},
pc as ast,
shared::Reference,
};
use std::collections::BTreeMap;

Expand Down Expand Up @@ -375,7 +376,7 @@ fn compile_simple_expression(expr: &ast::SimpleExpression, context: &mut Context
}
}

fn compile_reference(expr: &ast::Reference, context: &mut Context) {
fn compile_reference(expr: &Reference, context: &mut Context) {
let mut parts = expr.path.iter().peekable();

while let Some(part) = parts.next() {
Expand Down
5 changes: 3 additions & 2 deletions libs/evaluator/src/css/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use paperclip_proto::ast::graph_ext::{self as graph};
use paperclip_proto::ast::graph_ext::{self as graph_ref, Expr};
use paperclip_proto::ast::pc::override_body_item;
use paperclip_proto::ast::pc::{self as ast};
use paperclip_proto::ast::shared::Reference;
use paperclip_proto::virt::css::Rule;
use std::string::ToString;

Expand Down Expand Up @@ -391,7 +392,7 @@ fn get_render_node_ns<F: FileResolver>(context: &DocumentContext<F>) -> Option<S

fn evaluate_variant_styles<F: FileResolver>(
style: &ast::Style,
variant_refs: &Vec<ast::Reference>,
variant_refs: &Vec<Reference>,
expanded_combo_selectors: &Vec<Vec<VariantTrigger>>,
context: &mut DocumentContext<F>,
) {
Expand Down Expand Up @@ -714,7 +715,7 @@ fn get_combo_selectors2(
// [[or, or, or] and [or]]

fn collect_style_variant_selectors<F: FileResolver>(
variant_refs: &Vec<ast::Reference>,
variant_refs: &Vec<Reference>,
context: &mut DocumentContext<F>,
) -> Vec<Vec<VariantTrigger>> {
let mut combo_triggers = vec![];
Expand Down
5 changes: 3 additions & 2 deletions libs/parser/src/css/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::core::parser_context::Context;
use crate::core::string_scanner::StringScanner;
use paperclip_common::id::IDGenerator;
use paperclip_proto::ast::css as ast;
use paperclip_proto::ast::shared as shared_ast;
use std::str;

type ParserContext<'tokenizer, 'scanner, 'idgenerator, 'src> =
Expand Down Expand Up @@ -239,7 +240,7 @@ fn parse_call(context: &mut ParserContext) -> Result<ast::FunctionCall, err::Par
})
}

fn parse_reference(context: &mut ParserContext) -> Result<ast::Reference, err::ParserError> {
fn parse_reference(context: &mut ParserContext) -> Result<shared_ast::Reference, err::ParserError> {
let start = context.curr_u16pos.clone();
let mut path: Vec<String> = vec![];

Expand All @@ -255,7 +256,7 @@ fn parse_reference(context: &mut ParserContext) -> Result<ast::Reference, err::P
}
}
let end = context.curr_u16pos.clone();
Ok(ast::Reference {
Ok(shared_ast::Reference {
id: context.next_id(),
range: Some(Range::new(start, end)),
path,
Expand Down
11 changes: 7 additions & 4 deletions libs/parser/src/pc/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::core::string_scanner::StringScanner;
use paperclip_proto::ast::css as css_ast;
use paperclip_proto::ast::docco as docco_ast;
use paperclip_proto::ast::pc::{self as ast, TriggerBodyItem};
use paperclip_proto::ast::shared as shared_ast;

use crate::css::parser::{
parse_style_declaration_with_string_scanner, parse_style_declarations_with_string_scanner,
Expand Down Expand Up @@ -319,11 +320,13 @@ fn parse_style(context: &mut PCContext, is_public: bool) -> Result<ast::Style, e
})
}

fn parse_style_extends(context: &mut PCContext) -> Result<Vec<ast::Reference>, err::ParserError> {
fn parse_style_extends(
context: &mut PCContext,
) -> Result<Vec<shared_ast::Reference>, err::ParserError> {
context.next_token()?; // eat
context.skip(is_superfluous_or_newline)?;

let mut extends: Vec<ast::Reference> = vec![];
let mut extends: Vec<shared_ast::Reference> = vec![];
loop {
extends.push(parse_ref(context)?);
context.skip(is_superfluous_or_newline)?;
Expand Down Expand Up @@ -833,11 +836,11 @@ fn parse_boolean(context: &mut PCContext) -> Result<base_ast::Bool, err::ParserE
})
}

fn parse_ref(context: &mut PCContext) -> Result<ast::Reference, err::ParserError> {
fn parse_ref(context: &mut PCContext) -> Result<shared_ast::Reference, err::ParserError> {
let start = context.curr_u16pos.clone();
let path: Vec<String> = parse_path(context)?;
let end = context.curr_u16pos.clone();
Ok(ast::Reference {
Ok(shared_ast::Reference {
id: context.next_id(),
range: Some(base_ast::Range::new(start, end)),
path,
Expand Down
1 change: 1 addition & 0 deletions libs/proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"src/service/designer.proto",
"src/ast/css.proto",
"src/ast/base.proto",
"src/ast/shared.proto",
"src/ast/graph.proto",
"src/ast/pc.proto",
"src/ast/docco.proto",
Expand Down
3 changes: 2 additions & 1 deletion libs/proto/src/ast/all/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub use super::base;
pub use super::docco;
pub use super::pc;
pub use super::shared;
use crc::{Crc, CRC_32_ISCSI};
pub mod visit;

Expand Down Expand Up @@ -193,7 +194,7 @@ expressions! {
pc::simple_expression::Inner::Reference,
pc::simple_expression::Inner::Ary
)),
(Reference, pc::Reference, self => &self.id),
(Reference, shared::Reference, self => &self.id),
(Ary, pc::Ary, self => &self.id),
(Element, pc::Element, self => &self.id),
(Node, pc::Node, self => &self.get_inner().get_id()),
Expand Down
8 changes: 2 additions & 6 deletions libs/proto/src/ast/all/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub use super::super::css;
pub use super::super::docco;
pub use super::super::graph;
pub use super::super::pc;
pub use super::super::shared;

macro_rules! visitable {
(
Expand Down Expand Up @@ -230,11 +231,6 @@ visitable! {
css::declaration_value::Inner::CommaList
)
}),
(css::Reference, visit_css_reference, (self, visitor) {
VisitorResult::Continue
},{
VisitorResult::Continue
}),
(css::Measurement, visit_css_measurement, (self, visitor) {
VisitorResult::Continue
},{
Expand Down Expand Up @@ -412,7 +408,7 @@ visit_enum!(self.get_inner_mut(), visitor, pc::trigger_body_item::Inner::Str, pc
}, {
visit_each!(&mut self.items, visitor)
}),
(pc::Reference, visit_reference, (self, visitor) {
(shared::Reference, visit_reference, (self, visitor) {
VisitorResult::Continue
}, {
VisitorResult::Continue
Expand Down
11 changes: 3 additions & 8 deletions libs/proto/src/ast/css.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
syntax = "proto3";
import "ast/base.proto";
import "ast/shared.proto";

package ast.css;

Expand All @@ -14,7 +15,7 @@ message StyleDeclaration {
message DeclarationValue {
oneof inner {
ast.base.Num number = 1;
Reference reference = 2;
ast.shared.Reference reference = 2;
ast.base.Str str = 3;
Measurement measurement = 4;
FunctionCall function_call = 5;
Expand All @@ -25,12 +26,6 @@ message DeclarationValue {
}
}

message Reference {
string id = 1;
ast.base.Range range = 2;
repeated string path = 3;
}

message Arithmetic {
string id = 1;
ast.base.Range range = 2;
Expand Down Expand Up @@ -69,4 +64,4 @@ message CommaList {
string id = 1;
ast.base.Range range = 2;
repeated DeclarationValue items = 3;
}
}
4 changes: 3 additions & 1 deletion libs/proto/src/ast/get_expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::graph_ext::{Dependency, Graph};
use super::pc;
use super::pc::{document_body_item, DocumentBodyItem, Element};
use super::pc::{document_body_item, DocumentBodyItem};
use super::shared;
use super::{all::ExpressionWrapper, pc::Component};

// use crate::ast::all::Visitable;
Expand Down Expand Up @@ -34,6 +35,7 @@ impl<'expr> Visitor<()> for GetExpr {
(visit_text_node, pc::TextNode),
(visit_variant, pc::Variant),
(visit_style, pc::Style),
(visit_reference, shared::Reference),
(visit_atom, pc::Atom)
}
}
Expand Down
1 change: 1 addition & 0 deletions libs/proto/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod get_expr;
pub mod graph;
pub mod graph_ext;
pub mod pc;
pub mod shared;
14 changes: 5 additions & 9 deletions libs/proto/src/ast/pc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
import "ast/base.proto";
import "ast/css.proto";
import "ast/docco.proto";
import "ast/shared.proto";

package ast.pc;

Expand Down Expand Up @@ -35,8 +36,8 @@ message Style {
string id = 1;
bool is_public = 2;
optional string name = 3;
repeated Reference variant_combo = 4;
repeated Reference extends = 5;
repeated ast.shared.Reference variant_combo = 4;
repeated ast.shared.Reference extends = 5;
ast.base.Range range = 6;
repeated ast.css.StyleDeclaration declarations = 7;
}
Expand Down Expand Up @@ -113,7 +114,7 @@ message TriggerBodyItemCombo {
message TriggerBodyItem {
oneof inner {
ast.base.Str str = 1;
Reference reference = 2;
ast.shared.Reference reference = 2;
ast.base.Bool bool = 3;
}
}
Expand All @@ -139,16 +140,11 @@ message SimpleExpression {
ast.base.Str str = 1;
ast.base.Num num = 2;
ast.base.Bool bool = 3;
Reference reference = 4;
ast.shared.Reference reference = 4;
Ary ary = 5;
}
}

message Reference {
string id = 1;
ast.base.Range range = 2;
repeated string path = 3;
}

message Ary {
string id = 1;
Expand Down
26 changes: 26 additions & 0 deletions libs/proto/src/ast/pc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::add_inner_wrapper;

use super::{
all::ExpressionWrapper,
get_expr::GetExpr,
graph::{Dependency, Graph},
};
Expand Down Expand Up @@ -53,6 +54,31 @@ impl Document {
pub fn get_import_by_src(&self, src: &str) -> Option<&Import> {
self.get_imports().into_iter().find(|imp| &imp.path == src)
}
pub fn get_export(&self, name: &str) -> Option<ExpressionWrapper> {
for child in &self.body {
match child.get_inner() {
document_body_item::Inner::Component(component) => {
if component.name == name {
return Some(component.into());
}
}
document_body_item::Inner::Style(style) => {
if let Some(style_name) = &style.name {
if style_name == name {
return Some(style.into());
}
}
}
document_body_item::Inner::Atom(atom) => {
if atom.name == name {
return Some(atom.into());
}
}
_ => {}
}
}
None
}
pub fn get_component_by_name(&self, name: &str) -> Option<&Component> {
self.get_components()
.into_iter()
Expand Down
10 changes: 10 additions & 0 deletions libs/proto/src/ast/shared.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";
import "ast/base.proto";

package ast.shared;

message Reference {
string id = 1;
ast.base.Range range = 2;
repeated string path = 3;
}
39 changes: 39 additions & 0 deletions libs/proto/src/ast/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use super::{
all::ExpressionWrapper,
graph::{Dependency, Graph},
};

include!(concat!(env!("OUT_DIR"), "/ast.shared.rs"));

impl Reference {
/**
* Follows reference to source
*/
pub fn follow<'a>(&'a self, graph: &'a Graph) -> Option<(ExpressionWrapper, &'a Dependency)> {
let (_, ref_dep) = graph.get_expr_dep(&self.id).expect("Dependency must exist");

let source = if self.path.len() == 2 {
let namespace = self.path.get(0).expect("Namespace must exist");
let name = self.path.get(1).expect("Name must exist");

if let Some(ref_dep) = ref_dep.resolve_import_from_ns(namespace, graph) {
Some((name.clone(), ref_dep))
} else {
None
}
} else if self.path.len() == 1 {
let name = self.path.get(0).expect("Name must exist");
Some((name.clone(), ref_dep))
} else {
None
};

if let Some((name, dep)) = source {
dep.get_document()
.get_export(&name)
.and_then(|expr| Some((expr, dep)))
} else {
None
}
}
}
7 changes: 7 additions & 0 deletions libs/proto/src/ast_mutate/mod.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ message Mutation {
SetId set_id = 16;
PasteExpression paste_expression = 21;
MoveExpressionToFile move_expression_to_file = 26;
AddImport add_import = 27;

// Instance
ToggleInstanceVariant toggle_instance_variant = 13;
Expand All @@ -51,6 +52,12 @@ message Mutation {
}
}


message AddImport {
string ns = 1;
string path = 2;
}

message WrapInElement {
string target_id = 1;
}
Expand Down
Loading

0 comments on commit 675fb5c

Please sign in to comment.