Skip to content

Commit bb4e8a6

Browse files
authored
Merge pull request #9 from Cirru/warning
handle warnings
2 parents e83100d + d3bfdb6 commit bb4e8a6

File tree

8 files changed

+91
-93
lines changed

8 files changed

+91
-93
lines changed

moon.mod.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "tiye/cirru-edn",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"deps": {
5-
"tiye/cirru-parser": "0.2.0"
5+
"tiye/cirru-parser": "0.2.1"
66
},
77
"readme": "README.md",
88
"repository": "https://github.com/Cirru/cirru-edn.mbt",

src/buffer.mbt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ struct EdnBufferView(Array[UInt]) derive(Eq)
33

44
///|
55
impl Hash for EdnBufferView with hash(self) {
6-
self.inner().length()
6+
self.0.length()
77
}
88

99
///|
1010
impl Hash for EdnBufferView with hash_combine(self, hasher) {
11-
for i in self.inner() {
11+
for i in self.0 {
1212
hasher.combine(i)
1313
}
1414
}
@@ -17,11 +17,11 @@ impl Hash for EdnBufferView with hash_combine(self, hasher) {
1717
impl Show for EdnBufferView with output(self, logger) {
1818
let mut s = ""
1919
s = s + "["
20-
for i = 0; i < self.inner().length(); i = i + 1 {
20+
for i = 0; i < self.0.length(); i = i + 1 {
2121
if i > 0 {
2222
s = s + ", "
2323
}
24-
s = s + self.inner()[i].to_string()
24+
s = s + self.0[i].to_string()
2525
}
2626
s = s + "]"
2727
logger.write_string(s)
@@ -32,16 +32,16 @@ impl Compare for EdnBufferView with compare(
3232
self : EdnBufferView,
3333
right : EdnBufferView,
3434
) -> Int {
35-
for i = 0; i < self.inner().length(); i = i + 1 {
36-
if i >= right.inner().length() {
35+
for i = 0; i < self.0.length(); i = i + 1 {
36+
if i >= right.0.length() {
3737
return 1
3838
}
39-
let ret = self.inner()[i].compare(right.inner()[i])
39+
let ret = self.0[i].compare(right.0[i])
4040
if ret != 0 {
4141
return ret
4242
}
4343
}
44-
if self.inner().length() < right.inner().length() {
44+
if self.0.length() < right.0.length() {
4545
return -1
4646
}
4747
0

src/lib.mbt

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ fn extract_cirru_edn(
4343
':' => Tag(try! s1[1:].to_string())
4444
'"' | '|' => Str(try! s1[1:].to_string())
4545
_ =>
46-
match
47-
(try? @strconv.parse_double(s1.trim(char_set=" ").to_string())) {
48-
Ok(f) => Number(f)
49-
Err(e) =>
46+
try
47+
@strconv.parse_double(s1.trim(char_set=" ").to_string())
48+
catch {
49+
e =>
5050
match e {
5151
@strconv.StrConvError(s) =>
5252
raise @strconv.StrConvError(
5353
"failed to parse number: " + s,
5454
)
5555
}
56+
} noraise {
57+
f => Number(f)
5658
}
5759
}
5860
}
@@ -86,7 +88,9 @@ fn extract_cirru_edn(
8688
None => raise @strconv.StrConvError("missing edn do value")
8789
Some(_v) => ()
8890
}
89-
ret.or_error(@strconv.StrConvError("missing edn do value"))
91+
ret.unwrap_or_error(
92+
@strconv.StrConvError("missing edn do value"),
93+
)
9094
}
9195
"atom" =>
9296
if xs.length() == 2 {
@@ -121,9 +125,10 @@ fn extract_cirru_edn(
121125
if is_comment(x) {
122126
continue
123127
}
124-
match (try? extract_cirru_edn(x)) {
125-
Ok(v) => ys.push(v)
126-
Err(v) => raise @strconv.StrConvError(v.to_string())
128+
try extract_cirru_edn(x) catch {
129+
v => raise @strconv.StrConvError(v.to_string())
130+
} noraise {
131+
v => ys.push(v)
127132
}
128133
}
129134
List(EdnListView(ys))
@@ -134,9 +139,10 @@ fn extract_cirru_edn(
134139
if is_comment(x) {
135140
continue
136141
}
137-
match (try? extract_cirru_edn(x)) {
138-
Ok(v) => ys.add(v)
139-
Err(v) => raise @strconv.StrConvError(v.to_string())
142+
try extract_cirru_edn(x) catch {
143+
v => raise @strconv.StrConvError(v.to_string())
144+
} noraise {
145+
v => ys.add(v)
140146
}
141147
}
142148
Edn::Set(EdnSetView(ys))
@@ -240,15 +246,16 @@ fn extract_cirru_edn(
240246
match x {
241247
Leaf(y) =>
242248
if y.length() == 2 {
243-
match (try? @strconv.parse_int(y)) {
244-
Ok(b) => ys.push(b.reinterpret_as_uint())
245-
Err(e) =>
249+
try @strconv.parse_int(y) catch {
250+
e =>
246251
match e {
247252
@strconv.StrConvError(s) =>
248253
raise @strconv.StrConvError(
249254
"expected length 2 hex string in buffer, got: \{y} \{s}",
250255
)
251256
}
257+
} noraise {
258+
b => ys.push(b.reinterpret_as_uint())
252259
}
253260
} else {
254261
raise @strconv.StrConvError(
@@ -289,7 +296,7 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru raise EdnCommonError {
289296
Bool(v) => Leaf(v.to_string())
290297
Number(n) => Leaf(n.to_string())
291298
Symbol(s) => Leaf("'\{s}")
292-
Tag(s) => Leaf(":" + s.inner())
299+
Tag(s) => Leaf(":" + s.0)
293300
Str(s) => Leaf("|" + s)
294301
Quote(v) => List([Leaf("quote"), v])
295302
List(xs) => {
@@ -303,7 +310,7 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru raise EdnCommonError {
303310
Set(xs) => {
304311
let ys : Array[@cirru_parser.Cirru] = []
305312
ys.push(@cirru_parser.Cirru::Leaf("#{}"))
306-
let items = xs.inner()
313+
let items = xs.0
307314
// items.sort()
308315
for x in items {
309316
ys.push(assemble_cirru_node(x))
@@ -313,7 +320,7 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru raise EdnCommonError {
313320
Map(xs) => {
314321
let ys : Array[@cirru_parser.Cirru] = []
315322
ys.push(@cirru_parser.Cirru::Leaf("{}"))
316-
let items = Array::from_iter(xs.inner().iter())
323+
let items = Array::from_iter(xs.0.iter())
317324
items.sort_by(fn(left, right) {
318325
let (a1, a2) = left
319326
let (b1, b2) = right
@@ -336,7 +343,7 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru raise EdnCommonError {
336343
Record({ tag: name, extra: entries }) => {
337344
let ys : Array[@cirru_parser.Cirru] = []
338345
ys.push(Leaf("%{}"))
339-
ys.push(Leaf(":" + name.inner()))
346+
ys.push(Leaf(":" + name.0))
340347
let ordered_entries = entries
341348
ordered_entries.sort_by(fn(left, right) {
342349
let (_a1, a2) = left
@@ -349,7 +356,7 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru raise EdnCommonError {
349356
})
350357
for entry in ordered_entries {
351358
let v = entry.1
352-
ys.push(List([Leaf(":" + entry.0.inner()), assemble_cirru_node(v)]))
359+
ys.push(List([Leaf(":" + entry.0.0), assemble_cirru_node(v)]))
353360
}
354361
List(ys)
355362
}
@@ -366,7 +373,7 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru raise EdnCommonError {
366373
Buffer(buf) => {
367374
let ys : Array[@cirru_parser.Cirru] = []
368375
ys.push(Leaf("buf"))
369-
for b in buf.inner() {
376+
for b in buf.0 {
370377
ys.push(Leaf(BigInt::from_int(b.reinterpret_as_int()).to_hex()))
371378
}
372379
List(ys)

src/list.mbt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ struct EdnListView(Array[Edn]) derive(Eq)
33

44
///|
55
impl Hash for EdnListView with hash(self) {
6-
self.inner().length()
6+
self.0.length()
77
}
88

99
///|
1010
impl Hash for EdnListView with hash_combine(self, hasher) {
11-
for i in self.inner() {
11+
for i in self.0 {
1212
i.hash_combine(hasher)
1313
}
1414
}
@@ -17,73 +17,73 @@ impl Hash for EdnListView with hash_combine(self, hasher) {
1717
impl Show for EdnListView with output(self, logger) {
1818
let mut s = ""
1919
s = s + "(list )"
20-
for i = 0; i < self.inner().length(); i = i + 1 {
20+
for i = 0; i < self.0.length(); i = i + 1 {
2121
if i > 0 {
2222
s = s + ", "
2323
}
24-
s = s + self.inner()[i].to_string()
24+
s = s + self.0[i].to_string()
2525
}
2626
s = s + ")"
2727
logger.write_string(s)
2828
}
2929

3030
///|
3131
impl Compare for EdnListView with compare(self, right) -> Int {
32-
for i = 0; i < self.inner().length(); i = i + 1 {
33-
if i >= right.inner().length() {
32+
for i = 0; i < self.0.length(); i = i + 1 {
33+
if i >= right.0.length() {
3434
return 1
3535
}
36-
let ret = self.inner()[i].compare(right.inner()[i])
36+
let ret = self.0[i].compare(right.0[i])
3737
if ret != 0 {
3838
return ret
3939
}
4040
}
41-
if self.inner().length() < right.inner().length() {
41+
if self.0.length() < right.0.length() {
4242
return -1
4343
}
4444
0
4545
}
4646

4747
///|
4848
pub fn EdnListView::get(self : EdnListView, idx : UInt) -> Edn? {
49-
if idx < self.inner().length().reinterpret_as_uint() {
50-
Some(self.inner()[idx.reinterpret_as_int()])
49+
if idx < self.0.length().reinterpret_as_uint() {
50+
Some(self.0[idx.reinterpret_as_int()])
5151
} else {
5252
None
5353
}
5454
}
5555

5656
///|
5757
pub fn EdnListView::get_or_nil(self : EdnListView, idx : UInt) -> Edn {
58-
if idx < self.inner().length().reinterpret_as_uint() {
59-
self.inner()[idx.reinterpret_as_int()]
58+
if idx < self.0.length().reinterpret_as_uint() {
59+
self.0[idx.reinterpret_as_int()]
6060
} else {
6161
Edn::Nil
6262
}
6363
}
6464

6565
///|
6666
pub fn EdnListView::length(self : EdnListView) -> UInt {
67-
self.inner().length().reinterpret_as_uint()
67+
self.0.length().reinterpret_as_uint()
6868
}
6969

7070
///|
7171
pub fn EdnListView::is_empty(self : EdnListView) -> Bool {
72-
self.inner().length() == 0
72+
self.0.length() == 0
7373
}
7474

7575
///|
7676
/// mutablely push an element to the end of the list
7777
pub fn EdnListView::push(self : EdnListView, x : Edn) -> Unit {
78-
self.inner().push(x)
78+
self.0.push(x)
7979
}
8080

8181
///|
8282
/// implement iterator for EdnListView
8383
pub fn EdnListView::iter(self : EdnListView) -> Iter[Edn] {
8484
Iter::new(fn(yielding) {
85-
for i = 0; i < self.inner().length(); i = i + 1 {
86-
if yielding(self.inner()[i]) == IterEnd {
85+
for i = 0; i < self.0.length(); i = i + 1 {
86+
if yielding(self.0[i]) == IterEnd {
8787
break IterEnd
8888
}
8989
} else {

src/main/main.mbt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ let demo =
1313

1414
///|
1515
fn main {
16-
match (try? Edn::parse(demo)) {
17-
Ok(x) => {
16+
try Edn::parse(demo) catch {
17+
e => println("error:" + e.to_string())
18+
} noraise {
19+
x => {
1820
println(x.to_string())
1921
println((try? x.format(use_inline=false)).unwrap())
2022
}
21-
Err(e) => println("error:" + e.to_string())
2223
}
2324
let v = (try? Edn::Atom(@cirru_edn.Edn::Number(1.0)).format(use_inline=true)).unwrap()
2425
println(v)

0 commit comments

Comments
 (0)