comparison python/c3/parser.py @ 228:7f18ed9b6b7e

Removal of emptystatement class
author Windel Bouwman
date Sat, 13 Jul 2013 11:12:24 +0200
parents 82dfe6a32717
children 88a1e0baef65
comparison
equal deleted inserted replaced
227:82dfe6a32717 228:7f18ed9b6b7e
25 self.Error('Excected: "{0}", got "{1}"'.format(typ, self.Peak)) 25 self.Error('Excected: "{0}", got "{1}"'.format(typ, self.Peak))
26 26
27 @property 27 @property
28 def Peak(self): 28 def Peak(self):
29 return self.token.typ 29 return self.token.typ
30
31 @property
32 def CurLoc(self):
33 return self.token.loc
30 34
31 def hasConsumed(self, typ): 35 def hasConsumed(self, typ):
32 if self.Peak == typ: 36 if self.Peak == typ:
33 self.Consume(typ) 37 self.Consume(typ)
34 return True 38 return True
179 self.Consume(')') 183 self.Consume(')')
180 yes = self.parseCompoundStatement() 184 yes = self.parseCompoundStatement()
181 if self.hasConsumed('else'): 185 if self.hasConsumed('else'):
182 no = self.parseCompoundStatement() 186 no = self.parseCompoundStatement()
183 else: 187 else:
184 no = astnodes.EmptyStatement() 188 no = None
185 return astnodes.IfStatement(condition, yes, no, loc) 189 return astnodes.IfStatement(condition, yes, no, loc)
186 190
187 def parseWhileStatement(self): 191 def parseWhileStatement(self):
188 loc = self.Consume('while').loc 192 loc = self.Consume('while').loc
189 self.Consume('(') 193 self.Consume('(')
201 def parseCompoundStatement(self): 205 def parseCompoundStatement(self):
202 self.Consume('{') 206 self.Consume('{')
203 statements = [] 207 statements = []
204 while not self.hasConsumed('}'): 208 while not self.hasConsumed('}'):
205 s = self.Statement() 209 s = self.Statement()
206 if type(s) is astnodes.EmptyStatement: 210 if s is None:
207 continue 211 continue
208 statements.append(s) 212 statements.append(s)
209 return astnodes.CompoundStatement(statements) 213 return astnodes.CompoundStatement(statements)
210 214
211 def Statement(self): 215 def Statement(self):
215 elif self.Peak == 'while': 219 elif self.Peak == 'while':
216 return self.parseWhileStatement() 220 return self.parseWhileStatement()
217 elif self.Peak == '{': 221 elif self.Peak == '{':
218 return self.parseCompoundStatement() 222 return self.parseCompoundStatement()
219 elif self.hasConsumed(';'): 223 elif self.hasConsumed(';'):
220 return astnodes.EmptyStatement() 224 pass
221 elif self.Peak == 'var': 225 elif self.Peak == 'var':
222 self.parseVarDef() 226 self.parseVarDef()
223 return astnodes.EmptyStatement()
224 elif self.Peak == 'return': 227 elif self.Peak == 'return':
225 return self.parseReturnStatement() 228 return self.parseReturnStatement()
226 else: 229 else:
227 return self.AssignmentOrCall() 230 return self.AssignmentOrCall()
228 231
299 return a 302 return a
300 303
301 # Domain of unary expressions: 304 # Domain of unary expressions:
302 305
303 def CastExpression(self): 306 def CastExpression(self):
304 # type cast conflicts with '(' expr ')', so introduce extra keyword 'cast' 307 """
308 the C-style type cast conflicts with '(' expr ')'
309 so introduce extra keyword 'cast'
310 """
305 if self.Peak == 'cast': 311 if self.Peak == 'cast':
306 self.Consume('cast') 312 self.Consume('cast')
307 self.Consume('<') 313 self.Consume('<')
308 t = self.parseTypeSpec() 314 t = self.parseTypeSpec()
309 self.Consume('>') 315 self.Consume('>')