Skip to content

Commit

Permalink
feat(parser): add simple layout syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 13, 2021
1 parent 521187e commit 23d1688
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
20 changes: 20 additions & 0 deletions quake_core/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct SourceUnit(pub Vec<SourceUnitPart>);
pub enum SourceUnitPart {
Action(ActionDecl),
Transflow(TransflowDecl),
SimpleLayout(SimpleLayoutDecl),
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -13,6 +14,25 @@ pub struct TransflowDecl {
pub(crate) flows: Vec<TransflowEnum>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SimpleLayoutDecl {
pub(crate) name: String,
pub(crate) rows: Vec<LayoutColumn>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LayoutColumn {
pub(crate) components: Vec<LayoutComponent>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LayoutComponent {
pub(crate) name: String,
pub(crate) is_empty: bool,
pub(crate) flow: Option<String>,
pub(crate) size: i32,
}

impl Default for TransflowDecl {
fn default() -> Self {
TransflowDecl {
Expand Down
26 changes: 21 additions & 5 deletions quake_core/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn parse(text: &str) -> Result<SourceUnit, Box<dyn Error>> {
let pairs = match QuakeParser::parse(Rule::earth, text) {
Ok(pairs) => pairs,
Err(e) => {
let string = format!("{:?}", e);
let string = format!("{:}", e);
return Err(Box::new(QuakeParserError::new(&*string)));
}
};
Expand Down Expand Up @@ -86,7 +86,7 @@ fn midway(decl: Pair<Rule>) -> Midway {
Rule::parameter => {
midway.end = value(pair);
}
Rule::from | Rule::to | Rule::left_bracket | Rule::right_bracket => {}
Rule::from | Rule::to | Rule::lbracket | Rule::rbracket => {}
Rule::filter_expr => {
for inner in pair.into_inner() {
match inner.as_rule() {
Expand Down Expand Up @@ -122,7 +122,7 @@ fn endway(decl: Pair<Rule>) -> Endway {
}
}
}
Rule::from | Rule::to | Rule::left_bracket | Rule::right_bracket => {}
Rule::from | Rule::to | Rule::lbracket | Rule::rbracket => {}
Rule::filter_expr => {
for inner in pair.into_inner() {
match inner.as_rule() {
Expand Down Expand Up @@ -177,8 +177,8 @@ fn parameters(decl: Pair<Rule>) -> Vec<Parameter> {

params.push(param)
}
Rule::left_bracket => {}
Rule::right_bracket => {}
Rule::lbracket => {}
Rule::rbracket => {}
_ => {
println!("{}", pair);
}
Expand Down Expand Up @@ -284,4 +284,20 @@ mod tests {
}
}
}

#[test]
fn should_parse_simple_layout() {
let unit = parse(
"layout Dashboard {
--------------------------
| Calendar(flow(\"show_calendar\"), 12x) |
--------------------------
| Empty(2x) | Timeline(flow(\"show_timeline\"), 8x) | Empty(2x) |
--------------------------
}",
)
.unwrap();

println!("{:?}", unit);
}
}
37 changes: 31 additions & 6 deletions quake_core/src/parser/quake.pest
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
earth = {
action_decl
| transflow_decl
| layout_decl
}

action_decl = {
Expand All @@ -16,15 +17,15 @@ transflow_expr = {
}

midway = {
from ~ parameters ~ "." ~ to ~ left_bracket ~ parameter ~ right_bracket ~ filter_expr?
from ~ parameters ~ "." ~ to ~ lbracket ~ parameter ~ rbracket ~ filter_expr?
}

endway = {
from ~ parameters ~ "." ~ to ~ left_bracket ~ component_decl ~ right_bracket ~ filter_expr?
from ~ parameters ~ "." ~ to ~ lbracket ~ component_decl ~ rbracket ~ filter_expr?
}

filter_expr = {
"." ~ filter_str ~ left_bracket ~ (double_quoted_string |single_quoted_string) ~ right_bracket
"." ~ filter_str ~ lbracket ~ (double_quoted_string |single_quoted_string) ~ rbracket
}

filter_str = { "filter" | "FILTER" }
Expand All @@ -41,17 +42,41 @@ from = { "from" }
to = { "to" }

parameters = {
left_bracket ~ parameter ~ ("," ~ parameter)* ~ right_bracket
lbracket ~ parameter ~ ("," ~ parameter)* ~ rbracket
}

left_bracket = {
lbracket = {
"(" | "("
}

right_bracket = {
rbracket = {
")" | ")"
}

layout_decl = {
"layout" ~ ident ~ "{" ~ flex_child* ~ "}"
}

flex_child = {
"-" ~ "-"*
| ("|" ~ component_use_decl) ~ ("|" ~ component_use_decl)* ~ "|"
}

component_use_decl = {
sized_empty_comp
| use_name ~ lbracket ~ call_flow ~ rbracket
}

sized_empty_comp = {
("EMPTY" | "empty" | "Empty") ~ lbracket ~ digits ~ ("x" | "X") ~ rbracket
}

call_flow = {
"flow" ~ lbracket ~ string ~ rbracket ~ ("," ~ digits ~ ("x" | "X"))?
}

use_name = { ident }

parameter = {
digits | single_quoted_string | double_quoted_string
}
Expand Down
1 change: 1 addition & 0 deletions quake_core/src/parser/quake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub fn quake(text: &str) -> Result<QuakeIt, Box<dyn Error>> {
let transflow = build_transflow(decl);
quakes.transflows.push(transflow);
}
SourceUnitPart::SimpleLayout(_) => {}
}
}

Expand Down

0 comments on commit 23d1688

Please sign in to comment.