ParseExpression parses the given buffer as a standalone HCL expression, returning it as an instance of Expression.
(src []byte, filename string, start hcl.Pos)
| 42 | // ParseExpression parses the given buffer as a standalone HCL expression, |
| 43 | // returning it as an instance of Expression. |
| 44 | func ParseExpression(src []byte, filename string, start hcl.Pos) (Expression, hcl.Diagnostics) { |
| 45 | tokens, diags := LexExpression(src, filename, start) |
| 46 | peeker := newPeeker(tokens, false) |
| 47 | parser := &parser{peeker: peeker} |
| 48 | |
| 49 | // Bare expressions are always parsed in "ignore newlines" mode, as if |
| 50 | // they were wrapped in parentheses. |
| 51 | parser.PushIncludeNewlines(false) |
| 52 | |
| 53 | expr, parseDiags := parser.ParseExpression() |
| 54 | diags = append(diags, parseDiags...) |
| 55 | |
| 56 | next := parser.Peek() |
| 57 | if next.Type != TokenEOF && !parser.recovery { |
| 58 | diags = append(diags, &hcl.Diagnostic{ |
| 59 | Severity: hcl.DiagError, |
| 60 | Summary: "Extra characters after expression", |
| 61 | Detail: "An expression was successfully parsed, but extra characters were found after it.", |
| 62 | Subject: &next.Range, |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | parser.PopIncludeNewlines() |
| 67 | |
| 68 | // Panic if the parser uses incorrect stack discipline with the peeker's |
| 69 | // newlines stack, since otherwise it will produce confusing downstream |
| 70 | // errors. |
| 71 | peeker.AssertEmptyIncludeNewlinesStack() |
| 72 | |
| 73 | return expr, diags |
| 74 | } |
| 75 | |
| 76 | // ParseTemplate parses the given buffer as a standalone HCL template, |
| 77 | // returning it as an instance of Expression. |