Skip to content

Commit 67367e3

Browse files
author
=
committed
feat: added css property
1 parent 0fb253e commit 67367e3

File tree

6 files changed

+76
-8
lines changed

6 files changed

+76
-8
lines changed

build/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/main.ln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@ App {
22
uses: [
33
"./other.ln"
44
],
5+
css: {
6+
display: "flex"
7+
justify-content: "center"
8+
flex-direction: "column"
9+
},
510
class: "this is my custom class",
611
id: "this is my custom id",
712

8-
h1 { "can we parse the component above?" }
13+
14+
h1 {
15+
css: {
16+
text-align: "center"
17+
background: "grey"
18+
color: "green"
19+
}
20+
21+
"can we parse the component above?"
22+
}
923
Other {}
1024
Other {}
1125

example/other.ln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Other {
22
uses: ["./hello.ln"],
33
class: "pretty_component",
4+
css: {
5+
width: "100%"
6+
height: "100%"
7+
background: "blue"
8+
}
49

510

611
h1 { "this is another component that i am using" }

magic/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct Component {
3535
pub tag: String,
3636
pub children: Vec<Component>,
3737
pub value: String,
38-
// pub info: HashMap<String, Value>, // store css and attributes refactor later
38+
pub info: HashMap<String, Value>, // store css and attributes refactor later
3939
}
4040

4141
#[derive(Debug, Clone)]
@@ -78,6 +78,12 @@ impl El {
7878
let _ = self.append_child(&e);
7979
self
8080
}
81+
82+
// this implementation is subjct to change
83+
pub fn css(self, css: &str) -> Self {
84+
let _ = self.set_attribute("style", css);
85+
self
86+
}
8187
}
8288

8389
fn tagify(tag: &str) -> &str {
@@ -97,6 +103,15 @@ fn create_dom_from_ir(root: Component) -> El {
97103
e = e.clone().child(create_dom_from_ir(c));
98104
});
99105
}
106+
107+
if root.info.contains_key("css") {
108+
let styles = match root.info.get("css").unwrap() {
109+
Value::STRING(x) => x,
110+
_ => "",
111+
};
112+
113+
e = e.css(styles);
114+
}
100115
e
101116
}
102117

src/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Lexer {
6363
fn read_identifier(&mut self) -> String {
6464
let position = self.position; // starting point
6565
// read the full identifier
66-
while self.ch.is_alphanumeric() {
66+
while self.ch.is_alphanumeric() || self.ch == '-' || self.ch == '_' {
6767
self.read_char()
6868
}
6969
self.input[position..self.position].to_string()

src/parser.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::{
55
lexer::Lexer,
66
token::{Token, TokenType},
77
};
8-
use serde;
98

109
#[derive(Debug, Default)]
1110
pub struct Parser {
@@ -152,10 +151,16 @@ impl Parser {
152151
fn parse_component_info(&mut self) -> HashMap<String, Value> {
153152
let mut vec_of_info = HashMap::<String, Value>::new();
154153
while self.peek_token_is(TokenType::COLON) {
154+
println!("{:?}", self.current_token);
155155
let key = self.current_token.literal.clone();
156+
156157
self.next_token(); // move to the colon
157158
self.next_token(); // move to the starting of the value for the key
158-
159+
if key == "css" {
160+
let v = self.parse_css();
161+
vec_of_info.insert(key, v);
162+
continue;
163+
}
159164
// for now it can be a string or an list of strings
160165
let value: Value = match self.current_token.tokentype {
161166
TokenType::STRING => {
@@ -183,6 +188,35 @@ impl Parser {
183188
vec_of_info
184189
}
185190

191+
// TODO: Change this implementation later
192+
fn parse_css(&mut self) -> Value {
193+
if self.cur_token_is(TokenType::LBRACE) {
194+
self.next_token();
195+
}
196+
let mut vec_of_css = Vec::<String>::new();
197+
while self.peek_token_is(TokenType::COLON) {
198+
let key = self.current_token.literal.clone();
199+
self.next_token(); // move to colon
200+
self.next_token(); // move to the value
201+
println!("css::::{} {:?}", key, self.current_token);
202+
if !self.cur_token_is(TokenType::STRING) {
203+
self.errors
204+
.push("parse_css: CSS values must be a string".to_string());
205+
}
206+
let v = self.current_token.literal.clone();
207+
self.next_token();
208+
209+
vec_of_css.push(format!("{}: {}", key, v));
210+
}
211+
if self.cur_token_is(TokenType::RBRACE) {
212+
self.next_token();
213+
}
214+
if self.cur_token_is(TokenType::COMMA) {
215+
self.next_token();
216+
}
217+
Value::STRING(vec_of_css.join(";"))
218+
}
219+
186220
fn parse_vec_of_token(&mut self, tk: TokenType) -> Value {
187221
let mut vec_of_tk = Vec::<Value>::new();
188222
while !self.cur_token_is(TokenType::RSQBRACE) {

0 commit comments

Comments
 (0)