Skip to content

Commit 16b8ddc

Browse files
authored
Merge pull request #4 from Cirru/warnings
handle warnings
2 parents e1bc631 + 77287f0 commit 16b8ddc

File tree

11 files changed

+153
-145
lines changed

11 files changed

+153
-145
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.0.5",
3+
"version": "0.0.7",
44
"deps": {
5-
"tiye/cirru-parser": "0.0.10"
5+
"tiye/cirru-parser": "0.0.12"
66
},
77
"readme": "README.md",
88
"repository": "https://github.com/Cirru/cirru-edn.mbt",

src/lib/buffer.mbt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
type EdnBufferView Array[UInt] derive(Eq)
33

44
///|
5-
impl Hash for EdnBufferView with hash(self) { self._.length() }
5+
impl Hash for EdnBufferView with hash(self) {
6+
self._.length()
7+
}
68

79
///|
810
impl Hash for EdnBufferView with hash_combine(self, hasher) {
@@ -12,7 +14,7 @@ impl Hash for EdnBufferView with hash_combine(self, hasher) {
1214
}
1315

1416
///|
15-
fn to_string(self : EdnBufferView) -> String {
17+
impl Show for EdnBufferView with output(self, logger) {
1618
let mut s = ""
1719
s = s + "["
1820
for i = 0, len = self._.length(); i < len; i = i + 1 {
@@ -22,11 +24,14 @@ fn to_string(self : EdnBufferView) -> String {
2224
s = s + self._[i].to_string()
2325
}
2426
s = s + "]"
25-
s
27+
logger.write_string(s)
2628
}
2729

2830
///|
29-
fn compare(self : EdnBufferView, right : EdnBufferView) -> Int {
31+
impl Compare for EdnBufferView with compare(
32+
self : EdnBufferView,
33+
right : EdnBufferView
34+
) -> Int {
3035
for i = 0, len = self._.length(); i < len; i = i + 1 {
3136
if i >= right._.length() {
3237
return 1

src/lib/edn.mbt

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
///|
2-
pub fn hello() -> String {
3-
"Hello, world!"
4-
}
5-
61
///|
72
pub(all) enum Edn {
83
Nil
@@ -24,8 +19,8 @@ pub(all) enum Edn {
2419
} derive(Eq, Hash, Default)
2520

2621
///|
27-
pub fn to_string(self : Edn) -> String {
28-
match self {
22+
pub impl Show for Edn with output(self, logger) {
23+
let s = match self {
2924
Nil => "nil".to_string()
3025
Tag(t) => t.to_string()
3126
Bool(b) => if b { "true" } else { "false" }
@@ -54,17 +49,13 @@ pub fn to_string(self : Edn) -> String {
5449
Record(r) => r.to_string()
5550
Atom(a) => "atom " + a.to_string()
5651
}
57-
}
58-
59-
///|
60-
pub impl Show for Edn with output(self, logger) -> Unit {
61-
logger.write_string(self.to_string())
52+
logger.write_string(s)
6253
}
6354

6455
// TODO Hash
6556

6657
///|
67-
pub fn compare(self : Edn, right : Edn) -> Int {
58+
pub impl Compare for Edn with compare(self : Edn, right : Edn) -> Int {
6859
let ret = match (self, right) {
6960
(Nil, Nil) => 0
7061
(Nil, _) => -1

src/lib/edn_test.mbt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
test "hello" {
2-
if @lib.hello() != "Hello, world!" {
3-
fail!("@lib.hello() != \"Hello, world!\"")
4-
}
5-
}
6-
1+
///|
72
test "atom inside edn" {
83
let edn = @lib.Edn::Atom(Number(1.0))
94
assert_eq!(@lib.format!(edn, true).trim("\n"), "atom 1")

src/lib/lib.mbt

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,63 @@
22
pub(all) type! EdnCommonError String derive(Eq, Hash, Default)
33

44
///|
5-
pub fn to_string(self : EdnCommonError) -> String {
5+
pub impl Show for EdnCommonError with output(self, logger) {
66
match self {
7-
EdnCommonError(s) => s
7+
EdnCommonError(s) => logger.write_string(s)
88
}
99
}
1010

1111
///|
12-
pub impl Show for EdnCommonError with output(self, logger) {
13-
Show::output(self.to_string(), logger)
12+
impl @strconv.FromStr for Edn with from_string(s) {
13+
parse!(s)
1414
}
1515

1616
///| parse Cirru code into data
17-
pub fn parse(s : String) -> Edn!EdnCommonError {
17+
pub fn parse(s : String) -> Edn!@strconv.StrConvError {
1818
let xs = @cirru_parser.parse?(s).unwrap()
1919
if xs.length() == 1 {
2020
match xs[0] {
21-
Leaf(s) => raise EdnCommonError("expected expr for data, got leaf: " + s)
21+
Leaf(s) =>
22+
raise @strconv.StrConvError("expected expr for data, got leaf: " + s)
2223
List(_) => extract_cirru_edn!(xs[0])
2324
}
2425
} else {
25-
raise EdnCommonError("Expected 1 expr for edn, got length \{xs.length()}")
26+
raise @strconv.StrConvError(
27+
"Expected 1 expr for edn, got length \{xs.length()}",
28+
)
2629
}
2730
}
2831

2932
///|
30-
fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
33+
fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!@strconv.StrConvError {
3134
match node {
3235
Leaf(s) =>
3336
match s {
3437
"nil" => Nil
3538
"true" => Bool(true)
3639
"false" => Bool(false)
37-
"" => raise EdnCommonError("empty string is invalid for edn")
40+
"" => raise @strconv.StrConvError("empty string is invalid for edn")
3841
s1 =>
3942
match s1[0] {
4043
'\'' => Symbol(s1.substring(start=1))
41-
':' => tag(s1.substring(start=1))
44+
':' => Edn::tag(s1.substring(start=1))
4245
'"' | '|' => Str(s1.substring(start=1))
4346
_ =>
4447
match @strconv.parse_double?(s1.trim(" ")) {
4548
Ok(f) => Number(f)
4649
Err(e) =>
4750
match e {
4851
@strconv.StrConvError(s) =>
49-
raise EdnCommonError("failed to parse number: " + s)
52+
raise @strconv.StrConvError(
53+
"failed to parse number: " + s,
54+
)
5055
}
5156
}
5257
}
5358
}
5459
List(xs) =>
5560
if xs.is_empty() {
56-
raise EdnCommonError("empty expr is invalid for edn")
61+
raise @strconv.StrConvError("empty expr is invalid for edn")
5762
} else {
5863
// println("extracting list: " + xs.to_string())
5964
match xs[0] {
@@ -63,7 +68,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
6368
if xs.length() == 2 {
6469
Quote(xs[1])
6570
} else {
66-
raise EdnCommonError("missing edn quote value")
71+
raise @strconv.StrConvError("missing edn quote value")
6772
}
6873
"do" => {
6974
let mut ret : Edn? = None
@@ -72,21 +77,22 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
7277
continue
7378
}
7479
match ret {
75-
Some(_) => raise EdnCommonError("multiple values in do")
80+
Some(_) =>
81+
raise @strconv.StrConvError("multiple values in do")
7682
None => ret = Some(extract_cirru_edn!(x))
7783
}
7884
}
7985
match ret {
80-
None => raise EdnCommonError("missing edn do value")
86+
None => raise @strconv.StrConvError("missing edn do value")
8187
Some(_v) => ()
8288
}
83-
ret.or_error!(EdnCommonError("missing edn do value"))
89+
ret.or_error!(@strconv.StrConvError("missing edn do value"))
8490
}
8591
"atom" =>
8692
if xs.length() == 2 {
8793
Atom(extract_cirru_edn!(xs[1]))
8894
} else {
89-
raise EdnCommonError("missing edn atom value")
95+
raise @strconv.StrConvError("missing edn atom value")
9096
}
9197
"::" => {
9298
let mut tag : Edn? = None
@@ -105,7 +111,8 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
105111
}
106112
match tag {
107113
Some(x0) => Tuple({ tag: x0, extra })
108-
None => raise EdnCommonError("missing edn :: fst value")
114+
None =>
115+
raise @strconv.StrConvError("missing edn :: fst value")
109116
}
110117
}
111118
"[]" => {
@@ -116,7 +123,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
116123
}
117124
match extract_cirru_edn?(x) {
118125
Ok(v) => ys.push(v)
119-
Err(v) => raise EdnCommonError(v.to_string())
126+
Err(v) => raise @strconv.StrConvError(v.to_string())
120127
}
121128
}
122129
List(EdnListView(ys))
@@ -129,7 +136,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
129136
}
130137
match extract_cirru_edn?(x) {
131138
Ok(v) => ys.add(v)
132-
Err(v) => raise EdnCommonError(v.to_string())
139+
Err(v) => raise @strconv.StrConvError(v.to_string())
133140
}
134141
}
135142
Edn::Set(EdnSetView(ys))
@@ -142,7 +149,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
142149
}
143150
match x {
144151
Leaf(s) =>
145-
raise EdnCommonError(
152+
raise @strconv.StrConvError(
146153
"expected a pair, invalid map entry: " + s,
147154
)
148155
List(ys) =>
@@ -151,11 +158,11 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
151158
(extract_cirru_edn?(ys[0]), extract_cirru_edn?(ys[1])) {
152159
(Ok(k), Ok(v)) => zs.set(k, v)
153160
(Err(e), _) =>
154-
raise EdnCommonError(
161+
raise @strconv.StrConvError(
155162
"invalid map entry `\{e}` from `\{ys[0]}`",
156163
)
157164
(Ok(k), Err(e)) =>
158-
raise EdnCommonError(
165+
raise @strconv.StrConvError(
159166
"invalid map entry for `\{k}`, got \{e}",
160167
)
161168
}
@@ -169,7 +176,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
169176
let name = match xs[1] {
170177
Leaf(s) => EdnTag::new(s.trim_start(":"))
171178
List(e) =>
172-
raise EdnCommonError(
179+
raise @strconv.StrConvError(
173180
"expected record name in string: " + e.to_string(),
174181
)
175182
}
@@ -180,7 +187,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
180187
}
181188
match x {
182189
Leaf(s) =>
183-
raise EdnCommonError(
190+
raise @strconv.StrConvError(
184191
"expected record, invalid record entry: " + s,
185192
)
186193
List(ys) =>
@@ -189,27 +196,29 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
189196
(Leaf(s), Ok(v)) =>
190197
entries.push((EdnTag::new(s.trim_start(":")), v))
191198
(Leaf(s), Err(e)) =>
192-
raise EdnCommonError(
199+
raise @strconv.StrConvError(
193200
"invalid record value for `\{s}`, got: \{e}",
194201
)
195202
(List(zs), _) =>
196-
raise EdnCommonError(
203+
raise @strconv.StrConvError(
197204
"invalid list as record key: \{zs}",
198205
)
199206
}
200207
} else {
201-
raise EdnCommonError(
208+
raise @strconv.StrConvError(
202209
"expected pair of 2: " + ys.to_string(),
203210
)
204211
}
205212
}
206213
}
207214
if entries.is_empty() {
208-
raise EdnCommonError("empty record is invalid")
215+
raise @strconv.StrConvError("empty record is invalid")
209216
}
210217
Edn::Record({ tag: name, extra: entries })
211218
} else {
212-
raise EdnCommonError("insufficient items for edn record")
219+
raise @strconv.StrConvError(
220+
"insufficient items for edn record",
221+
)
213222
}
214223
"buf" => {
215224
let ys : Array[UInt] = []
@@ -225,28 +234,30 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
225234
Err(e) =>
226235
match e {
227236
@strconv.StrConvError(s) =>
228-
raise EdnCommonError(
237+
raise @strconv.StrConvError(
229238
"expected length 2 hex string in buffer, got: \{y} \{s}",
230239
)
231240
}
232241
}
233242
} else {
234-
raise EdnCommonError(
243+
raise @strconv.StrConvError(
235244
"expected length 2 hex string in buffer, got: \{y}",
236245
)
237246
}
238247
_ =>
239-
raise EdnCommonError(
248+
raise @strconv.StrConvError(
240249
"expected hex string in buffer, got: " + x.to_string(),
241250
)
242251
}
243252
}
244253
Edn::Buffer(ys)
245254
}
246-
a => raise EdnCommonError("invalid operator for edn: " + a)
255+
a => raise @strconv.StrConvError("invalid operator for edn: " + a)
247256
}
248257
List(a) =>
249-
raise EdnCommonError("invalid nodes for edn: " + a.to_string())
258+
raise @strconv.StrConvError(
259+
"invalid nodes for edn: " + a.to_string(),
260+
)
250261
}
251262
}
252263
}

0 commit comments

Comments
 (0)