-
Notifications
You must be signed in to change notification settings - Fork 15
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
#983: Refactor CTPG script options Java parser code #462
Merged
tomuben
merged 8 commits into
master
from
refactoring/983_refactor_ctpg_script_options_java_parser_code
Oct 21, 2024
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
881daae
#983: Refactor CTPG script options Java parser code
tomuben b47db5c
Merge branch 'master' into refactoring/983_refactor_ctpg_script_optio…
tomuben e8315c9
Merge branch 'master' into refactoring/983_refactor_ctpg_script_optio…
tomuben c53d4b2
Fixes from review
tomuben 4382eb7
Fixes from review
tomuben 05d8824
Renamed ScriptImporter::ReplacedScripts => ScriptImporter::CollectedS…
tomuben b3990cc
Extracted script replacement into separate function
tomuben 0fb3e07
Fixes from review
tomuben 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 |
---|---|---|
|
@@ -27,6 +27,30 @@ void ScriptImporter::importScript(std::string & scriptCode, | |
importScript(scriptCode, options, 0); | ||
} | ||
|
||
void ScriptImporter::replaceScripts(const ScriptImporter::OptionValues_t & option_values, | ||
const size_t recursionDepth, | ||
std::vector<ReplacedScripts> &result) { | ||
for (const auto & option: option_values) { | ||
const char *importScriptCode = findImportScript(option.value); | ||
std::string importScriptCodeStr; | ||
if (m_importedScriptChecksums.addScript(importScriptCode) ) { | ||
// Script has not been imported yet | ||
// If this imported script contains %import statements | ||
// they will be resolved in the next recursion. | ||
ctpg_parser::options_map_t newOptions; | ||
try { | ||
ExecutionGraph::OptionsLineParser::CTPG::parseOptions(importScriptCode, newOptions); | ||
} catch(const ExecutionGraph::OptionParserException & ex) { | ||
Utils::rethrow(ex, "F-UDF-CL-SL-JAVA-1630"); | ||
} | ||
importScriptCodeStr.assign(importScriptCode); | ||
importScript(importScriptCodeStr, newOptions, recursionDepth + 1); | ||
} | ||
ReplacedScripts replacedScript = {.script = std::move(importScriptCodeStr), .origPos = option.idx_in_source, .origLen = option.size }; | ||
result.push_back(std::move(replacedScript)); | ||
} | ||
} | ||
|
||
void ScriptImporter::importScript(std::string & scriptCode, | ||
ctpg_parser::options_map_t & options, | ||
const size_t recursionDepth) { | ||
|
@@ -43,35 +67,11 @@ void ScriptImporter::importScript(std::string & scriptCode, | |
{ | ||
return first.idx_in_source < second.idx_in_source; | ||
}); | ||
struct ReplacedScripts { | ||
ReplacedScripts(ReplacedScripts&&) = default; | ||
std::string script; | ||
size_t origPos; | ||
size_t origLen; | ||
}; | ||
std::vector<ReplacedScripts> replacedScripts; | ||
replacedScripts.reserve(optionIt->second.size()); | ||
//In order to continue compatibility with legacy implementation we must collect import scripts in forward direction | ||
//but then replace in reverse direction (in order to keep consistency of positions) | ||
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. this one is already in replaceImportScripts 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. Removed
|
||
for (const auto & option: optionIt->second) { | ||
const char *importScriptCode = findImportScript(option.value); | ||
std::string importScriptCodeStr; | ||
if (m_importedScriptChecksums.addScript(importScriptCode) ) { | ||
// Script has not been imported yet | ||
// If this imported script contains %import statements | ||
// they will be resolved in the next recursion. | ||
ctpg_parser::options_map_t newOptions; | ||
try { | ||
ExecutionGraph::OptionsLineParser::CTPG::parseOptions(importScriptCode, newOptions); | ||
} catch(const ExecutionGraph::OptionParserException & ex) { | ||
Utils::rethrow(ex, "F-UDF-CL-SL-JAVA-1630"); | ||
} | ||
importScriptCodeStr.assign(importScriptCode); | ||
importScript(importScriptCodeStr, newOptions, recursionDepth + 1); | ||
} | ||
ReplacedScripts replacedScript = {.script = std::move(importScriptCodeStr), .origPos = option.idx_in_source, .origLen = option.size }; | ||
replacedScripts.push_back(std::move(replacedScript)); | ||
} | ||
replaceScripts(optionIt->second, recursionDepth, replacedScripts); | ||
tomuben marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//Now replace the imported script bodies from end to start. Doing it in forward order would invalidate the offsets of later import scripts. | ||
for (auto optionIt = replacedScripts.rbegin(); optionIt != replacedScripts.rend(); optionIt++) { | ||
tomuben marked this conversation as resolved.
Show resolved
Hide resolved
|
||
scriptCode.replace(optionIt->origPos, optionIt->origLen, optionIt->script); | ||
|
@@ -82,13 +82,15 @@ void ScriptImporter::importScript(std::string & scriptCode, | |
const char* ScriptImporter::findImportScript(const std::string & scriptKey) { | ||
if (!m_metaData) { | ||
m_metaData.reset(m_swigFactory.makeSwigMetadata()); | ||
if (!m_metaData) | ||
if (!m_metaData) { | ||
throw std::runtime_error("F-UDF-CL-SL-JAVA-1631: Failure while importing scripts"); | ||
} | ||
} | ||
const char *importScriptCode = m_metaData->moduleContent(scriptKey.c_str()); | ||
const char *exception = m_metaData->checkException(); | ||
if (exception) | ||
if (exception) { | ||
throw std::runtime_error("F-UDF-CL-SL-JAVA-1632: " + std::string(exception)); | ||
} | ||
return importScriptCode; | ||
} | ||
|
||
|
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
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.
this should probably go into collectImportScripts
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.
Hm, the comment is about both steps:
collectImportScripts()
+ ``replaceImportScripts`. I think it's better to keep it here