cppparser.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19  '''
20 
21 class CPPParser():
22 
23  def __init__(self):
24  pass
25 
26  @staticmethod
27  def parseFunctions(funcStr):
28  returnTypes = []
29  funcNames = []
30  codes = []
31  funcExists = True
32  while funcExists and len(funcStr) > 0 and funcStr.index('{') >= 0:
33  funcStr = funcStr.strip()
34  funcStartIndex = funcStr.index('{')
35  funcSignature = funcStr[0:funcStartIndex].strip()
36  returnType = funcSignature[0:funcSignature.index(' ')].strip()
37  returnTypes.append(returnType)
38  funcName = funcSignature[funcSignature.index(' '):].strip()
39  funcNames.append(funcName)
40  curlyCounter = 0
41  firstCurlyFound = False
42  firstCurlyIndex = None
43  lastCurlyIndex = None
44  for i, ch in enumerate(funcStr):
45  if ch == '{':
46  curlyCounter += 1
47  if not firstCurlyFound:
48  firstCurlyFound = True
49  firstCurlyIndex = i
50  elif ch == '}':
51  curlyCounter -= 1
52 
53  if curlyCounter == 0 and firstCurlyFound:
54  lastCurlyIndex = i
55  break
56  # print(firstCurlyIndex)
57  # print(lastCurlyIndex)
58  # print(funcStr[firstCurlyIndex:lastCurlyIndex+1])
59  codes.append(funcStr[firstCurlyIndex:lastCurlyIndex+1])
60  funcExists = False
61 
62  # check whether there is any other function
63  funcStr = funcStr[lastCurlyIndex+1:].strip()
64  if len(funcStr) > 0 and funcStr.index('{') >= 0:
65  funcExists = True
66 
67  # return returnType, funcName
68  # print(returnType)
69  # print(funcName)
70  return returnTypes, funcNames, codes
71 
72 
73  @staticmethod
74  def parseVariables(variableStr):
75  types = []
76  varNames = []
77  initialValues = []
78  variableStr = variableStr.strip()
79  variableLines = variableStr.split(';')
80  for varLine in variableLines:
81  varLine = varLine.strip()
82  if len(varLine) == 0:
83  continue
84 
85  varType = varLine[0:varLine.find(' ')]
86  varName = None
87  initialValue = None
88  if varLine.find('=') >= 0:
89  # if there is initial value
90  varName = varLine[varLine.find(' ')+1:varLine.find('=')].strip()
91  initialValue = varLine[varLine.find('=')+1:].strip()
92  else:
93  varName = varLine[varLine.find(' ')+1:].strip()
94 
95  types.append(varType)
96  varNames.append(varName)
97  initialValues.append(initialValue)
98 
99  return types, varNames, initialValues
100 
101 
102 if __name__ == '__main__':
103  sampleCode = '''
104  void myFunction(int a) {
105  int c;
106  c = a * 2;
107 }
108 
109 void myF2 ( int b ){
110  int c,d;
111  c = 10;
112  d = 12;
113  a = c*d*b;
114  if ( a == 2) {
115  b = 3;
116  }
117  return a;
118  }
119 
120 
121 
122  int myfunc3() {
123  int a = 12;
124  int b = 324;
125  int c = 0;
126  c = a + b;
127  for (int i = 0; i < 10; i++) {
128  c = a + b;
129  }
130  }
131 '''
132 
133  # returnTypes, funcNames, codes = CPPParser.parseFunctions(sampleCode)
134  # for i in range(len(returnTypes)):
135  # print(returnTypes[i])
136  # print(funcNames[i])
137  # print(codes[i])
138  # print(returnType, funcName)
139 
140  sampleVariables = '''
141  int a = 12; int b = 23;
142  float myVar; float myVar2 = 12.2;
143  '''
144 
145  types, varNames, initialValues = CPPParser.parseVariables(sampleVariables)
146  for i in range(len(types)):
147  print(types[i])
148  print(varNames[i])
149  print(initialValues[i])
150 


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20