Package com.bertramlabs.plugins.hcl4j
Class HCLParser
- java.lang.Object
-
- com.bertramlabs.plugins.hcl4j.HCLParser
-
public class HCLParser extends java.lang.ObjectParser for the Hashicorp Configuration Language (HCL). This is the primary endpoint and converts the HCL syntax into aMap. 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>functionRegistryprotected java.util.Map<java.lang.String,java.lang.Object>resultprotected 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.ObjectevaluateEvalSymbol(EvalSymbol evalSymbol)protected java.lang.ObjectevaluateFunctionCall(java.lang.String functionName, Function functionSymbol)protected java.lang.ObjectevaluateVariable(Variable var)protected java.lang.ObjectflattenContext(java.lang.Object context)protected java.lang.ObjectgetVariableObject(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 Readerjava.util.Map<java.lang.String,java.lang.Object>parse(java.io.Reader reader, java.lang.Boolean ignoreParserExceptions)Parses terraform configuration language from a Readerjava.util.Map<java.lang.String,java.lang.Object>parse(java.lang.String input)Parses terraform configuration language from a Stringjava.util.Map<java.lang.String,java.lang.Object>parse(java.lang.String input, java.lang.Boolean ignoreParserExceptions)Parses terraform configuration language from a Stringjava.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.ObjectprocessEvaluation(EvalSymbol evalSymbol, java.lang.Object context)protected java.lang.ObjectprocessValue(HCLValue value)voidregisterFunction(java.lang.String functionName, HCLFunction function)Registers Function implementations for HCL Common functions.voidsetVariable(java.lang.String variableName, java.lang.Object value)Sets a variable value individually.voidsetVariables(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
-
-
-
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
-
-
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.IOExceptionParses 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 theHCLBaseFunctionsare loaded. Additional functions can be defined via this method for custom method overrides if necessary.- Parameters:
functionName- the name of the function to be calledfunction- 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 definedvalue- 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.IOExceptionParses 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.IOExceptionParses terraform configuration language from a String- Parameters:
input- String input containing HCL syntaxignoreParserExceptions- 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.UnsupportedEncodingExceptionParses terraform syntax as it comes from a File.- Parameters:
input- A source file to process with a default charset of UTF-8ignoreParserExceptions- 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.UnsupportedEncodingExceptionParses 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.IOExceptionParses 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 processcs- 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.IOExceptionParses terraform syntax as it comes from a File.- Parameters:
input- A source file to processcharsetName- 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.IOExceptionParses terraform syntax as it comes from a File.- Parameters:
input- A source file to processcharsetName- The name of a supported charsetignoreParserExceptions- 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.IOExceptionParses 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.IOExceptionParses 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 lexercs- 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.UnsupportedEncodingExceptionParses 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 lexercharsetName- 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.UnsupportedEncodingExceptionParses 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 lexercharsetName- 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.IOExceptionParses 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.IOExceptionParses terraform configuration language from a Reader- Parameters:
reader- A reader object used for absorbing various streams or String variables containing the hcl codeignoreParserExceptions- 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.
-
processValue
protected java.lang.Object processValue(HCLValue value) throws HCLParserException
- Throws:
HCLParserException
-
evaluateFunctionCall
protected java.lang.Object evaluateFunctionCall(java.lang.String functionName, Function functionSymbol) throws HCLParserException- Throws:
HCLParserException
-
processEvaluation
protected java.lang.Object processEvaluation(EvalSymbol evalSymbol, java.lang.Object context) throws HCLParserException
- Throws:
HCLParserException
-
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)
-
-