moonrockz/krueger/printer does not have a README file
test {
let failed = try @printer.format("module A exposing (a)\n\na = (\n") catch {
ParseFailed(diagnostics) => diagnostics.length() > 0
Unprintable(_) => false
} noraise {
_ => false
}
inspect(failed, content="true")
}test {
let source = "module A exposing (a)\n\na = [ 1\n , 2 ]\n"
inspect(
@printer.format(source, layout=ElmFormat),
content=(
#|module A exposing (a)
#|
#|
#|a =
#| [ 1
#| , 2
#| ]
#|
),
)
inspect(
@printer.format(source, layout=Width(80)),
content=(
#|module A exposing (a)
#|
#|
#|a =
#| [ 1, 2 ]
#|
),
)
}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
UnplacedComment(Range)
} derive(Eq, Debug)test {
let source = "module A exposing (a)\n\na = f x -- why\n y\n"
inspect(
@printer.format(source),
content=(
#|module A exposing (a)
#|
#|
#|a =
#| f x
#| -- why
#| y
#|
),
)
}fn format_parsed(result : ParseResult, layout? : Layout, dialect? : Dialect) -> String raise FormatErrortest {
let result = @parser.parse_module(
@scanner.SourceText::new("module A exposing (a)\n\na = [1,2]\n"),
@scanner.DefaultScanner::new(),
)
inspect(
@printer.format_parsed(result, layout=Width(40)),
content=(
#|module A exposing (a)
#|
#|
#|a =
#| [ 1, 2 ]
#|
),
)
}test {
let text = "module A exposing (b, a)\n\nimport C\nimport B as B\n\n\na =\n 1\n"
let file = @parser.parse_module(
@scanner.SourceText::new(text),
@scanner.DefaultScanner::new(),
).ast.unwrap()
inspect(
@printer.print_file(@printer.normalize_file(file)),
content=(
#|module A exposing (a, b)
#|
#|import B
#|import C
#|
#|
#|a =
#| 1
#|
),
)
}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")
// elm-format writes a constructor with arguments before `::` in
// parentheses.
let just : @ast.Node[@ast.Pattern] = {
range: r,
value: NamedPattern({ module_name: [][:], name: "Just", }, [pvar("a")][:]),
}
inspect(
@printer.print_pattern({
range: r,
value: UnConsPattern(just, pvar("rest")),
}),
content="(Just a) :: 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