Skip to content

Commit 2749309

Browse files
committed
refactor some syntax; gain little perf via Rc; tag 0.1.27
1 parent eb59cce commit 2749309

File tree

6 files changed

+49
-48
lines changed

6 files changed

+49
-48
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ wasm-example/node_modules/
66
wasm-example/pkg/
77

88
wasm-example/dist/
9+
10+
/profile.json

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cirru_parser"
3-
version = "0.1.26"
3+
version = "0.1.27"
44
authors = ["jiyinyiyong <[email protected]>"]
55
edition = "2021"
66
license = "MIT"

examples/demo.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ defn fib (n)
1717
"#,
1818
);
1919

20-
let large_demo = "/Users/chenyong/repo/calcit-lang/editor/compact.cirru";
20+
let large_demo = "/Users/chenyong/repo/calcit-lang/editor/calcit.cirru";
2121
// let large_demo = "/Users/chen/repo/calcit-lang/respo-calcit-workflow/js-out/program-ir.cirru";
2222
// let large_demo = "/Users/chen/repo/calcit-lang/calcit_runner.rs/js-out/program-ir.cirru";
2323
let content = fs::read_to_string(large_demo).unwrap();
2424

2525
match parse(&content) {
2626
Ok(v) => {
2727
let writer_options = CirruWriterOptions { use_inline: false };
28-
println!("{}", format(&v, writer_options).unwrap());
28+
let t = format(&v, writer_options).unwrap();
29+
println!("{}", t.len());
2930
}
3031
Err(e) => println!("{:?}", e),
3132
}

src/parser.rs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -57,47 +57,45 @@ fn build_exprs(tokens: &[CirruLexItem]) -> Result<Vec<Cirru>, String> {
5757
loop {
5858
let chunk = pull_token();
5959

60-
if chunk.is_none() {
61-
return Ok(acc);
62-
}
63-
match chunk.unwrap() {
64-
CirruLexItem::Open => {
65-
let mut pointer: Vec<Cirru> = vec![];
66-
// guess a nested level of 16
67-
let mut pointer_stack: Vec<Vec<Cirru>> = Vec::with_capacity(16);
68-
loop {
69-
let cursor = pull_token();
70-
if cursor.is_none() {
71-
return Err(String::from("unexpected end of file"));
72-
}
73-
match cursor.unwrap() {
74-
CirruLexItem::Close => {
75-
if pointer_stack.is_empty() {
76-
acc.push(Cirru::List(pointer.to_owned()));
77-
break;
78-
} else {
79-
let v = pointer_stack.pop();
80-
let prev_p = pointer;
81-
match v {
82-
Some(collected) => {
83-
pointer = collected;
84-
pointer.push(Cirru::List(prev_p))
60+
match &chunk {
61+
None => return Ok(acc),
62+
Some(ck) => {
63+
match ck {
64+
CirruLexItem::Open => {
65+
let mut pointer: Vec<Cirru> = vec![];
66+
// guess a nested level of 16
67+
let mut pointer_stack: Vec<Vec<Cirru>> = Vec::with_capacity(16);
68+
loop {
69+
let cursor = pull_token();
70+
71+
match &cursor {
72+
None => return Err(String::from("unexpected end of file")),
73+
Some(c) => match c {
74+
CirruLexItem::Close => match pointer_stack.pop() {
75+
None => {
76+
acc.push(Cirru::List(pointer));
77+
break;
78+
}
79+
Some(v) => {
80+
let prev_p = pointer;
81+
pointer = v;
82+
pointer.push(Cirru::List(prev_p));
83+
}
84+
},
85+
CirruLexItem::Open => {
86+
pointer_stack.push(pointer);
87+
pointer = vec![];
8588
}
86-
None => return Err(String::from("unknown close item")),
87-
}
89+
CirruLexItem::Str(s) => pointer.push(Cirru::Leaf((**s).into())),
90+
CirruLexItem::Indent(n) => return Err(format!("unknown indent: {}", n)),
91+
},
8892
}
8993
}
90-
CirruLexItem::Open => {
91-
pointer_stack.push(pointer);
92-
pointer = vec![];
93-
}
94-
CirruLexItem::Str(s) => pointer.push(Cirru::Leaf(s.as_str().into())),
95-
CirruLexItem::Indent(n) => return Err(format!("unknown indent: {}", n)),
9694
}
95+
CirruLexItem::Close => return Err(String::from("unexpected \")\"")),
96+
a => return Err(format!("unknown item: {:?}", a)),
9797
}
9898
}
99-
CirruLexItem::Close => return Err(String::from("unexpected \")\"")),
100-
a => return Err(format!("unknown item: {:?}", a)),
10199
}
102100
}
103101
}
@@ -151,28 +149,28 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
151149
},
152150
CirruLexState::Token => match c {
153151
' ' => {
154-
acc.push(CirruLexItem::Str(buffer));
152+
acc.push(CirruLexItem::Str(buffer.into()));
155153
state = CirruLexState::Space;
156154
buffer = String::from("");
157155
}
158156
'"' => {
159-
acc.push(CirruLexItem::Str(buffer));
157+
acc.push(CirruLexItem::Str(buffer.into()));
160158
state = CirruLexState::Str;
161159
buffer = String::from("");
162160
}
163161
'\n' => {
164-
acc.push(CirruLexItem::Str(buffer));
162+
acc.push(CirruLexItem::Str(buffer.into()));
165163
state = CirruLexState::Indent;
166164
buffer = String::from("");
167165
}
168166
'(' => {
169-
acc.push(CirruLexItem::Str(buffer));
167+
acc.push(CirruLexItem::Str(buffer.into()));
170168
acc.push(CirruLexItem::Open);
171169
state = CirruLexState::Space;
172170
buffer = String::from("")
173171
}
174172
')' => {
175-
acc.push(CirruLexItem::Str(buffer));
173+
acc.push(CirruLexItem::Str(buffer.into()));
176174
acc.push(CirruLexItem::Close);
177175
state = CirruLexState::Space;
178176
buffer = String::from("")
@@ -184,7 +182,7 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
184182
},
185183
CirruLexState::Str => match c {
186184
'"' => {
187-
acc.push(CirruLexItem::Str(buffer));
185+
acc.push(CirruLexItem::Str(buffer.into()));
188186
state = CirruLexState::Space;
189187
buffer = String::from("");
190188
}
@@ -270,7 +268,7 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
270268
match state {
271269
CirruLexState::Space => Ok(acc),
272270
CirruLexState::Token => {
273-
acc.push(CirruLexItem::Str(buffer));
271+
acc.push(CirruLexItem::Str(buffer.into()));
274272
Ok(acc)
275273
}
276274
CirruLexState::Escape => Err(String::from("unknown escape")),

src/primes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use bincode::{Decode, Encode};
2-
use std::clone::Clone;
32
use std::fmt;
43
use std::hash::Hash;
54
use std::str;
65
use std::sync::Arc;
6+
use std::{clone::Clone, rc::Rc};
77

88
#[cfg(feature = "use-serde")]
99
use serde::{
@@ -206,7 +206,7 @@ pub enum CirruLexItem {
206206
Close,
207207
// supposed to be enough with indentation of 255
208208
Indent(u8),
209-
Str(String),
209+
Str(Rc<str>),
210210
}
211211

212212
impl From<&str> for CirruLexItem {

0 commit comments

Comments
 (0)