1 // Copyright 2014 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef XFA_FXFA_FORMCALC_CXFA_FMPARSER_H_ 8 #define XFA_FXFA_FORMCALC_CXFA_FMPARSER_H_ 9 10 #include <optional> 11 #include <vector> 12 13 #include "core/fxcrt/unowned_ptr.h" 14 #include "core/fxcrt/unowned_ptr_exclusion.h" 15 #include "fxjs/gc/heap.h" 16 #include "v8/include/cppgc/macros.h" 17 #include "v8/include/cppgc/member.h" 18 #include "xfa/fxfa/formcalc/cxfa_fmexpression.h" 19 #include "xfa/fxfa/formcalc/cxfa_fmlexer.h" 20 21 class CXFA_FMParser { 22 CPPGC_STACK_ALLOCATED(); // Allow Raw/Unowned pointers. 23 24 public: 25 CXFA_FMParser(cppgc::Heap* heap, CXFA_FMLexer* pLexer); 26 ~CXFA_FMParser(); 27 28 // Returned object is owned by cppgc heap. 29 CXFA_FMAST* Parse(); 30 bool HasError() const; 31 SetMaxParseDepthForTest(unsigned long max_depth)32 void SetMaxParseDepthForTest(unsigned long max_depth) { 33 m_max_parse_depth = max_depth; 34 } 35 36 private: 37 bool NextToken(); 38 bool CheckThenNext(XFA_FM_TOKEN op); 39 bool IncrementParseDepthAndCheck(); 40 41 std::vector<cppgc::Member<CXFA_FMExpression>> ParseExpressionList(); 42 CXFA_FMExpression* ParseFunction(); 43 CXFA_FMExpression* ParseExpression(); 44 CXFA_FMExpression* ParseDeclarationExpression(); 45 CXFA_FMExpression* ParseExpExpression(); 46 CXFA_FMExpression* ParseIfExpression(); 47 CXFA_FMExpression* ParseWhileExpression(); 48 CXFA_FMExpression* ParseForExpression(); 49 CXFA_FMExpression* ParseForeachExpression(); 50 CXFA_FMExpression* ParseDoExpression(); 51 CXFA_FMSimpleExpression* ParseParenExpression(); 52 CXFA_FMSimpleExpression* ParseSimpleExpression(); 53 CXFA_FMSimpleExpression* ParseLogicalOrExpression(); 54 CXFA_FMSimpleExpression* ParseLogicalAndExpression(); 55 CXFA_FMSimpleExpression* ParseEqualityExpression(); 56 CXFA_FMSimpleExpression* ParseRelationalExpression(); 57 CXFA_FMSimpleExpression* ParseAdditiveExpression(); 58 CXFA_FMSimpleExpression* ParseMultiplicativeExpression(); 59 CXFA_FMSimpleExpression* ParseUnaryExpression(); 60 CXFA_FMSimpleExpression* ParsePrimaryExpression(); 61 CXFA_FMSimpleExpression* ParsePostExpression(CXFA_FMSimpleExpression* e); 62 CXFA_FMSimpleExpression* ParseIndexExpression(); 63 CXFA_FMSimpleExpression* ParseLiteral(); 64 std::optional<std::vector<cppgc::Member<CXFA_FMSimpleExpression>>> 65 ParseArgumentList(); 66 67 UnownedPtr<cppgc::Heap> const m_heap; 68 UNOWNED_PTR_EXCLUSION CXFA_FMLexer* const m_lexer; // Stack allocated. 69 CXFA_FMLexer::Token m_token; 70 bool m_error = false; 71 unsigned long m_parse_depth = 0; 72 unsigned long m_max_parse_depth; 73 }; 74 75 #endif // XFA_FXFA_FORMCALC_CXFA_FMPARSER_H_ 76