本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
892B

  1. package hcl
  2. import (
  3. "fmt"
  4. "github.com/hashicorp/hcl/hcl/ast"
  5. hclParser "github.com/hashicorp/hcl/hcl/parser"
  6. jsonParser "github.com/hashicorp/hcl/json/parser"
  7. )
  8. // ParseBytes accepts as input byte slice and returns ast tree.
  9. //
  10. // Input can be either JSON or HCL
  11. func ParseBytes(in []byte) (*ast.File, error) {
  12. return parse(in)
  13. }
  14. // ParseString accepts input as a string and returns ast tree.
  15. func ParseString(input string) (*ast.File, error) {
  16. return parse([]byte(input))
  17. }
  18. func parse(in []byte) (*ast.File, error) {
  19. switch lexMode(in) {
  20. case lexModeHcl:
  21. return hclParser.Parse(in)
  22. case lexModeJson:
  23. return jsonParser.Parse(in)
  24. }
  25. return nil, fmt.Errorf("unknown config format")
  26. }
  27. // Parse parses the given input and returns the root object.
  28. //
  29. // The input format can be either HCL or JSON.
  30. func Parse(input string) (*ast.File, error) {
  31. return parse([]byte(input))
  32. }
上海开阖软件有限公司 沪ICP备12045867号-1