xref: /aosp_15_r20/external/pdfium/xfa/fxfa/formcalc/cxfa_fmlexer.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_FMLEXER_H_
8 #define XFA_FXFA_FORMCALC_CXFA_FMLEXER_H_
9 
10 #include "core/fxcrt/widestring.h"
11 #include "v8/include/cppgc/macros.h"
12 
13 enum XFA_FM_TOKEN {
14   TOKand,
15   TOKlparen,
16   TOKrparen,
17   TOKmul,
18   TOKplus,
19   TOKcomma,
20   TOKminus,
21   TOKdot,
22   TOKdiv,
23   TOKlt,
24   TOKassign,
25   TOKgt,
26   TOKlbracket,
27   TOKrbracket,
28   TOKor,
29   TOKdotscream,
30   TOKdotstar,
31   TOKdotdot,
32   TOKle,
33   TOKne,
34   TOKeq,
35   TOKge,
36   TOKdo,
37   TOKkseq,
38   TOKksge,
39   TOKksgt,
40   TOKif,
41   TOKin,
42   TOKksle,
43   TOKkslt,
44   TOKksne,
45   TOKksor,
46   TOKnull,
47   TOKbreak,
48   TOKksand,
49   TOKend,
50   TOKeof,
51   TOKfor,
52   TOKnan,
53   TOKksnot,
54   TOKvar,
55   TOKthen,
56   TOKelse,
57   TOKexit,
58   TOKdownto,
59   TOKreturn,
60   TOKinfinity,
61   TOKendwhile,
62   TOKforeach,
63   TOKendfunc,
64   TOKelseif,
65   TOKwhile,
66   TOKendfor,
67   TOKthrow,
68   TOKstep,
69   TOKupto,
70   TOKcontinue,
71   TOKfunc,
72   TOKendif,
73   TOKstar,
74   TOKidentifier,
75   TOKunderscore,
76   TOKdollar,
77   TOKexclamation,
78   TOKcall,
79   TOKstring,
80   TOKnumber,
81   TOKreserver
82 };
83 
84 class CXFA_FMLexer {
85   CPPGC_STACK_ALLOCATED();  // Raw pointers allowed.
86 
87  public:
88   class Token {
89    public:
90     Token();
91     explicit Token(XFA_FM_TOKEN token);
92     Token(XFA_FM_TOKEN token, WideStringView str);
93     Token(const Token& that);
94     ~Token();
95 
GetType()96     XFA_FM_TOKEN GetType() const { return m_type; }
GetString()97     WideStringView GetString() const { return m_string; }
98 #ifndef NDEBUG
99     WideString ToDebugString() const;
100 #endif  // NDEBUG
101 
102    private:
103     XFA_FM_TOKEN m_type = TOKreserver;
104     WideStringView m_string;
105   };
106 
107   explicit CXFA_FMLexer(WideStringView wsFormcalc);
108   ~CXFA_FMLexer();
109 
110   Token NextToken();
IsComplete()111   bool IsComplete() const { return m_nCursor >= m_spInput.size(); }
112 
113  private:
114   Token AdvanceForNumber();
115   Token AdvanceForString();
116   Token AdvanceForIdentifier();
117   void AdvanceForComment();
118 
RaiseError()119   void RaiseError() { m_bLexerError = true; }
120 
121   pdfium::span<const wchar_t> m_spInput;
122   size_t m_nCursor = 0;
123   bool m_bLexerError = false;
124 };
125 
126 #endif  // XFA_FXFA_FORMCALC_CXFA_FMLEXER_H_
127