Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ Docs: https://mooncakes.io/docs/#/tiye/cirru-parser/lib/members
moon add tiye/cirru-parser
```

import in `moon.pkg.json`:

```js
{
"path": "tiye/cirru-parser",
"alias": "cirru_parser"
}
```

```moonbit
typealias @cirru_parser.Cirru
using @cirru_parser {type Cirru}

// parse Cirru code
Cirru::parse(code: String) : Array[Cirru]!CirruParseError
Cirru::parse(code: String) : Array[Cirru] raise CirruParseError

// format Cirru code
Cirru::format(cirru: Array[Cirru], {use_inline: false}) : String!FormatCirruError
Cirru::format(cirru: Array[Cirru], use_inline=false) : String raise FormatCirruError
```

### License
Expand Down
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiye/cirru-parser",
"version": "0.1.1",
"version": "0.2.0",
"deps": {},
"readme": "README.md",
"repository": "https://github.com/Cirru/parser.mbt",
Expand Down
4 changes: 0 additions & 4 deletions src/lib/moon.pkg.json

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///|
typealias @lib.Cirru
using @lib {type Cirru}

///|
pub fn main_parser() -> Unit {
Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn main_writer() -> Unit {
for item in tree {
println(item.to_json().stringify())
}
let ret = (try? Cirru::format(tree, { use_inline: false })).unwrap()
let ret = (try? Cirru::format(tree, use_inline=false)).unwrap()
println("Generated:")
println(ret)
println("Expected:")
Expand Down
7 changes: 6 additions & 1 deletion src/main/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"is-main": true,
"import": ["tiye/cirru-parser/lib"],
"import": [
{
"path": "tiye/cirru-parser",
"alias": "lib"
}
],
"test-import": []
}
9 changes: 9 additions & 0 deletions src/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"import": [],
"test-import": [
{
"path": "tiye/cirru-parser",
"alias": "lib"
}
]
}
File renamed without changes.
6 changes: 2 additions & 4 deletions src/lib/parser_test.mbt → src/parser_test.mbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///|
typealias @lib.Cirru
using @lib {type Cirru}

///|
test "parser" {
Expand All @@ -20,9 +20,7 @@ test "existed demos" {
let defined = (try? @json.parse(json_file)).unwrap()
let defined_str = defined.stringify()
assert_eq(tree, defined_str)
let formatted = Cirru::format(@json.from_json(defined), {
use_inline: false,
})
let formatted = Cirru::format(@json.from_json(defined), use_inline=false)
let ret = (try? Cirru::parse(formatted)).unwrap().to_json().stringify()
assert_eq(ret, defined_str)
}
Expand Down
File renamed without changes.
11 changes: 6 additions & 5 deletions src/lib/s_expr.mbt → src/s_expr.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn format_to_lisp(xs : Array[Cirru]) -> String raise FormatError {
///|
/// format single expression to Lisp-like code
fn format_lispy_expr(self : Cirru, indent : Int) -> String raise FormatError {
let emptySpace = " "
let empty_space = " "
match self {
List(xs) =>
if xs.length() > 0 && xs[0].is_comment() {
Expand All @@ -26,17 +26,17 @@ fn format_lispy_expr(self : Cirru, indent : Int) -> String raise FormatError {
}

// chunk = format!("{}{}", chunk.trim_end(), gen_newline(indent));
chunk = "\{chunk.trim_end(emptySpace)}\{gen_newline(indent)}"
chunk = "\{chunk.trim_end(char_set=empty_space)}\{gen_newline(indent)}"
chunk
} else {
let mut chunk = "("
for idx, x in xs {
if x.is_nested() {
chunk = "\{chunk.trim_end(emptySpace)}\{gen_newline(indent + 1)}"
chunk = "\{chunk.trim_end(char_set=empty_space)}\{gen_newline(indent + 1)}"
}
let next = x.format_lispy_expr(indent + 1)
if next.has_prefix("\n") {
chunk = "\{chunk.trim_end(emptySpace)}\{next}"
chunk = "\{chunk.trim_end(char_set=empty_space)}\{next}"
} else {
chunk = "\{chunk}\{next}"
}
Expand All @@ -53,7 +53,8 @@ fn format_lispy_expr(self : Cirru, indent : Int) -> String raise FormatError {
} else {
let s0 = token[0]
if s0 == '|' || s0 == '"' {
"\"" + escape_string(token.substring(start=1)) + "\""
let sliced = (try? token[1:]).unwrap().to_string()
"\"" + escape_string(sliced) + "\""
} else if token.contains(" ") ||
token.contains("\n") ||
token.contains("\"") {
Expand Down
File renamed without changes.
42 changes: 25 additions & 17 deletions src/lib/writer.mbt → src/writer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ fn render_newline(n : Int) -> String {
ret
}

///|
/// options for writer, `use_inline` for more compact format.
pub(all) struct CirruWriterOptions {
use_inline : Bool
}

///|
/// kind for writer nodes
fn Cirru::get_node_kind(self : Cirru) -> WriterNode {
Expand All @@ -180,7 +174,7 @@ pub(all) suberror FormatCirruError String derive(Show)
fn generate_tree(
xs : Array[Cirru],
insist_head : Bool,
options : CirruWriterOptions,
use_inline? : Bool = false,
base_level : Int,
in_tail : Bool,
) -> String raise FormatCirruError {
Expand Down Expand Up @@ -208,7 +202,7 @@ fn generate_tree(
"$"
} else {
let mut ret = "$ "
ret = ret + generate_tree(ys, false, options, level, at_tail)
ret = ret + generate_tree(ys, false, use_inline~, level, at_tail)
ret
}
} else if idx == 0 && insist_head {
Expand All @@ -224,19 +218,29 @@ fn generate_tree(
} else if kind == SimpleExpr {
if prev_kind == Leaf {
generate_inline_expr(ys)
} else if options.use_inline && prev_kind == SimpleExpr {
} else if use_inline && prev_kind == SimpleExpr {
let mut ret = " "
ret = ret + generate_inline_expr(ys)
ret
} else {
let mut ret = render_newline(next_level)
ret = ret +
generate_tree(ys, child_insist_head, options, next_level, false)
generate_tree(
ys,
child_insist_head,
use_inline~,
next_level,
false,
)
ret
}
} else if kind == Expr {
let content = generate_tree(
ys, child_insist_head, options, next_level, false,
ys,
child_insist_head,
use_inline~,
next_level,
false,
)
if content.has_prefix("\n") {
content
Expand All @@ -247,7 +251,11 @@ fn generate_tree(
}
} else if kind == BoxedExpr {
let content = generate_tree(
ys, child_insist_head, options, next_level, false,
ys,
child_insist_head,
use_inline~,
next_level,
false,
)
if prev_kind == Nil || prev_kind == Leaf || prev_kind == SimpleExpr {
content
Expand Down Expand Up @@ -282,7 +290,7 @@ fn generate_tree(
if kind == SimpleExpr {
if idx == 0 && insist_head {
prev_kind = SimpleExpr
} else if options.use_inline {
} else if use_inline {
if prev_kind == Leaf || prev_kind == SimpleExpr {
prev_kind = SimpleExpr
} else {
Expand All @@ -309,15 +317,15 @@ fn generate_tree(
///|
fn generate_statements(
ys : Array[Cirru],
options : CirruWriterOptions,
use_inline? : Bool = false,
) -> String raise FormatCirruError {
let mut zs = ""
for y in ys {
match y {
Leaf(_) => raise FormatCirruError("expected an exprs at top level")
List(cs) => {
zs += "\n"
zs += generate_tree(cs, true, options, 0, false)
zs += generate_tree(cs, true, use_inline~, 0, false)
zs += "\n"
}
}
Expand All @@ -329,7 +337,7 @@ fn generate_statements(
/// format Cirru code, use options to control `use_inline` option
pub fn Cirru::format(
xs : Array[Cirru],
options : CirruWriterOptions,
use_inline? : Bool = false,
) -> String raise FormatCirruError {
generate_statements(xs, options)
generate_statements(xs, use_inline~)
}