use crate::{Error, Result, Value};
use std::collections::BTreeMap;
const MAX_DEPTH: usize = 128;
#[cfg(feature = "hcl")]
pub struct HclParser<'a> {
content: &'a str,
}
#[cfg(feature = "hcl")]
pub fn parse(source: &str) -> Result<Value> {
parse_hcl(source)
}
#[cfg(not(feature = "hcl"))]
pub fn parse(_source: &str) -> Result<Value> {
Err(crate::Error::feature_not_enabled("hcl"))
}
#[cfg(feature = "hcl")]
pub fn parse_hcl(content: &str) -> Result<Value> {
let mut parser = HclParser::new(content);
parser.parse()
}
impl<'a> HclParser<'a> {
pub fn new(content: &'a str) -> Self {
Self { content }
}
pub fn parse(&mut self) -> Result<Value> {
let mut cursor = Cursor {
text: self.content,
pos: 0,
line: 1,
column: 1,
depth: 0,
};
let body = cursor.body(false)?;
Ok(Value::table(body))
}
}
struct Cursor<'a> {
text: &'a str,
pos: usize,
line: usize,
column: usize,
depth: usize,
}
impl Cursor<'_> {
fn error(&self, message: &str) -> Error {
Error::parse(message, self.line, self.column)
}
fn peek(&self) -> Option<char> {
self.text.get(self.pos..)?.chars().next()
}
fn peek_at(&self, offset: usize) -> Option<char> {
self.text.get(self.pos + offset..)?.chars().next()
}
fn bump(&mut self) -> Option<char> {
let c = self.peek()?;
self.pos += c.len_utf8();
if c == '\n' {
self.line += 1;
self.column = 1;
} else {
self.column += 1;
}
Some(c)
}
fn skip(&mut self, newlines: bool) -> Result<bool> {
let mut crossed = false;
loop {
match self.peek() {
Some(' ' | '\t' | '\r') => {
self.bump();
}
Some('\n') if newlines => {
crossed = true;
self.bump();
}
Some('#') => self.skip_line(),
Some('/') if self.peek_at(1) == Some('/') => self.skip_line(),
Some('/') if self.peek_at(1) == Some('*') => {
let (line, column) = (self.line, self.column);
self.bump();
self.bump();
loop {
match self.peek() {
None => return Err(Error::parse("Unterminated comment", line, column)),
Some('*') if self.peek_at(1) == Some('/') => {
self.bump();
self.bump();
break;
}
Some(_) => {
if self.bump() == Some('\n') {
crossed = true;
}
}
}
}
}
_ => return Ok(crossed),
}
}
}
fn skip_line(&mut self) {
while let Some(c) = self.peek() {
if c == '\n' {
break;
}
self.bump();
}
}
fn enter(&mut self) -> Result<()> {
if self.depth >= MAX_DEPTH {
return Err(self.error("Blocks, lists or objects are nested too deeply"));
}
self.depth += 1;
Ok(())
}
fn identifier(&mut self) -> Option<String> {
let start = self.pos;
match self.peek() {
Some(c) if c.is_alphabetic() || c == '_' => {}
_ => return None,
}
while let Some(c) = self.peek() {
if c.is_alphanumeric() || c == '_' || c == '-' {
self.bump();
} else {
break;
}
}
Some(self.text[start..self.pos].to_string())
}
fn body(&mut self, nested: bool) -> Result<BTreeMap<String, Value>> {
let mut table = BTreeMap::new();
loop {
self.skip(true)?;
match self.peek() {
None if nested => return Err(self.error("A block is not closed")),
None => return Ok(table),
Some('}') if nested => {
self.bump();
return Ok(table);
}
Some('}') => return Err(self.error("Unexpected '}'")),
_ => {}
}
let (line, column) = (self.line, self.column);
let name = match self.peek() {
Some('"') => self.string()?,
_ => self
.identifier()
.ok_or_else(|| self.error("Expected an attribute or block name"))?,
};
self.skip(false)?;
if self.peek() == Some('=') {
self.bump();
self.skip(false)?;
let value = self.expression()?;
self.end_of_item()?;
if table.contains_key(&name) {
return Err(Error::parse("An attribute is defined twice", line, column));
}
table.insert(name, value);
} else {
self.block(&mut table, name, line, column)?;
}
}
}
fn end_of_item(&mut self) -> Result<()> {
let crossed = self.skip(false)?;
match self.peek() {
None | Some('\n' | '}') => Ok(()),
_ if crossed => Ok(()),
_ => Err(self.error("Expected a line end after the value")),
}
}
fn block(
&mut self,
table: &mut BTreeMap<String, Value>,
name: String,
line: usize,
column: usize,
) -> Result<()> {
let mut path = vec![name];
loop {
self.skip(false)?;
match self.peek() {
Some('{') => break,
Some('"') => path.push(self.string()?),
Some(c) if c.is_alphabetic() || c == '_' => {
if let Some(label) = self.identifier() {
path.push(label);
}
}
_ => return Err(self.error("Expected '=' or a block")),
}
}
self.bump();
self.enter()?;
let body = self.body(true)?;
self.depth -= 1;
insert_block(table, &path, body).map_err(|message| Error::parse(message, line, column))
}
fn expression(&mut self) -> Result<Value> {
match self.peek() {
Some('"') => Ok(Value::string(self.string()?)),
Some('[') => self.list(),
Some('{') => self.object(),
Some('<') if self.peek_at(1) == Some('<') => {
Err(self.error("Heredoc strings are not supported"))
}
Some(c) if c.is_ascii_digit() || c == '-' || c == '+' || c == '.' => self.number(),
Some(c) if c.is_alphabetic() || c == '_' => {
let (line, column) = (self.line, self.column);
let word = self.identifier().unwrap_or_default();
match word.as_str() {
"true" => Ok(Value::bool(true)),
"false" => Ok(Value::bool(false)),
"null" => Ok(Value::null()),
_ => Err(Error::parse(
"References, function calls and other expressions are not supported",
line,
column,
)),
}
}
_ => Err(self.error("Expected a value")),
}
.and_then(|value| {
self.skip(false)?;
match self.peek() {
Some(
'+' | '-' | '*' | '/' | '%' | '<' | '>' | '!' | '&' | '|' | '?' | '.' | '(',
) if !(self.peek() == Some('/') && matches!(self.peek_at(1), Some('/' | '*'))) => {
Err(self.error("Operators and other expressions are not supported"))
}
_ => Ok(value),
}
})
}
fn number(&mut self) -> Result<Value> {
let (line, column) = (self.line, self.column);
let start = self.pos;
if matches!(self.peek(), Some('-' | '+')) {
self.bump();
}
while let Some(c) = self.peek() {
let exponent_sign = matches!(c, '+' | '-')
&& matches!(self.text[..self.pos].chars().last(), Some('e' | 'E'));
if c.is_ascii_digit() || matches!(c, '.' | 'e' | 'E') || exponent_sign {
self.bump();
} else {
break;
}
}
let text = &self.text[start..self.pos];
if let Ok(i) = text.parse::<i64>() {
return Ok(Value::integer(i));
}
match super::decimal_float(text) {
Some(f) => Ok(Value::float(f)),
None => Err(Error::parse("Invalid number", line, column)),
}
}
fn list(&mut self) -> Result<Value> {
self.bump();
self.enter()?;
let mut items = Vec::new();
loop {
self.skip(true)?;
if self.peek() == Some(']') {
self.bump();
break;
}
items.push(self.expression()?);
self.skip(true)?;
match self.peek() {
Some(',') => {
self.bump();
}
Some(']') => {
self.bump();
break;
}
None => return Err(self.error("A list is not closed")),
_ => return Err(self.error("Expected ',' or ']' in a list")),
}
}
self.depth -= 1;
Ok(Value::array(items))
}
fn object(&mut self) -> Result<Value> {
self.bump();
self.enter()?;
let mut table = BTreeMap::new();
loop {
self.skip(true)?;
match self.peek() {
Some('}') => {
self.bump();
break;
}
None => return Err(self.error("An object is not closed")),
_ => {}
}
let (line, column) = (self.line, self.column);
let key = match self.peek() {
Some('"') => self.string()?,
_ => self
.identifier()
.ok_or_else(|| self.error("Expected an object key"))?,
};
self.skip(false)?;
match self.peek() {
Some('=' | ':') => {
self.bump();
}
_ => return Err(self.error("Expected '=' or ':' after an object key")),
}
self.skip(false)?;
let value = self.expression()?;
if table.contains_key(&key) {
return Err(Error::parse("An object key is defined twice", line, column));
}
table.insert(key, value);
let crossed = self.skip(false)?;
match self.peek() {
Some(',') => {
self.bump();
}
Some('}' | '\n') => {}
_ if crossed => {}
None => return Err(self.error("An object is not closed")),
_ => return Err(self.error("Expected ',', a line end or '}' in an object")),
}
}
self.depth -= 1;
Ok(Value::table(table))
}
fn string(&mut self) -> Result<String> {
let (line, column) = (self.line, self.column);
self.bump();
let mut out = String::new();
loop {
match self.bump() {
None | Some('\n') => {
return Err(Error::parse("Unterminated string", line, column));
}
Some('"') => return Ok(out),
Some('\\') => match self.bump() {
Some('n') => out.push('\n'),
Some('r') => out.push('\r'),
Some('t') => out.push('\t'),
Some('"') => out.push('"'),
Some('\\') => out.push('\\'),
Some(kind @ ('u' | 'U')) => {
let len = if kind == 'u' { 4 } else { 8 };
let hex = self.text.get(self.pos..self.pos + len).unwrap_or("");
let ch = u32::from_str_radix(hex, 16)
.ok()
.filter(|_| hex.len() == len)
.and_then(char::from_u32)
.ok_or_else(|| self.error("Invalid unicode escape"))?;
for _ in 0..len {
self.bump();
}
out.push(ch);
}
_ => return Err(self.error("Invalid escape sequence")),
},
Some(c) => out.push(c),
}
}
}
}
fn insert_block(
table: &mut BTreeMap<String, Value>,
path: &[String],
body: BTreeMap<String, Value>,
) -> std::result::Result<(), &'static str> {
let (last, parents) = match path.split_last() {
Some(split) => split,
None => return Ok(()),
};
let mut current = table;
for part in parents {
let entry = current
.entry(part.clone())
.or_insert_with(|| Value::table(BTreeMap::new()));
current = match entry {
Value::Table(inner) => inner,
_ => return Err("A block has the name of an attribute"),
};
}
match current.get_mut(last) {
None => {
current.insert(last.clone(), Value::table(body));
}
Some(Value::Array(items)) if items.iter().all(Value::is_table) => {
items.push(Value::table(body));
}
Some(existing @ Value::Table(_)) => {
let first = std::mem::replace(existing, Value::Null);
*existing = Value::array(vec![first, Value::table(body)]);
}
Some(_) => return Err("A block has the name of an attribute"),
}
Ok(())
}
#[cfg(not(feature = "hcl"))]
pub fn parse_hcl(_content: &str) -> Result<Value> {
Err(crate::error::Error::feature_not_enabled("hcl"))
}
#[cfg(all(test, feature = "hcl"))]
mod tests {
use super::*;
#[test]
fn test_simple_hcl() {
let hcl = r#"
database {
host = "localhost"
port = 5432
enabled = true
}
app {
name = "MyApp"
version = "1.0.0"
}
"#;
let result = parse_hcl(hcl).unwrap();
if let Value::Table(config) = result {
if let Some(Value::Table(db)) = config.get("database") {
assert_eq!(db.get("host"), Some(&Value::string("localhost")));
assert_eq!(db.get("port"), Some(&Value::integer(5432)));
assert_eq!(db.get("enabled"), Some(&Value::bool(true)));
} else {
panic!("Expected database configuration");
}
if let Some(Value::Table(app)) = config.get("app") {
assert_eq!(app.get("name"), Some(&Value::string("MyApp")));
assert_eq!(app.get("version"), Some(&Value::string("1.0.0")));
} else {
panic!("Expected app configuration");
}
} else {
panic!("Expected table result");
}
}
#[test]
fn test_terraform_style_hcl() {
let hcl = r#"
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
Environment = "production"
}
}
variable "region" {
description = "AWS region"
type = "string"
default = "us-west-2"
}
"#;
let result = parse_hcl(hcl);
match result {
Ok(Value::Table(_)) => {
}
Ok(_) => panic!("Expected table result"),
Err(_) => {
}
}
}
#[test]
fn test_hcl_arrays_and_objects() {
let hcl = r#"
servers = ["web1", "web2", "web3"]
database {
replicas = [
{
host = "db1.example.com"
role = "master"
},
{
host = "db2.example.com"
role = "slave"
}
]
}
"#;
let result = parse_hcl(hcl).unwrap();
if let Value::Table(config) = result {
if let Some(Value::Array(servers)) = config.get("servers") {
assert_eq!(servers.len(), 3);
assert_eq!(servers[0], Value::string("web1"));
} else {
panic!("Expected servers array");
}
if let Some(Value::Table(db)) = config.get("database") {
if let Some(Value::Array(replicas)) = db.get("replicas") {
assert_eq!(replicas.len(), 2);
if let Value::Table(replica1) = &replicas[0] {
assert_eq!(replica1.get("role"), Some(&Value::string("master")));
}
} else {
panic!("Expected replicas array");
}
} else {
panic!("Expected database configuration");
}
} else {
panic!("Expected table result");
}
}
}