Parser and parsing utilities for the Gleam programming language in MoonBit
inspect(
@gleam.format("pub fn main(){\n io.println( \"hi\" )\n}\n"),
content="pub fn main() {\n io.println(\"hi\")\n}\n",
)test {
let r = @gleam.parse_text("import gleam/io\n\npub fn main() {\n io.println(\"hi\")\n}\n")
inspect(r.module_.definitions.length(), content="2")
inspect(r.has_errors(), content="false")
}test {
let tree = @gleam.Tree(@gleam.parse_text("pub fn add(a, b) {\n a + b\n}\n"))
// Byte 25 is the `b` in `a + b`.
let b = tree.node_at(@gleam.ByteOffset::from_int(25).unwrap()).unwrap()
inspect("\{b.category()}/\{b.kind()}", content="expression/var")
inspect(tree.node_path(b).unwrap(), content="definitions[0].body[0].expression[0].right[0]")
}moon add moonrockz/gleam-parserimport {
"moonrockz/gleam-parser" @gleam,
}test {
let orbit = @gleam.Target::extension("orbit")
let dialect = @gleam.Dialect::gleam_1_19().register_target(orbit)
let config = { ..@gleam.Config::default(), dialect }
let result = @gleam.parse_text("@target(orbit) pub const answer = 42", config~)
assert_eq(result.has_errors(), false)
let document = @gleam.decode_extended(@gleam.encode_extended(result))
assert_eq(document.parsed, result.parsed())
}@target(morphir)
pub const destination = "morphir"
@external(erlang, "erlang", "length")
@external(javascript, "./list.mjs", "length")
@external(morphir, "morphir/sdk/list", "length")
pub fn length(xs: List(a)) -> Int
@target(morphir)
@external(morphir, "example/domain", "CustomerId")
pub type CustomerIdlet dialect = @gleam.Dialect::gleam_1_19().with_profile(@morphir.profile())
let config = { ..@gleam.Config::default(), dialect }
let result = @gleam.parse_text(
"@external(morphir, \"example/domain\", \"CustomerId\")\npub type CustomerId\n",
config~,
)test {
let value : @gleam.Expr = Var(
location=@gleam.SrcSpan::from_ints(0, 1).unwrap(),
name="x",
)
assert_eq(@gleam.decode_expr(@gleam.encode_expr(value)), value)
}fn diagnostic_description(code : String) -> String?test {
let orbit = @gleam.Target::extension("orbit")
let config = {
..@gleam.Config::default(),
dialect: @gleam.Dialect::gleam_1_19().register_target(orbit),
}
let result = @gleam.parse_text("@target(orbit) const answer = 42", config~)
let document = @gleam.decode_extended(@gleam.encode_extended(result))
assert_eq(document.parsed, result.parsed())
}test {
let parsed = @gleam.parse_text("const x = 1").parsed()
assert_eq(@gleam.decode_parsed(@gleam.encode_parsed(parsed)), parsed)
}test {
inspect(@gleam.format("const x = 1\n"), content="const x = 1\n")
}fn parse_float_value(value : String) -> Double?test {
let r = @gleam.parse_text("pub fn main() {\n io.println(\"hi\")\n}\n")
inspect(r.module_.definitions.length(), content="1")
inspect(r.has_errors(), content="false")
}fn version() -> Stringtest {
inspect(@gleam.version().split(".").count(), content="3")
}test {
let root = @gleam.NodeRef::of_result(
@gleam.parse_text("pub fn f(x) {\n x\n}\n"),
)
let kinds = []
@gleam.walk(root, n => {
kinds.push(n.kind())
Continue
})
inspect(kinds.join(" "), content="module function arg expression var")
}Install
Download zipParser and parsing utilities for the Gleam programming language in MoonBit