Check performs type-checking on the input Ast and yields a checked Ast and/or set of Issues. If any `ASTValidators` are configured on the environment, they will be applied after a valid type-check result. If any issues are detected, the validators will provide them on the output Issues object. Eith
(ast *Ast)
| 479 | // It is possible to have both non-nil Ast and Issues values returned from this call: however, |
| 480 | // the mere presence of an Ast does not imply that it is valid for use. |
| 481 | func (e *Env) Check(ast *Ast) (*Ast, *Issues) { |
| 482 | // Surface any error recorded while the Ast was loaded (e.g. an over-deep AST rejected by |
| 483 | // ParsedExprToAst / CheckedExprToAst) before recursing into the type checker on it. |
| 484 | if ast != nil && ast.loadErr != nil { |
| 485 | errs := common.NewErrors(ast.Source()) |
| 486 | errs.ReportErrorString(common.NoLocation, ast.loadErr.Error()) |
| 487 | return nil, NewIssuesWithSourceInfo(errs, ast.NativeRep().SourceInfo()) |
| 488 | } |
| 489 | if nodeLimit := e.configuredExpressionNodeLimit(); nodeLimit > 0 && ast != nil && ast.NativeRep() != nil { |
| 490 | if count := celast.NodeCount(ast.NativeRep()); count > nodeLimit { |
| 491 | errs := common.NewErrors(ast.Source()) |
| 492 | errs.ReportErrorString(common.NoLocation, fmt.Sprintf("expression node count exceeds limit: count %d, limit %d", count, nodeLimit)) |
| 493 | return nil, NewIssuesWithSourceInfo(errs, ast.NativeRep().SourceInfo()) |
| 494 | } |
| 495 | } |
| 496 | // Construct the internal checker env, erroring if there is an issue adding the declarations. |
| 497 | chk, err := e.initChecker() |
| 498 | if err != nil { |
| 499 | errs := common.NewErrors(ast.Source()) |
| 500 | errs.ReportErrorString(common.NoLocation, err.Error()) |
| 501 | return nil, NewIssuesWithSourceInfo(errs, ast.NativeRep().SourceInfo()) |
| 502 | } |
| 503 | |
| 504 | checked, errs := checker.Check(ast.NativeRep(), ast.Source(), chk) |
| 505 | if len(errs.GetErrors()) > 0 { |
| 506 | return nil, NewIssuesWithSourceInfo(errs, ast.NativeRep().SourceInfo()) |
| 507 | } |
| 508 | // Manually create the Ast to ensure that the Ast source information (which may be more |
| 509 | // detailed than the information provided by Check), is returned to the caller. |
| 510 | ast = &Ast{ |
| 511 | source: ast.Source(), |
| 512 | impl: checked} |
| 513 | |
| 514 | // Avoid creating a validator config if it's not needed. |
| 515 | if len(e.validators) == 0 { |
| 516 | return ast, nil |
| 517 | } |
| 518 | |
| 519 | // Generate a validator configuration from the set of configured validators. |
| 520 | vConfig := newValidatorConfig() |
| 521 | for _, v := range e.validators { |
| 522 | if cv, ok := v.(ASTValidatorConfigurer); ok { |
| 523 | cv.Configure(vConfig) |
| 524 | } |
| 525 | } |
| 526 | // Apply additional validators on the type-checked result. |
| 527 | iss := NewIssuesWithSourceInfo(errs, ast.NativeRep().SourceInfo()) |
| 528 | for _, v := range e.validators { |
| 529 | v.Validate(e, vConfig, checked, iss) |
| 530 | } |
| 531 | if iss.Err() != nil { |
| 532 | return nil, iss |
| 533 | } |
| 534 | return ast, nil |
| 535 | } |
| 536 | |
| 537 | // configuredExpressionSizeLimit returns the effective expression size code point limit. |
| 538 | // A zero value means "use the parser default". |