-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add interfaces and coordinating types for type checker.
Add a stub implementation. PiperOrigin-RevId: 673541214
- Loading branch information
1 parent
60b119f
commit 9d1b1f0
Showing
12 changed files
with
616 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
package( | ||
# Under active development, not yet being released. | ||
default_visibility = ["//visibility:public"], | ||
) | ||
|
||
cc_library( | ||
name = "type_check_issue", | ||
srcs = ["type_check_issue.cc"], | ||
hdrs = ["type_check_issue.h"], | ||
deps = [ | ||
"//common:source", | ||
"@com_google_absl//absl/strings", | ||
"@com_google_absl//absl/strings:str_format", | ||
"@com_google_absl//absl/strings:string_view", | ||
], | ||
) | ||
|
||
cc_test( | ||
name = "type_check_issue_test", | ||
srcs = ["type_check_issue_test.cc"], | ||
deps = [ | ||
":type_check_issue", | ||
"//common:source", | ||
"//internal:status_macros", | ||
"//internal:testing", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "validation_result", | ||
hdrs = ["validation_result.h"], | ||
deps = [ | ||
":type_check_issue", | ||
"//common:ast", | ||
"@com_google_absl//absl/base:nullability", | ||
"@com_google_absl//absl/status", | ||
"@com_google_absl//absl/status:statusor", | ||
"@com_google_absl//absl/types:span", | ||
], | ||
) | ||
|
||
cc_test( | ||
name = "validation_result_test", | ||
srcs = ["validation_result_test.cc"], | ||
deps = [ | ||
":type_check_issue", | ||
":validation_result", | ||
"//base/ast_internal:ast_impl", | ||
"//internal:testing", | ||
"@com_google_absl//absl/status", | ||
"@com_google_absl//absl/status:status_matchers", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "type_checker", | ||
hdrs = ["type_checker.h"], | ||
deps = [ | ||
":validation_result", | ||
"//common:ast", | ||
"@com_google_absl//absl/status:statusor", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "type_checker_builder", | ||
srcs = ["type_checker_builder.cc"], | ||
hdrs = ["type_checker_builder.h"], | ||
deps = [ | ||
":type_checker", | ||
"//checker/internal:type_checker_impl", | ||
"@com_google_absl//absl/status:statusor", | ||
], | ||
) |
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,32 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
package( | ||
# Implementation details for the checker library. | ||
default_visibility = ["//visibility:public"], | ||
) | ||
|
||
cc_library( | ||
name = "type_checker_impl", | ||
srcs = ["type_checker_impl.cc"], | ||
hdrs = ["type_checker_impl.h"], | ||
deps = [ | ||
"//checker:type_check_issue", | ||
"//checker:type_checker", | ||
"//checker:validation_result", | ||
"//common:ast", | ||
"//common:source", | ||
"@com_google_absl//absl/status:statusor", | ||
], | ||
) |
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,33 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "checker/internal/type_checker_impl.h" | ||
|
||
#include <memory> | ||
|
||
#include "absl/status/statusor.h" | ||
#include "checker/type_check_issue.h" | ||
#include "checker/validation_result.h" | ||
#include "common/ast.h" | ||
#include "common/source.h" | ||
|
||
namespace cel::checker_internal { | ||
|
||
absl::StatusOr<ValidationResult> TypeCheckerImpl::Check( | ||
std::unique_ptr<Ast> ast) const { | ||
return ValidationResult( | ||
{TypeCheckIssue::CreateError(SourceLocation{}, "Not yet implemented")}); | ||
} | ||
|
||
} // namespace cel::checker_internal |
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,45 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef THIRD_PARTY_CEL_CPP_CHECKER_INTERNAL_TYPE_CHECKER_IMPL_H_ | ||
#define THIRD_PARTY_CEL_CPP_CHECKER_INTERNAL_TYPE_CHECKER_IMPL_H_ | ||
|
||
#include <memory> | ||
|
||
#include "absl/status/statusor.h" | ||
#include "checker/type_checker.h" | ||
#include "checker/validation_result.h" | ||
#include "common/ast.h" | ||
|
||
namespace cel::checker_internal { | ||
|
||
// Implementation of the TypeChecker interface. | ||
// | ||
// See cel::TypeCheckerBuilder for constructing instances. | ||
class TypeCheckerImpl : public TypeChecker { | ||
public: | ||
TypeCheckerImpl() = default; | ||
|
||
TypeCheckerImpl(const TypeCheckerImpl&) = delete; | ||
TypeCheckerImpl& operator=(const TypeCheckerImpl&) = delete; | ||
TypeCheckerImpl(TypeCheckerImpl&&) = delete; | ||
TypeCheckerImpl& operator=(TypeCheckerImpl&&) = delete; | ||
|
||
absl::StatusOr<ValidationResult> Check( | ||
std::unique_ptr<Ast> ast) const override; | ||
}; | ||
|
||
} // namespace cel::checker_internal | ||
|
||
#endif // THIRD_PARTY_CEL_CPP_CHECKER_INTERNAL_TYPE_CHECKER_IMPL_H_ |
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,53 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "checker/type_check_issue.h" | ||
|
||
#include <string> | ||
|
||
#include "absl/strings/str_cat.h" | ||
#include "absl/strings/str_format.h" | ||
#include "absl/strings/string_view.h" | ||
#include "common/source.h" | ||
|
||
namespace cel { | ||
|
||
namespace { | ||
|
||
absl::string_view SeverityString(TypeCheckIssue::Severity severity) { | ||
switch (severity) { | ||
case TypeCheckIssue::Severity::kInformation: | ||
return "INFORMATION"; | ||
case TypeCheckIssue::Severity::kWarning: | ||
return "WARNING"; | ||
case TypeCheckIssue::Severity::kError: | ||
return "ERROR"; | ||
case TypeCheckIssue::Severity::kDeprecated: | ||
return "DEPRECATED"; | ||
default: | ||
return "SEVERITY_UNSPECIFIED"; | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
std::string TypeCheckIssue::ToDisplayString(const Source& source) const { | ||
return absl::StrCat( | ||
absl::StrFormat("%s: %s:%d:%d: %s", SeverityString(severity_), | ||
source.description(), location_.line, location_.column, | ||
message_), | ||
source.DisplayErrorLocation(location_)); | ||
} | ||
|
||
} // namespace cel |
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,65 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef THIRD_PARTY_CEL_CPP_CHECKER_TYPE_CHECK_ISSUE_H_ | ||
#define THIRD_PARTY_CEL_CPP_CHECKER_TYPE_CHECK_ISSUE_H_ | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "absl/strings/string_view.h" | ||
#include "common/source.h" | ||
|
||
namespace cel { | ||
|
||
// Represents a single issue identified in type checking. | ||
class TypeCheckIssue { | ||
public: | ||
enum class Severity { kError, kWarning, kInformation, kDeprecated }; | ||
|
||
TypeCheckIssue(Severity severity, SourceLocation location, | ||
std::string message) | ||
: severity_(severity), | ||
location_(location), | ||
message_(std::move(message)) {} | ||
|
||
// Factory for error-severity issues. | ||
static TypeCheckIssue CreateError(SourceLocation location, | ||
std::string message) { | ||
return TypeCheckIssue(Severity::kError, location, std::move(message)); | ||
} | ||
|
||
// Factory for error-severity issues. | ||
// line is 1-based, column is 0-based. | ||
static TypeCheckIssue CreateError(int line, int column, std::string message) { | ||
return TypeCheckIssue(Severity::kError, SourceLocation{line, column}, | ||
std::move(message)); | ||
} | ||
|
||
// Format the issue highlighting the source position. | ||
std::string ToDisplayString(const Source& source) const; | ||
|
||
absl::string_view message() const { return message_; } | ||
Severity severity() const { return severity_; } | ||
SourceLocation location() const { return location_; } | ||
|
||
private: | ||
Severity severity_; | ||
SourceLocation location_; | ||
std::string message_; | ||
}; | ||
|
||
} // namespace cel | ||
|
||
#endif // THIRD_PARTY_CEL_CPP_CHECKER_TYPE_CHECK_ISSUE_H_ |
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,47 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "checker/type_check_issue.h" | ||
|
||
#include "common/source.h" | ||
#include "internal/testing.h" | ||
|
||
namespace cel { | ||
namespace { | ||
|
||
TEST(TypeCheckIssueTest, DisplayString) { | ||
ASSERT_OK_AND_ASSIGN(auto source, NewSource("test{\n\tfield1: 123\n}")); | ||
TypeCheckIssue issue = TypeCheckIssue::CreateError(2, 2, "test error"); | ||
EXPECT_EQ(issue.ToDisplayString(*source), | ||
"ERROR: <input>:2:2: test error\n" | ||
" | field1: 123\n" | ||
" | ..^"); | ||
} | ||
|
||
TEST(TypeCheckIssueTest, DisplayStringNoPosition) { | ||
ASSERT_OK_AND_ASSIGN(auto source, NewSource("test{\n\tfield1: 123\n}")); | ||
TypeCheckIssue issue = TypeCheckIssue::CreateError(-1, -1, "test error"); | ||
EXPECT_EQ(issue.ToDisplayString(*source), "ERROR: <input>:-1:-1: test error"); | ||
} | ||
|
||
TEST(TypeCheckIssueTest, DisplayStringDeprecated) { | ||
ASSERT_OK_AND_ASSIGN(auto source, NewSource("test{\n\tfield1: 123\n}")); | ||
TypeCheckIssue issue = TypeCheckIssue(TypeCheckIssue::Severity::kDeprecated, | ||
{-1, -1}, "test error 2"); | ||
EXPECT_EQ(issue.ToDisplayString(*source), | ||
"DEPRECATED: <input>:-1:-1: test error 2"); | ||
} | ||
|
||
} // namespace | ||
} // namespace cel |
Oops, something went wrong.