Skip to content

Commit b73a896

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/main'
2 parents d2e271c + a58eeb1 commit b73a896

File tree

6 files changed

+196
-88
lines changed

6 files changed

+196
-88
lines changed

app/browser-rs/src/default_page.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
pub const DEFAULT_PAGE: &str = r#"<html>
2+
<head>
3+
<style>
4+
.leaf {
5+
background-color: green;
6+
height: 5;
7+
width: 5;
8+
}
9+
#leaf1 {
10+
margin-top: 50;
11+
margin-left: 275;
12+
}
13+
#leaf2 {
14+
margin-left: 270;
15+
}
16+
#leaf3 {
17+
margin-left: 265;
18+
}
19+
#id2 {
20+
background-color: orange;
21+
height: 20;
22+
width: 30;
23+
margin-left: 250;
24+
}
25+
#id3 {
26+
background-color: lightgray;
27+
height: 30;
28+
width: 80;
29+
margin-top: 3;
30+
margin-left: 225;
31+
}
32+
#id4 {
33+
background-color: lightgray;
34+
height: 30;
35+
width: 100;
36+
margin-top: 3;
37+
margin-left: 215;
38+
}
39+
</style>
40+
</head>
41+
<body>
42+
<div class=leaf id=leaf1></div>
43+
<div class=leaf id=leaf2></div>
44+
<div class=leaf id=leaf3></div>
45+
<div id=id2></div>
46+
<div id=id3></div>
47+
<div id=id4></div>
48+
</body>
49+
</html>"#;

app/browser-rs/src/main.rs

+3-51
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![no_main]
33
#![feature(exclusive_range_pattern)]
44

5+
mod default_page;
56
mod gui;
67
mod http;
78
mod net;
@@ -13,6 +14,7 @@ extern crate alloc;
1314
use alloc::string::ToString;
1415
use liumlib::*;
1516

17+
use crate::default_page::DEFAULT_PAGE;
1618
use crate::gui::ApplicationWindow;
1719
use crate::net::udp_response;
1820
use crate::renderer::render;
@@ -54,57 +56,7 @@ fn main() {
5456
app.initialize();
5557

5658
if default {
57-
let default_page = r#"<html>
58-
<head>
59-
<style>
60-
.leaf {
61-
background-color: green;
62-
height: 5;
63-
width: 5;
64-
}
65-
#leaf1 {
66-
margin-top: 50;
67-
margin-left: 275;
68-
}
69-
#leaf2 {
70-
margin-left: 270;
71-
}
72-
#leaf3 {
73-
margin-left: 265;
74-
}
75-
#id2 {
76-
background-color: orange;
77-
height: 20;
78-
width: 30;
79-
margin-left: 250;
80-
}
81-
#id3 {
82-
background-color: lightgray;
83-
height: 30;
84-
width: 80;
85-
margin-top: 3;
86-
margin-left: 225;
87-
}
88-
#id4 {
89-
background-color: lightgray;
90-
height: 30;
91-
width: 100;
92-
margin-top: 3;
93-
margin-left: 215;
94-
}
95-
</style>
96-
</head>
97-
<body>
98-
<div class=leaf id=leaf1></div>
99-
<div class=leaf id=leaf2></div>
100-
<div class=leaf id=leaf3></div>
101-
<div id=id2></div>
102-
<div id=id3></div>
103-
<div id=id4></div>
104-
</body>
105-
</html>"#;
106-
107-
render(default_page.to_string(), &app);
59+
render(DEFAULT_PAGE.to_string(), &app);
10860
} else {
10961
let parsed_url = ParsedUrl::new(url.to_string());
11062
println!("parsed_url: {:?}", parsed_url);

app/browser-rs/src/renderer/css/token.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use liumlib::*;
1111

1212
#[derive(Debug, Clone, PartialEq, Eq)]
1313
/// https://www.w3.org/TR/css-syntax-3/#consume-token
14+
/// https://www.w3.org/TR/css-syntax-3/#tokenization
1415
pub enum CssToken {
1516
/// https://www.w3.org/TR/css-syntax-3/#typedef-hash-token
1617
HashToken(String),

app/browser-rs/src/url.rs

+65-35
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,88 @@
1+
//! RFC 1738 - Uniform Resource Locators (URL): https://datatracker.ietf.org/doc/html/rfc1738
2+
//! RFC 3986 - Uniform Resource Identifier (URI): https://datatracker.ietf.org/doc/html/rfc3986
3+
14
use alloc::string::String;
25
use alloc::string::ToString;
36
use alloc::vec::Vec;
47

8+
#[derive(Debug)]
9+
enum Protocol {
10+
Http,
11+
}
12+
13+
impl Protocol {
14+
fn to_string(&self) -> String {
15+
match self {
16+
Protocol::Http => String::from("http"),
17+
}
18+
}
19+
}
20+
521
#[derive(Debug)]
622
pub struct ParsedUrl {
7-
scheme: String,
23+
scheme: Protocol,
824
pub host: String,
925
pub port: u16,
1026
pub path: String,
1127
}
1228

1329
impl ParsedUrl {
14-
pub fn new(u: String) -> Self {
15-
let url;
16-
let supported_protocol = "http://";
17-
if u.starts_with(supported_protocol) {
18-
url = u.split_at(supported_protocol.len()).1.to_string();
30+
fn extract_scheme(url: &String) -> Protocol {
31+
let splitted_url: Vec<&str> = url.split("://").collect();
32+
if splitted_url.len() == 2 && splitted_url[0] == Protocol::Http.to_string() {
33+
Protocol::Http
34+
} else if splitted_url.len() == 1 {
35+
// No scheme. Set "HTTP" as a default behavior.
36+
Protocol::Http
1937
} else {
20-
url = u;
38+
panic!("unsupported scheme: {}", url);
2139
}
40+
}
41+
42+
fn remove_scheme(url: &String, scheme: Protocol) -> String {
43+
// Remove "scheme://" from url if any.
44+
url.replacen(&(scheme.to_string() + "://"), "", 1)
45+
}
46+
47+
fn extract_host(url: &String) -> String {
48+
let splitted_url: Vec<&str> = url.splitn(2, '/').collect();
49+
splitted_url[0].to_string()
50+
}
2251

23-
let mut host = String::new();
24-
let mut path = String::new();
25-
{
26-
let v: Vec<&str> = url.splitn(2, '/').collect();
27-
if v.len() == 2 {
28-
host.push_str(v[0]);
29-
path.push_str("/");
30-
path.push_str(v[1]);
31-
} else if v.len() == 1 {
32-
host.push_str(v[0]);
33-
path.push_str("/index.html");
34-
} else {
35-
panic!("invalid url {}", url);
36-
}
52+
fn extract_path(url: &String) -> String {
53+
let splitted_url: Vec<&str> = url.splitn(2, '/').collect();
54+
if splitted_url.len() == 2 {
55+
splitted_url[1].to_string()
56+
} else {
57+
// There is no path in URL so set an empty string as a default value.
58+
String::from("")
3759
}
60+
}
3861

39-
let port;
40-
{
41-
let v: Vec<&str> = host.splitn(2, ':').collect();
42-
if v.len() == 2 {
43-
port = v[1].parse::<u16>().unwrap();
44-
} else if v.len() == 1 {
45-
port = 8888;
46-
} else {
47-
panic!("invalid host in url {}", host);
48-
}
62+
fn extract_port(host: &String) -> u16 {
63+
let splitted_host: Vec<&str> = host.splitn(2, ':').collect();
64+
if splitted_host.len() == 2 {
65+
splitted_host[1].parse::<u16>().unwrap()
66+
} else {
67+
// There is no port number in URL so set 8888 as a default value.
68+
80
4969
}
70+
}
71+
72+
pub fn new(original_url: String) -> Self {
73+
let scheme = Self::extract_scheme(&original_url);
74+
let url = Self::remove_scheme(&original_url, scheme);
75+
76+
let host = Self::extract_host(&url);
77+
let path = Self::extract_path(&url);
78+
79+
let port = Self::extract_port(&host);
5080

5181
Self {
52-
scheme: String::from("http"),
53-
host: host,
54-
port: port,
55-
path: path,
82+
scheme: Protocol::Http,
83+
host,
84+
port,
85+
path,
5686
}
5787
}
5888
}

app/browser-rs/window.bmp

512 KB
Binary file not shown.

app/saji/tests/saji.rs

+78-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
extern crate alloc;
88

99
use crate::alloc::string::ToString;
10+
use alloc::rc::Rc;
1011
use alloc::vec::Vec;
12+
use core::borrow::Borrow;
1113
use liumlib::*;
12-
use saji::ast::Parser;
14+
use saji::ast::{Node, Parser};
1315
use saji::runtime::{Runtime, RuntimeValue, Variable};
1416
use saji::token::Lexer;
1517

@@ -47,7 +49,9 @@ macro_rules! run_test {
4749
let mut parser = Parser::new(lexer);
4850
let ast = parser.parse_ast();
4951
println!("---------------------------------");
50-
println!("ast {:?}", ast);
52+
for n in ast.body() {
53+
print_ast(&Some(n.clone()), 0);
54+
}
5155
println!("---------------------------------");
5256

5357
let mut runtime = Runtime::new();
@@ -66,6 +70,78 @@ macro_rules! run_test {
6670
};
6771
}
6872

73+
fn print_ast(node: &Option<Rc<Node>>, depth: usize) {
74+
let n = match node {
75+
Some(n) => n,
76+
None => return,
77+
};
78+
79+
match n.borrow() {
80+
Node::ExpressionStatement(stmt) => {
81+
println!("{}ExpressionStatement", " ".repeat(depth));
82+
print_ast(&stmt, depth + 1);
83+
}
84+
Node::BlockStatement { body } => {
85+
println!("{}BlockStatement", " ".repeat(depth));
86+
for node in body {
87+
print_ast(&node, depth + 1);
88+
}
89+
}
90+
Node::ReturnStatement { argument } => {
91+
println!("{}ReturnStatement", " ".repeat(depth));
92+
print_ast(&argument, depth + 1);
93+
}
94+
Node::FunctionDeclaration { id, params, body } => {
95+
println!("{}FunctionDeclaration", " ".repeat(depth));
96+
print_ast(&id, depth + 1);
97+
for param in params {
98+
print_ast(&param, depth + 1);
99+
}
100+
print_ast(&body, depth + 1);
101+
}
102+
Node::VariableDeclaration { declarations } => {
103+
println!("{}VariableDeclaration", " ".repeat(depth));
104+
for decl in declarations {
105+
print_ast(&decl, depth + 1);
106+
}
107+
}
108+
Node::VariableDeclarator { id, init } => {
109+
println!("{}VariableDeclarator", " ".repeat(depth));
110+
print_ast(&id, depth + 1);
111+
print_ast(&init, depth + 1);
112+
}
113+
Node::BinaryExpression {
114+
operator,
115+
left,
116+
right,
117+
} => {
118+
print!("{}", " ".repeat(depth));
119+
println!("BinaryExpression: {}", operator);
120+
print_ast(&left, depth + 1);
121+
print_ast(&right, depth + 1);
122+
}
123+
Node::CallExpression { callee, arguments } => {
124+
println!("{}CallExpression", " ".repeat(depth));
125+
print_ast(&callee, depth + 1);
126+
for arg in arguments {
127+
print_ast(&arg, depth + 1);
128+
}
129+
}
130+
Node::Identifier(i) => {
131+
print!("{}", " ".repeat(depth));
132+
println!("Identifier: {}", i);
133+
}
134+
Node::NumericLiteral(n) => {
135+
print!("{}", " ".repeat(depth));
136+
println!("NumericLiteral: {}", n);
137+
}
138+
Node::StringLiteral(s) => {
139+
print!("{}", " ".repeat(depth));
140+
println!("StringLiteral: {}", s);
141+
}
142+
}
143+
}
144+
69145
#[cfg(test)]
70146
entry_point!(main);
71147
#[cfg(test)]

0 commit comments

Comments
 (0)