-
Notifications
You must be signed in to change notification settings - Fork 22
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
Simple pattern support + integer ranges #106
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -11,11 +11,13 @@ | |
#include <memory> | ||
#include <optional> | ||
#include <queue> | ||
#include <sstream> | ||
#include <string> | ||
#include <unordered_set> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "regex_converter.h" | ||
#include "support/logging.h" | ||
|
||
namespace xgrammar { | ||
|
@@ -652,6 +654,122 @@ std::string JSONSchemaConverter::VisitAny( | |
kBasicArray + " | " + kBasicObject; | ||
} | ||
|
||
std::string generateRangeRegex(std::optional<int> start, std::optional<int> end) { | ||
if (!start && !end) { | ||
return "^\\d+$"; // Match any positive number if no start or end is specified | ||
} | ||
|
||
std::vector<std::string> positiveParts; | ||
std::vector<std::string> negativeParts; | ||
|
||
auto generateGroup = [](int s, int e) -> std::string { | ||
std::ostringstream oss; | ||
|
||
if (s == e) { | ||
return std::to_string(s); | ||
} | ||
|
||
std::string startStr = std::to_string(s); | ||
std::string endStr = std::to_string(e); | ||
|
||
size_t commonPrefix = 0; | ||
while (commonPrefix < startStr.size() && startStr[commonPrefix] == endStr[commonPrefix]) { | ||
++commonPrefix; | ||
} | ||
|
||
if (commonPrefix > 0) { | ||
oss << startStr.substr(0, commonPrefix); | ||
} | ||
|
||
if (commonPrefix < startStr.size()) { | ||
oss << "["; | ||
oss << startStr[commonPrefix]; | ||
if (startStr[commonPrefix] != endStr[commonPrefix]) { | ||
oss << "-" << endStr[commonPrefix]; | ||
} | ||
oss << "]"; | ||
|
||
// Add trailing zero ranges | ||
if (commonPrefix + 1 < startStr.size()) { | ||
oss << "\\d{" << startStr.size() - commonPrefix - 1 << "}"; | ||
} | ||
} | ||
|
||
return oss.str(); | ||
}; | ||
|
||
if (start && end) { | ||
int rangeStart = start.value(); | ||
int rangeEnd = end.value(); | ||
|
||
// Handle negative part of the range | ||
if (rangeStart < 0) { | ||
int negativeEnd = std::min(rangeEnd, -1); | ||
while (rangeStart <= negativeEnd) { | ||
int nextRangeEnd = (rangeStart / 10 - 1) * 10 + 9; // Handle negative tens group | ||
if (nextRangeEnd < negativeEnd) { | ||
nextRangeEnd = negativeEnd; | ||
} | ||
negativeParts.push_back("-" + generateGroup(-nextRangeEnd, -rangeStart)); | ||
rangeStart = nextRangeEnd + 1; | ||
} | ||
} | ||
|
||
// Handle positive part of the range | ||
if (rangeEnd >= 0) { | ||
rangeStart = std::max(rangeStart, 0); | ||
while (rangeStart <= rangeEnd) { | ||
int nextRangeEnd = (rangeStart / 10 + 1) * 10 - 1; // Handle positive tens group | ||
if (nextRangeEnd > rangeEnd) { | ||
nextRangeEnd = rangeEnd; | ||
} | ||
positiveParts.push_back(generateGroup(rangeStart, nextRangeEnd)); | ||
rangeStart = nextRangeEnd + 1; | ||
} | ||
} | ||
} else if (start) { | ||
if (start.value() < 0) { | ||
negativeParts.push_back("-" + std::to_string(-start.value()) + "\\d*"); | ||
} else { | ||
positiveParts.push_back(std::to_string(start.value()) + "\\d*"); | ||
} | ||
} else if (end) { | ||
if (end.value() < 0) { | ||
negativeParts.push_back("-" + std::to_string(-end.value())); | ||
} else { | ||
positiveParts.push_back(std::to_string(end.value())); | ||
} | ||
} | ||
|
||
std::ostringstream result; | ||
result << "^("; | ||
if (!negativeParts.empty()) { | ||
result << "("; | ||
for (size_t i = 0; i < negativeParts.size(); ++i) { | ||
if (i > 0) { | ||
result << "|"; | ||
} | ||
result << negativeParts[i]; | ||
} | ||
result << ")"; | ||
if (!positiveParts.empty()) { | ||
result << "|"; | ||
} | ||
} | ||
if (!positiveParts.empty()) { | ||
result << "("; | ||
for (size_t i = 0; i < positiveParts.size(); ++i) { | ||
if (i > 0) { | ||
result << "|"; | ||
} | ||
result << positiveParts[i]; | ||
} | ||
result << ")"; | ||
} | ||
result << ")$"; | ||
return result.str(); | ||
} | ||
|
||
std::string JSONSchemaConverter::VisitInteger( | ||
const picojson::object& schema, const std::string& rule_name | ||
) { | ||
|
@@ -661,12 +779,38 @@ std::string JSONSchemaConverter::VisitInteger( | |
schema, | ||
{ | ||
"multipleOf", | ||
"minimum", | ||
"maximum", | ||
"exclusiveMinimum", | ||
"exclusiveMaximum", | ||
} | ||
); | ||
std::string range_regex = ""; | ||
try { | ||
if (schema.count("minimum") || schema.count("maximum") || schema.count("exclusiveMinimum") || | ||
schema.count("exclusiveMaximum")) { | ||
std::optional<int> start, end; | ||
if (schema.count("minimum")) { | ||
double start_double = schema.at("minimum").get<double>(); | ||
start = static_cast<int>(start_double); | ||
} | ||
if (schema.count("exclusiveMinimum")) { | ||
double start_double = schema.at("exclusiveMinimum").get<double>(); | ||
start = static_cast<int>(start_double); | ||
} | ||
if (schema.count("maximum")) { | ||
double end_double = schema.at("maximum").get<double>(); | ||
end = static_cast<int>(end_double); | ||
} | ||
if (schema.count("exclusiveMaximum")) { | ||
double end_double = schema.at("exclusiveMaximum").get<double>(); | ||
end = static_cast<int>(end_double); | ||
} | ||
range_regex = generateRangeRegex(start, end); | ||
} | ||
if (!range_regex.empty()) { | ||
std::string converted_regex = RegexToEBNF(range_regex, false); | ||
return converted_regex; // not " " for numbers | ||
} | ||
} catch (const std::exception& e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the usage of try-catch block and std::exception. The reason is we maintain a typescript binding running in WASM, which has not supported exceptions yet. Just throw an error at the error line. |
||
XGRAMMAR_LOG(WARNING) << "Failed to convert range for integer schema"; | ||
} | ||
return "(\"0\" | \"-\"? [1-9] [0-9]*)"; | ||
} | ||
|
||
|
@@ -698,10 +842,19 @@ std::string JSONSchemaConverter::VisitString( | |
{ | ||
"minLength", | ||
"maxLength", | ||
"pattern", | ||
"format", | ||
} | ||
); | ||
if (schema.count("pattern")) { | ||
try { | ||
std::string regex_pattern = schema.at("pattern").get<std::string>(); | ||
std::string converted_regex = RegexToEBNF(regex_pattern, false); | ||
return "\"\\\"\" " + converted_regex + " \"\\\"\""; | ||
} catch (const std::exception& e) { | ||
XGRAMMAR_LOG(WARNING) << "Failed to convert regex pattern " | ||
<< schema.at("pattern").get<std::string>(); | ||
} | ||
} | ||
return "[\"] " + kBasicStringSub; | ||
} | ||
|
||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make generateRangeRegex a static member function of JSONSchemaConverter, and follow the naming conversion (GenerateRangeRegex)?