# Path: pif_ir/bir/utils/exceptions.py
# class BIRParsingError(BIRError):
#     """ something went wrong while parsing an instruction or control state
#     """
#     def __init__(self, msg):
#         super(BIRParsingError, self).__init__()
#         self.value = msg


from ply import lex
from ply.lex import TOKEN
from ply import yacc
from pif_ir.bir.utils.exceptions import BIRParsingError
                      | DONE
        """
        p[0] = self.bit_offset if p[1] == "$offset$" else None

    # field_ref: header field value in the packet
    def p_field_ref_1(self, p):
        """ field_ref : ID
        """
        size = self.header.field_size(p[1])
        offset = self.bit_offset + self.header.field_offset(p[1])
        p[0] = self.packet.get_bits(size, offset)

    # field_ref: metadata value
    def p_field_ref_2(self, p):
        """ field_ref : ID PERIOD ID
        """
        p[0] = int(self.packet.metadata[p[1]].get_value(p[3]))

    def p_int_const(self, p):
        """ int_const : INT_CONST_HEX
                      | INT_CONST_DEC
        """
        p[0] = p[1]

    def p_error(self, p):
        if p is None:
            msg = "Incomplete expression: {}".format(self.exp)
            raise BIRParsingError(msg)
        else:
            msg = "Syntax error while parsing token {}".format(p.value)

# Path: pif_ir/bir/utils/exceptions.py
# class BIRParsingError(BIRError):
#     """ something went wrong while parsing an instruction or control state
#     """
#     def __init__(self, msg):
#         super(BIRParsingError, self).__init__()
#         self.value = msg


# Path: pif_ir/bir/utils/bir_parser.py
from ply import lex
from ply.lex import TOKEN
from ply import yacc
from pif_ir.bir.utils.exceptions import BIRParsingError

                      | DONE
        """
        p[0] = self.bit_offset if p[1] == "$offset$" else None

    # field_ref: header field value in the packet
    def p_field_ref_1(self, p):
        """ field_ref : ID
        """
        size = self.header.field_size(p[1])
        offset = self.bit_offset + self.header.field_offset(p[1])
        p[0] = self.packet.get_bits(size, offset)

    # field_ref: metadata value
    def p_field_ref_2(self, p):
        """ field_ref : ID PERIOD ID
        """
        p[0] = int(self.packet.metadata[p[1]].get_value(p[3]))

    def p_int_const(self, p):
        """ int_const : INT_CONST_HEX
                      | INT_CONST_DEC
        """
        p[0] = p[1]

    def p_error(self, p):
        if p is None:
            msg = "Incomplete expression: {}".format(self.exp)
            raise BIRParsingError(msg)
        else:
            msg = "Syntax error while parsing token {}".format(p.value)

            raise BIRParsingError(msg)