summaryrefslogtreecommitdiff
path: root/xfa/fxfa/fm2js/cxfa_fmparser.cpp
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2018-02-21 22:16:26 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-02-21 22:16:26 +0000
commitaa2aff78e082f14e4bc418f68b27817f90e3f07a (patch)
treee646df3a814cb66508350049ec2132635f231f36 /xfa/fxfa/fm2js/cxfa_fmparser.cpp
parent00c1d0b07542611d295dc666ff55d46b0d3f712b (diff)
downloadpdfium-aa2aff78e082f14e4bc418f68b27817f90e3f07a.tar.xz
[formcalc] Cleanup if expression parsing.
This CL simplifies the if expression parsing. The elseif construct has been split into its own list of IfExpressions and no longer gets stuffed into the elseExpression. A loop was added into the output methods for the elseif expressions, which also means we can go from having: if () { } else { if () { } else { } } to if () { } else if () { } else { } Which is a bit nicer. Change-Id: I6edf75215500d6a32a8d7218d477b6960f03de51 Reviewed-on: https://pdfium-review.googlesource.com/27571 Reviewed-by: Ryan Harrison <rharrison@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'xfa/fxfa/fm2js/cxfa_fmparser.cpp')
-rw-r--r--xfa/fxfa/fm2js/cxfa_fmparser.cpp89
1 files changed, 31 insertions, 58 deletions
diff --git a/xfa/fxfa/fm2js/cxfa_fmparser.cpp b/xfa/fxfa/fm2js/cxfa_fmparser.cpp
index ccd107bb0b..9beda2c564 100644
--- a/xfa/fxfa/fm2js/cxfa_fmparser.cpp
+++ b/xfa/fxfa/fm2js/cxfa_fmparser.cpp
@@ -943,73 +943,46 @@ std::unique_ptr<CXFA_FMExpression> CXFA_FMParser::ParseIfExpression() {
if (HasError() || !IncrementParseDepthAndCheck())
return nullptr;
- // This should be CheckThenNext(TOKif) but we come in here for elseif as well.
- if (!NextToken())
+ if (!CheckThenNext(TOKif))
return nullptr;
- std::unique_ptr<CXFA_FMSimpleExpression> pExpression = ParseParenExpression();
- if (m_token.m_type != TOKthen) {
- m_error = true;
- return nullptr;
- }
- if (!NextToken())
+ std::unique_ptr<CXFA_FMSimpleExpression> pCondition = ParseParenExpression();
+ if (!CheckThenNext(TOKthen))
return nullptr;
- auto exprs = ParseExpressionList();
- if (exprs.empty()) {
- m_error = true;
- return nullptr;
- }
- if (m_token.m_type != TOKelseif && m_token.m_type != TOKelse &&
- m_token.m_type != TOKendif) {
- m_error = true;
- return nullptr;
- }
- auto pIfExpression =
- pdfium::MakeUnique<CXFA_FMBlockExpression>(std::move(exprs));
+ auto pIfExpressions =
+ pdfium::MakeUnique<CXFA_FMBlockExpression>(ParseExpressionList());
- std::unique_ptr<CXFA_FMExpression> pElseExpression;
- switch (m_token.m_type) {
- case TOKeof:
- case TOKendif:
- if (!CheckThenNext(TOKendif))
- return nullptr;
- break;
- case TOKif:
- pElseExpression = ParseIfExpression();
- if (!pElseExpression || !CheckThenNext(TOKendif)) {
- m_error = true;
- return nullptr;
- }
- break;
- case TOKelseif:
- pElseExpression = ParseIfExpression();
- if (!pElseExpression) {
- m_error = true;
- return nullptr;
- }
- break;
- case TOKelse: {
- if (!NextToken())
- return nullptr;
+ std::vector<std::unique_ptr<CXFA_FMIfExpression>> pElseIfExpressions;
+ while (m_token.m_type == TOKelseif) {
+ if (!NextToken())
+ return nullptr;
- auto else_exprs = ParseExpressionList();
- if (else_exprs.empty() || !CheckThenNext(TOKendif)) {
- m_error = true;
- return nullptr;
- }
+ auto elseIfCondition = ParseParenExpression();
+ if (!CheckThenNext(TOKthen))
+ return nullptr;
- pElseExpression =
- pdfium::MakeUnique<CXFA_FMBlockExpression>(std::move(else_exprs));
- break;
- }
- default:
- m_error = true;
+ auto elseIfExprs = ParseExpressionList();
+ pElseIfExpressions.push_back(pdfium::MakeUnique<CXFA_FMIfExpression>(
+ std::move(elseIfCondition),
+ pdfium::MakeUnique<CXFA_FMBlockExpression>(std::move(elseIfExprs)),
+ std::vector<std::unique_ptr<CXFA_FMIfExpression>>(), nullptr));
+ }
+
+ std::unique_ptr<CXFA_FMExpression> pElseExpression;
+ if (m_token.m_type == TOKelse) {
+ if (!NextToken())
return nullptr;
+
+ pElseExpression =
+ pdfium::MakeUnique<CXFA_FMBlockExpression>(ParseExpressionList());
}
- return pdfium::MakeUnique<CXFA_FMIfExpression>(std::move(pExpression),
- std::move(pIfExpression),
- std::move(pElseExpression));
+ if (!CheckThenNext(TOKendif))
+ return nullptr;
+
+ return pdfium::MakeUnique<CXFA_FMIfExpression>(
+ std::move(pCondition), std::move(pIfExpressions),
+ std::move(pElseIfExpressions), std::move(pElseExpression));
}
// While := 'while' '(' SimpleExpression ')' 'do' ExpressionList 'endwhile'