123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139(* Yoann Padioleau
*
* Copyright (C) 2012 Facebook
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License (GPL)
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* file license.txt for more details.
*)openCommonopenParser_java(*****************************************************************************)(* Prelude *)(*****************************************************************************)(*
* This module transforms certain tokens like '<', normally a LT
* into a LT2, which helps solving conflicts in the original
* Java grammar.
*
* This is similar to what we do for C/C++.
* See pfff/lang_cpp/parsing/parsing_hacks.ml for more information.
*)(*****************************************************************************)(* Fix tokens *)(*****************************************************************************)letfix_tokensxs=letrecauxenvxs=letdepth_angle=envinifdepth_angle<0thenbeginpr2(spf"depth_angle < 0, %d"depth_angle);pr2_gen(List.hdxs);(* failwith "depth < 0" *)aux0xsendelsematchxswith|[]->[](* dont transform the < of type parameters in LT2. Transforms
* only for type arguments (but increment depth_angle because
* we may still need to transform some >> into > >).
*)|CLASSii::TCommentSpaceii2::IDENTIFIER(s3,ii3)::LTii4::xs->CLASSii::TCommentSpaceii2::IDENTIFIER(s3,ii3)::LTii4::aux(depth_angle+1)xs|CLASSii::TCommentSpaceii2::IDENTIFIER(s3,ii3)::TCommentSpaceiisp::LTii4::xs->CLASSii::TCommentSpaceii2::IDENTIFIER(s3,ii3)::TCommentSpaceiisp::LTii4::aux(depth_angle+1)xs|INTERFACEii::TCommentSpaceii2::IDENTIFIER(s3,ii3)::LTii4::xs->INTERFACEii::TCommentSpaceii2::IDENTIFIER(s3,ii3)::LTii4::aux(depth_angle+1)xs|INTERFACEii::TCommentSpaceii2::IDENTIFIER(s3,ii3)::TCommentSpaceiisp::LTii4::xs->INTERFACEii::TCommentSpaceii2::IDENTIFIER(s3,ii3)::TCommentSpaceiisp::LTii4::aux(depth_angle+1)xs(* UGLY HARDCODE, proper way is to have a phase where filter all
* TCommentSpace and Newline
*)|CLASSii::TCommentNewlineiinewline::TCommentSpaceii2::IDENTIFIER(s3,ii3)::LTii4::xs->CLASSii::TCommentNewlineiinewline::TCommentSpaceii2::IDENTIFIER(s3,ii3)::LTii4::aux(depth_angle+1)xs(* too many FPs
| IDENTIFIER (s, ii1)::TCommentSpace iispace::LT ii2::xs
when s =~ "^[A-Z]" ->
IDENTIFIER (s, ii1)::TCommentSpace iispace::LT2 ii2::
aux (depth_angle + 1) xs
*)(* less: allow also a small space, but usually we should fix
* this code. But pb, see previous comment.
*)|IDENTIFIER(s,ii1)::LTii2::xswhens=~"^[A-Z]"->IDENTIFIER(s,ii1)::LT2ii2::aux(depth_angle+1)xs|IDENTIFIER(s,ii1)::TCommentSpaceiispace::LTii2::IDENTIFIER(s3,ii3)::xswhens=~"^[A-Z]"&&s3=~"^[A-Z]"->IDENTIFIER(s,ii1)::TCommentSpaceiispace::LT2ii2::aux(depth_angle+1)(IDENTIFIER(s3,ii3)::xs)|IDENTIFIER(s,ii1)::TCommentSpaceiispace::LTii2::CONDii3::xswhens=~"^[A-Z]"->IDENTIFIER(s,ii1)::TCommentSpaceiispace::LT2ii2::aux(depth_angle+1)(CONDii3::xs)(* xxx.<type>of(...), actually don't have to transform in a LT2
* but it's a type context so we need to augment depth_angle
* so at least the >> get transformed into > >.
*)|DOTii1::LTii2::xs->DOTii1::LT2ii2::aux(depth_angle+1)xs(* <T extends ...> bar().
* could also check for public|static|... just before the <
* which is also the sign of generic method.
*)|LTii1::IDENTIFIER(s,ii2)::TCommentSpaceiispace::EXTENDSii3::xs->LTii1::IDENTIFIER(s,ii2)::TCommentSpaceiispace::EXTENDSii3::aux(depth_angle+1)xs|GTii::xswhendepth_angle>0->GTii::aux(depth_angle-1)xs(* transform >> into two > > *)|SRSii::xswhendepth_angle>0->(* todo: split ii *)GTii::GTii::aux(depth_angle-2)xs(* transform >>> into three > > > *)|URSii::xswhendepth_angle>0->(* todo: split ii *)GTii::GTii::GTii::aux(depth_angle-3)xs|x::xs->x::auxenvxsinaux0xs