From 630c998146a9cb28f1e1e6380101b37253073007 Mon Sep 17 00:00:00 2001 From: David Estes Date: Thu, 29 Feb 2024 13:39:43 -0500 Subject: [PATCH] fixing hcl4j bug with \ --- .../bertramlabs/plugins/hcl4j/HCLParser.java | 23 ++++++++++++++----- .../bertramlabs/plugins/hcl4j/HCLLexer.jflex | 2 +- .../plugins/hcl4j/HCLParserSpec.groovy | 5 ++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java b/src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java index 597dd42..d0405ab 100644 --- a/src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java +++ b/src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java @@ -945,15 +945,18 @@ protected Object processValue(HCLValue value) throws HCLParserException { } protected Object evaluateFunctionCall(String functionName,Function functionSymbol) throws HCLParserException { + return evaluateFunctionCall(functionName,functionSymbol,null); + } + protected Object evaluateFunctionCall(String functionName,Function functionSymbol, Map localVars) throws HCLParserException { if(functionRegistry.get(functionName) != null) { HCLFunction functionMethod = functionRegistry.get(functionName); ArrayList functionArguments = new ArrayList<>(); for(Symbol child : functionSymbol.getChildren()) { Object elementResult = null; if(child instanceof EvalSymbol) { - elementResult = processEvaluation((EvalSymbol) child,null); + elementResult = processEvaluation((EvalSymbol) child,null,localVars); } else if(child instanceof Symbol) { - elementResult = processSymbol(child,result); + elementResult = processSymbol(child,result,localVars); } if(elementResult instanceof VariableTree) { //we were unable to evaluate a sub argument... abort function evaluation return null; // function evaluation aborted. perhaps throw error for later based on if ignoreExceptions is on or not @@ -969,7 +972,10 @@ protected Object evaluateFunctionCall(String functionName,Function functionSymbo protected Object evaluateComputedTuple(ComputedTuple thisTuple) throws HCLParserException { -// System.out.println("Evaluating Tuple"); + return evaluateComputedTuple(thisTuple,null); + } + protected Object evaluateComputedTuple(ComputedTuple thisTuple, Map localVars) throws HCLParserException { + ArrayList computedResult = new ArrayList(); Variable indexVariable = null; Variable valueVariable = null; @@ -984,6 +990,7 @@ protected Object evaluateComputedTuple(ComputedTuple thisTuple) throws HCLParser log.warn("Computed Tuple loop found with too many variables at line {}",thisTuple.getLine()); return null; } + System.out.println("Evaluating Tuple: " + valueVariable.getName()); ForConditional tupleConditional = null; for(Symbol child : thisTuple.getChildren()) { if(child instanceof ForConditional) { @@ -1009,12 +1016,16 @@ protected Object evaluateComputedTuple(ComputedTuple thisTuple) throws HCLParser } if(sourceExpression.getChildren().size() > 0 && iteratorExpression.getChildren().size() > 0) { - elementResult = processSymbol(sourceExpression,result); + elementResult = processSymbol(sourceExpression,result,localVars); if(elementResult instanceof List) { List elementResultList = (List)elementResult; System.out.println("Iterating over list"); for(int counter=0;counter < elementResultList.size();counter++){ + Map stackVars = new LinkedHashMap<>(); + if(localVars != null) { + stackVars = new LinkedHashMap<>(localVars); + } if(indexVariable != null) { stackVars.put(indexVariable.getName(),counter); } @@ -1210,14 +1221,14 @@ protected Object processEvaluation(EvalSymbol evalSymbol, Object context,Map 0) { - return evaluateFunctionCall(thisFunction.getName(),thisFunction); + return evaluateFunctionCall(thisFunction.getName(),thisFunction,stackVars); } else { return null; } } else if(evalSymbol instanceof ComputedTuple) { //time to actually implemented a tuple loop ComputedTuple thisTuple = (ComputedTuple) evalSymbol; - return evaluateComputedTuple(thisTuple); + return evaluateComputedTuple(thisTuple,stackVars); } else if(evalSymbol instanceof Variable) { System.out.println("Evaluating Variable: " + evalSymbol.getName()); diff --git a/src/main/jflex/com/bertramlabs/plugins/hcl4j/HCLLexer.jflex b/src/main/jflex/com/bertramlabs/plugins/hcl4j/HCLLexer.jflex index f685804..9cb1942 100644 --- a/src/main/jflex/com/bertramlabs/plugins/hcl4j/HCLLexer.jflex +++ b/src/main/jflex/com/bertramlabs/plugins/hcl4j/HCLLexer.jflex @@ -435,7 +435,7 @@ AssignmentExpression = [^] \\t { string.append('\t'); } \\n { string.append('\n'); } \\r { string.append('\r'); } - \\ { string.append('\\'); } + \\\\ { string.append('\\'); } } { diff --git a/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy b/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy index 7763edb..cbcb9a0 100644 --- a/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy +++ b/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy @@ -1212,14 +1212,15 @@ swagger_path_method_parameters = [for my_value in local.swagger_path_methods_spl given: def hcl = ''' locals { - swagger_path_methods_split = [["a","b"],["c","d"]] + swagger_path_methods_split = [[0,"a"],[0,"b"]] json_data = { - paths = {a: {b: [1,2]}, c:{d: [3,4]]}} + paths = [{a: "test"},{b:"test2"}] } swagger_path_method_parameters = [for my_value in local.swagger_path_methods_split: [for val2 in local.json_data.paths[my_value[0]][my_value[1]] : val2] ] } + ''' HCLParser parser = new HCLParser(); when: