def parse_two_points(cli_input: str) -> ParseResult: """ Parse two Points from given input. """ point_result1 = PointParser().parse_point(cli_input) if point_result1.is_successful(): point_result2 = PointParser().parse_point( point_result1.get_remainder()) if point_result2.is_successful(): return Success( [point_result1.get_match(), point_result2.get_match()], point_result2.get_remainder()) return Failure("<POINT> <POINT>", cli_input)
def parse_params(self, cli_input: str) -> ParseResult: result = PointParser().parse_point(cli_input) if result.is_successful(): abs_point = self.convert_points([result.get_match()]) x = abs_point[0].x y = abs_point[0].y return Success(RemoveShapeCommand(self._controller, x, y), result.get_remainder()) else: return result
def parse_point_and_nat(self, cli_input: str) -> ParseResult: """ Parse a Point and a Natural number from given input. """ point_result = PointParser().parse_point(cli_input) if point_result.is_successful(): radius_result = self.radius_parser.parse_input( point_result.get_remainder()) if radius_result.is_successful(): return Success( [point_result.get_match(), radius_result.get_match()], radius_result.get_remainder()) return Failure("circle <POINT> <NAT>", cli_input)
def parse_params(self, cli_input: str) -> ParseResult: points_result = self.parse_two_points(cli_input) if points_result.is_successful(): points = points_result.get_match() # two points parsed successfully, try to parse a color remainder = points_result.get_remainder() color_result = self.parse_color(remainder, self.color_parser) if color_result.is_successful(): # color parsed successfully, no more points will be parsed color = color_result.get_match() abs_points = self.convert_points(points_result.get_match()) start_x = abs_points[0].x start_y = abs_points[0].y end_x = abs_points[1].x end_y = abs_points[1].y return Success( PrintLineCommand(self._controller, start_x, start_y, end_x, end_y, color), '') else: # try to parse a point or a color while True: point_result = PointParser().parse_point(remainder) if point_result.is_successful(): points.append(point_result.get_match()) remainder = point_result.get_remainder() else: color_result = self.parse_color( remainder, self.color_parser) if color_result.is_successful(): # color parsed successfully, no more points will be parsed color = color_result.get_match() abs_points = self.convert_points(points) return Success( PrintPolylineCommand(self._controller, [(p.x, p.y) for p in abs_points], color), color_result.get_remainder()) else: break return Failure("line <POINT> <POINTS>", cli_input)
def parse_point_and_nats(self, cli_input: str) -> ParseResult: """ Parse a Point and two Natural numbers from given input. """ point_result = PointParser().parse_point(cli_input) if point_result.is_successful(): width_result = self.width_parser.parse_input( point_result.get_remainder()) if width_result.is_successful(): height_result = self.height_parser.parse_input( width_result.get_remainder()) if height_result.is_successful(): return Success([ point_result.get_match(), width_result.get_match(), height_result.get_match() ], height_result.get_remainder()) return Failure("rect <POINT> <NAT> <NAT>", cli_input)
def parse_params(self, cli_input: str) -> ParseResult: point_result = PointParser().parse_point(cli_input) if point_result.is_successful(): abs_point = self.convert_points([point_result.get_match()]) point_x = abs_point[0].x point_y = abs_point[0].y # Parse color color_result = self.parse_color(point_result.get_remainder(), self.color_parser) if color_result.is_successful(): color = color_result.get_match() return Success( PrintDotCommand(self._controller, point_x, point_y, color), '') else: return Failure(color_result.get_expected(), point_result.get_remainder()) return Failure("dot <POINT>", cli_input)