From f93767ae9b9a5adea4d0b1c46a96d4816446dc3b Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 21 Feb 2018 21:02:57 +0000 Subject: [formcalc] Minor parser cleanups This CL makes minor clarity cleanups to the formcalc parser and attempts to record the productions each method is processing. Change-Id: Ie12bb55647abf06251d1734fd05d08a964a32ebc Reviewed-on: https://pdfium-review.googlesource.com/27550 Reviewed-by: Ryan Harrison Commit-Queue: dsinclair --- xfa/fxfa/fm2js/cxfa_fmparser.cpp | 127 ++++++++++++++++++++++----------------- xfa/fxfa/fm2js/cxfa_fmparser.h | 1 - 2 files changed, 73 insertions(+), 55 deletions(-) diff --git a/xfa/fxfa/fm2js/cxfa_fmparser.cpp b/xfa/fxfa/fm2js/cxfa_fmparser.cpp index b67f94fc10..ccd107bb0b 100644 --- a/xfa/fxfa/fm2js/cxfa_fmparser.cpp +++ b/xfa/fxfa/fm2js/cxfa_fmparser.cpp @@ -89,15 +89,17 @@ CXFA_FMParser::ParseExpressionList() { return expressions; } +// Func := 'func' Identifier '(' ParameterList ')' do ExpressionList 'endfunc' +// ParamterList := (Not actually defined in the grammar) ..... +// (Identifier (',' Identifier)*)? std::unique_ptr CXFA_FMParser::ParseFunction() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) return nullptr; WideStringView ident; - std::vector arguments; std::vector> expressions; - if (!NextToken()) + if (!CheckThenNext(TOKfunc)) return nullptr; if (m_token.m_type != TOKidentifier) { m_error = true; @@ -109,6 +111,8 @@ std::unique_ptr CXFA_FMParser::ParseFunction() { } if (!CheckThenNext(TOKlparen)) return nullptr; + + std::vector arguments; if (m_token.m_type == TOKrparen) { if (!NextToken()) return nullptr; @@ -126,16 +130,13 @@ std::unique_ptr CXFA_FMParser::ParseFunction() { return nullptr; continue; } - if (m_token.m_type == TOKrparen) { - if (!NextToken()) - return nullptr; - } else { - if (!CheckThenNext(TOKrparen)) - return nullptr; - } + if (!CheckThenNext(TOKrparen)) + return nullptr; + break; } } + if (!CheckThenNext(TOKdo)) return nullptr; if (m_token.m_type == TOKendfunc) { @@ -151,6 +152,9 @@ std::unique_ptr CXFA_FMParser::ParseFunction() { false, ident, std::move(arguments), std::move(expressions)); } +// Expression := IfExpression | WhileExpression | ForExpression | +// ForEachExpression | AssignmentExpression | +// DeclarationExpression | SimpleExpression std::unique_ptr CXFA_FMParser::ParseExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) @@ -203,6 +207,9 @@ std::unique_ptr CXFA_FMParser::ParseExpression() { return expr; } +// Declaration := 'var' Variable | 'var' Variable '=' SimpleExpression | +// 'Func' Identifier '(' ParameterList ')' do ExpressionList 'EndFunc' +// TODO(dsinclair): We appear to be handling the 'func' case elsewhere. std::unique_ptr CXFA_FMParser::ParseDeclarationExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) @@ -233,6 +240,7 @@ std::unique_ptr CXFA_FMParser::ParseDeclarationExpression() { return pdfium::MakeUnique(ident, std::move(expr)); } +// SimpleExpression := LogicalOrExpression std::unique_ptr CXFA_FMParser::ParseSimpleExpression() { if (HasError()) @@ -241,6 +249,7 @@ CXFA_FMParser::ParseSimpleExpression() { return ParseLogicalOrExpression(); } +// Exp := SimpleExpression ( '=' SimpleExpression )? std::unique_ptr CXFA_FMParser::ParseExpExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) @@ -264,6 +273,8 @@ std::unique_ptr CXFA_FMParser::ParseExpExpression() { return pdfium::MakeUnique(std::move(pExp1)); } +// LogicalOr := LogicalAndExpression | +// LogicalOrExpression LogicalOrOperator LogicalAndExpression std::unique_ptr CXFA_FMParser::ParseLogicalOrExpression() { AutoRestorer restorer(&m_parse_depth); @@ -274,6 +285,7 @@ CXFA_FMParser::ParseLogicalOrExpression() { if (!e1) return nullptr; + // TODO(dsinclair): Is this for() needed? for (;;) { switch (m_token.m_type) { case TOKor: @@ -298,6 +310,8 @@ CXFA_FMParser::ParseLogicalOrExpression() { return e1; } +// LogicalAnd := EqualityExpression | +// LogicalAndExpression LogicalAndOperator EqualityExpression std::unique_ptr CXFA_FMParser::ParseLogicalAndExpression() { AutoRestorer restorer(&m_parse_depth); @@ -308,6 +322,7 @@ CXFA_FMParser::ParseLogicalAndExpression() { if (!e1) return nullptr; + // TODO(dsinclair): Is this for() needed? for (;;) { switch (m_token.m_type) { case TOKand: @@ -331,6 +346,8 @@ CXFA_FMParser::ParseLogicalAndExpression() { return e1; } +// Equality := RelationExpression | +// EqualityExpression EqulaityOperator RelationalExpression std::unique_ptr CXFA_FMParser::ParseEqualityExpression() { AutoRestorer restorer(&m_parse_depth); @@ -340,6 +357,8 @@ CXFA_FMParser::ParseEqualityExpression() { std::unique_ptr e1 = ParseRelationalExpression(); if (!e1) return nullptr; + + // TODO(dsinclair): Is this for() needed? for (;;) { std::unique_ptr e2; switch (m_token.m_type) { @@ -375,6 +394,8 @@ CXFA_FMParser::ParseEqualityExpression() { return e1; } +// Relational := AdditiveExpression | +// RelationalExpression RelationalOperator AdditiveExpression std::unique_ptr CXFA_FMParser::ParseRelationalExpression() { AutoRestorer restorer(&m_parse_depth); @@ -385,6 +406,7 @@ CXFA_FMParser::ParseRelationalExpression() { if (!e1) return nullptr; + // TODO(dsinclair): Is this for() needed? for (;;) { std::unique_ptr e2; switch (m_token.m_type) { @@ -444,6 +466,8 @@ CXFA_FMParser::ParseRelationalExpression() { return e1; } +// Additive := MultiplicativeExpression | +// AdditiveExpression AdditiveOperator MultiplicativeExpression std::unique_ptr CXFA_FMParser::ParseAddtiveExpression() { AutoRestorer restorer(&m_parse_depth); @@ -454,6 +478,7 @@ CXFA_FMParser::ParseAddtiveExpression() { if (!e1) return nullptr; + // TODO(dsinclair): Is this for() needed? for (;;) { std::unique_ptr e2; switch (m_token.m_type) { @@ -487,6 +512,8 @@ CXFA_FMParser::ParseAddtiveExpression() { return e1; } +// Multiplicative := UnaryExpression | +// MultiplicateExpression MultiplicativeOperator UnaryExpression std::unique_ptr CXFA_FMParser::ParseMultiplicativeExpression() { AutoRestorer restorer(&m_parse_depth); @@ -497,6 +524,7 @@ CXFA_FMParser::ParseMultiplicativeExpression() { if (!e1) return nullptr; + // TODO(dsinclair): Is this for() needed? for (;;) { std::unique_ptr e2; switch (m_token.m_type) { @@ -530,6 +558,7 @@ CXFA_FMParser::ParseMultiplicativeExpression() { return e1; } +// Unary := PrimaryExpression | UnaryOperator UnaryExpression std::unique_ptr CXFA_FMParser::ParseUnaryExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) @@ -576,6 +605,8 @@ std::unique_ptr CXFA_FMParser::ParseUnaryExpression() { return expr; } +// Primary := Literal | FunctionCall | Accessor ('.*' )? | +// '(' SimpleExpression ')' std::unique_ptr CXFA_FMParser::ParsePrimaryExpression() { AutoRestorer restorer(&m_parse_depth); @@ -636,6 +667,8 @@ std::unique_ptr CXFA_FMParser::ParseLiteral() { } } +// TODO(dsinclair): Make this match up to the grammar +// I believe this is parsing the accessor ( '.' | '..' | '.#' ) std::unique_ptr CXFA_FMParser::ParsePostExpression( std::unique_ptr expr) { AutoRestorer restorer(&m_parse_depth); @@ -833,27 +866,30 @@ std::unique_ptr CXFA_FMParser::ParsePostExpression( return expr; } +// Index := '[' ('*' | '+' SimpleExpression | '-' SimpleExpression) ']' std::unique_ptr CXFA_FMParser::ParseIndexExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) return nullptr; - if (!NextToken()) + if (!CheckThenNext(TOKlbracket)) return nullptr; - std::unique_ptr s; - XFA_FM_AccessorIndex accessorIndex = ACCESSOR_NO_RELATIVEINDEX; - std::unique_ptr pExp; if (m_token.m_type == TOKmul) { - pExp = pdfium::MakeUnique(accessorIndex, - std::move(s), true); + auto pExp = pdfium::MakeUnique( + ACCESSOR_NO_RELATIVEINDEX, nullptr, true); if (!pExp || !NextToken()) return nullptr; + + // TODO(dsinclair): This should CheckThenNext(TOKrbracket) but need to clean + // up the callsites. if (m_token.m_type != TOKrbracket) { m_error = true; return nullptr; } return pExp; } + + XFA_FM_AccessorIndex accessorIndex = ACCESSOR_NO_RELATIVEINDEX; if (m_token.m_type == TOKplus) { accessorIndex = ACCESSOR_POSITIVE_INDEX; if (!NextToken()) @@ -863,7 +899,8 @@ std::unique_ptr CXFA_FMParser::ParseIndexExpression() { if (!NextToken()) return nullptr; } - s = ParseSimpleExpression(); + + std::unique_ptr s = ParseSimpleExpression(); if (!s) return nullptr; if (m_token.m_type != TOKrbracket) { @@ -874,6 +911,7 @@ std::unique_ptr CXFA_FMParser::ParseIndexExpression() { false); } +// Paren := '(' SimpleExpression ')' std::unique_ptr CXFA_FMParser::ParseParenExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) @@ -896,26 +934,20 @@ std::unique_ptr CXFA_FMParser::ParseParenExpression() { return pExp1; } +// If := 'if' '(' SimpleExpression ')' 'then' ExpressionList +// ('elseif' '(' SimpleExpression ')' 'then' ExpressionList)* +// ('else' ExpressionList)? +// 'endif' std::unique_ptr CXFA_FMParser::ParseIfExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) return nullptr; - if (!NextToken() || !CheckThenNext(TOKlparen)) - return nullptr; - std::unique_ptr pExpression; - while (m_token.m_type != TOKrparen) { - pExpression = ParseSimpleExpression(); - if (!pExpression) - return nullptr; - if (m_token.m_type != TOKcomma) - break; - if (!NextToken()) - return nullptr; - } - if (!CheckThenNext(TOKrparen)) + // This should be CheckThenNext(TOKif) but we come in here for elseif as well. + if (!NextToken()) return nullptr; + std::unique_ptr pExpression = ParseParenExpression(); if (m_token.m_type != TOKthen) { m_error = true; return nullptr; @@ -980,11 +1012,12 @@ std::unique_ptr CXFA_FMParser::ParseIfExpression() { std::move(pElseExpression)); } +// While := 'while' '(' SimpleExpression ')' 'do' ExpressionList 'endwhile' std::unique_ptr CXFA_FMParser::ParseWhileExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) return nullptr; - if (!NextToken()) + if (!CheckThenNext(TOKwhile)) return nullptr; std::unique_ptr pCondition = ParseParenExpression(); @@ -1000,39 +1033,25 @@ std::unique_ptr CXFA_FMParser::ParseWhileExpression() { std::move(pCondition), pdfium::MakeUnique(std::move(exprs))); } - -std::unique_ptr -CXFA_FMParser::ParseSubassignmentInForExpression() { - AutoRestorer restorer(&m_parse_depth); - if (HasError() || !IncrementParseDepthAndCheck()) - return nullptr; - - if (m_token.m_type != TOKidentifier) { - m_error = true; - return nullptr; - } - std::unique_ptr expr = ParseSimpleExpression(); - if (!expr) - return nullptr; - return expr; -} - +// For := 'for' Assignment 'upto' Accessor ('step' SimpleExpression)? +// 'do' ExpressionList 'endfor' | +// 'for' Assignment 'downto' Accessor ('step' SimpleExpression)? +// 'do' ExpressionList 'endfor' std::unique_ptr CXFA_FMParser::ParseForExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) return nullptr; - - WideStringView wsVariant; - if (!NextToken()) + if (!CheckThenNext(TOKfor)) return nullptr; + if (m_token.m_type != TOKidentifier) { m_error = true; return nullptr; } - - wsVariant = m_token.m_string; + WideStringView wsVariant = m_token.m_string; if (!NextToken()) return nullptr; + if (m_token.m_type != TOKassign) { m_error = true; return nullptr; @@ -1094,7 +1113,7 @@ std::unique_ptr CXFA_FMParser::ParseForeachExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) return nullptr; - if (!NextToken()) + if (!CheckThenNext(TOKforeach)) return nullptr; if (m_token.m_type != TOKidentifier) { @@ -1145,7 +1164,7 @@ std::unique_ptr CXFA_FMParser::ParseDoExpression() { AutoRestorer restorer(&m_parse_depth); if (HasError() || !IncrementParseDepthAndCheck()) return nullptr; - if (!NextToken()) + if (!CheckThenNext(TOKdo)) return nullptr; auto exprs = ParseExpressionList(); diff --git a/xfa/fxfa/fm2js/cxfa_fmparser.h b/xfa/fxfa/fm2js/cxfa_fmparser.h index 9aff8d7bd6..103667d878 100644 --- a/xfa/fxfa/fm2js/cxfa_fmparser.h +++ b/xfa/fxfa/fm2js/cxfa_fmparser.h @@ -42,7 +42,6 @@ class CXFA_FMParser { std::unique_ptr ParseDoExpression(); std::unique_ptr ParseParenExpression(); std::unique_ptr ParseSimpleExpression(); - std::unique_ptr ParseSubassignmentInForExpression(); std::unique_ptr ParseLogicalOrExpression(); std::unique_ptr ParseLogicalAndExpression(); std::unique_ptr ParseEqualityExpression(); -- cgit v1.2.3