Skip to content

Commit

Permalink
feat(parser): add parse for layout
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 13, 2021
1 parent c5ae8ef commit a9bbb9f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
23 changes: 20 additions & 3 deletions quake_core/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ pub struct SimpleLayoutDecl {
pub(crate) rows: Vec<LayoutColumn>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LayoutColumn {
pub(crate) components: Vec<LayoutComponent>,
impl Default for SimpleLayoutDecl {
fn default() -> Self {
Self {
name: "".to_string(),
rows: vec![],
}
}
}

pub type LayoutColumn = Vec<LayoutComponent>;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LayoutComponent {
pub(crate) name: String,
Expand All @@ -33,6 +39,17 @@ pub struct LayoutComponent {
pub(crate) size: i32,
}

impl Default for LayoutComponent {
fn default() -> Self {
Self {
name: "".to_string(),
is_empty: false,
flow: None,
size: 0,
}
}
}

impl Default for TransflowDecl {
fn default() -> Self {
TransflowDecl {
Expand Down
67 changes: 65 additions & 2 deletions quake_core/src/parser/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::parser::ast::{
ActionDecl, Endway, Midway, Parameter, SourceUnit, SourceUnitPart, TransflowDecl, TransflowEnum,
ActionDecl, Endway, LayoutComponent, Midway, Parameter, SimpleLayoutDecl, SourceUnit,
SourceUnitPart, TransflowDecl, TransflowEnum,
};
use crate::parser::errors::QuakeParserError;
use pest::iterators::Pair;
Expand Down Expand Up @@ -33,6 +34,9 @@ pub fn parse(text: &str) -> Result<SourceUnit, Box<dyn Error>> {
Rule::transflow_decl => {
parts.push(SourceUnitPart::Transflow(transflow_decl(inner_pair)));
}
Rule::layout_decl => {
parts.push(SourceUnitPart::SimpleLayout(layout_decl(inner_pair)));
}
_ => println!("rule: {}", inner_pair),
};
}
Expand All @@ -41,6 +45,65 @@ pub fn parse(text: &str) -> Result<SourceUnit, Box<dyn Error>> {
Ok(SourceUnit(parts))
}

fn layout_decl(decl: Pair<Rule>) -> SimpleLayoutDecl {
let mut layout = SimpleLayoutDecl::default();
for pair in decl.into_inner() {
let mut row = vec![];
match pair.as_rule() {
Rule::flex_child => {
row.append(&mut parse_flex_child(pair));
}
Rule::ident => {
layout.name = String::from(pair.as_str());
}
_ => {
println!("{}", pair);
}
}

layout.rows.push(row);
}

layout
}

fn parse_flex_child(decl: Pair<Rule>) -> Vec<LayoutComponent> {
let mut components = vec![];
for pair in decl.into_inner() {
if let Rule::component_use_decl = pair.as_rule() {
components.push(component_use_decl(pair));
}
}

components
}

fn component_use_decl(decl: Pair<Rule>) -> LayoutComponent {
let mut component = LayoutComponent::default();
for pair in decl.into_inner() {
match pair.as_rule() {
Rule::sized_empty_comp => {
component.is_empty = true;
component.name = "Empty".to_string();
for inner in pair.into_inner() {
match inner.as_rule() {
Rule::digits => {
component.size = inner.as_str().parse().unwrap();
}
_ => {}
}
}
}
Rule::component_flow => {}
_ => {
println!("{}", pair);
}
}
}

component
}

fn transflow_decl(decl: Pair<Rule>) -> TransflowDecl {
let mut transflow = TransflowDecl::default();
for pair in decl.into_inner() {
Expand Down Expand Up @@ -298,6 +361,6 @@ mod tests {
)
.unwrap();

println!("{:?}", unit);
assert_eq!(1, unit.0.len());
}
}
6 changes: 5 additions & 1 deletion quake_core/src/parser/quake.pest
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ flex_child = {

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

component_flow = {
use_name ~ lbracket ~ call_flow ~ rbracket
}

sized_empty_comp = {
Expand Down

0 comments on commit a9bbb9f

Please sign in to comment.