Skip to content

Commit a3557bc

Browse files
authored
Merge pull request #9 from Cirru/fmt
"moon fmt" updated
2 parents c266e1e + 3856a93 commit a3557bc

File tree

7 files changed

+49
-20
lines changed

7 files changed

+49
-20
lines changed

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.9",
3+
"version": "0.0.10",
44
"deps": {},
55
"readme": "README.md",
66
"repository": "https://github.com/Cirru/parser.mbt",

src/lib/parser.mbt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
///|
12
fn build_exprs(tokens : Array[CirruLexItem]) -> Array[Cirru]!CirruParseError {
23
let acc : Array[Cirru] = Array::new()
34
let mut idx = 0
@@ -56,7 +57,7 @@ fn build_exprs(tokens : Array[CirruLexItem]) -> Array[Cirru]!CirruParseError {
5657
}
5758
}
5859

59-
/// main function to parse Cirru code into syntax tree
60+
///| main function to parse Cirru code into syntax tree
6061
pub fn parse(code : String) -> Array[Cirru]!CirruParseError {
6162
let tokens = resolve_indentations(lex!(code))
6263
// println("tokens: \{tokens}")
@@ -65,15 +66,17 @@ pub fn parse(code : String) -> Array[Cirru]!CirruParseError {
6566
resolve_comma(resolve_dollar(tree))
6667
}
6768

69+
///|
6870
type! CirruParseError String
6971

70-
/// display Cirru parse error
72+
///| display Cirru parse error
7173
pub fn to_string(self : CirruParseError) -> String {
7274
match self {
7375
CirruParseError(s) => s
7476
}
7577
}
7678

79+
///|
7780
fn parse_indentation(size : Int) -> CirruLexItem!CirruParseError {
7881
if size % 2 == 0 {
7982
CirruLexItem::Indent(size >> 1)
@@ -82,7 +85,7 @@ fn parse_indentation(size : Int) -> CirruLexItem!CirruParseError {
8285
}
8386
}
8487

85-
/// lexer is a simpler state machine to tokenize Cirru code
88+
///| lexer is a simpler state machine to tokenize Cirru code
8689
fn lex(initial_code : String) -> Array[CirruLexItem]!CirruParseError {
8790
let acc = []
8891
let mut state = CirruLexState::Indent
@@ -264,7 +267,7 @@ fn lex(initial_code : String) -> Array[CirruLexItem]!CirruParseError {
264267
}
265268
}
266269

267-
/// internal function for figuring out indentations after lexing
270+
///| internal function for figuring out indentations after lexing
268271
fn resolve_indentations(tokens : Array[CirruLexItem]) -> Array[CirruLexItem] {
269272
let size = tokens.length()
270273
let mut acc : Array[CirruLexItem] = Array::new()

src/lib/parser_test.mbt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
test "parser" {
2-
assert_eq!(
3-
@lib.parse!("def a"),
4-
[@lib.Cirru::List([@lib.Cirru::Leaf("def"), @lib.Cirru::Leaf("a")])],
5-
)
2+
assert_eq!(@lib.parse!("def a"), [
3+
@lib.Cirru::List([@lib.Cirru::Leaf("def"), @lib.Cirru::Leaf("a")]),
4+
])
65
}
76

87
test "existed demos" {
@@ -18,13 +17,15 @@ test "existed demos" {
1817
let defined = @json.parse?(json_file).unwrap()
1918
let defined_str = defined.stringify()
2019
assert_eq!(tree, defined_str)
21-
22-
let formatted = @lib.format!(@json.from_json!(defined), {use_inline: false})
20+
let formatted = @lib.format!(@json.from_json!(defined), {
21+
use_inline: false,
22+
})
2323
let ret = @lib.parse?(formatted).unwrap().to_json().stringify()
2424
assert_eq!(ret, defined_str)
2525
}
2626
}
2727

28+
///|
2829
pub extern "js" fn fsReadSync(path : String) -> String =
2930
#|(path) => {
3031
#| const fs = require("node:fs");

src/lib/primes.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn Cirru::default() -> Cirru {
7070
}
7171

7272
///|
73-
pub fn output(self : Cirru, logger : Logger) -> Unit {
73+
pub fn output(self : Cirru, logger : &Logger) -> Unit {
7474
logger.write_string(self.to_string())
7575
}
7676

@@ -176,7 +176,7 @@ enum CirruLexItem {
176176
}
177177

178178
///|
179-
fn output(self : CirruLexItem, logger : Logger) -> Unit {
179+
fn output(self : CirruLexItem, logger : &Logger) -> Unit {
180180
logger.write_string(self.to_string())
181181
}
182182

src/lib/s_expr.mbt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
///|
12
type! FormatError String
23

3-
/// format program to Cirru to Lisp-like code
4+
///| format program to Cirru to Lisp-like code
45
pub fn format_to_lisp(xs : Array[Cirru]) -> String!FormatError {
56
let mut content : String = "\n"
67
for expr in xs {
@@ -9,7 +10,7 @@ pub fn format_to_lisp(xs : Array[Cirru]) -> String!FormatError {
910
content
1011
}
1112

12-
/// format single expression to Lisp-like code
13+
///| format single expression to Lisp-like code
1314
fn format_lispy_expr(self : Cirru, indent : Int) -> String!FormatError {
1415
let emptySpace = " "
1516
match self {
@@ -62,6 +63,7 @@ fn format_lispy_expr(self : Cirru, indent : Int) -> String!FormatError {
6263
}
6364
}
6465

66+
///|
6567
fn ends_with_newline(s : String) -> Bool {
6668
for c in s.rev() {
6769
if c == 'c' {
@@ -74,6 +76,7 @@ fn ends_with_newline(s : String) -> Bool {
7476
false
7577
}
7678

79+
///|
7780
fn gen_newline(n : Int) -> String {
7881
let mut chunk : String = ""
7982
chunk = "\{chunk}\n"
@@ -83,7 +86,7 @@ fn gen_newline(n : Int) -> String {
8386
chunk
8487
}
8588

86-
/// `.escape_default()` in Rust
89+
///| `.escape_default()` in Rust
8790
fn escape_string(s : String) -> String {
8891
let mut chunk : String = ""
8992
for c in s {

src/lib/tree.mbt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
///|
12
fn resolve_comma(xs : Array[Cirru]) -> Array[Cirru] {
23
if xs.is_empty() {
34
[]
@@ -6,6 +7,7 @@ fn resolve_comma(xs : Array[Cirru]) -> Array[Cirru] {
67
}
78
}
89

10+
///|
911
fn comma_helper(initial_after : Array[Cirru]) -> Array[Cirru] {
1012
let before : Array[Cirru] = []
1113
let after : Array[Cirru] = initial_after
@@ -35,6 +37,7 @@ fn comma_helper(initial_after : Array[Cirru]) -> Array[Cirru] {
3537
}
3638
}
3739

40+
///|
3841
fn resolve_dollar(xs : Array[Cirru]) -> Array[Cirru] {
3942
if xs.is_empty() {
4043
[]
@@ -43,6 +46,7 @@ fn resolve_dollar(xs : Array[Cirru]) -> Array[Cirru] {
4346
}
4447
}
4548

49+
///|
4650
fn dollar_helper(initial_after : Array[Cirru]) -> Array[Cirru] {
4751
let before : Array[Cirru] = []
4852
let after : Array[Cirru] = initial_after

src/lib/writer.mbt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
///|
12
enum WriterNode {
23
Nil
34
Leaf
@@ -6,18 +7,23 @@ enum WriterNode {
67
Expr
78
} derive(Eq)
89

10+
///|
911
let char_close : Char = ')'
1012

13+
///|
1114
let char_open : Char = '('
1215

16+
///|
1317
let allowed_chars : String = "$-:<>[]{}*=+.,\\/!?~_@#&%^|;'"
1418

19+
///|
1520
fn is_a_digit(c : Char) -> Bool {
1621
let n = c.to_int()
1722
// ascii table https://tool.oschina.net/commons?type=4
1823
n >= 48 && n <= 57
1924
}
2025

26+
///|
2127
fn is_a_letter(c : Char) -> Bool {
2228
let n = c.to_int()
2329
if n >= 65 && n <= 90 {
@@ -29,6 +35,7 @@ fn is_a_letter(c : Char) -> Bool {
2935
false
3036
}
3137

38+
///|
3239
fn is_simple_expr(ys : Array[Cirru]) -> Bool {
3340
for y in ys {
3441
match y {
@@ -39,6 +46,7 @@ fn is_simple_expr(ys : Array[Cirru]) -> Bool {
3946
true
4047
}
4148

49+
///|
4250
fn is_boxed(ys : Array[Cirru]) -> Bool {
4351
for y in ys {
4452
match y {
@@ -49,17 +57,20 @@ fn is_boxed(ys : Array[Cirru]) -> Bool {
4957
true
5058
}
5159

60+
///|
5261
fn is_simple_char(x : Char) -> Bool {
5362
is_a_letter(x) || is_a_digit(x)
5463
}
5564

65+
///|
5666
fn is_char_allowed(x : Char) -> Bool {
5767
if is_simple_char(x) {
5868
return true
5969
}
6070
allowed_chars.contains_char(x)
6171
}
6272

73+
///|
6374
fn generate_leaf(s : String) -> String {
6475
let mut all_allowed = true
6576
for x in s {
@@ -89,10 +100,12 @@ fn generate_leaf(s : String) -> String {
89100
}
90101
}
91102

103+
///|
92104
fn generate_empty_expr() -> String {
93105
"()"
94106
}
95107

108+
///|
96109
fn generate_inline_expr(xs : Array[Cirru]) -> String {
97110
let mut result = char_open.to_string()
98111
for idx, x in xs {
@@ -109,7 +122,7 @@ fn generate_inline_expr(xs : Array[Cirru]) -> String {
109122
result
110123
}
111124

112-
/// by 2 spaces
125+
///| by 2 spaces
113126
fn push_spaces(buf : String, n : Int) -> String {
114127
let mut ret = buf
115128
// for _ in 0..n {
@@ -121,19 +134,20 @@ fn push_spaces(buf : String, n : Int) -> String {
121134
return ret
122135
}
123136

137+
///|
124138
fn render_newline(n : Int) -> String {
125139
let mut ret = ""
126140
ret = ret + "\n"
127141
ret = push_spaces(ret, n)
128142
ret
129143
}
130144

131-
/// options for writer, `use_inline` for more compact format.
145+
///| options for writer, `use_inline` for more compact format.
132146
pub(all) struct CirruWriterOptions {
133147
use_inline : Bool
134148
}
135149

136-
/// kind for writer nodes
150+
///| kind for writer nodes
137151
fn get_node_kind(self : Cirru) -> WriterNode {
138152
match self {
139153
Cirru::Leaf(_) => WriterNode::Leaf
@@ -150,14 +164,17 @@ fn get_node_kind(self : Cirru) -> WriterNode {
150164
}
151165
}
152166

167+
///|
153168
pub(all) type! FormatCirruError String
154169

170+
///|
155171
pub fn to_string(self : FormatCirruError) -> String {
156172
match self {
157173
FormatCirruError(s) => s
158174
}
159175
}
160176

177+
///|
161178
fn generate_tree(
162179
xs : Array[Cirru],
163180
insist_head : Bool,
@@ -291,6 +308,7 @@ fn generate_tree(
291308
result
292309
}
293310

311+
///|
294312
fn generate_statements(
295313
ys : Array[Cirru],
296314
options : CirruWriterOptions
@@ -309,7 +327,7 @@ fn generate_statements(
309327
zs
310328
}
311329

312-
/// format Cirru code, use options to control `use_inline` option
330+
///| format Cirru code, use options to control `use_inline` option
313331
pub fn format(
314332
xs : Array[Cirru],
315333
options : CirruWriterOptions

0 commit comments

Comments
 (0)