import re
from ship import ship_from_string

position_parser = re.compile(r'(?i)[A-L],?\s?[0-9]')
orientation_parser = re.compile(r'(?i)horizontal|vertical|horizontally|' +
                                 'vertically')
ship_parser = re.compile(r'(?i)submarine|sub|aircraft|patrol')


def parse_position_input(input):
    position_match = position_parser.search(input)
    if position_match is None:
        raise ValueError("Invalid position in input")
    return (position_match.group()[0].upper(), int(position_match.group()[-1]))


def parse_orientation_input(input):
    orientation_match = orientation_parser.search(input)
    if orientation_match is None:
        raise ValueError("Invalid orientation in input")
    return "horizontal" in orientation_match.group().lower()


def parse_ship_type_input(input):
    ship_match = ship_parser.search(input)
    if ship_match is None:
        raise ValueError("Invalid ship type in input")
    return ship_from_string(ship_match.group())


def parse(input):
    """Parse user's ship placement input string and returns the ship specified

    Raises: ValueError if input is invalid

    Returns: the ship object specified by the input
    """
    position = parse_position_input(input)
    orientation = parse_orientation_input(input)
    return parse_ship_type_input(input)(position, orientation)

@@ -82,12 +82,13 @@
 A-L%5D
+(
 ,
-?
 %5Cs
+)
 ?%5B0-
@@ -982,18 +982,26 @@
 .group()
+.lower()
 )%0A
-
 %0A%0Adef pa
@@ -1153,46 +1153,89 @@
 ns: 
-the ship object specified by the input
+a triple containing the type of ship to create, the position, and the orientation
 %0A   
@@ -1342,16 +1342,17 @@
  return 
+(
 parse_sh
@@ -1371,17 +1371,18 @@
 t(input)
-(
+, 
 position

return a ship spec triple and fix the regex to prevent a bug coordinate bug, the string 'place 1' would register a coordinate E1 because of the broken regex and that was a problem