///|
/// HCL Parser - parses HCL input into structured data
pub struct Parser {
lexer : Lexer
current : Token
peek : Token
next_prefix : String?
}
///|
/// Create a new parser from string input
pub fn Parser::new(input : String) -> HCLResult[Parser] {
let lexer = Lexer::new(input)
match lexer.next_token_with_trivia() {
Ok((current, lexer, prefix1)) =>
match lexer.next_token_with_trivia() {
Ok((peek, lexer, _)) =>
Ok({ lexer, current, peek, next_prefix: prefix1 })
Err(e) => Err(e)
}
Err(e) => Err(e)
}
}
///|
/// Create a new parser from lexer
pub fn Parser::from_lexer(lexer : Lexer) -> HCLResult[Parser] {
match lexer.next_token_with_trivia() {
Ok((current, lexer, prefix1)) =>
match lexer.next_token_with_trivia() {
Ok((peek, lexer, _)) =>
Ok({ lexer, current, peek, next_prefix: prefix1 })
Err(e) => Err(e)
}
Err(e) => Err(e)
}
}
///|
/// Advance to next token, using peek's prefix for new current
fn Parser::advance(self : Parser) -> HCLResult[Parser] {
match self.lexer.next_token_with_trivia() {
Ok((new_peek, new_lexer, peek_prefix)) =>
Ok({
lexer: new_lexer,
current: self.peek,
peek: new_peek,
next_prefix: peek_prefix,
})
Err(e) => Err(e)
}
}
///|
/// Check if current token matches expected
fn Parser::check(self : Parser, expected : Token) -> Bool {
self.current == expected
}
///|
/// Consume token if it matches expected
fn Parser::expect(self : Parser, expected : Token) -> HCLResult[Parser] {
if self.check(expected) {
self.advance()
} else {
let (line, col, offset) = self.lexer.location()
Err(
HCLError::parse_error_with_context(
"Expected " +
token_to_string(expected) +
", got " +
token_to_string(self.current),
line,
col,
offset,
self.lexer.get_line(line),
),
)
}
}
///|
/// Skip newlines
fn Parser::skip_newlines(self : Parser) -> HCLResult[Parser] {
let mut parser = self
while parser.check(Newline) {
match parser.advance() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
}
Ok(parser)
}
///|
/// Parse HCL input into a Body
pub fn Parser::parse_body(self : Parser) -> HCLResult[Body] {
match self.skip_newlines() {
Ok(start_parser) => {
let (start_line, start_col, start_offset) = start_parser.current_location()
let items = []
let mut parser = start_parser
while !parser.check(EOF) {
match parser.current {
Ident(_) =>
if parser.check(Equals) || parser.peek == Equals {
match parser.parse_attr() {
Ok((attr, new_parser)) => {
items.push(Attribute(attr))
parser = new_parser
}
Err(e) => return Err(e)
}
} else {
match parser.parse_block() {
Ok((block, new_parser)) => {
items.push(Block(block))
parser = new_parser
}
Err(e) => return Err(e)
}
}
Newline =>
match parser.advance() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
EOF => break
_ => {
let (line, col, offset) = parser.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Unexpected token: " + token_to_string(parser.current),
line,
col,
offset,
parser.lexer.get_line(line),
),
)
}
}
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
}
let (end_line, end_col, end_offset) = parser.current_location()
let span = Span::new(
start_line, start_col, start_offset, end_line, end_col, end_offset,
)
Ok(Body::from_items(items).set_span(span))
}
Err(e) => Err(e)
}
}
///|
/// Capture current lexer position
fn Parser::current_location(self : Parser) -> (Int, Int, Int) {
self.lexer.location()
}
///|
/// Collect prefix decor from pending trivia
fn Parser::collect_prefix(self : Parser) -> (Parser, Decor) {
match self.next_prefix {
Some(prefix) => {
let parser = { ..self, next_prefix: None }
(parser, Decor::new(Some(prefix), None))
}
None => (self, Decor::empty())
}
}
///|
/// Parse an attribute with decor collection
fn Parser::parse_attr(self : Parser) -> HCLResult[(Attr, Parser)] {
let (parser, prefix_decor) = self.collect_prefix()
let name = match parser.current {
Ident(n) => n
_ => {
let (line, col, offset) = parser.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected identifier",
line,
col,
offset,
parser.lexer.get_line(line),
),
)
}
}
match parser.advance() {
Ok(parser) =>
match parser.expect(Equals) {
Ok(parser) => {
let (start_line, start_col, start_offset) = parser.current_location()
match parser.parse_value() {
Ok((value, parser)) => {
let (end_line, end_col, end_offset) = parser.current_location()
let span = Span::new(
start_line, start_col, start_offset, end_line, end_col, end_offset,
)
let spanned_value = Spanned::new(value, span)
let decor = prefix_decor
Ok(
(
Attr::with_decor(Decorated::bare(name), spanned_value, decor),
parser,
),
)
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
Err(e) => Err(e)
}
}
///|
/// Parse a block with decor collection
fn Parser::parse_block(self : Parser) -> HCLResult[(Block, Parser)] {
let (parser, prefix_decor) = self.collect_prefix()
let (start_line, start_col, start_offset) = parser.current_location()
let type_name = match parser.current {
Ident(n) => n
_ => {
let (line, col, offset) = parser.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected block type name",
line,
col,
offset,
parser.lexer.get_line(line),
),
)
}
}
let labels = []
let mut result = match parser.advance() {
Ok(p) => p
Err(e) => return Err(e)
}
while !result.check(LBrace) && !result.check(EOF) {
match result.current {
StringLit(s) => {
labels.push(s)
match result.advance() {
Ok(p) => result = p
Err(e) => return Err(e)
}
}
Ident(s) => {
labels.push(s)
match result.advance() {
Ok(p) => result = p
Err(e) => return Err(e)
}
}
_ => {
let (line, col, offset) = result.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected block label or '{'",
line,
col,
offset,
result.lexer.get_line(line),
),
)
}
}
}
match result.expect(LBrace) {
Ok(start_parser) => {
let items = []
let mut parser = start_parser
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
while !parser.check(RBrace) && !parser.check(EOF) {
match parser.current {
Ident(_) =>
if parser.check(Equals) || parser.peek == Equals {
match parser.parse_attr() {
Ok((attr, new_parser)) => {
items.push(Attribute(attr))
parser = new_parser
}
Err(e) => return Err(e)
}
} else {
match parser.parse_block() {
Ok((block, new_parser)) => {
items.push(Block(block))
parser = new_parser
}
Err(e) => return Err(e)
}
}
Newline =>
match parser.advance() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
_ => {
let (line, col, offset) = parser.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Unexpected token: " + token_to_string(parser.current),
line,
col,
offset,
parser.lexer.get_line(line),
),
)
}
}
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
}
match parser.expect(RBrace) {
Ok(final_parser) => {
let (end_line, end_col, end_offset) = final_parser.current_location()
let span = Span::new(
start_line, start_col, start_offset, end_line, end_col, end_offset,
)
Ok(
(
Block::with_decor(
Decorated::bare(type_name),
labels.map(Decorated::bare),
{ items, decor: Decor::empty(), span: None },
prefix_decor,
).set_span(span),
final_parser,
),
)
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
///|
/// Convert token to BinaryOperator
fn token_to_binary_op(token : Token) -> BinaryOperator? {
match token {
Plus => Some(Plus)
Minus => Some(Minus)
Star => Some(Mul)
Slash => Some(Div)
Percent => Some(Mod)
EqEq => Some(Eq)
BangEq => Some(Neq)
Lt => Some(Lt)
Gt => Some(Gt)
LtEq => Some(Lte)
GtEq => Some(Gte)
AndAnd => Some(And)
OrOr => Some(Or)
_ => None
}
}
///|
/// Parse a value (including expressions with binary operators)
fn Parser::parse_value(self : Parser) -> HCLResult[(Expression, Parser)] {
match self.parse_primary() {
Ok((expr, parser)) => parser.parse_binary_expr(expr, 0)
Err(e) => Err(e)
}
}
///|
/// Parse binary expression with operator precedence (Pratt parser)
fn Parser::parse_binary_expr(
self : Parser,
left : Expression,
min_prec : Int,
) -> HCLResult[(Expression, Parser)] {
let mut result = left
let mut parser = match self.skip_newlines() {
Ok(p) => p
Err(e) => return Err(e)
}
let mut continue_loop = true
while continue_loop {
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
let prec = parser.current.precedence()
if prec == 0 || prec < min_prec {
continue_loop = false
continue
}
let op_token = parser.current
let bin_op = match token_to_binary_op(op_token) {
Some(op) => op
None => {
continue_loop = false
continue
}
}
match parser.advance() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
match parser.parse_primary() {
Ok((right, p)) => {
parser = p
let next_prec = parser.current.precedence()
if next_prec > prec {
match parser.parse_binary_expr(right, prec + 1) {
Ok((new_right, p)) => {
parser = p
result = ExprOperation(
Binary({ left: result, op: bin_op, right: new_right }),
)
}
Err(e) => return Err(e)
}
} else {
result = ExprOperation(Binary({ left: result, op: bin_op, right }))
}
}
Err(e) => return Err(e)
}
}
parser.parse_conditional_expr(result)
}
///|
/// Parse conditional expression (cond ? true : false)
fn Parser::parse_conditional_expr(
self : Parser,
condition : Expression,
) -> HCLResult[(Expression, Parser)] {
let parser = match self.skip_newlines() {
Ok(p) => p
Err(e) => return Err(e)
}
if parser.check(Question) {
match parser.advance() {
Ok(parser) =>
match parser.parse_value() {
Ok((true_expr, parser)) =>
match parser.skip_newlines() {
Ok(parser) =>
match parser.expect(Colon) {
Ok(parser) =>
match parser.parse_value() {
Ok((false_expr, parser)) =>
Ok(
(
ExprConditional({ condition, true_expr, false_expr }),
parser,
),
)
Err(e) => Err(e)
}
Err(e) => Err(e)
}
Err(e) => Err(e)
}
Err(e) => Err(e)
}
Err(e) => Err(e)
}
} else {
Ok((condition, parser))
}
}
///|
/// Parse primary value
fn Parser::parse_primary(self : Parser) -> HCLResult[(Expression, Parser)] {
match self.current {
StringLit(s) =>
match self.advance() {
Ok(p) => Ok((ExprString(s), p))
Err(e) => Err(e)
}
HeredocLit(delimiter, content, strip) =>
match Template::from_heredoc(content, delimiter, strip) {
Ok(template) =>
match template.evaluate({}, {}) {
Ok(result) =>
match self.advance() {
Ok(p) => Ok((ExprString(result), p))
Err(e) => Err(e)
}
Err(e) => Err(e)
}
Err(e) => Err(e)
}
NumberLit(n) =>
if n.has_prefix("0x") || n.has_prefix("0X") {
let mut result = 0
for c in n[2:] {
if c == '_' {
} else if c >= '0' && c <= '9' {
result = result * 16 + (c.to_int() - '0'.to_int())
} else if c >= 'a' && c <= 'f' {
result = result * 16 + (c.to_int() - 'a'.to_int() + 10)
} else if c >= 'A' && c <= 'F' {
result = result * 16 + (c.to_int() - 'A'.to_int() + 10)
}
}
match self.advance() {
Ok(p) => Ok((ExprInt(result), p))
Err(e) => Err(e)
}
} else if n.has_prefix("0o") || n.has_prefix("0O") {
let mut result = 0
for c in n[2:] {
if c == '_' {
} else if c >= '0' && c <= '7' {
result = result * 8 + (c.to_int() - '0'.to_int())
}
}
match self.advance() {
Ok(p) => Ok((ExprInt(result), p))
Err(e) => Err(e)
}
} else if n.has_prefix("0b") || n.has_prefix("0B") {
let mut result = 0
for c in n[2:] {
if c == '_' {
} else if c == '0' || c == '1' {
result = result * 2 + (c.to_int() - '0'.to_int())
}
}
match self.advance() {
Ok(p) => Ok((ExprInt(result), p))
Err(e) => Err(e)
}
} else {
let mut is_float = false
for c in n {
if c == '-' {
} else if c >= '0' && c <= '9' {
} else {
is_float = true
break
}
}
if is_float {
let mut result = 0.0
let mut decimal_place = 1.0
let mut in_decimal = false
let mut in_exponent = false
let mut exponent = 0
let mut exponent_negative = false
let mut has_exponent_sign = false
for c in n {
if c == '-' {
if in_exponent && !has_exponent_sign {
exponent_negative = true
has_exponent_sign = true
}
} else if c == '+' {
if in_exponent && !has_exponent_sign {
has_exponent_sign = true
}
} else if c == '.' {
in_decimal = true
} else if c == 'e' || c == 'E' {
in_exponent = true
} else if c >= '0' && c <= '9' {
if in_exponent {
exponent = exponent * 10 + (c.to_int() - '0'.to_int())
has_exponent_sign = true
} else if in_decimal {
decimal_place = decimal_place * 10.0
result = result +
(c.to_int() - '0'.to_int()).to_double() / decimal_place
} else {
result = result * 10.0 + (c.to_int() - '0'.to_int()).to_double()
}
}
}
if exponent != 0 {
let e = if exponent_negative { -exponent } else { exponent }
let mut scale = 1.0
let mut i = 0
while i < e.abs() {
scale = scale * 10.0
i = i + 1
}
if e >= 0 {
result = result * scale
} else {
result = result / scale
}
}
match self.advance() {
Ok(p) => Ok((ExprFloat(result), p))
Err(e) => Err(e)
}
} else {
let mut result = 0
for c in n {
if c >= '0' && c <= '9' {
result = result * 10 + (c.to_int() - '0'.to_int())
}
}
match self.advance() {
Ok(p) => Ok((ExprInt(result), p))
Err(e) => Err(e)
}
}
}
BoolLit(b) =>
match self.advance() {
Ok(p) => Ok((ExprBool(b), p))
Err(e) => Err(e)
}
NullLit =>
match self.advance() {
Ok(p) => Ok((ExprNull, p))
Err(e) => Err(e)
}
LBracket => self.parse_array()
LBrace => self.parse_object()
LParen => self.parse_paren_expr()
Minus => self.parse_negative()
Bang => self.parse_not()
Ident(name) => self.parse_ident_expr(name)
_ => {
let (line, col, offset) = self.lexer.location()
Err(
HCLError::parse_error_with_context(
"Unexpected token: " + token_to_string(self.current),
line,
col,
offset,
self.lexer.get_line(line),
),
)
}
}
}
///|
/// Parse identifier expression (variable, function call, or property access)
fn Parser::parse_ident_expr(
self : Parser,
name : String,
) -> HCLResult[(Expression, Parser)] {
match self.advance() {
Ok(parser) =>
if parser.check(LParen) {
parser.parse_function_call(name)
} else {
let var_expr = ExprVariable({ name, })
parser.parse_property_access(var_expr)
}
Err(e) => Err(e)
}
}
///|
/// Parse function call
fn Parser::parse_function_call(
self : Parser,
name : String,
) -> HCLResult[(Expression, Parser)] {
match self.expect(LParen) {
Ok(parser) => {
let args = []
let mut p = parser
match p.skip_newlines() {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
while !p.check(RParen) && !p.check(EOF) {
match p.parse_value() {
Ok((arg, new_p)) => {
args.push(arg)
p = new_p
}
Err(e) => return Err(e)
}
match p.skip_newlines() {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
if p.check(Comma) {
match p.advance() {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
match p.skip_newlines() {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
} else if !p.check(RParen) {
let (line, col, offset) = p.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected ',' or ')'",
line,
col,
offset,
p.lexer.get_line(line),
),
)
}
}
match p.expect(RParen) {
Ok(final_p) => {
let func_expr = ExprFuncCall({ name, args })
final_p.parse_property_access(func_expr)
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
///|
/// Parse property access (obj.key or obj["key"])
fn Parser::parse_property_access(
self : Parser,
obj : Expression,
) -> HCLResult[(Expression, Parser)] {
if self.check(Dot) {
match self.advance() {
Ok(parser) =>
// Check for splat: .*
if parser.check(Star) {
match parser.advance() {
Ok(p) => {
let builder = TraversalBuilder::new(obj).splat()
let traversal = builder.build()
p.parse_property_access(ExprTraversal(traversal))
}
Err(e) => Err(e)
}
} else {
let property = match parser.current {
Ident(name) => name
_ => {
let (line, col, offset) = parser.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected property name",
line,
col,
offset,
parser.lexer.get_line(line),
),
)
}
}
match parser.advance() {
Ok(p) => {
let builder = TraversalBuilder::new(obj).attr(property)
let traversal = builder.build()
p.parse_property_access(ExprTraversal(traversal))
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
} else if self.check(LBracket) {
match self.advance() {
Ok(parser) =>
// Check for splat: [*]
if parser.check(Star) {
match parser.advance() {
Ok(parser) =>
match parser.expect(RBracket) {
Ok(p) => {
let builder = TraversalBuilder::new(obj).splat()
let traversal = builder.build()
p.parse_property_access(ExprTraversal(traversal))
}
Err(e) => Err(e)
}
Err(e) => Err(e)
}
} else {
match parser.parse_value() {
Ok((key, parser)) =>
match parser.expect(RBracket) {
Ok(p) => {
let builder = TraversalBuilder::new(obj).index(key)
let traversal = builder.build()
p.parse_property_access(ExprTraversal(traversal))
}
Err(e) => Err(e)
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
} else {
Ok((obj, self))
}
}
///|
/// Parse an array or for expression
fn Parser::parse_array(self : Parser) -> HCLResult[(Expression, Parser)] {
match self.expect(LBracket) {
Ok(start_parser) => {
let mut parser = start_parser
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
if parser.check(For) {
return parser.parse_for_expr()
}
let items = []
while !parser.check(RBracket) && !parser.check(EOF) {
match parser.parse_value() {
Ok((value, new_parser)) => {
items.push(value)
parser = new_parser
}
Err(e) => return Err(e)
}
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
if parser.check(Comma) {
match parser.advance() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
} else if !parser.check(RBracket) {
let (line, col, offset) = parser.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected ',' or ']'",
line,
col,
offset,
parser.lexer.get_line(line),
),
)
}
}
match parser.expect(RBracket) {
Ok(parser) => Ok((ExprArray(items), parser))
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
///|
/// Parse for expression [for x in list: expr] or {for k, v in obj: k => v}
fn Parser::parse_for_expr(self : Parser) -> HCLResult[(Expression, Parser)] {
match self.expect(For) {
Ok(parser) => {
let vars = []
let mut p = parser
match p.current {
Ident(name) => {
vars.push(name)
match p.advance() {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
}
_ => {
let (line, col, offset) = p.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected identifier after 'for'",
line,
col,
offset,
p.lexer.get_line(line),
),
)
}
}
if p.check(Comma) {
match p.advance() {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
match p.current {
Ident(name) => {
vars.push(name)
match p.advance() {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
}
_ => {
let (line, col, offset) = p.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected identifier after ','",
line,
col,
offset,
p.lexer.get_line(line),
),
)
}
}
}
match p.expect(In) {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
match p.parse_value() {
Ok((collection, new_p)) => {
p = new_p
match p.expect(Colon) {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
match p.parse_value() {
Ok((value_expr, new_p)) => {
p = new_p
let filter = if p.check(If) {
match p.advance() {
Ok(new_p) => {
p = new_p
match p.parse_value() {
Ok((filter_expr, new_p)) => {
p = new_p
Some(filter_expr)
}
Err(e) => return Err(e)
}
}
Err(e) => return Err(e)
}
} else {
None
}
if vars.length() == 1 {
match p.expect(RBracket) {
Ok(final_p) =>
Ok(
(
ExprFor(
List({
var_name: vars[0],
collection,
value_expr,
filter,
}),
),
final_p,
),
)
Err(e) => Err(e)
}
} else {
match p.expect(FatArrow) {
Ok(new_p) => {
p = new_p
match p.parse_value() {
Ok((obj_value_expr, new_p)) => {
p = new_p
match p.expect(RBrace) {
Ok(final_p) =>
Ok(
(
ExprFor(
Object({
key_var: vars[0],
value_var: vars[1],
collection,
key_expr: value_expr,
value_expr: obj_value_expr,
filter,
}),
),
final_p,
),
)
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
///|
/// Parse for expression for object comprehension {for k, v in obj: k => v}
fn Parser::parse_for_expr_for_object(
self : Parser,
) -> HCLResult[(Expression, Parser)] {
match self.expect(For) {
Ok(parser) => {
let vars = []
let mut p = parser
match p.current {
Ident(name) => {
vars.push(name)
match p.advance() {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
}
_ => {
let (line, col, offset) = p.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected identifier after 'for'",
line,
col,
offset,
p.lexer.get_line(line),
),
)
}
}
match p.expect(Comma) {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
match p.current {
Ident(name) => {
vars.push(name)
match p.advance() {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
}
_ => {
let (line, col, offset) = p.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected identifier after ','",
line,
col,
offset,
p.lexer.get_line(line),
),
)
}
}
match p.expect(In) {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
match p.parse_value() {
Ok((collection, new_p)) => {
p = new_p
match p.expect(Colon) {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
match p.parse_value() {
Ok((key_expr, new_p)) => {
p = new_p
match p.expect(FatArrow) {
Ok(new_p) => p = new_p
Err(e) => return Err(e)
}
match p.parse_value() {
Ok((value_expr, new_p)) => {
p = new_p
let filter = if p.check(If) {
match p.advance() {
Ok(new_p) => {
p = new_p
match p.parse_value() {
Ok((filter_expr, new_p)) => {
p = new_p
Some(filter_expr)
}
Err(e) => return Err(e)
}
}
Err(e) => return Err(e)
}
} else {
None
}
match p.expect(RBrace) {
Ok(final_p) =>
Ok(
(
ExprFor(
Object({
key_var: vars[0],
value_var: vars[1],
collection,
key_expr,
value_expr,
filter,
}),
),
final_p,
),
)
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
///|
/// Parse an object or object for expression
fn Parser::parse_object(self : Parser) -> HCLResult[(Expression, Parser)] {
match self.expect(LBrace) {
Ok(start_parser) => {
let mut parser = start_parser
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
if parser.check(For) {
match parser.parse_for_expr_for_object() {
Ok(result) => return Ok(result)
Err(e) => return Err(e)
}
}
let obj : ExprObject = []
while !parser.check(RBrace) && !parser.check(EOF) {
let string_key = match parser.current {
StringLit(s) => s
Ident(s) => s
_ => {
let (line, col, offset) = parser.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected object key",
line,
col,
offset,
parser.lexer.get_line(line),
),
)
}
}
match parser.advance() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
if parser.check(Equals) {
match parser.advance() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
} else if parser.check(Colon) {
match parser.advance() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
} else {
let (line, col, offset) = parser.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected '=' or ':'",
line,
col,
offset,
parser.lexer.get_line(line),
),
)
}
match parser.parse_value() {
Ok((value, new_parser)) => {
let key = ObjectKey::Identifier(string_key)
obj.push((key, value))
parser = new_parser
}
Err(e) => return Err(e)
}
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
if parser.check(Comma) {
match parser.advance() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
match parser.skip_newlines() {
Ok(p) => parser = p
Err(e) => return Err(e)
}
} else if !parser.check(RBrace) {
let (line, col, offset) = parser.lexer.location()
return Err(
HCLError::parse_error_with_context(
"Expected ',' or '}'",
line,
col,
offset,
parser.lexer.get_line(line),
),
)
}
}
match parser.expect(RBrace) {
Ok(parser) => Ok((ExprObject(obj), parser))
Err(e) => Err(e)
}
}
Err(e) => Err(e)
}
}
///|
/// Parse parenthesized expression
fn Parser::parse_paren_expr(self : Parser) -> HCLResult[(Expression, Parser)] {
match self.expect(LParen) {
Ok(parser) =>
match parser.parse_value() {
Ok((value, parser)) =>
match parser.expect(RParen) {
Ok(parser) => Ok((value, parser))
Err(e) => Err(e)
}
Err(e) => Err(e)
}
Err(e) => Err(e)
}
}
///|
/// Parse negative expression
fn Parser::parse_negative(self : Parser) -> HCLResult[(Expression, Parser)] {
match self.expect(Minus) {
Ok(parser) =>
match parser.parse_value() {
Ok((value, parser)) =>
Ok((ExprOperation(Unary({ op: Negate, operand: value })), parser))
Err(e) => Err(e)
}
Err(e) => Err(e)
}
}
///|
/// Parse logical not
fn Parser::parse_not(self : Parser) -> HCLResult[(Expression, Parser)] {
match self.expect(Bang) {
Ok(parser) =>
match parser.parse_value() {
Ok((value, parser)) =>
Ok((ExprOperation(Unary({ op: Not, operand: value })), parser))
Err(e) => Err(e)
}
Err(e) => Err(e)
}
}
///|
/// Parse HCL string into Body
pub fn parse(input : String) -> HCLResult[Body] {
match Lexer::validate_input(input) {
Ok(_) =>
match Parser::new(input) {
Ok(parser) => parser.parse_body()
Err(e) => Err(e)
}
Err(e) => Err(e)
}
}
///|
/// Convert token to string for error messages
fn token_to_string(token : Token) -> String {
match token {
Ident(s) => s
StringLit(s) => "\"" + s + "\""
NumberLit(s) => s
BoolLit(b) => if b { "true" } else { "false" }
NullLit => "null"
LBrace => "{"
RBrace => "}"
LBracket => "["
RBracket => "]"
LParen => "("
RParen => ")"
Equals => "="
Comma => ","
Dot => "."
Plus => "+"
Minus => "-"
Star => "*"
Slash => "/"
Percent => "%"
Bang => "!"
Lt => "<"
Gt => ">"
LtEq => "<="
GtEq => ">="
EqEq => "=="
BangEq => "!="
AndAnd => "&&"
OrOr => "||"
Question => "?"
Colon => ":"
FatArrow => "=>"
Heredoc(strip) => if strip { "<<-" } else { "<<" }
HeredocLit(_, _, _) => "heredoc"
Newline => "newline"
EOF => "end of file"
For => "for"
In => "in"
If => "if"
}
}