Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/hcl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Typed HCL syntax parser for MoonBit, implemented with `cogna-dev/parkit/nom`.
- Nested bodies (recursive block parsing)
- Expressions:
- string
- mustache template (`{{ ... }}`)
- number
- bool
- null
Expand Down
16 changes: 16 additions & 0 deletions src/hcl/hcl_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ test "parse error on malformed input" {
let result = try? @hcl.parse("service { name = \"x\"")
inspect(result is Err(_), content="true")
}

///|
test "parse mustache template value" {
let input =
#|image = {{ repository }}
let doc = @hcl.parse(input)
guard doc.body.items[0] is @hcl.BodyItem::Attribute(image) else {
fail("expected image attribute")
}
inspect(
image.value,
content=(
#|MustacheTemplate(" repository ")
),
)
}
42 changes: 42 additions & 0 deletions src/hcl/parse.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,40 @@ fn string_parser() -> @nom.Parser[String] {
})
}

///|
fn mustache_template_parser() -> @nom.Parser[String] {
@nom.Parser::new(fn(input) {
let (rest_left, _) = match @nom.char_('{').parse(input) {
Ok(v) => v
Err(e) => return Err(e)
}
let (rest_open, _) = match @nom.char_('{').parse(rest_left) {
Ok(v) => v
Err(e) => return Err(e)
}
let mut i = 0
let mut out = ""
while i < rest_open.length() {
let c = match rest_open.get_char(i) {
Some(c) => c
None =>
return Err(
@nom.ParseError::new(rest_open, "unterminated mustache template"),
)
}
if c == '}' && i + 1 < rest_open.length() {
match rest_open.get_char(i + 1) {
Some('}') => return Ok((rest_open[i + 2:].to_string(), out))
_ => ()
}
}
out = out + String::make(1, c)
i += 1
}
Err(@nom.ParseError::new(rest_open, "unterminated mustache template"))
})
}

///|
fn key_parser() -> @nom.Parser[String] {
@nom.alt([string_parser(), identifier_parser()])
Expand All @@ -179,6 +213,14 @@ fn parse_value(input : String) -> @nom.IResult[Expression] {
Err(_) => ()
}

match
@nom.map(mustache_template_parser(), fn(v) {
Expression::MustacheTemplate(v)
}).parse(source) {
Ok(v) => return Ok(v)
Err(_) => ()
}

match parse_list(source) {
Ok(v) => return Ok(v)
Err(_) => ()
Expand Down
1 change: 1 addition & 0 deletions src/hcl/types.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(all) struct Block {
/// HCL expression values parsed by this package.
pub(all) enum Expression {
String(String)
MustacheTemplate(String)
Number(Double)
Bool(Bool)
Null
Expand Down
Loading