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

Css3 parser tryout #286

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ harness = false
phf = { version = "0.11.2", features = ["macros"] }
derive_more = "0.99"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = {version = "1.0", features = ["preserve_order"]}
serde_derive = "1.0"
regex = "1"
lazy_static = "1.4"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.SILENT:

SHELL=/usr/bin/env bash -O globstar
#SHELL=/usr/bin/env bash -O globstar

all: help

Expand Down
6 changes: 6 additions & 0 deletions src/css3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod ast;
mod tokenizer;
mod nom;
mod unicode;
mod span;
mod parser;
40 changes: 40 additions & 0 deletions src/css3/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use core::fmt::{Debug, Formatter};
use std::collections::HashMap;
use serde_derive::Serialize;
use serde_json::json;

/// For now an AST does not contain different node kinds, but a single node that is distinguished
/// by the name. This will change in the near future.

#[derive(Clone, Serialize)]
pub struct Node {
pub name: String,
pub attributes: HashMap<String, String>,
pub children: Vec<Node>,
}

impl Node {
pub(crate) fn with_attribute(&self, key: &str, val: String) -> Self {
let mut node = self.clone();
node.attributes.insert(key.to_string(), val);
node
}
}

impl Node {
pub fn new(name: &str) -> Node {
// trace!("Creating node: {}", name);
Node {
name: name.to_string(),
attributes: HashMap::new(),
children: Vec::new(),
}
}
}

impl Debug for Node {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let obj = json!(self);
write!(f, "{}", serde_json::to_string_pretty(&obj).unwrap())
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want a simple unit test to work against, e.g., here. I would not attempt this purely from first principles, myself.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind. I see you have a unit test down below.

120 changes: 0 additions & 120 deletions src/css3/mod.rs

This file was deleted.

Loading
Loading