From c4b1fa006e29b76299c27ba69413dbbec5f3f7f1 Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 14 Sep 2024 02:07:17 +0800 Subject: [PATCH] get parser test passed --- .github/workflows/check.yml | 13 ++++++------- lib/parser.mbt | 13 ++++++++----- lib/parser_test.mbt | 24 ++++++++++++++++++++++-- main/main.mbt | 23 +++++++++-------------- 4 files changed, 45 insertions(+), 28 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index b587d21..f5d1bde 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -15,8 +15,8 @@ jobs: - name: Setup Moonbit uses: hustcer/setup-moonbit@v1.5 - - name: moon check - run: moon check + # - name: moon check + # run: moon check # - name: moon info # run: | @@ -25,7 +25,6 @@ jobs: - name: moon test run: | - moon test moon test --target js # - name: moon bundle @@ -34,7 +33,7 @@ jobs: # - name: check core size # run: ls -alh `find ./target/bundle -name *.core` - - name: format diff - run: | - moon fmt - git diff + # - name: format diff + # run: | + # moon fmt + # git diff diff --git a/lib/parser.mbt b/lib/parser.mbt index 5bfbba9..c4d5cd4 100644 --- a/lib/parser.mbt +++ b/lib/parser.mbt @@ -58,9 +58,9 @@ fn build_exprs(tokens : Array[CirruLexItem]) -> Array[Cirru]!CirruParseError { pub fn parse(code : String) -> Array[Cirru]!CirruParseError { let tokens = resolve_indentations(lex!(code)) - println("tokens: \{tokens}") + // println("tokens: \{tokens}") let tree = build_exprs!(tokens) - println("tree: \{tree}") + // println("tree: \{tree}") resolve_comma(resolve_dollar(tree)) } @@ -74,7 +74,7 @@ pub fn to_string(self : CirruParseError) -> String { fn parse_indentation(size : Int) -> CirruLexItem!CirruParseError { if size % 2 == 0 { - CirruLexItem::Indent(size % 2) + CirruLexItem::Indent(size >> 1) } else { raise CirruParseError("odd indentation size: \{size}") } @@ -86,7 +86,10 @@ fn lex(initial_code : String) -> Array[CirruLexItem]!CirruParseError { let mut buffer = "" let code = initial_code.to_array() for idx, c in code { - // println("lexing \{c} in \{state} and \{buffer}") + // println("acc: \{acc}") + // println( + // "lexing \{c.to_string().escape()} in \{state} and \{buffer.escape()}", + // ) match state { CirruLexState::Space => match c { @@ -245,7 +248,7 @@ fn lex(initial_code : String) -> Array[CirruLexItem]!CirruParseError { } } } - println("end of file in lex \{acc}") + // println("end of file in lex \{acc}") match state { CirruLexState::Space => acc CirruLexState::Token => { diff --git a/lib/parser_test.mbt b/lib/parser_test.mbt index 304e31d..47270bd 100644 --- a/lib/parser_test.mbt +++ b/lib/parser_test.mbt @@ -1,7 +1,27 @@ test "parser" { - println(@lib.parse!("def a")) assert_eq!( @lib.parse!("def a"), - [@lib.Cirru::Leaf("def"), @lib.Cirru::Leaf("a")], + [@lib.Cirru::List([@lib.Cirru::Leaf("def"), @lib.Cirru::Leaf("a")])], ) } + +test "existed demos" { + let demos = [ + "comma", "demo", "folding", "html", "indent-twice", "indent", "let", "line", + "list-match", "paren-indent", "paren-indent2", "parentheses", "quote", "spaces", + "unfolding", + ] + for demo in demos { + let demo_file = fsReadSync("./test/cirru/\{demo}.cirru") + let json_file = fsReadSync("./test/data/\{demo}.json") + let tree = @lib.parse?(demo_file).unwrap().to_json().stringify() + let defined = @json.parse?(json_file).unwrap().stringify() + assert_eq!(tree, defined) + } +} + +pub extern "js" fn fsReadSync(path : String) -> String = + #|(path) => { + #| const fs = require("node:fs"); + #| return fs.readFileSync(path, { encoding: "utf8" }); + #|} diff --git a/main/main.mbt b/main/main.mbt index 516929f..6166897 100644 --- a/main/main.mbt +++ b/main/main.mbt @@ -7,29 +7,24 @@ fn main { // Result::Err(err) => println(err.to_string()) // } - let demo_file = fsReadSync("./test/cirru/indent.cirru") - let json_file = fsReadSync("./test/data/indent.json") - + let demo_file = fsReadSync("./test/cirru/unfolding.cirru") + let json_file = fsReadSync("./test/data/unfolding.json") let tree = @lib.parse?(demo_file) match tree { Result::Ok(tree) => { println(tree.to_json().stringify()) match @json.parse?(json_file) { - Result::Ok(json) => { - println(json.stringify()) - } + Result::Ok(json) => println(json.stringify()) Result::Err(err) => println(err.to_string()) } - println(tree) + // println(tree) } Result::Err(err) => println(err.to_string()) } - - } -pub extern "js" fn fsReadSync(path: String) -> String = -#|(path) => { -#| const fs = require("node:fs"); -#| return fs.readFileSync(path, { encoding: "utf8" }); -#|} +pub extern "js" fn fsReadSync(path : String) -> String = + #|(path) => { + #| const fs = require("node:fs"); + #| return fs.readFileSync(path, { encoding: "utf8" }); + #|}