moonrockz/krueger/printer does not have a README file
pub(all) enum PrintProblem {
InvalidName(NameKind, String)
EmptyModuleName
NonFiniteFloat(Double)
UnrepresentableInt(Int64)
NegativePattern
ShortApplication
ShortTuple
NoFields
EmptyExposing
EmptyCase
EmptyLet
NoConstructors
NoLambdaArguments
UnknownOperator(String)
InvalidPrecedence(Int)
InvalidGlsl
InvalidDocumentation
LetDocumentation
TooDeep
} derive(Eq, Debug)fn print_declaration(d : Node[Declaration], width? : Int, dialect? : Dialect) -> String raise PrintErrortest {
let r : @ast.Range = {
start: { row: 0, column: 0, },
end: { row: 0, column: 0, },
}
let int : @ast.Node[@ast.TypeAnnotation] = {
range: r,
value: Typed({ range: r, value: ([][:], "Int"), }, [][:]),
}
let decl : @ast.Node[@ast.Declaration] = {
range: r,
value: AliasDeclaration({
documentation: None,
name: { range: r, value: "Age", },
generics: [][:],
type_annotation: int,
}),
}
inspect(
@printer.print_declaration(decl),
content=(
#|type alias Age =
#| Int
),
)
}fn print_expression(e : Node[Expression], width? : Int, dialect? : Dialect) -> String raise PrintErrortest {
let r : @ast.Range = {
start: { row: 0, column: 0, },
end: { row: 0, column: 0, },
}
fn v(name : String) -> @ast.Node[@ast.Expression] {
{ range: r, value: FunctionOrValue([][:], name), }
}
let sum : @ast.Node[@ast.Expression] = {
range: r,
value: OperatorApplication("+", Left, v("a"), v("b")),
}
let product : @ast.Node[@ast.Expression] = {
range: r,
value: OperatorApplication("*", Left, sum, v("c")),
}
inspect(@printer.print_expression(product), content="(a + b) * c")
}test {
let text = "module Main exposing (main)\n\n\nmain =\n 1 + 2\n"
let result = @parser.parse_module(
@scanner.SourceText::new(text),
@scanner.DefaultScanner::new(),
)
inspect(@printer.print_file(result.ast.unwrap()) == text, content="true")
}test {
let r : @ast.Range = {
start: { row: 0, column: 0, },
end: { row: 0, column: 0, },
}
fn pvar(name : String) -> @ast.Node[@ast.Pattern] {
{ range: r, value: VarPattern(name), }
}
let inner : @ast.Node[@ast.Pattern] = {
range: r,
value: UnConsPattern(pvar("a"), pvar("b")),
}
let outer : @ast.Node[@ast.Pattern] = {
range: r,
value: UnConsPattern(inner, pvar("rest")),
}
// `::` is right-associative, so an `::` on its left gets parentheses.
inspect(@printer.print_pattern(outer), content="(a :: b) :: rest")
}fn print_type_annotation(t : Node[TypeAnnotation], width? : Int, dialect? : Dialect) -> String raise PrintErrortest {
let r : @ast.Range = {
start: { row: 0, column: 0, },
end: { row: 0, column: 0, },
}
let a : @ast.Node[@ast.TypeAnnotation] = {
range: r,
value: GenericType("a"),
}
let maybe : @ast.Node[@ast.TypeAnnotation] = {
range: r,
value: Typed({ range: r, value: ([][:], "Maybe"), }, [a][:]),
}
let list : @ast.Node[@ast.TypeAnnotation] = {
range: r,
value: Typed({ range: r, value: ([][:], "List"), }, [maybe][:]),
}
inspect(@printer.print_type_annotation(list), content="List (Maybe a)")
}Install
Download zipParser and parsing utilities for Elm and Elm-like dialects (e.g. Morphir) in MoonBit
Dependencies