Skip to content

Commit

Permalink
Add whitespace control for expression (#162)
Browse files Browse the repository at this point in the history
* Add whitespace control for expression

* Fix Environment::set_expression not working properly
  • Loading branch information
tindy2013 authored Aug 12, 2020
1 parent 918aa2a commit ed58da7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions include/inja/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ struct LexerConfig {
std::string statement_close_force_rstrip {"-%}"};
std::string line_statement {"##"};
std::string expression_open {"{{"};
std::string expression_open_force_lstrip {"{{-"};
std::string expression_close {"}}"};
std::string expression_close_force_rstrip {"-}}"};
std::string comment_open {"{#"};
std::string comment_close {"#}"};
std::string open_chars {"#{"};
Expand All @@ -46,6 +48,9 @@ struct LexerConfig {
if (open_chars.find(expression_open[0]) == std::string::npos) {
open_chars += expression_open[0];
}
if (open_chars.find(expression_open_force_lstrip[0]) == std::string::npos) {
open_chars += expression_open_force_lstrip[0];
}
if (open_chars.find(comment_open[0]) == std::string::npos) {
open_chars += comment_open[0];
}
Expand Down
2 changes: 2 additions & 0 deletions include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ class Environment {
/// Sets the opener and closer for template expressions
void set_expression(const std::string &open, const std::string &close) {
lexer_config.expression_open = open;
lexer_config.expression_open_force_lstrip = open + "-";
lexer_config.expression_close = close;
lexer_config.expression_close_force_rstrip = "-" + close;
lexer_config.update_open_chars();
}

Expand Down
15 changes: 13 additions & 2 deletions include/inja/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Lexer {
enum class State {
Text,
ExpressionStart,
ExpressionStartForceLstrip,
ExpressionBody,
LineStart,
LineBody,
Expand Down Expand Up @@ -306,7 +307,12 @@ class Lexer {
nonstd::string_view open_str = m_in.substr(pos);
bool must_lstrip = false;
if (inja::string_view::starts_with(open_str, config.expression_open)) {
state = State::ExpressionStart;
if (inja::string_view::starts_with(open_str, config.expression_open_force_lstrip)) {
state = State::ExpressionStartForceLstrip;
must_lstrip = true;
} else {
state = State::ExpressionStart;
}
} else if (inja::string_view::starts_with(open_str, config.statement_open)) {
if (inja::string_view::starts_with(open_str, config.statement_open_no_lstrip)) {
state = State::StatementStartNoLstrip;
Expand Down Expand Up @@ -343,6 +349,11 @@ class Lexer {
pos += config.expression_open.size();
return make_token(Token::Kind::ExpressionOpen);
}
case State::ExpressionStartForceLstrip: {
state = State::ExpressionBody;
pos += config.expression_open_force_lstrip.size();
return make_token(Token::Kind::ExpressionOpen);
}
case State::LineStart: {
state = State::LineBody;
pos += config.line_statement.size();
Expand All @@ -369,7 +380,7 @@ class Lexer {
return make_token(Token::Kind::CommentOpen);
}
case State::ExpressionBody:
return scan_body(config.expression_close, Token::Kind::ExpressionClose);
return scan_body(config.expression_close, Token::Kind::ExpressionClose, config.expression_close_force_rstrip);
case State::LineBody:
return scan_body("\n", Token::Kind::LineStatementClose);
case State::StatementBody:
Expand Down
22 changes: 20 additions & 2 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,9 @@ struct LexerConfig {
std::string statement_close_force_rstrip {"-%}"};
std::string line_statement {"##"};
std::string expression_open {"{{"};
std::string expression_open_force_lstrip {"{{-"};
std::string expression_close {"}}"};
std::string expression_close_force_rstrip {"-}}"};
std::string comment_open {"{#"};
std::string comment_close {"#}"};
std::string open_chars {"#{"};
Expand All @@ -1485,6 +1487,9 @@ struct LexerConfig {
if (open_chars.find(expression_open[0]) == std::string::npos) {
open_chars += expression_open[0];
}
if (open_chars.find(expression_open_force_lstrip[0]) == std::string::npos) {
open_chars += expression_open_force_lstrip[0];
}
if (open_chars.find(comment_open[0]) == std::string::npos) {
open_chars += comment_open[0];
}
Expand Down Expand Up @@ -1893,6 +1898,7 @@ class Lexer {
enum class State {
Text,
ExpressionStart,
ExpressionStartForceLstrip,
ExpressionBody,
LineStart,
LineBody,
Expand Down Expand Up @@ -2180,7 +2186,12 @@ class Lexer {
nonstd::string_view open_str = m_in.substr(pos);
bool must_lstrip = false;
if (inja::string_view::starts_with(open_str, config.expression_open)) {
state = State::ExpressionStart;
if (inja::string_view::starts_with(open_str, config.expression_open_force_lstrip)) {
state = State::ExpressionStartForceLstrip;
must_lstrip = true;
} else {
state = State::ExpressionStart;
}
} else if (inja::string_view::starts_with(open_str, config.statement_open)) {
if (inja::string_view::starts_with(open_str, config.statement_open_no_lstrip)) {
state = State::StatementStartNoLstrip;
Expand Down Expand Up @@ -2217,6 +2228,11 @@ class Lexer {
pos += config.expression_open.size();
return make_token(Token::Kind::ExpressionOpen);
}
case State::ExpressionStartForceLstrip: {
state = State::ExpressionBody;
pos += config.expression_open_force_lstrip.size();
return make_token(Token::Kind::ExpressionOpen);
}
case State::LineStart: {
state = State::LineBody;
pos += config.line_statement.size();
Expand All @@ -2243,7 +2259,7 @@ class Lexer {
return make_token(Token::Kind::CommentOpen);
}
case State::ExpressionBody:
return scan_body(config.expression_close, Token::Kind::ExpressionClose);
return scan_body(config.expression_close, Token::Kind::ExpressionClose, config.expression_close_force_rstrip);
case State::LineBody:
return scan_body("\n", Token::Kind::LineStatementClose);
case State::StatementBody:
Expand Down Expand Up @@ -3953,7 +3969,9 @@ class Environment {
/// Sets the opener and closer for template expressions
void set_expression(const std::string &open, const std::string &close) {
lexer_config.expression_open = open;
lexer_config.expression_open_force_lstrip = open + "-";
lexer_config.expression_close = close;
lexer_config.expression_close_force_rstrip = "-" + close;
lexer_config.update_open_chars();
}

Expand Down

0 comments on commit ed58da7

Please sign in to comment.