Skip to content

Commit e26b0f9

Browse files
committed
update moonbit; tag 0.0.9
1 parent f8d2624 commit e26b0f9

3 files changed

Lines changed: 27 additions & 24 deletions

File tree

moon.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tiye/cirru-parser",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"deps": {},
55
"readme": "README.md",
66
"repository": "https://github.com/Cirru/parser.mbt",

src/lib/primes.mbt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/// Cirru uses nested Vecters and Strings as data structure
1+
///| Cirru uses nested Vecters and Strings as data structure
22
pub(all) enum Cirru {
33
/// Leaf node, with a string
44
Leaf(String)
55
/// List node, with a list of children
66
List(Array[Cirru])
77
} derive(Eq)
88

9+
///|
910
pub impl Hash for Cirru with hash(self) {
1011
let mut i = 0
1112
match self {
@@ -18,6 +19,7 @@ pub impl Hash for Cirru with hash(self) {
1819
i
1920
}
2021

22+
///|
2123
pub impl Hash for Cirru with hash_combine(self, hasher) {
2224
match self {
2325
Cirru::Leaf(s) => hasher.combine_string(s)
@@ -28,6 +30,7 @@ pub impl Hash for Cirru with hash_combine(self, hasher) {
2830
}
2931
}
3032

33+
///|
3134
pub fn to_json(self : Cirru) -> Json {
3235
match self {
3336
Cirru::Leaf(s) => Json::String(s)
@@ -41,6 +44,7 @@ pub fn to_json(self : Cirru) -> Json {
4144
}
4245
}
4346

47+
///|
4448
pub impl @json.FromJson for Cirru with from_json(json, path) {
4549
match json {
4650
String(a) => Cirru::Leaf(a)
@@ -60,14 +64,17 @@ pub impl @json.FromJson for Cirru with from_json(json, path) {
6064
}
6165
}
6266

67+
///|
6368
fn Cirru::default() -> Cirru {
6469
Cirru::List(Array::new())
6570
}
6671

72+
///|
6773
pub fn output(self : Cirru, logger : Logger) -> Unit {
6874
logger.write_string(self.to_string())
6975
}
7076

77+
///|
7178
pub fn to_string(self : Cirru) -> String {
7279
match self {
7380
Cirru::Leaf(s) =>
@@ -90,6 +97,7 @@ pub fn to_string(self : Cirru) -> String {
9097
}
9198
}
9299

100+
///|
93101
pub fn compare(self : Cirru, other : Cirru) -> Int {
94102
match (self, other) {
95103
(Cirru::Leaf(a), Cirru::Leaf(b)) => a.compare(b)
@@ -110,37 +118,23 @@ pub fn compare(self : Cirru, other : Cirru) -> Int {
110118
}
111119
}
112120

113-
pub fn debug_write(self : Cirru, buffer : Buffer) -> Unit {
114-
match self {
115-
Cirru::Leaf(s) => buffer.write_string(s)
116-
Cirru::List(xs) => {
117-
buffer.write_char('(')
118-
for i = 0; i < xs.length(); i = i + 1 {
119-
let x = xs[i]
120-
buffer.write_string(x.to_string())
121-
if i < xs.length() - 1 {
122-
buffer.write_char(' ')
123-
}
124-
}
125-
buffer.write_char(')')
126-
}
127-
}
128-
}
129-
121+
///|
130122
pub fn length(self : Cirru) -> Int {
131123
match self {
132124
Leaf(s) => s.length()
133125
List(xs) => xs.length()
134126
}
135127
}
136128

129+
///|
137130
pub fn is_empty(self : Cirru) -> Bool {
138131
match self {
139132
Leaf(s) => s.length() == 0
140133
List(xs) => xs.length() == 0
141134
}
142135
}
143136

137+
///|
144138
pub fn is_nested(self : Cirru) -> Bool {
145139
match self {
146140
Leaf(_) => false
@@ -156,13 +150,15 @@ pub fn is_nested(self : Cirru) -> Bool {
156150
}
157151
}
158152

153+
///|
159154
pub fn is_comment(self : Cirru) -> Bool {
160155
match self {
161156
Leaf(s) => s[0] == ';'
162157
_ => false
163158
}
164159
}
165160

161+
///|
166162
enum CirruLexState {
167163
Space
168164
Token
@@ -171,17 +167,20 @@ enum CirruLexState {
171167
Str
172168
} derive(Show)
173169

170+
///|
174171
enum CirruLexItem {
175172
Open
176173
Close
177174
Indent(Int)
178175
Str(String)
179176
}
180177

178+
///|
181179
fn output(self : CirruLexItem, logger : Logger) -> Unit {
182180
logger.write_string(self.to_string())
183181
}
184182

183+
///|
185184
fn to_string(self : CirruLexItem) -> String {
186185
match self {
187186
CirruLexItem::Open => "("
@@ -191,6 +190,7 @@ fn to_string(self : CirruLexItem) -> String {
191190
}
192191
}
193192

193+
///|
194194
fn CirruLexItem::is_normal_str(tok : String) -> Bool {
195195
let size = tok.length()
196196
if size == 0 {
@@ -227,6 +227,7 @@ fn CirruLexItem::is_normal_str(tok : String) -> Bool {
227227
return true
228228
}
229229

230+
///|
230231
fn escape_cirru_leaf(s : String) -> String {
231232
if is_normal_str(s) {
232233
return "\"\{s}\""

src/main/main.mbt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
///|
12
pub fn main_parser() -> Unit {
23
// let tree = @lib.parse?("DEMO")
34
// match tree {
@@ -23,30 +24,31 @@ pub fn main_parser() -> Unit {
2324
}
2425
}
2526

27+
///|
2628
pub fn main_writer() -> Unit {
2729
let demo = "unfolding"
2830
let demo_file = fsReadSync("./test/cirru/\{demo}.cirru")
2931
let json_file = fsReadSync("./test/data/\{demo}.json")
30-
31-
let defined= @json.parse?(json_file).unwrap()
32-
let tree: Array[@lib.Cirru] = @json.from_json?(defined).unwrap()
32+
let defined = @json.parse?(json_file).unwrap()
33+
let tree : Array[@lib.Cirru] = @json.from_json?(defined).unwrap()
3334
println("TREE:")
3435
for item in tree {
3536
println(item.to_json().stringify())
3637
}
37-
38-
let ret = @lib.format?(tree, {use_inline: false}).unwrap()
38+
let ret = @lib.format?(tree, { use_inline: false }).unwrap()
3939
println("Generated:")
4040
println(ret)
4141
println("Expected:")
4242
println(demo_file)
4343
println("")
4444
}
4545

46+
///|
4647
fn main {
4748
main_parser()
4849
}
4950

51+
///|
5052
pub extern "js" fn fsReadSync(path : String) -> String =
5153
#|(path) => {
5254
#| const fs = require("node:fs");

0 commit comments

Comments
 (0)