vinter 0.6.6 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/vinter/parser.rb CHANGED
@@ -17,6 +17,10 @@ module Vinter
17
17
  }
18
18
  end
19
19
 
20
+ def parse_initializer
21
+ { expression: parse_expression, errors: @errors }
22
+ end
23
+
20
24
  private
21
25
 
22
26
  def current_token
@@ -135,7 +139,12 @@ module Vinter
135
139
  current_token[:type] == :local_variable ||
136
140
  current_token[:type] == :global_variable ||
137
141
  current_token[:type] == :script_local)
138
- loop_vars << advance
142
+ variable = advance
143
+ if current_token&.dig(:type) == :colon
144
+ advance
145
+ variable = variable.merge(var_type: parse_type)
146
+ end
147
+ loop_vars << variable
139
148
  else
140
149
  add_error("Expected identifier in for loop variables")
141
150
  break
@@ -245,6 +254,10 @@ module Vinter
245
254
  return nil
246
255
  end
247
256
 
257
+ if current_token[:type] == :ex_command_line
258
+ return advance
259
+ end
260
+
248
261
  # Handle pipe as command separator
249
262
  if current_token[:type] == :operator && current_token[:value] == '|'
250
263
  advance # Skip the pipe
@@ -308,6 +321,19 @@ module Vinter
308
321
  return nil
309
322
  end
310
323
 
324
+ if current_token[:type] == :terminal_mapping
325
+ token = advance
326
+ arguments = token[:arguments].lstrip
327
+ options = []
328
+ while (option = arguments.match(/\A<(?:buffer|nowait|silent|script|expr|unique|special)>[ \t]*/i))
329
+ options << option[0].rstrip
330
+ arguments = option.post_match
331
+ end
332
+ key, target = arguments.split(/[ \t]+/, 2)
333
+ return { type: :mapping_statement, command: token[:value], options: options,
334
+ key_sequence: key, target: target.to_s, line: token[:line], column: token[:column] }
335
+ end
336
+
311
337
  # Add case for mapping commands
312
338
  if current_token[:type] == :keyword &&
313
339
  ['nnoremap', 'nmap', 'inoremap', 'imap', 'vnoremap', 'vmap',
@@ -322,7 +348,11 @@ module Vinter
322
348
  when 'if'
323
349
  parse_if_statement
324
350
  when 'command'
325
- parse_command_definition
351
+ if peek_token&.dig(:value) == '->'
352
+ parse_expression_statement
353
+ else
354
+ parse_command_definition
355
+ end
326
356
  when 'while'
327
357
  parse_while_statement
328
358
  when 'for'
@@ -372,7 +402,12 @@ module Vinter
372
402
  when 'call'
373
403
  parse_call_statement
374
404
  when 'delete'
375
- parse_delete_statement
405
+ if peek_token && peek_token[:type] == :paren_open
406
+ token = advance
407
+ parse_function_call(token[:value], token[:line], token[:column])
408
+ else
409
+ parse_delete_command
410
+ end
376
411
  when 'set', 'setlocal'
377
412
  parse_set_command
378
413
  when 'syntax'
@@ -400,7 +435,17 @@ module Vinter
400
435
  nil
401
436
  end
402
437
  elsif current_token[:type] == :identifier
403
- if current_token[:value] == "echo"
438
+ if current_token[:value] == 'type' && peek_token&.dig(:type) == :identifier
439
+ parse_type_alias
440
+ elsif %w[lcd chdir cd tcd setl redraw quit cunmap abclear wincmd].include?(current_token[:value]) && peek_token&.dig(:type) != :paren_open
441
+ parse_command_line_call
442
+ elsif current_token[:value] == 'func' && [:identifier, :global_variable, :script_local, :operator].include?(peek_token&.dig(:type))
443
+ parse_legacy_function
444
+ elsif current_token[:value] == 'syn' && peek_token&.dig(:type) == :identifier
445
+ parse_syntax_command
446
+ elsif %w[hi highlight].include?(current_token[:value])
447
+ parse_highlight_command
448
+ elsif current_token[:value] == "echo"
404
449
  parse_echo_statement
405
450
  elsif current_token[:value] == "augroup"
406
451
  parse_augroup_statement
@@ -408,15 +453,13 @@ module Vinter
408
453
  parse_autocmd_statement
409
454
  elsif current_token[:value] == "filter" || current_token[:value] == "filt"
410
455
  parse_filter_command
411
- elsif current_token[:value] == "command"
412
- parse_command_definition
413
456
  else
414
457
  parse_expression_statement
415
458
  end
416
459
  elsif current_token[:type] == :comment
417
460
  parse_comment
418
- #elsif current_token[:type] == :string && current_token[:value].start_with?('"')
419
- #parse_comment
461
+ elsif current_token[:type] == :string && current_token[:value].start_with?('"')
462
+ parse_comment
420
463
  elsif current_token[:type] == :silent_bang
421
464
  parse_silent_command
422
465
  elsif current_token[:type] == :identifier && current_token[:value] == 'delete'
@@ -443,6 +486,8 @@ module Vinter
443
486
  parse_command_line_call
444
487
  elsif current_token[:type] == :number
445
488
  advance
489
+ elsif [:paren_open, :bracket_open].include?(current_token[:type])
490
+ parse_expression_statement
446
491
  elsif current_token[:type] == :paren_close
447
492
  advance
448
493
  elsif current_token[:type] == :brace_close
@@ -459,7 +504,11 @@ module Vinter
459
504
  while current_token && current_token[:value] != stop_token
460
505
  advance
461
506
  end
462
- elsif [:bracket_open, :bracket_close, :operator, :question_mark].include?(current_token[:type])
507
+ elsif [:bracket_close, :operator, :question_mark].include?(current_token[:type])
508
+ advance
509
+ elsif current_token[:type] == :mode_command
510
+ advance
511
+ elsif current_token[:type] == :exec_command
463
512
  advance
464
513
  else
465
514
  @warnings << {
@@ -479,7 +528,8 @@ module Vinter
479
528
  column = token[:column]
480
529
 
481
530
  # Extract the command or range
482
- command = token[:value][1..] # Remove the leading ':'
531
+ command = token[:value].delete_prefix(':')
532
+ command = advance[:value] if command.empty? && current_token && current_token[:line] == line
483
533
 
484
534
  # Check if the command is a range (e.g., `:2`, `:$`)
485
535
  if command.match?(/^\d+$/) || command == '$'
@@ -493,9 +543,19 @@ module Vinter
493
543
 
494
544
  # Otherwise, it's a regular command (e.g., `:exe`)
495
545
  args = []
496
- while current_token && current_token[:type] != :newline
497
- args << current_token[:value]
498
- advance
546
+ argument_line = line
547
+ escaped = false
548
+ while current_token
549
+ if current_token[:line] != argument_line
550
+ break unless [:line_continuation, :backslash].include?(current_token[:type])
551
+ argument_line = current_token[:line]
552
+ advance
553
+ escaped = false
554
+ next
555
+ end
556
+ break if current_token[:value] == '|' && !escaped
557
+ escaped = current_token[:type] == :backslash && !escaped
558
+ args << advance[:value]
499
559
  end
500
560
 
501
561
  {
@@ -600,35 +660,28 @@ module Vinter
600
660
  column: column
601
661
  }
602
662
 
603
- # Property access (dot notation)
604
- while current_token && current_token[:type] == :operator && current_token[:value] == '.'
605
- dot_token = advance
606
- if current_token && (current_token[:type] == :identifier || current_token[:type] == :keyword)
607
- property_token = advance
608
- target = {
609
- type: :property_access,
610
- object: target,
611
- property: property_token[:value],
612
- line: dot_token[:line],
613
- column: dot_token[:column]
614
- }
663
+ loop do
664
+ if current_token && current_token[:type] == :bracket_open
665
+ bracket = advance
666
+ index = parse_expression
667
+ expect(:bracket_close)
668
+ target = { type: :indexed_access, object: target, index: index, line: bracket[:line], column: bracket[:column] }
669
+ elsif current_token && current_token[:value] == '.'
670
+ dot = advance
671
+ unless current_token && [:identifier, :keyword].include?(current_token[:type])
672
+ add_error("Expected property name after '.' in global variable")
673
+ break
674
+ end
675
+ target = { type: :property_access, object: target, property: advance[:value], line: dot[:line], column: dot[:column] }
615
676
  else
616
- add_error("Expected property name after '.' in global variable")
677
+ break
617
678
  end
618
679
  end
619
680
 
620
- # Indexed access (e.g., g:foo[bar])
621
- while current_token && current_token[:type] == :bracket_open
622
- bracket_token = advance
623
- index_expr = parse_expression
624
- expect(:bracket_close)
625
- target = {
626
- type: :indexed_access,
627
- object: target,
628
- index: index_expr,
629
- line: bracket_token[:line],
630
- column: bracket_token[:column]
631
- }
681
+ if current_token && (current_token[:value] == '->' ||
682
+ (current_token[:type] == :paren_open && current_token[:line] == @tokens[@position - 1][:line]))
683
+ expression = parse_postfix_expression(target)
684
+ return { type: :expression_statement, expression: expression, line: line, column: column }
632
685
  end
633
686
 
634
687
  # Assignment operator (= or compound)
@@ -939,44 +992,8 @@ module Vinter
939
992
  add_error("Expected assignment operator after variable in let statement")
940
993
  end
941
994
 
942
- # Parse the value expression
943
- value = nil
944
-
945
- # Special handling for function() references in let statements
946
- if current_token && current_token[:type] == :keyword && current_token[:value] == 'function'
947
- advance # Skip 'function'
948
-
949
- # Expect opening parenthesis
950
- if current_token && current_token[:type] == :paren_open
951
- paren_token = advance # Skip '('
952
-
953
- # Parse the function name as a string
954
- func_name = nil
955
- if current_token && current_token[:type] == :string
956
- func_name = current_token[:value]
957
- advance
958
- else
959
- add_error("Expected string with function name in function() call")
960
- end
961
-
962
- # Expect closing parenthesis
963
- expect(:paren_close) # Skip ')'
995
+ value = parse_expression
964
996
 
965
- value = {
966
- type: :function_reference,
967
- function_name: func_name,
968
- line: line,
969
- column: column
970
- }
971
- else
972
- # If not followed by parenthesis, parse as a normal expression
973
- @position -= 1 # Go back to the 'function' keyword
974
- value = parse_expression
975
- end
976
- else
977
- # Normal expression parsing
978
- value = parse_expression
979
- end
980
997
  {
981
998
  type: :let_statement,
982
999
  target: target,
@@ -992,6 +1009,7 @@ module Vinter
992
1009
  line = token[:line]
993
1010
  column = token[:column]
994
1011
 
1012
+ advance if current_token && current_token[:value] == '!'
995
1013
  # Parse the variable to unlet
996
1014
  target = nil
997
1015
  if current_token
@@ -1273,6 +1291,10 @@ module Vinter
1273
1291
  current_token[:type] == :script_local ||
1274
1292
  current_token[:type] == :builtin_funcs)
1275
1293
  loop_var = advance
1294
+ if current_token&.dig(:type) == :colon
1295
+ advance
1296
+ loop_var = loop_var.merge(var_type: parse_type)
1297
+ end
1276
1298
  else
1277
1299
  add_error("Expected identifier as for loop variable")
1278
1300
  end
@@ -1315,7 +1337,7 @@ module Vinter
1315
1337
  advance
1316
1338
  end
1317
1339
 
1318
- if current_token && [:identifier, :global_variable].include?(current_token[:type])
1340
+ if current_token && [:identifier, :global_variable, :type].include?(current_token[:type])
1319
1341
  name = advance
1320
1342
  end
1321
1343
  #name = expect(:identifier)
@@ -1330,12 +1352,32 @@ module Vinter
1330
1352
  advance # Skip ':'
1331
1353
  return_type = parse_type
1332
1354
  end
1333
- body = []
1334
- while current_token && current_token[:value] != 'enddef'
1335
- stmt = parse_statement
1336
- body << stmt if stmt
1355
+ # Parse the body within its own bounds. Recovery inside a statement
1356
+ # must not consume enddef or a following top-level declaration.
1357
+ start = @position
1358
+ depth = 1
1359
+ while current_token
1360
+ break if current_token[:type] == :keyword && current_token[:value] == 'enddef' && depth == 1
1361
+ if current_token[:type] == :keyword
1362
+ previous = @tokens[@position - 1]
1363
+ highlight_modifier = previous && previous[:line] == current_token[:line] && %w[hi highlight].include?(previous[:value])
1364
+ depth += 1 if current_token[:value] == 'def' && !highlight_modifier
1365
+ depth -= 1 if current_token[:value] == 'enddef'
1366
+ end
1367
+ advance
1368
+ end
1369
+ result = self.class.new(@tokens[start...@position], @source_text).parse
1370
+ body = result[:ast][:body]
1371
+ [result[:errors], result[:warnings]].each do |diagnostics|
1372
+ diagnostics.each { |issue| issue[:position] += start if issue[:position] }
1373
+ end
1374
+ @errors.concat(result[:errors])
1375
+ @warnings.concat(result[:warnings])
1376
+ if current_token
1377
+ advance
1378
+ else
1379
+ add_error("Expected enddef to close block", token)
1337
1380
  end
1338
- advance
1339
1381
 
1340
1382
  {
1341
1383
  type: :def_function,
@@ -1351,111 +1393,99 @@ module Vinter
1351
1393
  def parse_parameter_list
1352
1394
  params = []
1353
1395
 
1354
- # Empty parameter list
1355
- if current_token && current_token[:type] == :paren_close
1356
- advance
1357
- return params
1396
+ until current_token.nil? || current_token[:type] == :paren_close
1397
+ name = advance
1398
+ annotation = nil
1399
+ if current_token && current_token[:type] == :colon
1400
+ advance
1401
+ annotation = parse_type
1402
+ end
1403
+ params << { type: :parameter, name: name[:value], param_type: annotation,
1404
+ line: name[:line], column: name[:column] }
1405
+
1406
+ # Defaults may contain nested calls, lists, and dictionaries.
1407
+ depth = 0
1408
+ while current_token
1409
+ kind = current_token[:type]
1410
+ break if depth.zero? && [:comma, :paren_close].include?(kind)
1411
+ depth += 1 if [:paren_open, :bracket_open, :brace_open].include?(kind) || (kind == :builtin_funcs && current_token[:value].end_with?('('))
1412
+ depth -= 1 if [:paren_close, :bracket_close, :brace_close].include?(kind)
1413
+ advance
1414
+ end
1415
+ advance if current_token && current_token[:type] == :comma
1358
1416
  end
1359
- # TODO actually save params
1360
- required_parens = 1
1361
- while current_token && required_parens > 0
1362
- if current_token[:type] == :paren_open
1363
- required_parens += 1
1364
- elsif current_token[:type] == :paren_close
1365
- required_parens -= 1
1366
- end
1367
- advance
1368
- end
1369
-
1370
- # Parse parameters until we find a closing parenthesis
1371
- #while @position < @tokens.length && current_token && current_token[:type] != :paren_close
1372
- ## Check for variable args
1373
- #if current_token && current_token[:type] == :ellipsis
1374
- #ellipsis_token = advance
1375
-
1376
- ## Parse type for variable args if present
1377
- #param_type = nil
1378
- #if current_token && current_token[:type] == :colon
1379
- #advance # Skip ':'
1380
- #param_type = parse_type
1381
- #end
1382
-
1383
- #params << {
1384
- #type: :var_args,
1385
- #param_type: param_type,
1386
- #line: ellipsis_token[:line],
1387
- #column: ellipsis_token[:column]
1388
- #}
1389
-
1390
- ## After varargs, we expect closing paren
1391
- #if current_token && current_token[:type] != :paren_close
1392
- #add_error("Expected closing parenthesis after varargs", current_token)
1393
- #end
1394
-
1395
- #break
1396
- #end
1397
-
1398
- #param_name = advance
1399
-
1400
- ## Check for type annotation
1401
- #param_type = nil
1402
- #if current_token && current_token[:type] == :colon
1403
- #advance # Skip ':'
1404
- #param_type = parse_type
1405
- #end
1406
-
1407
- ## Check for default value
1408
- #default_value = nil
1409
- #if current_token && current_token[:type] == :operator && current_token[:value] == '='
1410
- #advance # Skip '='
1411
- #default_value = parse_expression
1412
- #end
1413
-
1414
- #params << {
1415
- #type: :parameter,
1416
- #name: param_name[:value],
1417
- #param_type: param_type,
1418
- #optional: default_value != nil,
1419
- #default_value: default_value,
1420
- #line: param_name[:line],
1421
- #column: param_name[:column]
1422
- #}
1423
-
1424
- ## If we have a comma, advance past it and continue
1425
- #if current_token && current_token[:type] == :comma
1426
- #advance
1427
- ## If we don't have a comma, we should have a closing paren
1428
- #elsif current_token && current_token[:type] != :paren_close
1429
- #add_error("Expected comma or closing parenthesis after parameter", current_token)
1430
- #break
1431
- #end
1432
- #end
1433
-
1417
+ expect(:paren_close)
1434
1418
  params
1435
1419
  end
1436
1420
 
1421
+ def parse_type_alias
1422
+ token = advance
1423
+ name = expect(:identifier)
1424
+ if current_token&.dig(:value) == '='
1425
+ advance
1426
+ value = parse_type
1427
+ else
1428
+ add_error("Expected = after type alias name", token)
1429
+ end
1430
+ { type: :type_alias, name: name&.dig(:value), value: value, line: token[:line], column: token[:column] }
1431
+ end
1432
+
1437
1433
  def parse_type
1434
+ if current_token&.dig(:type) == :ellipsis
1435
+ advance
1436
+ return { type: :variadic_type, element_type: parse_type }
1437
+ end
1438
1438
  if current_token && [:identifier, :keyword, :type].include?(current_token[:type])
1439
1439
  type_name = advance
1440
1440
 
1441
+ if current_token&.dig(:value) == '.' && peek_token&.dig(:type) == :identifier
1442
+ advance
1443
+ return "#{type_name[:value]}.#{advance[:value]}"
1444
+ end
1445
+
1441
1446
  # Handle generic types like list<string>
1442
1447
  if current_token && current_token[:type] == :operator && current_token[:value] == '<'
1443
1448
  advance # Skip '<'
1444
1449
  inner_type = parse_type
1445
- expect(:operator) # This should be '>'
1450
+ types = [inner_type]
1451
+ while type_name[:value] == 'tuple' && current_token&.dig(:type) == :comma
1452
+ advance
1453
+ types << parse_type
1454
+ end
1455
+ if current_token&.dig(:value) == '>'
1456
+ advance
1457
+ else
1458
+ add_error("Expected > after generic type")
1459
+ end
1446
1460
 
1447
1461
  return {
1448
1462
  type: :generic_type,
1449
1463
  base_type: type_name[:value],
1450
- inner_type: inner_type,
1464
+ inner_type: type_name[:value] == 'tuple' ? types : inner_type,
1451
1465
  line: type_name[:line],
1452
1466
  column: type_name[:column]
1453
1467
  }
1454
1468
  end
1455
1469
 
1470
+ if type_name[:value] == 'func' && current_token&.dig(:type) == :paren_open
1471
+ advance
1472
+ parameters = []
1473
+ until !current_token || current_token[:type] == :paren_close
1474
+ parameters << parse_type
1475
+ break unless current_token&.dig(:type) == :comma
1476
+ advance
1477
+ end
1478
+ expect(:paren_close)
1479
+ if current_token&.dig(:type) == :colon
1480
+ advance
1481
+ return_type = parse_type
1482
+ end
1483
+ return { type: :function_type, parameters: parameters, return_type: return_type }
1484
+ end
1485
+
1456
1486
  return type_name[:value]
1457
1487
  else
1458
- add_error("Expected type identifier: #{current_token[:value]}, #{current_token[:type]}")
1488
+ add_error("Expected type identifier")
1459
1489
  advance
1460
1490
  return "unknown"
1461
1491
  end
@@ -1475,7 +1505,7 @@ module Vinter
1475
1505
  advance
1476
1506
  name += current_token[:value]
1477
1507
  end
1478
- elsif !current_token || ![:identifier, :keyword].include?(current_token[:type])
1508
+ elsif !current_token || current_token[:type] != :identifier
1479
1509
  add_error("Expected variable name: #{current_token[:type]}")
1480
1510
  return nil
1481
1511
  else
@@ -1494,9 +1524,25 @@ module Vinter
1494
1524
  initializer = nil
1495
1525
  if current_token && (current_token[:type] == :operator && current_token[:value] == '=')
1496
1526
  advance # Skip '='
1497
- assignment_line = current_token[:line]
1498
- while current_token[:line] == assignment_line
1499
- advance
1527
+ start = @position
1528
+ depth = 0
1529
+ while current_token
1530
+ token = advance
1531
+ kind = token[:type]
1532
+ depth += 1 if [:paren_open, :bracket_open, :brace_open].include?(kind) || (kind == :builtin_funcs && token[:value].end_with?('('))
1533
+ depth -= 1 if [:paren_close, :bracket_close, :brace_close].include?(kind)
1534
+ break unless current_token
1535
+ next if current_token[:line] == token[:line] || depth > 0
1536
+ break unless Lexer::CONTINUATION_OPERATORS.include?(current_token[:value]) ||
1537
+ Lexer::CONTINUATION_OPERATORS.include?(token[:value])
1538
+ end
1539
+ # Parse supported initializer forms within the declaration boundary.
1540
+ # A following statement must never become part of its expression.
1541
+ expression_tokens = @tokens[start...@position]
1542
+ if expression_tokens.first && [:number, :string, :bracket_open, :paren_open].include?(expression_tokens.first[:type])
1543
+ result = self.class.new(expression_tokens).parse_initializer
1544
+ initializer = result[:expression]
1545
+ @errors.concat(result[:errors])
1500
1546
  end
1501
1547
  end
1502
1548
 
@@ -1520,6 +1566,7 @@ module Vinter
1520
1566
  # Check if we've reached the end of the file, end of line, or a semicolon
1521
1567
  if @position < @tokens.length &&
1522
1568
  current_token &&
1569
+ current_token[:line] == line && current_token[:value] != '|' &&
1523
1570
  current_token[:type] != :semicolon &&
1524
1571
  !(current_token[:type] == :keyword &&
1525
1572
  ['endif', 'endwhile', 'endfor', 'endfunction', 'endfunc'].include?(current_token[:value]))
@@ -1610,9 +1657,9 @@ module Vinter
1610
1657
  expr = parse_binary_expression
1611
1658
 
1612
1659
  # Check if this is a ternary expression
1613
- if current_token && (current_token[:type] == :question_mark && peek_token[:type] != :question_mark || (current_token[:type] == :operator && current_token[:value] == '?'))
1660
+ if current_token && (current_token[:type] == :question_mark && peek_token&.dig(:type) != :question_mark || (current_token[:type] == :operator && current_token[:value] == '?'))
1614
1661
  question_token = advance # Skip '?'
1615
- while current_token[:value] != ':'
1662
+ while current_token && current_token[:value] != ':'
1616
1663
  advance
1617
1664
  end
1618
1665
  advance # skip :
@@ -1627,9 +1674,16 @@ module Vinter
1627
1674
  left = parse_unary_expression
1628
1675
 
1629
1676
  # Handle multi-line expressions where operators may appear at line beginnings
1630
- while current_token &&
1631
- (current_token[:type] == :operator ||
1632
- (peek_token && peek_token[:type] == :operator && current_token[:type] == :whitespace))
1677
+ loop do
1678
+ if current_token&.dig(:type) == :comment
1679
+ following = @position
1680
+ following += 1 while @tokens[following]&.dig(:type) == :comment
1681
+ @position = following if @tokens[following]&.dig(:type) == :operator
1682
+ end
1683
+ break unless current_token && (current_token[:type] == :operator ||
1684
+ (current_token[:type] == :whitespace && peek_token&.dig(:type) == :operator))
1685
+
1686
+ break if current_token[:value] == '|'
1633
1687
 
1634
1688
  # Skip any whitespace before the operator if it's at the beginning of a line
1635
1689
  if current_token[:type] == :whitespace
@@ -1725,7 +1779,7 @@ module Vinter
1725
1779
  2
1726
1780
  when '&&' # Logical AND
1727
1781
  3
1728
- when '==', '!=', '>', '<', '>=', '<=' # Comparison
1782
+ when /\A(?:==|!=|>=?|<=?|=~|!~)[#?]?\z/ # Comparison
1729
1783
  4
1730
1784
  when '+', '-' # Addition, subtraction
1731
1785
  5
@@ -1780,80 +1834,18 @@ module Vinter
1780
1834
  # This is the type() function call
1781
1835
  return parse_builtin_function_call
1782
1836
  elsif token[:value] == 'function'
1783
- # Check if this is a function() call or just 'function' as property name
1784
- if peek_token && peek_token[:type] == :paren_open
1785
- advance # Skip 'function'
1786
- # This is a function() call
1787
- if current_token && current_token[:type] == :paren_open
1788
- # This is a function reference call
1789
- paren_token = advance # Skip '('
1790
-
1791
- # Parse the function name as a string or arrow function
1792
- if current_token && current_token[:type] == :string
1793
- func_name = current_token[:value]
1794
- advance
1795
-
1796
- # Check for additional arguments (function() can take multiple arguments)
1797
- args = []
1798
- while current_token && current_token[:type] == :comma
1799
- advance # Skip comma
1800
- arg = parse_expression
1801
- args << arg if arg
1802
- end
1803
-
1804
- # Expect closing parenthesis
1805
- expect(:paren_close) # Skip ')'
1806
-
1807
- return {
1808
- type: :function_reference,
1809
- function_name: func_name,
1810
- arguments: args,
1811
- line: line,
1812
- column: column
1813
- }
1814
- elsif current_token && current_token[:type] == :brace_open
1815
- # This is an arrow function inside function()
1816
- arrow_function = parse_vim_lambda(line, column)
1817
-
1818
- # Expect closing parenthesis
1819
- expect(:paren_close) # Skip ')'
1820
-
1821
- return {
1822
- type: :function_reference,
1823
- function_body: arrow_function,
1824
- line: line,
1825
- column: column
1826
- }
1827
- elsif current_token && current_token[:type] == :option_variable
1828
- option_var = advance
1829
- expect(:paren_close)
1830
- return {
1831
- type: :function_reference,
1832
- function_body: option_var[:value],
1833
- line: line,
1834
- column: column
1835
- }
1836
- else
1837
- @errors << {
1838
- message: "Expected string or arrow function definition in function() call: #{current_token}",
1839
- position: @position,
1840
- line: current_token ? current_token[:line] : 0,
1841
- column: current_token ? current_token[:column] : 0
1842
- }
1843
- end
1844
- end
1837
+ advance
1838
+ if current_token && current_token[:type] == :paren_open
1839
+ expr = parse_function_reference(token)
1845
1840
  else
1846
- # If not followed by parenthesis, treat 'function' as an identifier (property name)
1847
- advance # Skip 'function'
1848
- return {
1849
- type: :identifier,
1850
- name: token[:value],
1851
- line: line,
1852
- column: column
1853
- }
1841
+ expr = { type: :identifier, name: token[:value], line: line, column: column }
1854
1842
  end
1855
- elsif token[:value] == 'command'
1843
+ elsif token[:value] == 'call' && peek_token&.dig(:type) == :paren_open
1856
1844
  advance
1845
+ expr = parse_function_call('call', line, column)
1846
+ elsif %w[source command call].include?(token[:value])
1847
+ advance
1848
+ expr = { type: :identifier, name: token[:value], line: line, column: column }
1857
1849
  elsif token[:value] == 'return'
1858
1850
  advance
1859
1851
  elsif token[:value] == 'elseif'
@@ -1878,27 +1870,9 @@ module Vinter
1878
1870
  column: column
1879
1871
  }
1880
1872
  when :string
1881
- # parse_string
1882
- if token[:value].start_with?('"')
1883
- advance
1884
- return {
1885
- type: :comment,
1886
- value: token[:value],
1887
- line: line,
1888
- column: column
1889
- }
1890
- else
1891
- string_value = token[:value]
1892
- advance
1893
- expr = {
1894
- type: :literal,
1895
- value: string_value,
1896
- raw_value: string_value, # Store the raw string to preserve escapes
1897
- token_type: :string,
1898
- line: line,
1899
- column: column
1900
- }
1901
- end
1873
+ advance
1874
+ expr = { type: :literal, value: token[:value], raw_value: token[:value],
1875
+ token_type: :string, line: line, column: column }
1902
1876
  when :option_variable
1903
1877
  # Handle Vim option variables (like &compatible)
1904
1878
  advance
@@ -1940,8 +1914,7 @@ module Vinter
1940
1914
  line: line,
1941
1915
  column: column
1942
1916
  }
1943
- when :buffer_local
1944
- # Handle script-local variables/functions (like s:var)
1917
+ when :buffer_local, :window_local, :tab_local
1945
1918
  advance
1946
1919
 
1947
1920
  # Check if this is a function call
@@ -1950,16 +1923,16 @@ module Vinter
1950
1923
  end
1951
1924
 
1952
1925
  expr = {
1953
- type: :buffer_local,
1926
+ type: token[:type],
1954
1927
  name: token[:value],
1955
1928
  line: line,
1956
1929
  column: column
1957
1930
  }
1958
- when :global_variable
1931
+ when :global_variable, :environment_variable
1959
1932
  # Handle global variables (like g:var)
1960
1933
  advance
1961
1934
  expr = {
1962
- type: :global_variable,
1935
+ type: token[:type],
1963
1936
  name: token[:value],
1964
1937
  line: line,
1965
1938
  column: column
@@ -2037,19 +2010,18 @@ module Vinter
2037
2010
  if is_lambda_expression
2038
2011
  return parse_lambda_expression(line, column)
2039
2012
  else
2040
- advance # Skip '('
2041
- required_parens = 1
2042
- while current_token && required_parens > 0
2043
- if current_token[:type] == :paren_open
2044
- required_parens += 1
2045
- elsif current_token[:type] == :paren_close
2046
- required_parens -= 1
2013
+ advance
2014
+ expr = parse_expression
2015
+ if current_token&.dig(:type) == :comma
2016
+ elements = [expr]
2017
+ while current_token&.dig(:type) == :comma
2018
+ advance
2019
+ break if current_token&.dig(:type) == :paren_close
2020
+ elements << parse_expression
2047
2021
  end
2048
- advance
2022
+ expr = { type: :tuple_literal, elements: elements, line: line, column: column }
2049
2023
  end
2050
- #expr = parse_expression
2051
- #binding.pry
2052
- #expect(:paren_close) # Expect and skip ')'
2024
+ expect(:paren_close)
2053
2025
  end
2054
2026
  when :bracket_open
2055
2027
  expr = parse_list_literal(line, column)
@@ -2081,6 +2053,7 @@ module Vinter
2081
2053
  expr = parse_builtin_function_call
2082
2054
  when :type
2083
2055
  advance
2056
+ expr = { type: :identifier, name: token[:value], line: line, column: column }
2084
2057
  when :colon
2085
2058
  advance
2086
2059
  when :operator
@@ -2102,6 +2075,12 @@ module Vinter
2102
2075
  return nil
2103
2076
  end
2104
2077
 
2078
+ parse_postfix_expression(expr)
2079
+ end
2080
+
2081
+ def parse_postfix_expression(expr)
2082
+ return expr unless expr
2083
+
2105
2084
  # Now handle any chained property access or method calls
2106
2085
  while current_token
2107
2086
  # Check for property access with dot
@@ -2116,7 +2095,8 @@ module Vinter
2116
2095
  expr[:type] == :arg_variable || expr[:type] == :buffer_local ||
2117
2096
  expr[:type] == :window_local || expr[:type] == :tab_local ||
2118
2097
  expr[:type] == :local_variable || expr[:type] == :property_access ||
2119
- expr[:type] == :indexed_access)
2098
+ expr[:type] == :indexed_access || expr[:type] == :cast_expression ||
2099
+ expr[:type] == :call_expression || expr[:type] == :method_call)
2120
2100
 
2121
2101
  dot_token = advance # Skip '.'
2122
2102
  # Next token should be an identifier (property name)
@@ -2147,49 +2127,36 @@ module Vinter
2147
2127
  # Check for method call with arrow ->
2148
2128
  elsif current_token[:type] == :operator && current_token[:value] == '->'
2149
2129
  arrow_token = advance # Skip '->'
2150
- advance if current_token[:type] == :line_continuation
2130
+ advance if current_token && current_token[:type] == :line_continuation
2151
2131
 
2152
2132
  # Next token should be an identifier (method name)
2153
2133
  if current_token && current_token[:type] == :builtin_funcs
2154
- parse_builtin_function_call
2155
- elsif !current_token || current_token[:type] != :identifier
2156
- @errors << {
2157
- message: "Expected method name after '->'",
2158
- position: @position,
2159
- line: current_token ? current_token[:line] : 0,
2160
- column: current_token ? current_token[:column] : 0
2161
- }
2162
- break
2163
- end
2164
-
2165
- method_name = advance[:value] # Get method name
2166
-
2167
- # Check for arguments
2168
- args = []
2169
- if current_token && current_token[:type] == :paren_open
2170
- paren_count = 1
2171
- while current_token && paren_count > 0
2172
- if current_token[:type] == :paren_open
2173
- paren_count += 1
2174
- elsif current_token[:type] == :paren_close
2175
- paren_count -= 1
2176
- end
2134
+ method_name = parse_builtin_function_call[:name].delete_suffix('(')
2135
+ elsif current_token && ([:identifier, :type].include?(current_token[:type]) ||
2136
+ (current_token[:type] == :keyword && peek_token&.dig(:type) == :paren_open))
2137
+ method_name = advance[:value]
2138
+ if current_token && current_token[:type] == :paren_open
2177
2139
  advance
2140
+ consume_call_arguments
2178
2141
  end
2142
+ else
2143
+ add_error("Expected method name after '->'")
2144
+ break
2179
2145
  end
2180
2146
 
2181
2147
  expr = {
2182
2148
  type: :method_call,
2183
2149
  object: expr,
2184
2150
  method: method_name,
2185
- arguments: args,
2151
+ arguments: [],
2186
2152
  line: arrow_token[:line],
2187
2153
  column: arrow_token[:column]
2188
2154
  }
2189
2155
  # Check for indexing with brackets
2190
2156
  elsif current_token[:type] == :bracket_open
2157
+ break if current_token[:line] != @tokens[@position - 1][:line]
2191
2158
  bracket_token = advance # Skip '['
2192
- index_expr = parse_expression
2159
+ index_expr = parse_expression unless current_token&.dig(:type) == :colon
2193
2160
 
2194
2161
  # Add support for list slicing with colon
2195
2162
  end_index = nil
@@ -2207,8 +2174,8 @@ module Vinter
2207
2174
  end_index = parse_expression
2208
2175
  end
2209
2176
  # handles array assignment [a,b] = split(thing)
2210
- elsif current_token[:type] == :comma
2211
- while current_token[:type] == :comma
2177
+ elsif current_token && current_token[:type] == :comma
2178
+ while current_token && current_token[:type] == :comma
2212
2179
  advance
2213
2180
  parse_expression
2214
2181
  end
@@ -2237,7 +2204,7 @@ module Vinter
2237
2204
  }
2238
2205
  end
2239
2206
  # Check for function call directly on an expression
2240
- elsif current_token[:type] == :paren_open
2207
+ elsif current_token[:type] == :paren_open && current_token[:line] == @tokens[@position - 1][:line]
2241
2208
  paren_token = advance # Skip '('
2242
2209
 
2243
2210
  args = []
@@ -2250,6 +2217,7 @@ module Vinter
2250
2217
 
2251
2218
  if current_token && current_token[:type] == :comma
2252
2219
  advance # Skip comma
2220
+ break if current_token&.dig(:type) == :paren_close
2253
2221
  else
2254
2222
  break
2255
2223
  end
@@ -2274,33 +2242,48 @@ module Vinter
2274
2242
  return expr
2275
2243
  end
2276
2244
 
2245
+ def parse_function_reference(token)
2246
+ advance # Opening parenthesis
2247
+ reference = { type: :function_reference, line: token[:line], column: token[:column] }
2248
+ case current_token && current_token[:type]
2249
+ when :brace_open
2250
+ reference[:function_body] = parse_vim_lambda(token[:line], token[:column])
2251
+ when :option_variable
2252
+ reference[:function_body] = advance[:value]
2253
+ when nil, :paren_close, :comma
2254
+ add_error("Expected function name in function()")
2255
+ else
2256
+ name = parse_expression
2257
+ reference[:function_name] = if name && name[:token_type] == :string
2258
+ name[:value]
2259
+ else
2260
+ name
2261
+ end
2262
+ reference[:arguments] = []
2263
+ end
2264
+ while current_token && current_token[:type] == :comma
2265
+ advance
2266
+ (reference[:arguments] ||= []) << parse_expression
2267
+ end
2268
+ expect(:paren_close)
2269
+ reference
2270
+ end
2271
+
2277
2272
  def parse_builtin_function_call
2278
2273
  token = advance # Consume the `builtin_funcs` token
2279
2274
  line = token[:line]
2280
2275
  column = token[:column]
2281
2276
  name = token[:value]
2282
2277
 
2283
- #expect(:paren_open)
2284
- required_parens = 1
2285
- while current_token && required_parens > 0
2286
- if current_token[:type] == :paren_close
2287
- required_parens -= 1
2288
- elsif current_token[:type] == :paren_open
2289
- required_parens += 1
2290
- end
2278
+ if name.end_with?('(')
2279
+ consume_call_arguments
2280
+ elsif current_token && current_token[:type] == :paren_open
2291
2281
  advance
2282
+ consume_call_arguments
2292
2283
  end
2293
2284
 
2294
- # Collect arguments (if any) until the end of the line
2295
- #args = []
2296
- #while current_token && current_token[:type] != :comment && current_token[:value] != "\n"
2297
- #puts current_token[:value]
2298
- #args << current_token[:value]
2299
- #advance
2300
- #end
2301
-
2302
2285
  {
2303
- type: :builtin_function_call,
2286
+ type: name == 'filter(' ? :filter_command : :builtin_function_call,
2304
2287
  name: name,
2305
2288
  #arguments: args,
2306
2289
  line: line,
@@ -2309,62 +2292,33 @@ module Vinter
2309
2292
  end
2310
2293
 
2311
2294
  def is_lambda_expression
2312
- # Save the current position
2313
- original_position = @position
2314
-
2315
- # Skip the opening parenthesis
2316
- advance if current_token && current_token[:type] == :paren_open
2317
-
2318
- # Check for a parameter list followed by => or ): =>
2319
- has_params = false
2320
- has_arrow = false
2321
-
2322
- # Skip past parameters
2323
- loop do
2324
- break if !current_token
2325
-
2326
- if current_token[:type] == :identifier
2327
- has_params = true
2328
- advance
2329
-
2330
- # Either comma for another parameter, or close paren
2331
- if current_token && current_token[:type] == :comma
2332
- advance
2333
- elsif current_token && current_token[:type] == :paren_close
2334
- advance
2335
- break
2336
- else
2337
- # Not a valid lambda parameter list
2338
- break
2339
- end
2340
- elsif current_token[:type] == :paren_close
2341
- # Empty parameter list is valid
2342
- advance
2343
- break
2344
- else
2345
- # Not a valid lambda parameter list
2346
- break
2347
- end
2348
- end
2349
-
2350
- # After parameters, check for either => or ): =>
2351
- if current_token
2352
- if current_token[:type] == :operator && current_token[:value] == '=>'
2353
- has_arrow = true
2354
- elsif current_token[:type] == :colon
2355
- # There might be a return type annotation
2356
- advance
2357
- # Skip the type
2358
- advance if current_token && [:identifier, :keyword].include?(current_token[:type])
2359
- # Check for the arrow
2360
- has_arrow = current_token && current_token[:type] == :operator && current_token[:value] == '=>'
2361
- end
2362
- end
2363
-
2364
- # Reset position to where we started
2365
- @position = original_position
2366
-
2367
- return has_params && has_arrow
2295
+ # Inspect the signature without changing the expression parser's position.
2296
+ index = @position + 1
2297
+ depth = 1
2298
+ while (token = @tokens[index])
2299
+ depth += 1 if token[:type] == :paren_open || (token[:type] == :builtin_funcs && token[:value].end_with?('('))
2300
+ depth -= 1 if token[:type] == :paren_close
2301
+ index += 1
2302
+ break if depth.zero?
2303
+ end
2304
+ return false unless depth.zero?
2305
+ return true if @tokens[index]&.dig(:value) == '=>'
2306
+ return false unless @tokens[index]&.dig(:type) == :colon
2307
+
2308
+ # A typed lambda has a return type between the closing paren and arrow.
2309
+ index += 1
2310
+ return false unless [:identifier, :type].include?(@tokens[index]&.dig(:type))
2311
+ index += 1
2312
+ if @tokens[index]&.dig(:value) == '<'
2313
+ nesting = 0
2314
+ while (part = @tokens[index])
2315
+ nesting += 1 if part[:value] == '<'
2316
+ nesting -= 1 if part[:value] == '>'
2317
+ index += 1
2318
+ break if nesting.zero?
2319
+ end
2320
+ end
2321
+ @tokens[index]&.dig(:value) == '=>'
2368
2322
  end
2369
2323
 
2370
2324
  def parse_vim_lambda(line, column)
@@ -2490,13 +2444,18 @@ module Vinter
2490
2444
  param_type = parse_type
2491
2445
  end
2492
2446
 
2493
- params << {
2447
+ param = {
2494
2448
  type: :parameter,
2495
2449
  name: param_name,
2496
2450
  param_type: param_type,
2497
2451
  line: param_token[:line],
2498
2452
  column: param_token[:column]
2499
2453
  }
2454
+ if current_token&.dig(:type) == :operator && current_token[:value] == '='
2455
+ advance
2456
+ param[:default] = parse_expression
2457
+ end
2458
+ params << param
2500
2459
  else
2501
2460
  @errors << {
2502
2461
  message: "Expected parameter name in lambda expression",
@@ -2627,7 +2586,10 @@ module Vinter
2627
2586
 
2628
2587
  # Parse key (string or identifier)
2629
2588
  key = nil
2630
- if current_token && [:string, :identifier, :keyword].include?(current_token[:type])
2589
+ inline_colon = current_token[:type] == :namespace_prefix
2590
+ if inline_colon
2591
+ key = advance[:value].delete_suffix(':')
2592
+ elsif [:string, :identifier, :keyword, :type].include?(current_token[:type])
2631
2593
  key = current_token[:value]
2632
2594
  advance # Skip key
2633
2595
  else
@@ -2654,7 +2616,7 @@ module Vinter
2654
2616
  # Expect colon
2655
2617
  if current_token && current_token[:type] == :colon
2656
2618
  advance # Skip colon
2657
- else
2619
+ elsif !inline_colon
2658
2620
  @errors << {
2659
2621
  message: "Expected colon after dictionary key",
2660
2622
  position: @position,
@@ -2757,7 +2719,7 @@ module Vinter
2757
2719
  # Parse list elements
2758
2720
  while @position < @tokens.length
2759
2721
  # Handle line continuation characters (backslash)
2760
- if current_token && [:backslash, :line_continuation].include?(current_token[:type])
2722
+ if current_token && [:backslash, :line_continuation, :comment].include?(current_token[:type])
2761
2723
  # Skip the backslash token
2762
2724
  advance
2763
2725
 
@@ -2832,6 +2794,18 @@ module Vinter
2832
2794
  end
2833
2795
 
2834
2796
  def parse_unary_expression
2797
+ if current_token&.dig(:value) == '<' && [:type, :identifier].include?(peek_token&.dig(:type))
2798
+ opening = advance
2799
+ cast_type = parse_type
2800
+ if current_token&.dig(:value) == '>'
2801
+ advance
2802
+ else
2803
+ add_error("Expected > after cast type", opening)
2804
+ end
2805
+ return { type: :cast_expression, cast_type: cast_type, operand: parse_unary_expression,
2806
+ line: opening[:line], column: opening[:column] }
2807
+ end
2808
+
2835
2809
  # Check for unary operators (!, -, +)
2836
2810
  if current_token && current_token[:type] == :operator &&
2837
2811
  ['!', '-', '+'].include?(current_token[:value])
@@ -2852,27 +2826,27 @@ module Vinter
2852
2826
  return parse_primary_expression
2853
2827
  end
2854
2828
 
2829
+ def consume_call_arguments
2830
+ depth = 1
2831
+ while current_token && depth.positive?
2832
+ token = advance
2833
+ if token[:type] == :paren_open || (token[:type] == :builtin_funcs && token[:value].end_with?('('))
2834
+ depth += 1
2835
+ elsif token[:type] == :paren_close
2836
+ depth -= 1
2837
+ end
2838
+ end
2839
+ end
2840
+
2855
2841
  def parse_function_call(name, line, column)
2856
2842
  expect(:paren_open)
2857
2843
 
2858
- args = []
2859
-
2860
- # Parse arguments until we find a closing parenthesis
2861
-
2862
- required_parens = 1
2863
- while current_token && required_parens > 0
2864
- if current_token[:type] == :paren_open
2865
- required_parens += 1
2866
- elsif current_token[:type] == :paren_close
2867
- required_parens -= 1
2868
- end
2869
- advance
2870
- end
2844
+ consume_call_arguments
2871
2845
 
2872
2846
  {
2873
2847
  type: :function_call,
2874
2848
  name: name,
2875
- arguments: args,
2849
+ arguments: [],
2876
2850
  line: line,
2877
2851
  column: column
2878
2852
  }
@@ -2931,44 +2905,20 @@ module Vinter
2931
2905
  line = token[:line]
2932
2906
  column = token[:column]
2933
2907
 
2934
- # Handle 'import autoload'
2935
- is_autoload = false
2936
- module_name = nil
2908
+ is_autoload = current_token&.dig(:value) == 'autoload'
2909
+ module_name = advance[:value] if is_autoload
2937
2910
  path = nil
2938
2911
 
2939
- if current_token && current_token[:type] == :identifier && current_token[:value] == 'autoload'
2940
- is_autoload = true
2941
- module_name = advance[:value] # Store "autoload" as the module name
2942
-
2943
- # After "autoload" keyword, expect a string path
2944
- if current_token && current_token[:type] == :string
2945
- path = current_token[:value]
2946
- advance
2947
- else
2948
- @errors << {
2949
- message: "Expected string path after 'autoload'",
2950
- position: @position,
2951
- line: current_token ? current_token[:line] : 0,
2952
- column: current_token ? current_token[:column] : 0
2953
- }
2954
- end
2912
+ if !current_token || current_token[:line] != line ||
2913
+ current_token[:type] == :comment || current_token[:value] == 'as'
2914
+ add_error("Expected import path expression", token)
2955
2915
  else
2956
- # Regular import with a string path
2957
- if current_token && current_token[:type] == :string
2958
- path = current_token[:value]
2959
-
2960
- # Extract module name from the path
2961
- # This is simplified logic - you might need more complex extraction
2962
- module_name = path.gsub(/['"]/, '').split('/').last.split('.').first
2963
-
2964
- advance
2916
+ expression = parse_expression
2917
+ if expression && expression[:token_type] == :string
2918
+ path = expression[:value]
2919
+ module_name ||= File.basename(path[1...-1]).split('.').first
2965
2920
  else
2966
- # Handle other import formats
2967
- module_expr = parse_expression()
2968
- if module_expr && module_expr[:type] == :literal && module_expr[:token_type] == :string
2969
- path = module_expr[:value]
2970
- module_name = path.gsub(/['"]/, '').split('/').last.split('.').first
2971
- end
2921
+ path = expression
2972
2922
  end
2973
2923
  end
2974
2924
 
@@ -3026,6 +2976,8 @@ module Vinter
3026
2976
 
3027
2977
  if current_token[:type] == :keyword || current_token[:type] == :identifier
3028
2978
  case current_token[:value]
2979
+ when 'type'
2980
+ exported_item = parse_type_alias
3029
2981
  when 'def'
3030
2982
  exported_item = parse_def_function
3031
2983
  when 'function'
@@ -3133,18 +3085,22 @@ module Vinter
3133
3085
  attribute = attributes.pop # Take the last attribute we added
3134
3086
  advance # Skip the '='
3135
3087
 
3136
- # Get the value (number or identifier)
3137
- if current_token && (current_token[:type] == :number || current_token[:type] == :identifier) || current_token[:type] == :operator
3138
- attribute += "=#{current_token[:value]}"
3088
+ if current_token && current_token[:line] == line
3089
+ value = advance
3090
+ attribute += "=#{value[:value]}"
3091
+ while current_token && current_token[:line] == line &&
3092
+ current_token[:column] == value[:column] + value[:value].length
3093
+ value = advance
3094
+ attribute += value[:value]
3095
+ end
3139
3096
  attributes << attribute
3140
- advance
3141
3097
  end
3142
3098
  end
3143
3099
  end
3144
3100
 
3145
3101
  # Parse the command name
3146
3102
  command_name = nil
3147
- advance if current_token[:type] == :line_continuation
3103
+ advance if current_token && [:line_continuation, :backslash].include?(current_token[:type])
3148
3104
  if current_token && current_token[:type] == :identifier
3149
3105
  command_name = current_token[:value]
3150
3106
  advance
@@ -3159,16 +3115,26 @@ module Vinter
3159
3115
 
3160
3116
  # Parse the command implementation - collect all remaining tokens as raw parts
3161
3117
  implementation_parts = []
3162
- while @position < @tokens.length
3163
- # Break on end of line or comment
3164
- if !current_token || current_token[:type] == :comment ||
3165
- (current_token[:value] == "\n" && !current_token[:value].start_with?("\\"))
3166
- break
3118
+ body_line = current_token ? @tokens[@position - 1][:line] : line
3119
+ block_body = current_token&.dig(:type) == :brace_open && current_token[:line] == body_line
3120
+ depth = 0
3121
+ while current_token
3122
+ if !block_body
3123
+ break if current_token[:type] == :comment
3124
+ if current_token[:line] != body_line
3125
+ break unless [:line_continuation, :backslash].include?(current_token[:type])
3126
+ body_line = current_token[:line]
3127
+ end
3128
+ end
3129
+ part = advance
3130
+ implementation_parts << part
3131
+ if block_body
3132
+ depth += 1 if part[:type] == :brace_open
3133
+ depth -= 1 if part[:type] == :brace_close
3134
+ break if depth.zero?
3167
3135
  end
3168
-
3169
- implementation_parts << current_token
3170
- advance
3171
3136
  end
3137
+ add_error("Expected } after command body", token) if block_body && depth.positive?
3172
3138
 
3173
3139
  {
3174
3140
  type: :command_definition,
@@ -3193,37 +3159,13 @@ module Vinter
3193
3159
  advance # Skip '!'
3194
3160
  end
3195
3161
 
3196
- # For script-local functions or other scoped functions
3197
- is_script_local = false
3198
- function_scope = nil
3199
-
3200
- # Check if we have a script-local function (s:)
3201
- if current_token && current_token[:type] == :script_local
3202
- is_script_local = true
3203
- function_scope = current_token[:value]
3204
- advance # Skip s: prefix
3205
- elsif current_token && current_token[:type] == :identifier && current_token[:value].include?(':')
3206
- # Handle other scoped functions like g: or b:
3207
- parts = current_token[:value].split(':')
3208
- if parts.length == 2
3209
- function_scope = parts[0] + ':'
3210
- advance
3211
- end
3212
- end
3213
-
3214
- # Now handle the function name, which might be separate from the scope
3215
- name = nil
3216
- if !is_script_local && function_scope.nil? && current_token && current_token[:type] == :identifier
3217
- name = current_token
3218
- advance
3219
- elsif is_script_local || !function_scope.nil?
3220
- if current_token && current_token[:type] == :identifier
3221
- name = current_token
3222
- advance
3223
- end
3224
- else
3225
- name = expect(:identifier)
3226
- end
3162
+ name = if [:global_variable, :script_local].include?(current_token&.dig(:type))
3163
+ advance
3164
+ else
3165
+ expect(:identifier)
3166
+ end
3167
+ is_script_local = name&.dig(:type) == :script_local
3168
+ function_scope = name[:value][0, 2] if name && [:global_variable, :script_local].include?(name[:type])
3227
3169
 
3228
3170
  # Parse parameter list
3229
3171
  expect(:paren_open)
@@ -3235,7 +3177,7 @@ module Vinter
3235
3177
  while current_token
3236
3178
  if current_token[:type] == :keyword && current_token[:value] == 'abort'
3237
3179
  attributes << advance[:value]
3238
- elsif current_token[:type] == :identifier &&
3180
+ elsif [:identifier, :type].include?(current_token[:type]) &&
3239
3181
  ['range', 'dict', 'closure'].include?(current_token[:value])
3240
3182
  attributes << advance[:value]
3241
3183
  else
@@ -3250,9 +3192,6 @@ module Vinter
3250
3192
  expect_end_keyword('endfunction', 'endfunc')
3251
3193
 
3252
3194
  function_name = name ? name[:value] : nil
3253
- if function_scope
3254
- function_name = function_scope + function_name if function_name
3255
- end
3256
3195
 
3257
3196
  {
3258
3197
  type: :legacy_function,
@@ -3319,7 +3258,7 @@ module Vinter
3319
3258
 
3320
3259
  # Regular parameter parsing (existing logic)
3321
3260
  loop do
3322
- if current_token && current_token[:type] == :identifier
3261
+ if current_token && [:identifier, :type].include?(current_token[:type])
3323
3262
  param_name = advance
3324
3263
 
3325
3264
  # Check for default value
@@ -3451,14 +3390,7 @@ module Vinter
3451
3390
  pattern = ''
3452
3391
  pattern_tokens = []
3453
3392
 
3454
- # Collect all tokens until we hit a newline or a statement
3455
- while @position < @tokens.length
3456
- if !current_token ||
3457
- (current_token[:type] == :whitespace && current_token[:value].include?("\n")) ||
3458
- current_token[:type] == :comment
3459
- break
3460
- end
3461
-
3393
+ while current_token && current_token[:line] == catch_line && current_token[:type] != :comment && current_token[:value] != '|'
3462
3394
  pattern_tokens << current_token
3463
3395
  pattern += current_token[:value]
3464
3396
  advance
@@ -3531,6 +3463,10 @@ module Vinter
3531
3463
  line = token[:line]
3532
3464
  column = token[:column]
3533
3465
 
3466
+ if current_token && current_token[:type] == :paren_open && current_token[:column] == column + token[:value].length
3467
+ return parse_function_call('call', line, column)
3468
+ end
3469
+
3534
3470
  # Parse the function call expression that follows 'call'
3535
3471
  func_expr = nil
3536
3472
 
@@ -3815,13 +3751,27 @@ module Vinter
3815
3751
  end
3816
3752
 
3817
3753
  def parse_set_command
3754
+ if peek_token && [:line_continuation, :backslash].include?(peek_token[:type]) &&
3755
+ peek_token[:line] != current_token[:line]
3756
+ return parse_command_line_call
3757
+ end
3818
3758
  token = advance # Skip 'set'
3819
3759
  line = token[:line]
3820
3760
  column = token[:column]
3821
3761
 
3762
+ first = parse_set_option(line, column)
3763
+ options = [first.dup]
3764
+ while current_token && current_token[:line] == line && [:identifier, :type].include?(current_token[:type])
3765
+ options << parse_set_option(line, current_token[:column])
3766
+ end
3767
+ first[:options] = options if options.size > 1
3768
+ first
3769
+ end
3770
+
3771
+ def parse_set_option(line, column)
3822
3772
  # Parse option name
3823
3773
  option_name = nil
3824
- if current_token && current_token[:type] == :identifier
3774
+ if current_token && [:identifier, :type].include?(current_token[:type])
3825
3775
  option_name = current_token[:value]
3826
3776
  advance
3827
3777
  elsif current_token && current_token[:type] == :option_variable
@@ -3859,7 +3809,11 @@ module Vinter
3859
3809
  advance # Skip '='
3860
3810
 
3861
3811
  # Special handling for foldmarker and similar options that use brace notation
3862
- if option_name == 'foldmarker' && current_token && current_token[:type] == :brace_open
3812
+ if !current_token || current_token[:line] != line || current_token[:value] == '|'
3813
+ value = ''
3814
+ elsif current_token[:type] == :keyword
3815
+ value = advance[:value]
3816
+ elsif option_name == 'foldmarker' && current_token && current_token[:type] == :brace_open
3863
3817
  # Parse as special brace sequence value (e.g., {{{,}}})
3864
3818
  value = parse_brace_sequence_value
3865
3819
  # Special handling for comma-separated option values
@@ -3922,28 +3876,33 @@ module Vinter
3922
3876
 
3923
3877
  # Parse syntax subcommand (match, region, keyword, etc.)
3924
3878
  subcommand = nil
3925
- if current_token && current_token[:type] == :identifier
3879
+ if current_token && current_token[:line] == line && current_token[:type] == :identifier
3926
3880
  subcommand = current_token[:value]
3927
3881
  advance
3928
3882
  end
3929
3883
 
3930
3884
  # Parse syntax group name
3931
3885
  group_name = nil
3932
- if current_token && current_token[:type] == :identifier
3886
+ if current_token && current_token[:line] == line && current_token[:type] == :identifier
3933
3887
  group_name = current_token[:value]
3934
3888
  advance
3935
3889
  end
3936
3890
 
3937
3891
  # Parse pattern (regex or other pattern)
3938
3892
  pattern = nil
3939
- if current_token && current_token[:type] == :regex
3893
+ if current_token && current_token[:line] == line && current_token[:type] == :regex
3940
3894
  pattern = current_token[:value]
3941
3895
  advance
3942
3896
  end
3943
3897
 
3944
3898
  # Parse any remaining arguments as raw text until end of line
3945
3899
  args = []
3946
- while current_token && current_token[:type] != :comment
3900
+ argument_line = line
3901
+ while current_token && current_token[:type] != :comment && current_token[:value] != '|'
3902
+ if current_token[:line] != argument_line
3903
+ break unless [:line_continuation, :backslash].include?(current_token[:type])
3904
+ argument_line = current_token[:line]
3905
+ end
3947
3906
  args << current_token[:value]
3948
3907
  advance
3949
3908
  end
@@ -3964,16 +3923,19 @@ module Vinter
3964
3923
  line = token[:line]
3965
3924
  column = token[:column]
3966
3925
 
3926
+ advance if current_token && current_token[:line] == line && %w[def default].include?(current_token[:value])
3927
+ advance if current_token && current_token[:line] == line && current_token[:value] == 'link'
3928
+
3967
3929
  # Parse highlight group name
3968
3930
  group_name = nil
3969
- if current_token && current_token[:type] == :identifier
3931
+ if current_token && current_token[:line] == line && current_token[:type] == :identifier
3970
3932
  group_name = current_token[:value]
3971
3933
  advance
3972
3934
  end
3973
3935
 
3974
3936
  # Parse highlight attributes as key=value pairs
3975
3937
  attributes = {}
3976
- while current_token && current_token[:type] != :comment
3938
+ while current_token && current_token[:line] == line && current_token[:type] != :comment && current_token[:value] != '|'
3977
3939
  # Parse attribute name
3978
3940
  if current_token && current_token[:type] == :identifier
3979
3941
  attr_name = current_token[:value]