robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.unit.testutil' ) 2local describe =t . describe 3local itp =t . gen_itp ( t . it ) 4local t_viml =require ( 'test.unit.viml.testutil' ) 5 6local make_enum_conv_tab =t . make_enum_conv_tab 7local child_call_once =t . child_call_once 8local alloc_log_new =t . alloc_log_new 9local kvi_destroy =t . kvi_destroy 10local conv_enum =t . conv_enum 11local debug_log =t . debug_log 12local ptr2key =t . ptr2key 13local cimport =t . cimport 14local ffi =t . ffi 15local neq =t . neq 16local eq =t . eq 17local mergedicts_copy =t . mergedicts_copy 18local format_string =require ( 'test.format_string' ). format_string 19local format_luav =require ( 'test.format_string' ). format_luav 20local intchar2lua =t . intchar2lua 21local dictdiff =t . dictdiff 22 23local conv_ccs =t_viml . conv_ccs 24local new_pstate =t_viml . new_pstate 25local conv_cmp_type =t_viml . conv_cmp_type 26local pstate_set_str =t_viml . pstate_set_str 27local conv_expr_asgn_type =t_viml . conv_expr_asgn_type 28 29local lib =cimport ( './src/nvim/viml/parser/expressions.h' , './src/nvim/syntax.h' ) 30 31local alloc_log =alloc_log_new () 32 33local predefined_hl_defs ={ 34-- From highlight_init_both 35Conceal =true , 36Cursor =true , 37lCursor =true , 38DiffText =true , 39ErrorMsg =true , 40IncSearch =true , 41ModeMsg =true , 42NonText =true , 43PmenuSbar =true , 44StatusLine =true , 45StatusLineNC =true , 46TabLineFill =true , 47TabLineSel =true , 48TermCursor =true , 49VertSplit =true , 50WildMenu =true , 51WinSeparator =true , 52EndOfBuffer =true , 53QuickFixLine =true , 54Substitute =true , 55Whitespace =true , 56Error =true , 57Todo =true , 58String =true , 59Character =true , 60Number =true , 61Boolean =true , 62Float =true , 63Function =true , 64Conditional =true , 65Repeat =true , 66Label =true , 67Operator =true , 68Keyword =true , 69Exception =true , 70Include =true , 71Define =true , 72Macro =true , 73PreCondit =true , 74StorageClass =true , 75Structure =true , 76Typedef =true , 77Tag =true , 78SpecialChar =true , 79Delimiter =true , 80SpecialComment =true , 81Debug =true , 82Dimmed =true , 83 84-- From highlight_init_(dark|light) 85ColorColumn =true , 86CursorColumn =true , 87CursorLine =true , 88CursorLineNr =true , 89DiffAdd =true , 90DiffChange =true , 91DiffDelete =true , 92Directory =true , 93FoldColumn =true , 94Folded =true , 95LineNr =true , 96MatchParen =true , 97MoreMsg =true , 98Pmenu =true , 99PmenuSel =true , 100PmenuThumb =true , 101Question =true , 102Search =true , 103SignColumn =true , 104SpecialKey =true , 105SpellBad =true , 106SpellCap =true , 107SpellLocal =true , 108SpellRare =true , 109TabLine =true , 110Title =true , 111Visual =true , 112WarningMsg =true , 113Normal =true , 114Comment =true , 115Constant =true , 116Special =true , 117Identifier =true , 118Statement =true , 119PreProc =true , 120Type =true , 121Underlined =true , 122Ignore =true , 123} 124 125local nvim_hl_defs ={} 126 127child_call_once ( function () 128local i =0 129while lib . highlight_init_cmdline [ i ] ~= nil do 130local hl_args =lib . highlight_init_cmdline [ i ] 131local s =ffi . string ( hl_args ) 132local err , msg =pcall ( function () 133if s : sub ( 1 , 13 ) == 'default link ' then 134local new_grp , grp_link =s : match ( '^default link (%w+) (%w+)$' ) 135neq ( nil , new_grp ) 136-- Note: group to link to must be already defined at the time of 137-- linking, otherwise it will be created as cleared. So existence 138-- of the group is checked here and not in the next pass over 139-- nvim_hl_defs. 140eq ( true , not not ( nvim_hl_defs [ grp_link ] or predefined_hl_defs [ grp_link ])) 141eq ( false , not not ( nvim_hl_defs [ new_grp ] or predefined_hl_defs [ new_grp ])) 142nvim_hl_defs [ new_grp ] ={ 'link' , grp_link } 143else 144local new_grp , grp_args =s : match ( '^(%w+) (.*)' ) 145neq ( nil , new_grp ) 146eq ( false , not not ( nvim_hl_defs [ new_grp ] or predefined_hl_defs [ new_grp ])) 147nvim_hl_defs [ new_grp ] ={ 'definition' , grp_args } 148end 149end ) 150if not err then 151msg =format_string ( 'Error while processing string %s at position %u:\n%s' , s , i , msg ) 152error ( msg ) 153end 154i =i + 1 155end 156for k , _ in ipairs ( nvim_hl_defs ) do 157eq ( 'Nvim' , k : sub ( 1 , 4 )) 158-- NvimInvalid 159-- 12345678901 160local err , msg =pcall ( function () 161if k : sub ( 5 , 11 ) == 'Invalid' then 162neq ( nil , nvim_hl_defs [ 'Nvim' .. k : sub ( 12 )]) 163else 164neq ( nil , nvim_hl_defs [ 'NvimInvalid' .. k : sub ( 5 )]) 165end 166end ) 167if not err then 168msg =format_string ( 'Error while processing group %s:\n%s' , k , msg ) 169error ( msg ) 170end 171end 172end ) 173 174local function hls_to_hl_fs ( hls ) 175local ret ={} 176local next_col =0 177for i , v in ipairs ( hls ) do 178local group , line , col , str =v : match ( '^Nvim([a-zA-Z]+):(%d+):(%d+):(.*)$' ) 179col =tonumber ( col ) 180line =tonumber ( line ) 181assert ( line == 0 ) 182local col_shift =col - next_col 183assert ( col_shift >= 0 ) 184next_col =col + # str 185ret [ i ] =format_string ( 186'hl(%r, %r%s)' , 187group , 188str , 189( col_shift == 0 and '' or ( ', %u' ): format ( col_shift )) 190) 191end 192return ret 193end 194 195local function format_check ( expr , format_check_data , opts ) 196-- That forces specific order. 197local zflags =opts . flags [ 1 ] 198local zdata =format_check_data [ zflags ] 199local dig_len 200if opts . funcname then 201( format_string ( '\n%s(%r, {' , opts . funcname , expr )) 202dig_len =# opts . funcname + 2 203else 204( format_string ( '\n_check_parsing(%r, %r, {' , opts , expr )) 205dig_len =# "_check_parsing(, '" + # ( format_string ( '%r' , opts )) 206end 207local digits =' --' .. ( ' ' ): rep ( dig_len - # ' --' ) 208local digits2 =digits : sub ( 1 , - 10 ) 209for i =0 , # expr - 1 do 210if i % 10 == 0 then 211digits2 =( '%s%10u' ): format ( digits2 , i / 10 ) 212end 213digits =( '%s%u' ): format ( digits , i % 10 ) 214end 215( digits ) 216if # expr > 10 then 217( digits2 ) 218end 219if zdata . ast . len then 220(( ' len = %u,' ): format ( zdata . ast . len )) 221end 222( ' ast = ' .. format_luav ( zdata . ast . ast , ' ' ) .. ',' ) 223if zdata . ast . err then 224( ' err = {' ) 225( ' arg = ' .. format_luav ( zdata . ast . err . arg ) .. ',' ) 226( ' msg = ' .. format_luav ( zdata . ast . err . msg ) .. ',' ) 227( ' },' ) 228end 229( '}, {' ) 230for _ , v in ipairs ( zdata . hl_fs ) do 231( ' ' .. v .. ',' ) 232end 233local diffs ={} 234local diffs_num =0 235for flags , v in pairs ( format_check_data ) do 236if flags ~= zflags then 237diffs [ flags ] =dictdiff ( zdata , v ) 238if diffs [ flags ] then 239if flags == 3 + zflags then 240if 241dictdiff ( format_check_data [ 1 + zflags ], format_check_data [ 3 + zflags ]) == nil 242or dictdiff ( format_check_data [ 2 + zflags ], format_check_data [ 3 + zflags ]) == nil 243then 244diffs [ flags ] =nil 245else 246diffs_num =diffs_num + 1 247end 248else 249diffs_num =diffs_num + 1 250end 251end 252end 253end 254if diffs_num ~= 0 then 255( '}, {' ) 256local flags =1 257while diffs_num ~= 0 do 258if diffs [ flags ] then 259diffs_num =diffs_num - 1 260local diff =diffs [ flags ] 261(( ' [%u] = {' ): format ( flags )) 262if diff . ast then 263( ' ast = ' .. format_luav ( diff . ast , ' ' ) .. ',' ) 264end 265if diff . hl_fs then 266( ' hl_fs = ' .. format_luav ( diff . hl_fs , ' ' , { 267literal_strings =true , 268} ) .. ',' ) 269end 270( ' },' ) 271end 272flags =flags + 1 273end 274end 275( '})' ) 276end 277 278local east_node_type_tab 279make_enum_conv_tab ( 280lib , 281{ 282'kExprNodeMissing' , 283'kExprNodeOpMissing' , 284'kExprNodeTernary' , 285'kExprNodeTernaryValue' , 286'kExprNodeRegister' , 287'kExprNodeSubscript' , 288'kExprNodeListLiteral' , 289'kExprNodeUnaryPlus' , 290'kExprNodeBinaryPlus' , 291'kExprNodeNested' , 292'kExprNodeCall' , 293'kExprNodePlainIdentifier' , 294'kExprNodePlainKey' , 295'kExprNodeComplexIdentifier' , 296'kExprNodeUnknownFigure' , 297'kExprNodeLambda' , 298'kExprNodeDictLiteral' , 299'kExprNodeCurlyBracesIdentifier' , 300'kExprNodeComma' , 301'kExprNodeColon' , 302'kExprNodeArrow' , 303'kExprNodeComparison' , 304'kExprNodeConcat' , 305'kExprNodeConcatOrSubscript' , 306'kExprNodeInteger' , 307'kExprNodeFloat' , 308'kExprNodeSingleQuotedString' , 309'kExprNodeDoubleQuotedString' , 310'kExprNodeOr' , 311'kExprNodeAnd' , 312'kExprNodeUnaryMinus' , 313'kExprNodeBinaryMinus' , 314'kExprNodeNot' , 315'kExprNodeMultiplication' , 316'kExprNodeDivision' , 317'kExprNodeMod' , 318'kExprNodeOption' , 319'kExprNodeEnvironment' , 320'kExprNodeAssignment' , 321} , 322'kExprNode' , 323function ( ret ) 324east_node_type_tab =ret 325end 326) 327 328local function conv_east_node_type ( typ ) 329return conv_enum ( east_node_type_tab , typ ) 330end 331 332local eastnodelist2lua 333 334local function eastnode2lua ( pstate , eastnode , checked_nodes ) 335local key =ptr2key ( eastnode ) 336if checked_nodes [ key ] then 337checked_nodes [ key ]. duplicate_key =key 338return { duplicate =key } 339end 340local typ =conv_east_node_type ( eastnode . type ) 341local ret ={} 342checked_nodes [ key ] =ret 343ret . children =eastnodelist2lua ( pstate , eastnode . children , checked_nodes ) 344local str =pstate_set_str ( pstate , eastnode . start , eastnode . len ) 345local ret_str 346if str . error then 347ret_str ='error:' .. str . error 348else 349ret_str =( '%u:%u:%s' ): format ( str . start . line , str . start . col , str . str ) 350end 351if typ == 'Register' then 352typ =typ .. ( '(name=%s)' ): format ( tostring ( intchar2lua ( eastnode . data . reg . name ))) 353elseif typ == 'PlainIdentifier' then 354typ =typ 355.. ( '(scope=%s,ident=%s)' ): format ( 356tostring ( intchar2lua ( eastnode . data . var . scope )), 357ffi . string ( eastnode . data . var . ident , eastnode . data . var . ident_len ) 358) 359elseif typ == 'PlainKey' then 360typ =typ 361.. ( '(key=%s)' ): format ( ffi . string ( eastnode . data . var . ident , eastnode . data . var . ident_len )) 362elseif 363typ == 'UnknownFigure' 364or typ == 'DictLiteral' 365or typ == 'CurlyBracesIdentifier' 366or typ == 'Lambda' 367then 368typ =typ 369.. ( '(%s)' ): format ( 370( eastnode . data . fig . type_guesses . allow_lambda and '\\' or '-' ) 371.. ( eastnode . data . fig . type_guesses . allow_dict and 'd' or '-' ) 372.. ( eastnode . data . fig . type_guesses . allow_ident and 'i' or '-' ) 373) 374elseif typ == 'Comparison' then 375typ =typ 376.. ( '(type=%s,inv=%u,ccs=%s)' ): format ( 377conv_cmp_type ( eastnode . data . cmp . type ), 378eastnode . data . cmp . inv and 1 or 0 , 379conv_ccs ( eastnode . data . cmp . ccs ) 380) 381elseif typ == 'Integer' then 382typ =typ .. ( '(val=%u)' ): format ( tonumber ( eastnode . data . num . value )) 383elseif typ == 'Float' then 384typ =typ .. format_string ( '(val=%e)' , tonumber ( eastnode . data . flt . value )) 385elseif typ == 'SingleQuotedString' or typ == 'DoubleQuotedString' then 386if eastnode . data . str . value == nil then 387typ =typ .. '(val=NULL)' 388else 389local s =ffi . string ( eastnode . data . str . value , eastnode . data . str . size ) 390typ =format_string ( '%s(val=%q)' , typ , s ) 391end 392elseif typ == 'Option' then 393typ =( '%s(scope=%s,ident=%s)' ): format ( 394typ , 395tostring ( intchar2lua ( eastnode . data . opt . scope )), 396ffi . string ( eastnode . data . opt . ident , eastnode . data . opt . ident_len ) 397) 398elseif typ == 'Environment' then 399typ =( '%s(ident=%s)' ): format ( 400typ , 401ffi . string ( eastnode . data . env . ident , eastnode . data . env . ident_len ) 402) 403elseif typ == 'Assignment' then 404typ =( '%s(%s)' ): format ( typ , conv_expr_asgn_type ( eastnode . data . ass . type )) 405end 406ret_str =typ .. ':' .. ret_str 407local can_simplify =not ret . children 408if can_simplify then 409ret =ret_str 410else 411ret [ 1 ] =ret_str 412end 413return ret 414end 415 416eastnodelist2lua =function ( pstate , eastnode , checked_nodes ) 417local ret ={} 418while eastnode ~= nil do 419ret [ # ret + 1 ] =eastnode2lua ( pstate , eastnode , checked_nodes ) 420eastnode =eastnode . next 421end 422if # ret == 0 then 423ret =nil 424end 425return ret 426end 427 428local function east2lua ( str , pstate , east ) 429local checked_nodes ={} 430local len =tonumber ( pstate . pos . col ) 431if pstate . pos . line == 1 then 432len =tonumber ( pstate . reader . lines . items [ 0 ]. size ) 433end 434if type ( str ) == 'string' and len == # str then 435len =nil 436end 437return { 438err =east . err . msg ~= nil and { 439msg =ffi . string ( east . err . msg ), 440arg =ffi . string ( east . err . arg , east . err . arg_len ), 441} or nil , 442len =len , 443ast =eastnodelist2lua ( pstate , east . root , checked_nodes ), 444} 445end 446 447local function phl2lua ( pstate ) 448local ret ={} 449for i =0 , ( tonumber ( pstate . colors . size ) - 1 ) do 450local chunk =pstate . colors . items [ i ] 451local chunk_tbl =pstate_set_str ( pstate , chunk . start , chunk . end_col - chunk . start . col , { 452group =ffi . string ( chunk . group ), 453} ) 454ret [ i + 1 ] =( '%s:%u:%u:%s' ): format ( 455chunk_tbl . group , 456chunk_tbl . start . line , 457chunk_tbl . start . col , 458chunk_tbl . str 459) 460end 461return ret 462end 463 464describe ( 'Expressions parser' , function () 465local function _check_parsing ( opts , str , exp_ast , exp_highlighting_fs , nz_flags_exps ) 466local zflags =opts . flags [ 1 ] 467nz_flags_exps =nz_flags_exps or {} 468local format_check_data ={} 469for _ , flags in ipairs ( opts . flags ) do 470debug_log (( 'Running test case (%s, %u)' ): format ( str , flags )) 471local err , msg =pcall ( function () 472if os . getenv ( 'NVIM_TEST_PARSER_SPEC_PRINT_TEST_CASE' ) == '1' then 473( str , flags ) 474end 475alloc_log : check ( {} ) 476 477local pstate =new_pstate ( { str } ) 478local east =lib . viml_pexpr_parse ( pstate , flags ) 479local ast =east2lua ( str , pstate , east ) 480local hls =phl2lua ( pstate ) 481if exp_ast == nil then 482format_check_data [ flags ] ={ ast =ast , hl_fs =hls_to_hl_fs ( hls ) } 483else 484local exps ={ 485ast =exp_ast , 486hl_fs =exp_highlighting_fs , 487} 488local add_exps =nz_flags_exps [ flags ] 489if not add_exps and flags == 3 + zflags then 490add_exps =nz_flags_exps [ 1 + zflags ] or nz_flags_exps [ 2 + zflags ] 491end 492if add_exps then 493if add_exps . ast then 494exps . ast =mergedicts_copy ( exps . ast , add_exps . ast ) 495end 496if add_exps . hl_fs then 497exps . hl_fs =mergedicts_copy ( exps . hl_fs , add_exps . hl_fs ) 498end 499end 500eq ( exps . ast , ast ) 501if exp_highlighting_fs then 502local exp_highlighting ={} 503local next_col =0 504for i , h in ipairs ( exps . hl_fs ) do 505exp_highlighting [ i ], next_col =h ( next_col ) 506end 507eq ( exp_highlighting , hls ) 508end 509end 510lib . viml_pexpr_free_ast ( east ) 511kvi_destroy ( pstate . colors ) 512alloc_log : clear_tmp_allocs ( true ) 513alloc_log : check ( {} ) 514end ) 515if not err then 516msg =format_string ( 'Error while processing test (%r, %u):\n%s' , str , flags , msg ) 517error ( msg ) 518end 519end 520if exp_ast == nil then 521format_check ( str , format_check_data , opts ) 522end 523end 524local function hl ( group , str , shift ) 525return function ( next_col ) 526if nvim_hl_defs [ 'Nvim' .. group ] == nil then 527error (( 'Unknown group: Nvim%s' ): format ( group )) 528end 529local col =next_col + ( shift or 0 ) 530return (( '%s:%u:%u:%s' ): format ( 'Nvim' .. group , 0 , col , str )), ( col + # str ) 531end 532end 533local function fmtn ( typ , args , rest ) 534return ( '%s(%s)%s' ): format ( typ , args , rest ) 535end 536require ( 'test.unit.viml.expressions.parser_tests' )( itp , _check_parsing , hl , fmtn ) 537end )