def _parse_type(self): """ constructor: ConstructorId fields? sum: constructor ('|' constructor)* type: product | sum """ if self.cur_token.kind == TokenKind.LParen: # If we see a (, it's a product return self._parse_product() else: # Otherwise it's a sum. Look for ConstructorId sumlist = [] while True: cons_name = self._match(TokenKind.ConstructorId) shared_type = None fields = None if self.cur_token.kind == TokenKind.LParen: fields = self._parse_fields() elif self.cur_token.kind == TokenKind.Percent: self._advance() shared_type = self._match(TokenKind.TypeId) else: pass cons = Constructor(cons_name, shared_type, fields) sumlist.append(cons) if self.cur_token.kind != TokenKind.Pipe: break self._advance() return Sum(sumlist, self._parse_optional_attributes())
def _parse_type(self): if self.cur_token.kind == TokenKind.LParen: # If we see a (, it's a product return self._parse_product() else: # Otherwise it's a sum. Look for ConstructorId sumlist = [Constructor(self._match(TokenKind.ConstructorId), self._parse_optional_fields())] while self.cur_token.kind == TokenKind.Pipe: # More constructors self._advance() sumlist.append(Constructor( self._match(TokenKind.ConstructorId), self._parse_optional_fields())) return Sum(sumlist, self._parse_optional_attributes())