Class HCLParser


  • public class HCLParser
    extends java.lang.Object
    Parser for the Hashicorp Configuration Language (HCL). This is the primary endpoint and converts the HCL syntax into a Map. This parser utilizes a lexer to generate symbols based on the HCL spec. String interpolation is not evaluated at this point in the parsing.

    Below is an example of how HCL might be parsed.

         
         import com.bertramlabs.plugins.hcl4j.HCLParser;
    
         File terraformFile = new File("terraform.tf");
    
         Map results = new HCLParser().parse(terraformFile);
         
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map<java.lang.String,​HCLFunction> functionRegistry  
      protected java.util.Map<java.lang.String,​java.lang.Object> result  
      protected java.util.Map<java.lang.String,​java.lang.Object> variables  
    • Constructor Summary

      Constructors 
      Constructor Description
      HCLParser()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Object evaluateEvalSymbol​(EvalSymbol evalSymbol)  
      protected java.lang.Object evaluateFunctionCall​(java.lang.String functionName, Function functionSymbol)  
      protected java.lang.Object evaluateVariable​(Variable var)  
      protected java.lang.Object flattenContext​(java.lang.Object context)  
      protected java.lang.Object getVariableObject​(java.lang.String varName, java.lang.Object source)  
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input)
      Parses terraform syntax as it comes from a File.
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input, java.lang.Boolean ignoreParserExceptions)
      Parses terraform syntax as it comes from a File.
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input, java.lang.String charsetName)
      Parses terraform syntax as it comes from a File.
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input, java.lang.String charsetName, java.lang.Boolean ignoreParserExceptions)
      Parses terraform syntax as it comes from a File.
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input, java.nio.charset.Charset cs)
      Parses terraform syntax as it comes from a File.
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.InputStream input)
      Parses terraform syntax as it comes from an input stream.
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.InputStream input, java.lang.String charsetName)
      Parses terraform syntax as it comes from an input stream.
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.InputStream input, java.lang.String charsetName, java.lang.Boolean ignoreParserExceptions)
      Parses terraform syntax as it comes from an input stream.
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.InputStream input, java.nio.charset.Charset cs)
      Parses terraform syntax as it comes from an input stream.
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.Reader reader)
      Parses terraform configuration language from a Reader
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.Reader reader, java.lang.Boolean ignoreParserExceptions)
      Parses terraform configuration language from a Reader
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.lang.String input)
      Parses terraform configuration language from a String
      java.util.Map<java.lang.String,​java.lang.Object> parse​(java.lang.String input, java.lang.Boolean ignoreParserExceptions)
      Parses terraform configuration language from a String
      java.util.Map<java.lang.String,​java.lang.Object> parseVars​(java.lang.String input, java.lang.Boolean ignoreParseException)
      Parses Var files into the variables context (example would be a tfvars file from terraform)
      protected java.lang.Object processEvaluation​(EvalSymbol evalSymbol, java.lang.Object context)  
      protected java.lang.Object processValue​(HCLValue value)  
      void registerFunction​(java.lang.String functionName, HCLFunction function)
      Registers Function implementations for HCL Common functions.
      void setVariable​(java.lang.String variableName, java.lang.Object value)
      Sets a variable value individually.
      void setVariables​(java.util.Map<java.lang.String,​java.lang.Object> variableMap)
      Sets a Map of variables into the context of the HCLParser for parse runtime operations
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • result

        protected java.util.Map<java.lang.String,​java.lang.Object> result
      • variables

        protected java.util.Map<java.lang.String,​java.lang.Object> variables
      • functionRegistry

        protected java.util.Map<java.lang.String,​HCLFunction> functionRegistry
    • Constructor Detail

      • HCLParser

        public HCLParser()
    • Method Detail

      • parseVars

        public java.util.Map<java.lang.String,​java.lang.Object> parseVars​(java.lang.String input,
                                                                                java.lang.Boolean ignoreParseException)
                                                                         throws HCLParserException,
                                                                                java.io.IOException
        Parses Var files into the variables context (example would be a tfvars file from terraform)
        Parameters:
        input - String input containing HCL syntax
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
      • registerFunction

        public void registerFunction​(java.lang.String functionName,
                                     HCLFunction function)
        Registers Function implementations for HCL Common functions. By default the HCLBaseFunctions are loaded. Additional functions can be defined via this method for custom method overrides if necessary.
        Parameters:
        functionName - the name of the function to be called
        function - the lambda function implementation to be evaluated during the parse
      • setVariable

        public void setVariable​(java.lang.String variableName,
                                java.lang.Object value)
        Sets a variable value individually. One can also use the parseTfVars method to load tf vars.
        Parameters:
        variableName - the name of the variable being defined
        value - the value of the variable
      • setVariables

        public void setVariables​(java.util.Map<java.lang.String,​java.lang.Object> variableMap)
        Sets a Map of variables into the context of the HCLParser for parse runtime operations
        Parameters:
        variableMap - A Map of variables to be bulk applied to the parser Context
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.lang.String input)
                                                                     throws HCLParserException,
                                                                            java.io.IOException
        Parses terraform configuration language from a String
        Parameters:
        input - String input containing HCL syntax
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.lang.String input,
                                                                            java.lang.Boolean ignoreParserExceptions)
                                                                     throws HCLParserException,
                                                                            java.io.IOException
        Parses terraform configuration language from a String
        Parameters:
        input - String input containing HCL syntax
        ignoreParserExceptions - if set to true, we ignore any parse exceptions and still return the symbol map
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input,
                                                                            java.lang.Boolean ignoreParserExceptions)
                                                                     throws HCLParserException,
                                                                            java.io.IOException,
                                                                            java.io.UnsupportedEncodingException
        Parses terraform syntax as it comes from a File.
        Parameters:
        input - A source file to process with a default charset of UTF-8
        ignoreParserExceptions - if set to true, we ignore any parse exceptions and still return the symbol map
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
        java.io.UnsupportedEncodingException
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input)
                                                                     throws HCLParserException,
                                                                            java.io.IOException,
                                                                            java.io.UnsupportedEncodingException
        Parses terraform syntax as it comes from a File.
        Parameters:
        input - A source file to process with a default charset of UTF-8
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
        java.io.UnsupportedEncodingException
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input,
                                                                            java.nio.charset.Charset cs)
                                                                     throws HCLParserException,
                                                                            java.io.IOException
        Parses terraform syntax as it comes from a File. closed at the end of the parse operation (commonly via wrapping in a finally block)
        Parameters:
        input - A source file to process
        cs - A charset
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input,
                                                                            java.lang.String charsetName)
                                                                     throws HCLParserException,
                                                                            java.io.IOException
        Parses terraform syntax as it comes from a File.
        Parameters:
        input - A source file to process
        charsetName - The name of a supported charset
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
        java.io.UnsupportedEncodingException - If the charset ( UTF-8 by default if unspecified) encoding is not supported
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.File input,
                                                                            java.lang.String charsetName,
                                                                            java.lang.Boolean ignoreParserExceptions)
                                                                     throws HCLParserException,
                                                                            java.io.IOException
        Parses terraform syntax as it comes from a File.
        Parameters:
        input - A source file to process
        charsetName - The name of a supported charset
        ignoreParserExceptions - if set to true, we ignore any parse exceptions and still return the symbol map
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
        java.io.UnsupportedEncodingException - If the charset ( UTF-8 by default if unspecified) encoding is not supported
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.InputStream input)
                                                                     throws HCLParserException,
                                                                            java.io.IOException
        Parses terraform syntax as it comes from an input stream. The end user is responsible for ensuring the stream is closed at the end of the parse operation (commonly via wrapping in a finally block)
        Parameters:
        input - Streamable input of text going to the lexer
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.InputStream input,
                                                                            java.nio.charset.Charset cs)
                                                                     throws HCLParserException,
                                                                            java.io.IOException
        Parses terraform syntax as it comes from an input stream. The end user is responsible for ensuring the stream is closed at the end of the parse operation (commonly via wrapping in a finally block)
        Parameters:
        input - Streamable input of text going to the lexer
        cs - CharSet with which to read the contents of the stream (default UTF-8)
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.InputStream input,
                                                                            java.lang.String charsetName)
                                                                     throws HCLParserException,
                                                                            java.io.IOException,
                                                                            java.io.UnsupportedEncodingException
        Parses terraform syntax as it comes from an input stream. The end user is responsible for ensuring the stream is closed at the end of the parse operation (commonly via wrapping in a finally block)
        Parameters:
        input - Streamable input of text going to the lexer
        charsetName - String lookup of the character set this stream is providing (default UTF-8)
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
        java.io.UnsupportedEncodingException - If the charset ( UTF-8 by default if unspecified) encoding is not supported.
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.InputStream input,
                                                                            java.lang.String charsetName,
                                                                            java.lang.Boolean ignoreParserExceptions)
                                                                     throws HCLParserException,
                                                                            java.io.IOException,
                                                                            java.io.UnsupportedEncodingException
        Parses terraform syntax as it comes from an input stream. The end user is responsible for ensuring the stream is closed at the end of the parse operation (commonly via wrapping in a finally block)
        Parameters:
        input - Streamable input of text going to the lexer
        charsetName - String lookup of the character set this stream is providing (default UTF-8)
        ignoreParserExceptions - if set to true, we ignore any parse exceptions and still return the symbol map
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
        java.io.UnsupportedEncodingException - If the charset ( UTF-8 by default if unspecified) encoding is not supported.
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.Reader reader)
                                                                     throws HCLParserException,
                                                                            java.io.IOException
        Parses terraform configuration language from a Reader
        Parameters:
        reader - A reader object used for absorbing various streams or String variables containing the hcl code
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
      • parse

        public java.util.Map<java.lang.String,​java.lang.Object> parse​(java.io.Reader reader,
                                                                            java.lang.Boolean ignoreParserExceptions)
                                                                     throws HCLParserException,
                                                                            java.io.IOException
        Parses terraform configuration language from a Reader
        Parameters:
        reader - A reader object used for absorbing various streams or String variables containing the hcl code
        ignoreParserExceptions - if set to true, we ignore any parse exceptions and still return the symbol map
        Returns:
        Mapped result of object tree coming from HCL (values of keys can be variable).
        Throws:
        HCLParserException - Any type of parsing errors are returned as this exception if the syntax is invalid.
        java.io.IOException - In the event the reader is unable to pull from the input source this exception is thrown.
      • flattenContext

        protected java.lang.Object flattenContext​(java.lang.Object context)
      • evaluateEvalSymbol

        protected java.lang.Object evaluateEvalSymbol​(EvalSymbol evalSymbol)
      • evaluateVariable

        protected java.lang.Object evaluateVariable​(Variable var)
      • getVariableObject

        protected java.lang.Object getVariableObject​(java.lang.String varName,
                                                     java.lang.Object source)