S-matrix propagation algorithm for full-vectorial electromagnetic simulation and design of gratings