Skip to content

Commit

Permalink
fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Apr 22, 2024
1 parent 92e3621 commit 1512f64
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions internal/parser/hcl_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
switch token.Type {
case hclsyntax.TokenOBrace: // {
top := len(stack) - 1
if top < 0 {
return nil
}
state := stack[top]
key := state.GetCurrentKey()
if state.ExpectKey {
Expand Down Expand Up @@ -145,6 +148,9 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
})
case hclsyntax.TokenCBrace: // }
top := len(stack) - 1
if top < 0 {
return nil
}
state := stack[top]
if !state.ExpectKey {
log.Printf("[WARN] expect value but got }")
Expand All @@ -161,6 +167,9 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
stack = stack[0:top]
case hclsyntax.TokenOBrack: // [
top := len(stack) - 1
if top < 0 {
return nil
}
state := stack[top]
if state.ExpectKey {
log.Printf("[WARN] expect key but got [")
Expand Down Expand Up @@ -190,6 +199,9 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
})
case hclsyntax.TokenCBrack: // ]
top := len(stack) - 1
if top < 0 {
return nil
}
state := stack[top]
if state.Value != nil && *state.Value != "" && strings.Contains(*state.Value, "[") {
updateStateValue(&stack[top], token)
Expand All @@ -211,6 +223,9 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
stack = stack[0:top]
case hclsyntax.TokenQuotedLit:
top := len(stack) - 1
if top < 0 {
return nil
}
if stack[top].ExpectKey {
foundKey(&stack[top], token)
stack[top].CurrentHclNode.KeyValueFormat = QuotedKeyEqualValue
Expand All @@ -219,13 +234,19 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
}
case hclsyntax.TokenIdent:
top := len(stack) - 1
if top < 0 {
return nil
}
if stack[top].ExpectKey {
foundKey(&stack[top], token)
} else {
updateStateValue(&stack[top], token)
}
case hclsyntax.TokenColon: // :
top := len(stack) - 1
if top < 0 {
return nil
}
if stack[top].ExpectEqual {
if stack[top].ExpectKey {
log.Printf("[WARN] expect key but got =")
Expand All @@ -240,6 +261,9 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
}
case hclsyntax.TokenEqual: // =
top := len(stack) - 1
if top < 0 {
return nil
}
if stack[top].ExpectEqual {
if stack[top].ExpectKey {
log.Printf("[WARN] expect key but got =")
Expand All @@ -252,6 +276,9 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
}
case hclsyntax.TokenNewline:
top := len(stack) - 1
if top < 0 {
return nil
}
state := stack[top]
if !state.ExpectKey {
if stack[top].Index == nil {
Expand All @@ -271,6 +298,9 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
}
case hclsyntax.TokenComma:
top := len(stack) - 1
if top < 0 {
return nil
}
state := stack[top]
if !state.ExpectKey {
if state.Index == nil {
Expand All @@ -295,6 +325,9 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
comment := string(token.Bytes)
if strings.HasSuffix(comment, "\n") {
top := len(stack) - 1
if top < 0 {
return nil
}
state := stack[top]
if !state.ExpectKey {
if stack[top].Index == nil {
Expand All @@ -314,10 +347,10 @@ func BuildHclNode(tokens hclsyntax.Tokens) *HclNode {
}
}
default:
if len(stack) == 0 {
break
}
top := len(stack) - 1
if top < 0 {
return nil
}
if !stack[top].ExpectEqual {
updateStateValue(&stack[top], token)
}
Expand Down

0 comments on commit 1512f64

Please sign in to comment.