xref: /aosp_15_r20/external/pdfium/xfa/fxfa/formcalc/cxfa_fmparser.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 <vector>
11 
12 #include "core/fxcrt/unowned_ptr_exclusion.h"
13 #include "fxjs/gc/heap.h"
14 #include "third_party/abseil-cpp/absl/types/optional.h"
15 #include "v8/include/cppgc/macros.h"
16 #include "v8/include/cppgc/member.h"
17 #include "xfa/fxfa/formcalc/cxfa_fmexpression.h"
18 #include "xfa/fxfa/formcalc/cxfa_fmlexer.h"
19 
20 class CXFA_FMParser {
21   CPPGC_STACK_ALLOCATED();  // Allow Raw/Unowned pointers.
22 
23  public:
24   CXFA_FMParser(cppgc::Heap* heap, CXFA_FMLexer* pLexer);
25   ~CXFA_FMParser();
26 
27   // Returned object is owned by cppgc heap.
28   CXFA_FMAST* Parse();
29   bool HasError() const;
30 
SetMaxParseDepthForTest(unsigned long max_depth)31   void SetMaxParseDepthForTest(unsigned long max_depth) {
32     m_max_parse_depth = max_depth;
33   }
34 
35  private:
36   bool NextToken();
37   bool CheckThenNext(XFA_FM_TOKEN op);
38   bool IncrementParseDepthAndCheck();
39 
40   std::vector<cppgc::Member<CXFA_FMExpression>> ParseExpressionList();
41   CXFA_FMExpression* ParseFunction();
42   CXFA_FMExpression* ParseExpression();
43   CXFA_FMExpression* ParseDeclarationExpression();
44   CXFA_FMExpression* ParseExpExpression();
45   CXFA_FMExpression* ParseIfExpression();
46   CXFA_FMExpression* ParseWhileExpression();
47   CXFA_FMExpression* ParseForExpression();
48   CXFA_FMExpression* ParseForeachExpression();
49   CXFA_FMExpression* ParseDoExpression();
50   CXFA_FMSimpleExpression* ParseParenExpression();
51   CXFA_FMSimpleExpression* ParseSimpleExpression();
52   CXFA_FMSimpleExpression* ParseLogicalOrExpression();
53   CXFA_FMSimpleExpression* ParseLogicalAndExpression();
54   CXFA_FMSimpleExpression* ParseEqualityExpression();
55   CXFA_FMSimpleExpression* ParseRelationalExpression();
56   CXFA_FMSimpleExpression* ParseAdditiveExpression();
57   CXFA_FMSimpleExpression* ParseMultiplicativeExpression();
58   CXFA_FMSimpleExpression* ParseUnaryExpression();
59   CXFA_FMSimpleExpression* ParsePrimaryExpression();
60   CXFA_FMSimpleExpression* ParsePostExpression(CXFA_FMSimpleExpression* e);
61   CXFA_FMSimpleExpression* ParseIndexExpression();
62   CXFA_FMSimpleExpression* ParseLiteral();
63   absl::optional<std::vector<cppgc::Member<CXFA_FMSimpleExpression>>>
64   ParseArgumentList();
65 
66   UnownedPtr<cppgc::Heap> const m_heap;
67   UNOWNED_PTR_EXCLUSION CXFA_FMLexer* const m_lexer;  // Stack allocated.
68   CXFA_FMLexer::Token m_token;
69   bool m_error = false;
70   unsigned long m_parse_depth = 0;
71   unsigned long m_max_parse_depth;
72 };
73 
74 #endif  // XFA_FXFA_FORMCALC_CXFA_FMPARSER_H_
75