123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904(*
Copyright 2012-2020 Codinuum Software Lab <https://codinuum.com>
Copyright 2020-2025 Chiba Institute of Technology
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)(* Author: Masatomo Hashimoto <m.hashimoto@stair.center> *)[%%prepare_logger]moduleXhash=Diffast_misc.XhashmoduleXprint=Diffast_misc.XprintmoduleParserlib_base=Langs_common.Parserlib_basemoduleAstloc=Langs_common.AstlocmoduleC=ContextmodulePB=Parserlib_baseopenTokens_letwhite_space=[%sedlex.regexp?Chars" \009\012\005"]letline_terminator=[%sedlex.regexp?Chars"\013\010"|"\013\010"]leth_char=[%sedlex.regexp?Compl(Chars">\013\010")]letq_char=[%sedlex.regexp?Compl(Chars"\"\013\010")]letheader_name=[%sedlex.regexp?'<',Plush_char,'>'|'"',Plusq_char,'"']letdigit=[%sedlex.regexp?'0'..'9']letnondigit=[%sedlex.regexp?'a'..'z'|'A'..'Z'|Chars"_$"|"##"]lethexadecimal_digit=[%sedlex.regexp?'0'..'9'|'a'..'f'|'A'..'F']lethex_quad=[%sedlex.regexp?hexadecimal_digit,hexadecimal_digit,hexadecimal_digit,hexadecimal_digit]letuniversal_character_name=[%sedlex.regexp?'u',hex_quad|'U',hex_quad,hex_quad]letshell_var=[%sedlex.regexp?"${",nondigit,Star(nondigit|digit|Chars":-"),'}']letidentifier_nondigit=[%sedlex.regexp?nondigit|universal_character_name|shell_var|'?','?',Plus'?']letidentifier=[%sedlex.regexp?identifier_nondigit,Star(identifier_nondigit|digit)]letsign=[%sedlex.regexp?Chars"+-"]letpp_number=[%sedlex.regexp?Opt'.',digit,Star(digit|identifier_nondigit|'\'',digit|'\'',nondigit|Chars"eEpP",sign|'.')]letunsigned_suffix=[%sedlex.regexp?Chars"uU"]letlong_suffix=[%sedlex.regexp?Chars"lL"]letlong_long_suffix=[%sedlex.regexp?"ll"|"LL"]letinteger_suffix=[%sedlex.regexp?unsigned_suffix,Optlong_suffix|unsigned_suffix,Optlong_long_suffix|long_suffix,Optunsigned_suffix|long_long_suffix,Optunsigned_suffix]letbinary_digit=[%sedlex.regexp?'0'..'1']letoctal_digit=[%sedlex.regexp?'0'..'7']letnonzero_digit=[%sedlex.regexp?'1'..'9']lethexadecimal_prefix=[%sedlex.regexp?'0',Chars"xX"]lethexadecimal_digit_sequence=[%sedlex.regexp?Star(Plushexadecimal_digit,'\''),Plushexadecimal_digit]letbinary_literal=[%sedlex.regexp?'0',Chars"bB",Star(Plusbinary_digit,'\''),Plusbinary_digit]letoctal_literal=[%sedlex.regexp?'0',Star(Staroctal_digit,'\'',octal_digit),Staroctal_digit]letdecimal_literal=[%sedlex.regexp?nonzero_digit,Star(Stardigit,'\'',digit),Stardigit]lethexadecimal_literal=[%sedlex.regexp?hexadecimal_prefix,hexadecimal_digit_sequence]letinteger_literal=[%sedlex.regexp?binary_literal,Optinteger_suffix|octal_literal,Optinteger_suffix|decimal_literal,Optinteger_suffix|hexadecimal_literal,Optinteger_suffix](*let escape_sequence = [%sedlex.regexp?
'\\',
(
Chars "'\"?\\abfnrtv" |
octal_digit, Opt (octal_digit, Opt octal_digit) |
'x', Plus hexadecimal_digit
)
]*)letescape_sequence=[%sedlex.regexp?'\\',(Chars"'\"?\\%"|'a'..'z'|(* !!! to handle "\con" for instance *)octal_digit,Opt(octal_digit,Optoctal_digit)|Chars"xU",Plushexadecimal_digit(* to handle non-standard \U *))]letc_char=[%sedlex.regexp?Compl(Chars"'\\\013\010")|escape_sequence|universal_character_name]letencoding_prefix=[%sedlex.regexp?"u8"|Chars"uUL"]letcharacter_literal=[%sedlex.regexp?Optencoding_prefix,'\'',Plusc_char,'\''|"'\\'"]letdigit_sequence=[%sedlex.regexp?Star(Plusdigit,'\''),Plusdigit]letfractional_constant=[%sedlex.regexp?Optdigit_sequence,'.',digit_sequence|digit_sequence,'.']lethexadecimal_fractional_constant=[%sedlex.regexp?Opthexadecimal_digit_sequence,'.',hexadecimal_digit_sequence|hexadecimal_digit_sequence,'.']letexponent_part=[%sedlex.regexp?Chars"eE",Optsign,digit_sequence]letbinary_exponent_part=[%sedlex.regexp?Chars"pP",Optsign,digit_sequence]letfloating_suffix=[%sedlex.regexp?Chars"flFL"]letdecimal_floating_literal=[%sedlex.regexp?fractional_constant,Optexponent_part,Optfloating_suffix|digit_sequence,exponent_part,Optfloating_suffix]lethexadecimal_floating_literal=[%sedlex.regexp?hexadecimal_prefix,hexadecimal_fractional_constant,binary_exponent_part,Optfloating_suffix|hexadecimal_prefix,hexadecimal_digit_sequence,binary_exponent_part,Optfloating_suffix]letfloating_literal=[%sedlex.regexp?decimal_floating_literal|hexadecimal_floating_literal]lets_char=[%sedlex.regexp?Compl(Chars"\"\\\013\010")|escape_sequence|universal_character_name]lets_char_no_bq=[%sedlex.regexp?Compl(Chars"`\"\\\013\010")]letd_char=[%sedlex.regexp?Compl(Chars" ()\\\009\011\012\013\010")]letr_char=[%sedlex.regexp?'.'](* !!! *)(*let s_char_sequence = [%sedlex.regexp?
Plus (s_char|'\\', (identifier|line_terminator|'('|')'|'/'|','|']'|'>')|line_terminator, Compl '#')
]*)lets_char_sequence=[%sedlex.regexp?Plus(s_char|'\\',(identifier|line_terminator|Chars"()/,]>"))]lets_char_sequence_no_bq=[%sedlex.regexp?Pluss_char_no_bq]letd_char_sequence=[%sedlex.regexp?Plusd_char]letr_char_sequence=[%sedlex.regexp?Plusr_char]letraw_string_head=[%sedlex.regexp?Optencoding_prefix,'R','"',Optd_char_sequence,'(']letraw_string_tail=[%sedlex.regexp?')',Optd_char_sequence,'"']letstring_literal=[%sedlex.regexp?Optencoding_prefix,'"',Opts_char_sequence,'"'|'@','"',Opts_char_sequence,'"'|'`',s_char_sequence_no_bq,'`']letboolean_literal=[%sedlex.regexp?"false"|"true"](*let pointer_literal = [%sedlex.regexp? "nullptr"]*)letuser_defined_integer_literal=[%sedlex.regexp?integer_literal,identifier]letuser_defined_floating_literal=[%sedlex.regexp?floating_literal,identifier]letuser_defined_string_literal=[%sedlex.regexp?string_literal,identifier]letuser_defined_character_literal=[%sedlex.regexp?character_literal,identifier]letuser_defined_literal=[%sedlex.regexp?user_defined_integer_literal|user_defined_floating_literal|user_defined_string_literal|user_defined_character_literal](*let literal = [%sedlex.regexp?
integer_literal | character_literal | floating_literal | string_literal |
boolean_literal (*| pointer_literal*) | user_defined_literal
]*)letpp_keyword=[%sedlex.regexp?'#',Starwhite_space,identifier]letws_or_lt=[%sedlex.regexp?white_space|'\\',line_terminator]letident_or_symbol=[%sedlex.regexp?identifier|'.'|'='|Plus('0'..'9'),Optidentifier]letpp_concatenated_identifier=[%sedlex.regexp?Plus(identifier,Starws_or_lt,"##",Starws_or_lt),Star(ident_or_symbol,Starws_or_lt,"##",Starws_or_lt),ident_or_symbol|Plus(identifier,"#"),Optidentifier]letgarbage=[%sedlex.regexp?'%',Starsign,Plus('0'..'9'),Starsign,'%'](* *)letws_pat=Str.regexp"[ \009\012\013\010\\]+"letnormalize_pp_keywordk=lets=Str.global_replacews_pat""kinString.lowercase_asciisletnormalize_pp_concatenated_identk=IDENT(Str.global_replacews_pat""k)letraw_string_head_pat=Str.regexp"^\\(u8\\|u\\|U\\|L\\)?R\"\\([^' ' '(' ')' '\\' '\009' '\011' '\012' '\013' '\010']*\\)($"letget_markers=letb=Str.string_matchraw_string_head_pats0inifbthenStr.matched_group2selse""moduleStringHash=structtypet=stringletequals0s1=String.equals0s1lethashs=Hashtbl.hash_param58sendmoduleStringHashtbl=Hashtbl.Make(StringHash)(*module StringMap = Map.Make (String)*)let_find_objc_keyword=letkeyword_list=["@class",OBJC_CLASS;"@interface",OBJC_INTERFACE;"@end",OBJC_END;"@property",OBJC_PROPERTY;"@private",OBJC_PRIVATE;"@public",OBJC_PUBLIC;"@protected",OBJC_PROTECTED;"@package",OBJC_PACKAGE;"@protocol",OBJC_PROTOCOL;"@encode",OBJC_ENCODE;"@synchronized",OBJC_SYNCHRONIZED;"@selector",OBJC_SELECTOR;"@defs",OBJC_DEFS;"@try",OBJC_TRY;"@throw",OBJC_THROW;"@catch",OBJC_CATCH;"@finally",OBJC_FINALLY;"@synthesize",OBJC_SYNTHESIZE;"@dynamic",OBJC_DYNAMIC;"@optional",OBJC_OPTIONAL;"@required",OBJC_REQUIRED;"@autoreleasepool",OBJC_AUTORELEASEPOOL;"@available",OBJC_AVAILABLE;]inletkeyword_table=StringHashtbl.create(List.lengthkeyword_list)inlet_=List.iter(fun(kwd,tok)->StringHashtbl.addkeyword_tablekwdtok)keyword_listinfuns->StringHashtbl.findkeyword_tablesletfind_objc_keywords=try_find_objc_keywordswithNot_found->(*OBJC_UNKNOWN*)IDENTslet_find_pp_keyword=letkeyword_list=["#include",(fun()->PP_INCLUDE);"#define",(fun()->PP_DEFINE);"#undef",(fun()->PP_UNDEF);"#line",(fun()->PP_LINE);"#error",(fun()->PP_ERROR);"#pragma",(fun()->PP_PRAGMA);"#if",(fun()->PP_IF);"#ifdef",(fun()->PP_IFDEF);"#ifndef",(fun()->PP_IFNDEF);"#elif",(fun()->PP_ELIF(ref""));"#else",(fun()->PP_ELSE(ref""));"#endif",(fun()->PP_ENDIF(ref""));"#import",(fun()->PP_IMPORT);]inletkeyword_table=StringHashtbl.create(List.lengthkeyword_list)inlet_=List.iter(fun(kwd,tok)->StringHashtbl.addkeyword_tablekwdtok)keyword_listinfuns->(StringHashtbl.findkeyword_table(normalize_pp_keywords))()(*let keyword_map =
List.fold_left (fun m (kwd, tok) -> StringMap.add kwd tok m) StringMap.empty keyword_list
in
fun s -> StringMap.find (normalize_pp_keyword s) keyword_map*)letfind_pp_keywords=try_find_pp_keywordswithNot_found->PP_UNKNOWNslet_find_keyword=letkeyword_list=["alignas",ALIGNAS;"alignof",ALIGNOF;"asm",ASM;"auto",AUTO;"bool",BOOL;"break",BREAK;"case",CASE;"catch",CATCH;"char",CHAR;"char8_t",CHAR8_T;"char16_t",CHAR16_T;"char32_t",CHAR32_T;"class",CLASS;"concept",CONCEPT;"const",CONST;"consteval",CONSTEVAL;"constexpr",CONSTEXPR;"constinit",CONSTINIT;"const_cast",CONST_CAST;"continue",CONTINUE;"decltype",DECLTYPE;"default",DEFAULT;"delete",DELETE;"double",DOUBLE;"do",DO;"dynamic_cast",DYNAMIC_CAST;"else",ELSE;"enum",ENUM;"explicit",EXPLICIT;"export",EXPORT;"extern",EXTERN;"false",FALSE;"float",FLOAT;"for",FOR;"friend",FRIEND;"goto",GOTO;"if",IF;"inline",INLINE;"int",INT;"long",LONG;"mutable",MUTABLE;"namespace",NAMESPACE;"new",NEW;"noexcept",NOEXCEPT;"nullptr",NULLPTR;"operator",OPERATOR;"private",PRIVATE;"protected",PROTECTED;"public",PUBLIC;"register",REGISTER;"reinterpret_cast",REINTERPRET_CAST;"requires",REQUIRES;"return",RETURN;"short",SHORT;"signed",SIGNED;"sizeof",SIZEOF;"static",STATIC;"static_assert",STATIC_ASSERT;"static_cast",STATIC_CAST;"struct",STRUCT;"switch",SWITCH;"template",TEMPLATE;"this",THIS;"thread_local",THREAD_LOCAL;"throw",THROW;"true",TRUE;"try",TRY;"typedef",TYPEDEF;"typeid",TYPEID;"typename",TYPENAME;"union",UNION;"unsigned",UNSIGNED;"using",USING;"virtual",VIRTUAL;"void",VOID;"volatile",VOLATILE;"wchar_t",WCHAR_T;"while",WHILE;"and",AMP_AMP"and";"or",BAR_BAR"or";"xor",HAT"xor";"not",EXCLAM"not";"bitand",AMP"bitand";"bitor",BAR"bitor";"compl",TILDE"compl";"and_eq",AMP_EQ"and_eq";"or_eq",BAR_EQ"or_eq";"xor_eq",HAT_EQ"xor_eq";"not_eq",EXCLAM_EQ"not_eq";"audit",AUDIT;"axiom",AXIOM;"final",FINAL;"override",OVERRIDE;"defined",DEFINED;"__has_include",HAS_INCLUDE;"__has_cpp_attribute",HAS_CPP_ATTRIBUTE;"restrict",RESTRICT"restrict";(* C99 *)"__restrict__",RESTRICT"__restrict__";"__asm__",GNU_ASM"__asm__";"__attribute__",GNU_ATTR"__attribute__";"__attribute",GNU_ATTR"__attribute";"__stdcall",MS_STDCALL"__stdcall";"WINAPI",MS_STDCALL"WINAPI";"APIENTRY",MS_STDCALL"APIENTRY";"__cdecl",MS_CDECL"__cdecl";"__asm",MS_ASM"__asm";"_asm",MS_ASM"_asm";]inletkeyword_table=StringHashtbl.create(List.lengthkeyword_list)inlet_=List.iter(fun(kwd,tok)->StringHashtbl.addkeyword_tablekwdtok)keyword_listinStringHashtbl.findkeyword_table(*let keyword_map =
List.fold_left (fun m (kwd, tok) -> StringMap.add kwd tok m) StringMap.empty keyword_list
in
fun s -> StringMap.find s keyword_map*)letfind_keywords=try_find_keywordswithNot_found->IDENTs[%%capture_pathmoduleF(Stat:Parser_aux.STATE_T)=structopenStatletlexeme=Sedlexing.Utf8.lexemeletfrom_string=Sedlexing.Utf8.from_string(*let lexeme = Sedlexing.Latin1.lexeme*)(*let from_string = Sedlexing.Latin1.from_string*)letlexing_pos_start=Langs_common.Position.lexing_pos_startletlexing_pos_end=Langs_common.Position.lexing_pos_endletget_lc=Langs_common.Position.get_lcletmktok?(st_opt=None)rawtokulexbuf=letst_pos=matchst_optwith|Somex->x|None->lexing_pos_startulexbufin[%debug_log"%s"(Token.rawtoken_to_stringrawtok)];env#clear_lex_line_head_flag();leted_pos=lexing_pos_endulexbufinrawtok,st_pos,ed_posletmktok_nlrawtokulexbuf=letlen=Sedlexing.lexeme_lengthulexbufinletst_pos=lexing_pos_startulexbufin[%debug_log"%s"(Token.rawtoken_to_stringrawtok)];env#clear_lex_line_head_flag();leted_pos=iflen<=1thenst_poselseAstloc.incr_n_lexpos(len-1)st_posinrawtok,st_pos,ed_posletadd_comment_region(*loc*)_=()letrec_tokenlexbuf=match%sedlex