-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from CodeKaio/hcl-parsing
✨ : HCL parsing
- Loading branch information
Showing
20 changed files
with
2,735 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
grammar hcl; | ||
|
||
file | ||
: directive+ | ||
; | ||
|
||
directive | ||
: variableDirective | ||
| outputDirective | ||
; | ||
|
||
variableDirective | ||
: 'variable' STRING variableBlock | ||
; | ||
|
||
variableBlock | ||
: '{' (variableType|variableDescription|variableDefault)+ '}' | ||
; | ||
|
||
outputDirective | ||
: 'output' STRING outputBlock | ||
; | ||
|
||
outputBlock | ||
: '{' (outputValue|outputDescription|outputSensitive)+ '}' | ||
; | ||
|
||
outputValue | ||
: 'value' '=' expression | ||
; | ||
|
||
outputDescription | ||
: 'description' '=' STRING | ||
; | ||
|
||
outputSensitive | ||
: 'sensitive' '=' BOOLEAN | ||
; | ||
|
||
variableType | ||
: 'type' '=' type | ||
; | ||
|
||
type | ||
: TYPE | ||
| 'list' | ||
| 'list(' type ')' | ||
| 'map(' type ')' | ||
| 'object' '(' object ')' | ||
; | ||
|
||
object | ||
: '{' '}' | ||
| '{' field+ '}' | ||
; | ||
|
||
field: IDENTIFIER '=' expression; | ||
|
||
variableDescription | ||
: 'description' '=' STRING | ||
; | ||
|
||
variableDefault | ||
: 'default' '=' expression | ||
; | ||
|
||
expression | ||
: STRING | ||
| NUMBER | ||
| BOOLEAN | ||
| array | ||
| object | ||
| complexExpression | ||
; | ||
|
||
functionCall | ||
: IDENTIFIER '(' expression ( ',' expression )* ','? ')' | ||
; | ||
|
||
complexExpression | ||
: IDENTIFIER | ||
| complexExpression '.' complexExpression // attribute access | ||
| complexExpression '[' index ']' // indexed array access | ||
| complexExpression '.' index // indexed attribute access | ||
| functionCall | ||
; | ||
|
||
array | ||
: '[' ']' | ||
| '[' expression ( ',' expression )* ','? ']' | ||
; | ||
|
||
index | ||
: NUMBER | ||
| '*' | ||
; | ||
|
||
BOOLEAN | ||
: 'true' | ||
| '"true"' | ||
| 'false' | ||
| '"false"' | ||
; | ||
|
||
TYPE | ||
: 'string' | ||
| '"string"' | ||
| 'number' | ||
| '"number"' | ||
| 'bool' | ||
| '"bool"' | ||
| 'any' | ||
; | ||
|
||
IDENTIFIER: Letter LetterOrDigit*; | ||
|
||
fragment LetterOrDigit | ||
: Letter | ||
| [0-9] | ||
; | ||
fragment Letter | ||
: [a-zA-Z$_] // these are the "java letters" below 0x7F | ||
| ~[\u0000-\u007F\uD800-\uDBFF] // covers all characters above 0x7F which are not a surrogate | ||
| [\uD800-\uDBFF] [\uDC00-\uDFFF] // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF | ||
; | ||
|
||
/** | ||
* STRING Lexer Rule comes from the JSON grammar | ||
* https://github.com/antlr/grammars-v4/blob/master/json/JSON.g4 | ||
*/ | ||
STRING | ||
: '"' (ESC | SAFECODEPOINT)* '"' | ||
; | ||
|
||
fragment ESC | ||
: '\\' (["\\/bfnrt] | UNICODE) | ||
; | ||
fragment UNICODE | ||
: 'u' HEX HEX HEX HEX | ||
; | ||
fragment HEX | ||
: [0-9a-fA-F] | ||
; | ||
fragment SAFECODEPOINT | ||
: ~ ["\\\u0000-\u001F] | ||
; | ||
|
||
NUMBER | ||
: '0' | [1-9] [0-9]* | ||
; | ||
|
||
// comments and whitespaces | ||
COMMENT: '/*' .*? '*/' -> channel(HIDDEN); | ||
LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN); | ||
HAS_COMMENT: '#' ~ [\r\n]* -> channel(HIDDEN); | ||
WS: [ \t\r\n]+ -> channel(HIDDEN); // skip spaces, tabs, newlines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.codeka.gaia.hcl | ||
|
||
import io.codeka.gaia.hcl.antlr.hclLexer | ||
import io.codeka.gaia.hcl.antlr.hclParser | ||
import io.codeka.gaia.modules.bo.Output | ||
import io.codeka.gaia.modules.bo.Variable | ||
import org.antlr.v4.runtime.CharStreams | ||
import org.antlr.v4.runtime.CommonTokenStream | ||
|
||
class HclParser { | ||
|
||
private fun parseContent(content: String): HclVisitor { | ||
// loading test file | ||
val charStream = CharStreams.fromString(content) | ||
|
||
// configuring antlr lexer | ||
val lexer = hclLexer(charStream) | ||
|
||
// using the lexer to configure a token stream | ||
val tokenStream = CommonTokenStream(lexer) | ||
|
||
// configuring antlr parser using the token stream | ||
val parser = hclParser(tokenStream) | ||
|
||
// visit the AST | ||
val hclVisitor = HclVisitor() | ||
hclVisitor.visit(parser.file()) | ||
return hclVisitor | ||
} | ||
|
||
fun parseVariables(content:String): List<Variable> { | ||
val hclVisitor = parseContent(content) | ||
return hclVisitor.variables | ||
} | ||
|
||
fun parseOutputs(content:String): List<Output> { | ||
val hclVisitor = parseContent(content) | ||
return hclVisitor.outputs | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.codeka.gaia.hcl | ||
|
||
import io.codeka.gaia.hcl.antlr.hclBaseVisitor | ||
import io.codeka.gaia.hcl.antlr.hclParser | ||
import io.codeka.gaia.modules.bo.Output | ||
import io.codeka.gaia.modules.bo.Variable | ||
import java.util.* | ||
|
||
class HclVisitor : hclBaseVisitor<Unit>() { | ||
|
||
var variables: MutableList<Variable> = LinkedList() | ||
var outputs: MutableList<Output> = LinkedList() | ||
|
||
private var currentVariable: Variable = Variable(name = "") | ||
private var currentOutput: Output = Output() | ||
|
||
override fun visitVariableDirective(ctx: hclParser.VariableDirectiveContext) { | ||
currentVariable = Variable(name = ctx.STRING().text.removeSurrounding("\"")) | ||
variables.add(currentVariable) | ||
visitChildren(ctx) | ||
} | ||
|
||
override fun visitVariableType(ctx: hclParser.VariableTypeContext) { | ||
currentVariable.type = ctx.type().text.removeSurrounding("\"") | ||
} | ||
|
||
override fun visitVariableDefault(ctx: hclParser.VariableDefaultContext) { | ||
currentVariable.defaultValue = ctx.expression().text.removeSurrounding("\"") | ||
} | ||
|
||
override fun visitVariableDescription(ctx: hclParser.VariableDescriptionContext) { | ||
currentVariable.description = ctx.STRING().text.removeSurrounding("\"") | ||
} | ||
|
||
override fun visitOutputDirective(ctx: hclParser.OutputDirectiveContext) { | ||
currentOutput = Output(name = ctx.STRING().text.removeSurrounding("\"")) | ||
outputs.add(currentOutput) | ||
visitChildren(ctx) | ||
} | ||
|
||
override fun visitOutputDescription(ctx: hclParser.OutputDescriptionContext) { | ||
currentOutput.description = ctx.STRING().text.removeSurrounding("\"") | ||
} | ||
|
||
override fun visitOutputValue(ctx: hclParser.OutputValueContext) { | ||
currentOutput.value = ctx.expression().text.removeSurrounding("\"") | ||
} | ||
|
||
override fun visitOutputSensitive(ctx: hclParser.OutputSensitiveContext) { | ||
currentOutput.sensitive = ctx.BOOLEAN().text.removeSurrounding("\"") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.codeka.gaia.modules.bo | ||
|
||
/** | ||
* Represents a module output | ||
*/ | ||
data class Output( | ||
var name: String = "", | ||
var value: String = "", | ||
var description: String = "", | ||
var sensitive: String = "false"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 0 additions & 71 deletions
71
src/main/java/io/codeka/gaia/modules/bo/TerraformVariable.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.