HQLPARSER-39, HQLPARSER-40

@Sanne, could you take a quick look and merge if you think it's good? This applies the original change from @anistor plus a fix for HQLPARSER-40 which was revealed by that fix.

HQLPARSER-41 is another issue uncovered, I've disabled the four affected tests for the time being which I think is ok as this is quite an edge case using legacy syntax.