• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MAPLEBE_MDGEN_INCLUDE_MDLEXER_H
17 #define MAPLEBE_MDGEN_INCLUDE_MDLEXER_H
18 
19 #include <fstream>
20 #include <iostream>
21 #include <string>
22 #include <unordered_map>
23 #include "stdio.h"
24 #include "mdtokens.h"
25 #include "mpl_logging.h"
26 #include "types_def.h"
27 
28 namespace MDGen {
29 using namespace maple;
30 class MDLexer {
31 public:
MDLexer()32     MDLexer()
33     {
34         keywords.clear();
35         /* input can be improved */
36         (void)keywords.insert(std::make_pair("Def", kMDDef));
37         (void)keywords.insert(std::make_pair("Class", kMDClass));
38         (void)keywords.insert(std::make_pair("DefType", kMDDefType));
39     };
~MDLexer()40     ~MDLexer()
41     {
42         if (mdFileInternal.is_open()) {
43             mdFileInternal.close();
44         }
45     };
46 
47     MDTokenKind ReturnError() const;
48     MDTokenKind NextToken();
49     MDTokenKind LexToken();
50     MDTokenKind GetTokenIdentifier();
51     MDTokenKind GetTokenConstVal();
52     int ReadOneLine();
53     bool SkipCComment();
54     void SkipALineComment();
55 
56     void PrepareFile(const std::string &mdfileName);
GetStrToken()57     const std::string &GetStrToken() const
58     {
59         return strToken;
60     }
GetIntVal()61     int64_t GetIntVal() const
62     {
63         return intVal;
64     }
GetStrLine()65     const std::string &GetStrLine() const
66     {
67         return strLine;
68     }
GetStrLineSize()69     size_t GetStrLineSize() const
70     {
71         return strLine.size();
72     }
RemoveInValidAtBack()73     void RemoveInValidAtBack()
74     {
75         if (strLine.length() == 0) {
76             return;
77         }
78         if (strLine.back() == '\n') {
79             strLine.pop_back();
80         }
81         if (strLine.back() == '\r') {
82             strLine.pop_back();
83         }
84     }
GetCurKind()85     MDTokenKind GetCurKind() const
86     {
87         return curKind;
88     }
GetCurChar()89     char GetCurChar()
90     {
91         return curPos < GetStrLineSize() ? strLine[curPos] : 0;
92     }
GetNextChar()93     char GetNextChar()
94     {
95         ++curPos;
96         return curPos < GetStrLineSize() ? strLine[curPos] : 0;
97     }
ViewNextChar()98     char ViewNextChar() const
99     {
100         return curPos < GetStrLineSize() ? strLine[curPos] : 0;
101     }
GetCharAt(uint32 pos)102     char GetCharAt(uint32 pos)
103     {
104         if (pos >= GetStrLineSize()) {
105             return 0;
106         }
107         return strLine[pos];
108     }
GetLineNumber()109     int GetLineNumber() const
110     {
111         return lineNumber;
112     }
113 
114     MDTokenKind GetHexConst(uint32 startPos, bool isNegative);
115     MDTokenKind GetIntConst(uint32 digitStartPos, bool isNegative);
116     MDTokenKind GetFloatConst();
117 
118 private:
119     static constexpr int maxNumLength = 10;
120     std::ifstream *mdFile = nullptr;
121     std::ifstream mdFileInternal;
122     uint32 lineNumber = 0;                                 /* current Processing Line */
123     uint32 curPos = 0;                                     /* Position in a line */
124     std::string strLine = "";                              /* current token line */
125     std::string strToken = "";                             /* store ID,keywords ... */
126     int32 intVal = 0;                                      /* store integer when token */
127     float floatVal = 0;                                    /* store float value when token */
128     MDTokenKind curKind = kMDInvalid;                      /* current token kind */
129     std::unordered_map<std::string, MDTokenKind> keywords; /* store keywords defined for md files */
130 };
131 } /* namespace MDGen */
132 
133 #endif /* MAPLEBE_MDGEN_INCLUDE_MDLEXER_H */
134