Theory of impurity states in semiconductors