-
Notifications
You must be signed in to change notification settings - Fork 1
Add pluggable regex engine support #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c16cce8
Add pluggable regex support
rayokota d8a074c
copilot feedback
rayokota 35ece88
copilot feedback
rayokota 78c8e7a
copilot feedback
rayokota da814c1
copilot feedback
rayokota 5ea946e
copilot feedback
rayokota dc1c4eb
Add timeout and stack guardrails
rayokota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,84 @@ | ||
| /** | ||
| * jsonata-cpp is the JSONata C++ reference port | ||
| * | ||
| * Copyright Robert Yokota | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <functional> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace jsonata { | ||
|
|
||
| // A single match, engine-agnostic (the equivalent of std::smatch, but not | ||
| // tied to std::regex so alternative engines such as RE2 can produce it too). | ||
| struct RegexMatch { | ||
| std::string text; | ||
| size_t position = 0; | ||
| size_t length = 0; | ||
| // groups[0] is capture group 1, matching std::smatch's [1] indexing | ||
| // offset by the implicit full-match group 0. | ||
| std::vector<std::optional<std::string>> groups; | ||
| }; | ||
|
|
||
| // Flags parsed from a JSONata regex literal's /pattern/flags suffix. Engines | ||
| // translate these into their own native flag representation. | ||
| struct RegexFlags { | ||
| bool caseInsensitive = false; | ||
| bool multiline = false; | ||
| }; | ||
|
|
||
| // Engine-agnostic compiled regex. JSONata regex literals (and patterns given | ||
| // as plain strings to functions like $match/$replace/$split) are compiled | ||
| // into this interface, so the evaluator never depends on std::regex | ||
| // directly. The default engine wraps std::regex (see StdRegex.h); a custom | ||
| // engine (e.g. RE2) can be injected via Jsonata's constructor. | ||
| class IRegex { | ||
| public: | ||
| virtual ~IRegex() = default; | ||
|
|
||
| // True if the pattern matches anywhere in str. Backs $contains. | ||
| virtual bool test(const std::string& str) const = 0; | ||
|
|
||
| // The first match starting the search no earlier than byte offset `pos`. | ||
| // Backs $replace's first/limited-match paths, and lazy one-at-a-time | ||
| // iteration when a regex literal is invoked as a function -- callers | ||
| // that only want the first few matches of a large input should call | ||
| // this repeatedly (advancing `pos` past each match) rather than use | ||
| // findAll, which materializes every match up front. | ||
| virtual std::optional<RegexMatch> findFirst(const std::string& str, | ||
| size_t pos = 0) const = 0; | ||
|
|
||
| // All non-overlapping matches, in order. Backs $match. | ||
| virtual std::vector<RegexMatch> findAll(const std::string& str) const = 0; | ||
|
|
||
| // Splits str on every match, returning the segments between matches | ||
| // (including empty leading/trailing segments). Backs $split. | ||
| virtual std::vector<std::string> split(const std::string& str) const = 0; | ||
| }; | ||
|
|
||
| // Compiles a pattern into an IRegex. Used both for JSONata regex literals | ||
| // and for patterns supplied as plain strings at runtime. | ||
| using RegexEngine = std::function<std::shared_ptr<IRegex>( | ||
| const std::string& pattern, RegexFlags flags)>; | ||
|
|
||
| // The built-in std::regex-backed engine; this is jsonata-cpp's default and | ||
| // preserves its pre-existing regex behavior exactly. | ||
| RegexEngine defaultRegexEngine(); | ||
|
|
||
| } // namespace jsonata |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,41 @@ | ||
| /** | ||
| * jsonata-cpp is the JSONata C++ reference port | ||
| * | ||
| * Copyright Robert Yokota | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <regex> | ||
|
|
||
| #include "jsonata/IRegex.h" | ||
|
|
||
| namespace jsonata { | ||
|
|
||
| // Default IRegex implementation, backed by std::regex (ECMAScript grammar). | ||
| class StdRegex : public IRegex { | ||
| public: | ||
| StdRegex(const std::string& pattern, RegexFlags flags); | ||
|
|
||
| bool test(const std::string& str) const override; | ||
| std::optional<RegexMatch> findFirst(const std::string& str, size_t pos) const override; | ||
| std::vector<RegexMatch> findAll(const std::string& str) const override; | ||
| std::vector<std::string> split(const std::string& str) const override; | ||
|
|
||
| private: | ||
| std::regex regex_; | ||
| static RegexMatch toRegexMatch(const std::smatch& m); | ||
| }; | ||
|
|
||
| } // namespace jsonata |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.