Home | History | Annotate | Download | only in fm2js
      1 // Copyright 2014 PDFium Authors. All rights reserved.
      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_FM2JS_XFA_LEXER_H_
      8 #define XFA_FXFA_FM2JS_XFA_LEXER_H_
      9 
     10 #include <memory>
     11 
     12 #include "core/fxcrt/fx_string.h"
     13 #include "xfa/fxfa/fm2js/xfa_error.h"
     14 
     15 enum XFA_FM_TOKEN {
     16   TOKand,
     17   TOKlparen,
     18   TOKrparen,
     19   TOKmul,
     20   TOKplus,
     21   TOKcomma,
     22   TOKminus,
     23   TOKdot,
     24   TOKdiv,
     25   TOKlt,
     26   TOKassign,
     27   TOKgt,
     28   TOKlbracket,
     29   TOKrbracket,
     30   TOKor,
     31   TOKdotscream,
     32   TOKdotstar,
     33   TOKdotdot,
     34   TOKle,
     35   TOKne,
     36   TOKeq,
     37   TOKge,
     38   TOKdo,
     39   TOKkseq,
     40   TOKksge,
     41   TOKksgt,
     42   TOKif,
     43   TOKin,
     44   TOKksle,
     45   TOKkslt,
     46   TOKksne,
     47   TOKksor,
     48   TOKnull,
     49   TOKbreak,
     50   TOKksand,
     51   TOKend,
     52   TOKeof,
     53   TOKfor,
     54   TOKnan,
     55   TOKksnot,
     56   TOKvar,
     57   TOKthen,
     58   TOKelse,
     59   TOKexit,
     60   TOKdownto,
     61   TOKreturn,
     62   TOKinfinity,
     63   TOKendwhile,
     64   TOKforeach,
     65   TOKendfunc,
     66   TOKelseif,
     67   TOKwhile,
     68   TOKendfor,
     69   TOKthrow,
     70   TOKstep,
     71   TOKupto,
     72   TOKcontinue,
     73   TOKfunc,
     74   TOKendif,
     75   TOKstar,
     76   TOKidentifier,
     77   TOKunderscore,
     78   TOKdollar,
     79   TOKexclamation,
     80   TOKcall,
     81   TOKstring,
     82   TOKnumber,
     83   TOKreserver
     84 };
     85 
     86 struct XFA_FMKeyword {
     87   XFA_FM_TOKEN m_type;
     88   uint32_t m_uHash;
     89   const FX_WCHAR* m_keyword;
     90 };
     91 
     92 const FX_WCHAR* XFA_FM_KeywordToString(XFA_FM_TOKEN op);
     93 
     94 class CXFA_FMToken {
     95  public:
     96   CXFA_FMToken();
     97   explicit CXFA_FMToken(uint32_t uLineNum);
     98 
     99   CFX_WideStringC m_wstring;
    100   XFA_FM_TOKEN m_type;
    101   uint32_t m_uLinenum;
    102 };
    103 
    104 class CXFA_FMLexer {
    105  public:
    106   CXFA_FMLexer(const CFX_WideStringC& wsFormcalc, CXFA_FMErrorInfo* pErrorInfo);
    107   ~CXFA_FMLexer();
    108 
    109   CXFA_FMToken* NextToken();
    110   uint32_t Number(CXFA_FMToken* t, const FX_WCHAR* p, const FX_WCHAR*& pEnd);
    111   uint32_t String(CXFA_FMToken* t, const FX_WCHAR* p, const FX_WCHAR*& pEnd);
    112   uint32_t Identifiers(CXFA_FMToken* t,
    113                        const FX_WCHAR* p,
    114                        const FX_WCHAR*& pEnd);
    115   void Comment(const FX_WCHAR* p, const FX_WCHAR*& pEnd);
    116   XFA_FM_TOKEN IsKeyword(const CFX_WideStringC& p);
    117   void SetCurrentLine(uint32_t line) { m_uCurrentLine = line; }
    118   void SetToken(CXFA_FMToken* pToken) {
    119     if (m_pToken.get() != pToken)
    120       m_pToken.reset(pToken);
    121   }
    122   const FX_WCHAR* SavePos() { return m_ptr; }
    123   void RestorePos(const FX_WCHAR* pPos) { m_ptr = pPos; }
    124   void Error(const FX_WCHAR* msg, ...);
    125   bool HasError() const;
    126 
    127  protected:
    128   CXFA_FMToken* Scan();
    129 
    130   const FX_WCHAR* m_ptr;
    131   uint32_t m_uCurrentLine;
    132   std::unique_ptr<CXFA_FMToken> m_pToken;
    133   CXFA_FMErrorInfo* m_pErrorInfo;
    134 };
    135 
    136 #endif  // XFA_FXFA_FM2JS_XFA_LEXER_H_
    137