Skip to content

Commit

Permalink
TH_Tokenized: configurable indentation (char and number).
Browse files Browse the repository at this point in the history
  • Loading branch information
adriweb committed Apr 7, 2024
1 parent cee40e1 commit 0424550
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 20 deletions.
8 changes: 4 additions & 4 deletions TIVarsLib.js

Large diffs are not rendered by default.

Binary file modified TIVarsLib.wasm
Binary file not shown.
24 changes: 23 additions & 1 deletion src/TypeHandlers/TH_Tokenized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ namespace tivars
lang = PRGMLANG_BASIC;
}

char indent_char = INDENT_CHAR_SPACE;
if (has_option(options, "indent_char") && options.at("indent_char") == INDENT_CHAR_TAB)
{
indent_char = INDENT_CHAR_TAB;
}

size_t indent_n = indent_char == INDENT_CHAR_SPACE ? 3 : 1;
if (has_option(options, "indent_n"))
{
const size_t wanted_indent_n = options.at("indent_n");
if (wanted_indent_n != indent_n)
{
indent_n = wanted_indent_n;
}
}

std::string str(str_orig);

str = std::regex_replace(str, std::regex("([^\\s])(Del|Eff)Var "), "$1\n$2Var ");
Expand Down Expand Up @@ -217,6 +233,12 @@ namespace tivars
}
}

// Take care of NBSP stuff
for (auto& line : lines_tmp)
{
line = std::regex_replace(line, std::regex("^[\u00A0\uC2A0]*\\s*[\u00A0\uC2A0]*"), "");
}

std::vector<std::pair<uint16_t, std::string>> lines(lines_tmp.size()); // indent, text
for (const auto& line : lines_tmp)
{
Expand Down Expand Up @@ -266,7 +288,7 @@ namespace tivars
str = "";
for (const auto& line : lines)
{
str += str_repeat(" ", line.first * 3) + line.second + '\n';
str += std::string(line.first * indent_n, indent_char) + line.second + '\n';
}

return ltrim(rtrim(str, "\t\n\r\f\v"));
Expand Down
1 change: 1 addition & 0 deletions src/TypeHandlers/TypeHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace tivars
th();
enum lang { LANG_EN = 0, LANG_FR };
enum typelang { PRGMLANG_BASIC = 0, PRGMLANG_AXE, PRGMLANG_ICE };
enum indentchar : char { INDENT_CHAR_SPACE = ' ', INDENT_CHAR_TAB = '\t' };
struct token_posinfo { uint16_t line; uint16_t column; uint8_t len; };
std::string reindentCodeString(const std::string& str_orig, const options_t& options = options_t());
token_posinfo getPosInfoAtOffset(const data_t& data, uint16_t byteOffset, const options_t& options = options_t());
Expand Down
11 changes: 0 additions & 11 deletions src/tivarslib_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,6 @@ std::string trim(const std::string& s, const char* t)
return ltrim(rtrim(s, t), t);
}

std::string str_repeat(const std::string& str, unsigned int times)
{
std::string result;
result.reserve(times * str.length()); // avoid repeated reallocation
for (unsigned char i = 0; i < times; i++)
{
result += str;
}
return result;
}

// From http://stackoverflow.com/a/2481126/378298
void ParseCSV(const std::string& csvSource, std::vector<std::vector<std::string>>& lines)
{
Expand Down
2 changes: 0 additions & 2 deletions src/tivarslib_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ std::string rtrim(std::string s, const char* t = " \t\n\r\f\v");

std::string trim(const std::string& s, const char* t = " \t\n\r\f\v");

std::string str_repeat(const std::string& str, unsigned int times);

void ParseCSV(const std::string& csvSource, std::vector<std::vector<std::string>>& lines);

bool is_numeric(const std::string& str);
Expand Down
67 changes: 65 additions & 2 deletions tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,73 @@ int main(int argc, char** argv)
}

{
string test = "Disp 42:Wait 5:toString(42):Pause\nInput A,\"?\":Asdf(123)\nFor(I,1,10)\nThen\nDisp I:For(J,1,10)\nThen\nDisp J\nEnd\nEnd";
cout << "Indented code:" << endl << TH_Tokenized::reindentCodeString(test) << endl;
string test = "Disp 42\nInput A,\"?\":For(I,1,10)\nThen\nDisp I:For(J,1,10)\nThen\nDisp J\nEnd\nEnd";
const std::string reindented = TH_Tokenized::reindentCodeString(test);
cout << "Indented code:" << endl << reindented << endl;
const std::string expected = R"(Disp 42
Input A,"?"
For(I,1,10)
Then
Disp I
For(J,1,10)
Then
Disp J
End
End)";
assert(reindented == expected);
}

{
string test = "   Disp 42\nInput A,\"?\":  For(I,1,10)\n Then\n \xA0 Disp I:For(J,1,10)\nThen\n Disp J\nEnd\nEnd";
const std::string reindented = TH_Tokenized::reindentCodeString(test);
cout << "Indented code:" << endl << reindented << endl;
const std::string expected = R"(Disp 42
Input A,"?"
For(I,1,10)
Then
Disp I
For(J,1,10)
Then
Disp J
End
End)";
assert(reindented == expected);
}

{
string test = "Disp 42\nInput A,\"?\":For(I,1,10)\nThen\nDisp I:For(J,1,10)\nThen\nDisp J\nEnd\nEnd";
const std::string reindented = TH_Tokenized::reindentCodeString(test, {{"indent_n", 8}});
cout << "Indented code:" << endl << reindented << endl;
const std::string expected = R"(Disp 42
Input A,"?"
For(I,1,10)
Then
Disp I
For(J,1,10)
Then
Disp J
End
End)";
assert(reindented == expected);
}

{
string test = "Disp 42\nInput A,\"?\":For(I,1,10)\nThen\nDisp I:For(J,1,10)\nThen\nDisp J\nEnd\nEnd";
const std::string reindented = TH_Tokenized::reindentCodeString(test, {{"indent_char", TH_Tokenized::INDENT_CHAR_TAB}});
cout << "Indented code:" << endl << reindented << endl;
const std::string expected = R"(Disp 42
Input A,"?"
For(I,1,10)
Then
Disp I
For(J,1,10)
Then
Disp J
End
End)";
assert(reindented == expected);
}

{
TIVarFile testPrgmReindent = TIVarFile::createNew("Program", "asdf");
testPrgmReindent.setContentFromString("\"http://TIPlanet.org");
Expand Down

0 comments on commit 0424550

Please sign in to comment.