vinter 0.7.0 → 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
@@ -413,8 +458,8 @@ module Vinter
413
458
  end
414
459
  elsif current_token[:type] == :comment
415
460
  parse_comment
416
- #elsif current_token[:type] == :string && current_token[:value].start_with?('"')
417
- #parse_comment
461
+ elsif current_token[:type] == :string && current_token[:value].start_with?('"')
462
+ parse_comment
418
463
  elsif current_token[:type] == :silent_bang
419
464
  parse_silent_command
420
465
  elsif current_token[:type] == :identifier && current_token[:value] == 'delete'
@@ -441,6 +486,8 @@ module Vinter
441
486
  parse_command_line_call
442
487
  elsif current_token[:type] == :number
443
488
  advance
489
+ elsif [:paren_open, :bracket_open].include?(current_token[:type])
490
+ parse_expression_statement
444
491
  elsif current_token[:type] == :paren_close
445
492
  advance
446
493
  elsif current_token[:type] == :brace_close
@@ -457,7 +504,7 @@ module Vinter
457
504
  while current_token && current_token[:value] != stop_token
458
505
  advance
459
506
  end
460
- elsif [:bracket_open, :bracket_close, :operator, :question_mark].include?(current_token[:type])
507
+ elsif [:bracket_close, :operator, :question_mark].include?(current_token[:type])
461
508
  advance
462
509
  elsif current_token[:type] == :mode_command
463
510
  advance
@@ -481,7 +528,8 @@ module Vinter
481
528
  column = token[:column]
482
529
 
483
530
  # Extract the command or range
484
- 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
485
533
 
486
534
  # Check if the command is a range (e.g., `:2`, `:$`)
487
535
  if command.match?(/^\d+$/) || command == '$'
@@ -495,9 +543,19 @@ module Vinter
495
543
 
496
544
  # Otherwise, it's a regular command (e.g., `:exe`)
497
545
  args = []
498
- while current_token && current_token[:type] != :newline
499
- args << current_token[:value]
500
- 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]
501
559
  end
502
560
 
503
561
  {
@@ -602,35 +660,28 @@ module Vinter
602
660
  column: column
603
661
  }
604
662
 
605
- # Property access (dot notation)
606
- while current_token && current_token[:type] == :operator && current_token[:value] == '.'
607
- dot_token = advance
608
- if current_token && (current_token[:type] == :identifier || current_token[:type] == :keyword)
609
- property_token = advance
610
- target = {
611
- type: :property_access,
612
- object: target,
613
- property: property_token[:value],
614
- line: dot_token[:line],
615
- column: dot_token[:column]
616
- }
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] }
617
676
  else
618
- add_error("Expected property name after '.' in global variable")
677
+ break
619
678
  end
620
679
  end
621
680
 
622
- # Indexed access (e.g., g:foo[bar])
623
- while current_token && current_token[:type] == :bracket_open
624
- bracket_token = advance
625
- index_expr = parse_expression
626
- expect(:bracket_close)
627
- target = {
628
- type: :indexed_access,
629
- object: target,
630
- index: index_expr,
631
- line: bracket_token[:line],
632
- column: bracket_token[:column]
633
- }
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 }
634
685
  end
635
686
 
636
687
  # Assignment operator (= or compound)
@@ -941,44 +992,8 @@ module Vinter
941
992
  add_error("Expected assignment operator after variable in let statement")
942
993
  end
943
994
 
944
- # Parse the value expression
945
- value = nil
946
-
947
- # Special handling for function() references in let statements
948
- if current_token && current_token[:type] == :keyword && current_token[:value] == 'function'
949
- advance # Skip 'function'
950
-
951
- # Expect opening parenthesis
952
- if current_token && current_token[:type] == :paren_open
953
- paren_token = advance # Skip '('
954
-
955
- # Parse the function name as a string
956
- func_name = nil
957
- if current_token && current_token[:type] == :string
958
- func_name = current_token[:value]
959
- advance
960
- else
961
- add_error("Expected string with function name in function() call")
962
- end
963
-
964
- # Expect closing parenthesis
965
- expect(:paren_close) # Skip ')'
995
+ value = parse_expression
966
996
 
967
- value = {
968
- type: :function_reference,
969
- function_name: func_name,
970
- line: line,
971
- column: column
972
- }
973
- else
974
- # If not followed by parenthesis, parse as a normal expression
975
- @position -= 1 # Go back to the 'function' keyword
976
- value = parse_expression
977
- end
978
- else
979
- # Normal expression parsing
980
- value = parse_expression
981
- end
982
997
  {
983
998
  type: :let_statement,
984
999
  target: target,
@@ -994,6 +1009,7 @@ module Vinter
994
1009
  line = token[:line]
995
1010
  column = token[:column]
996
1011
 
1012
+ advance if current_token && current_token[:value] == '!'
997
1013
  # Parse the variable to unlet
998
1014
  target = nil
999
1015
  if current_token
@@ -1275,6 +1291,10 @@ module Vinter
1275
1291
  current_token[:type] == :script_local ||
1276
1292
  current_token[:type] == :builtin_funcs)
1277
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
1278
1298
  else
1279
1299
  add_error("Expected identifier as for loop variable")
1280
1300
  end
@@ -1317,7 +1337,7 @@ module Vinter
1317
1337
  advance
1318
1338
  end
1319
1339
 
1320
- if current_token && [:identifier, :global_variable].include?(current_token[:type])
1340
+ if current_token && [:identifier, :global_variable, :type].include?(current_token[:type])
1321
1341
  name = advance
1322
1342
  end
1323
1343
  #name = expect(:identifier)
@@ -1332,12 +1352,32 @@ module Vinter
1332
1352
  advance # Skip ':'
1333
1353
  return_type = parse_type
1334
1354
  end
1335
- body = []
1336
- while current_token && current_token[:value] != 'enddef'
1337
- stmt = parse_statement
1338
- 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)
1339
1380
  end
1340
- advance
1341
1381
 
1342
1382
  {
1343
1383
  type: :def_function,
@@ -1353,111 +1393,99 @@ module Vinter
1353
1393
  def parse_parameter_list
1354
1394
  params = []
1355
1395
 
1356
- # Empty parameter list
1357
- if current_token && current_token[:type] == :paren_close
1358
- advance
1359
- 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
1360
1416
  end
1361
- # TODO actually save params
1362
- required_parens = 1
1363
- while current_token && required_parens > 0
1364
- if current_token[:type] == :paren_open
1365
- required_parens += 1
1366
- elsif current_token[:type] == :paren_close
1367
- required_parens -= 1
1368
- end
1369
- advance
1370
- end
1371
-
1372
- # Parse parameters until we find a closing parenthesis
1373
- #while @position < @tokens.length && current_token && current_token[:type] != :paren_close
1374
- ## Check for variable args
1375
- #if current_token && current_token[:type] == :ellipsis
1376
- #ellipsis_token = advance
1377
-
1378
- ## Parse type for variable args if present
1379
- #param_type = nil
1380
- #if current_token && current_token[:type] == :colon
1381
- #advance # Skip ':'
1382
- #param_type = parse_type
1383
- #end
1384
-
1385
- #params << {
1386
- #type: :var_args,
1387
- #param_type: param_type,
1388
- #line: ellipsis_token[:line],
1389
- #column: ellipsis_token[:column]
1390
- #}
1391
-
1392
- ## After varargs, we expect closing paren
1393
- #if current_token && current_token[:type] != :paren_close
1394
- #add_error("Expected closing parenthesis after varargs", current_token)
1395
- #end
1396
-
1397
- #break
1398
- #end
1399
-
1400
- #param_name = advance
1401
-
1402
- ## Check for type annotation
1403
- #param_type = nil
1404
- #if current_token && current_token[:type] == :colon
1405
- #advance # Skip ':'
1406
- #param_type = parse_type
1407
- #end
1408
-
1409
- ## Check for default value
1410
- #default_value = nil
1411
- #if current_token && current_token[:type] == :operator && current_token[:value] == '='
1412
- #advance # Skip '='
1413
- #default_value = parse_expression
1414
- #end
1415
-
1416
- #params << {
1417
- #type: :parameter,
1418
- #name: param_name[:value],
1419
- #param_type: param_type,
1420
- #optional: default_value != nil,
1421
- #default_value: default_value,
1422
- #line: param_name[:line],
1423
- #column: param_name[:column]
1424
- #}
1425
-
1426
- ## If we have a comma, advance past it and continue
1427
- #if current_token && current_token[:type] == :comma
1428
- #advance
1429
- ## If we don't have a comma, we should have a closing paren
1430
- #elsif current_token && current_token[:type] != :paren_close
1431
- #add_error("Expected comma or closing parenthesis after parameter", current_token)
1432
- #break
1433
- #end
1434
- #end
1435
-
1417
+ expect(:paren_close)
1436
1418
  params
1437
1419
  end
1438
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
+
1439
1433
  def parse_type
1434
+ if current_token&.dig(:type) == :ellipsis
1435
+ advance
1436
+ return { type: :variadic_type, element_type: parse_type }
1437
+ end
1440
1438
  if current_token && [:identifier, :keyword, :type].include?(current_token[:type])
1441
1439
  type_name = advance
1442
1440
 
1441
+ if current_token&.dig(:value) == '.' && peek_token&.dig(:type) == :identifier
1442
+ advance
1443
+ return "#{type_name[:value]}.#{advance[:value]}"
1444
+ end
1445
+
1443
1446
  # Handle generic types like list<string>
1444
1447
  if current_token && current_token[:type] == :operator && current_token[:value] == '<'
1445
1448
  advance # Skip '<'
1446
1449
  inner_type = parse_type
1447
- 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
1448
1460
 
1449
1461
  return {
1450
1462
  type: :generic_type,
1451
1463
  base_type: type_name[:value],
1452
- inner_type: inner_type,
1464
+ inner_type: type_name[:value] == 'tuple' ? types : inner_type,
1453
1465
  line: type_name[:line],
1454
1466
  column: type_name[:column]
1455
1467
  }
1456
1468
  end
1457
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
+
1458
1486
  return type_name[:value]
1459
1487
  else
1460
- add_error("Expected type identifier: #{current_token[:value]}, #{current_token[:type]}")
1488
+ add_error("Expected type identifier")
1461
1489
  advance
1462
1490
  return "unknown"
1463
1491
  end
@@ -1496,9 +1524,25 @@ module Vinter
1496
1524
  initializer = nil
1497
1525
  if current_token && (current_token[:type] == :operator && current_token[:value] == '=')
1498
1526
  advance # Skip '='
1499
- assignment_line = current_token[:line]
1500
- while current_token[:line] == assignment_line
1501
- 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])
1502
1546
  end
1503
1547
  end
1504
1548
 
@@ -1522,6 +1566,7 @@ module Vinter
1522
1566
  # Check if we've reached the end of the file, end of line, or a semicolon
1523
1567
  if @position < @tokens.length &&
1524
1568
  current_token &&
1569
+ current_token[:line] == line && current_token[:value] != '|' &&
1525
1570
  current_token[:type] != :semicolon &&
1526
1571
  !(current_token[:type] == :keyword &&
1527
1572
  ['endif', 'endwhile', 'endfor', 'endfunction', 'endfunc'].include?(current_token[:value]))
@@ -1612,9 +1657,9 @@ module Vinter
1612
1657
  expr = parse_binary_expression
1613
1658
 
1614
1659
  # Check if this is a ternary expression
1615
- 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] == '?'))
1616
1661
  question_token = advance # Skip '?'
1617
- while current_token[:value] != ':'
1662
+ while current_token && current_token[:value] != ':'
1618
1663
  advance
1619
1664
  end
1620
1665
  advance # skip :
@@ -1629,9 +1674,16 @@ module Vinter
1629
1674
  left = parse_unary_expression
1630
1675
 
1631
1676
  # Handle multi-line expressions where operators may appear at line beginnings
1632
- while current_token &&
1633
- (current_token[:type] == :operator ||
1634
- (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] == '|'
1635
1687
 
1636
1688
  # Skip any whitespace before the operator if it's at the beginning of a line
1637
1689
  if current_token[:type] == :whitespace
@@ -1727,7 +1779,7 @@ module Vinter
1727
1779
  2
1728
1780
  when '&&' # Logical AND
1729
1781
  3
1730
- when '==', '!=', '>', '<', '>=', '<=' # Comparison
1782
+ when /\A(?:==|!=|>=?|<=?|=~|!~)[#?]?\z/ # Comparison
1731
1783
  4
1732
1784
  when '+', '-' # Addition, subtraction
1733
1785
  5
@@ -1782,80 +1834,18 @@ module Vinter
1782
1834
  # This is the type() function call
1783
1835
  return parse_builtin_function_call
1784
1836
  elsif token[:value] == 'function'
1785
- # Check if this is a function() call or just 'function' as property name
1786
- if peek_token && peek_token[:type] == :paren_open
1787
- advance # Skip 'function'
1788
- # This is a function() call
1789
- if current_token && current_token[:type] == :paren_open
1790
- # This is a function reference call
1791
- paren_token = advance # Skip '('
1792
-
1793
- # Parse the function name as a string or arrow function
1794
- if current_token && current_token[:type] == :string
1795
- func_name = current_token[:value]
1796
- advance
1797
-
1798
- # Check for additional arguments (function() can take multiple arguments)
1799
- args = []
1800
- while current_token && current_token[:type] == :comma
1801
- advance # Skip comma
1802
- arg = parse_expression
1803
- args << arg if arg
1804
- end
1805
-
1806
- # Expect closing parenthesis
1807
- expect(:paren_close) # Skip ')'
1808
-
1809
- return {
1810
- type: :function_reference,
1811
- function_name: func_name,
1812
- arguments: args,
1813
- line: line,
1814
- column: column
1815
- }
1816
- elsif current_token && current_token[:type] == :brace_open
1817
- # This is an arrow function inside function()
1818
- arrow_function = parse_vim_lambda(line, column)
1819
-
1820
- # Expect closing parenthesis
1821
- expect(:paren_close) # Skip ')'
1822
-
1823
- return {
1824
- type: :function_reference,
1825
- function_body: arrow_function,
1826
- line: line,
1827
- column: column
1828
- }
1829
- elsif current_token && current_token[:type] == :option_variable
1830
- option_var = advance
1831
- expect(:paren_close)
1832
- return {
1833
- type: :function_reference,
1834
- function_body: option_var[:value],
1835
- line: line,
1836
- column: column
1837
- }
1838
- else
1839
- @errors << {
1840
- message: "Expected string or arrow function definition in function() call: #{current_token}",
1841
- position: @position,
1842
- line: current_token ? current_token[:line] : 0,
1843
- column: current_token ? current_token[:column] : 0
1844
- }
1845
- end
1846
- end
1837
+ advance
1838
+ if current_token && current_token[:type] == :paren_open
1839
+ expr = parse_function_reference(token)
1847
1840
  else
1848
- # If not followed by parenthesis, treat 'function' as an identifier (property name)
1849
- advance # Skip 'function'
1850
- return {
1851
- type: :identifier,
1852
- name: token[:value],
1853
- line: line,
1854
- column: column
1855
- }
1841
+ expr = { type: :identifier, name: token[:value], line: line, column: column }
1856
1842
  end
1857
- elsif token[:value] == 'command'
1843
+ elsif token[:value] == 'call' && peek_token&.dig(:type) == :paren_open
1844
+ advance
1845
+ expr = parse_function_call('call', line, column)
1846
+ elsif %w[source command call].include?(token[:value])
1858
1847
  advance
1848
+ expr = { type: :identifier, name: token[:value], line: line, column: column }
1859
1849
  elsif token[:value] == 'return'
1860
1850
  advance
1861
1851
  elsif token[:value] == 'elseif'
@@ -1880,27 +1870,9 @@ module Vinter
1880
1870
  column: column
1881
1871
  }
1882
1872
  when :string
1883
- # parse_string
1884
- if token[:value].start_with?('"')
1885
- advance
1886
- return {
1887
- type: :comment,
1888
- value: token[:value],
1889
- line: line,
1890
- column: column
1891
- }
1892
- else
1893
- string_value = token[:value]
1894
- advance
1895
- expr = {
1896
- type: :literal,
1897
- value: string_value,
1898
- raw_value: string_value, # Store the raw string to preserve escapes
1899
- token_type: :string,
1900
- line: line,
1901
- column: column
1902
- }
1903
- end
1873
+ advance
1874
+ expr = { type: :literal, value: token[:value], raw_value: token[:value],
1875
+ token_type: :string, line: line, column: column }
1904
1876
  when :option_variable
1905
1877
  # Handle Vim option variables (like &compatible)
1906
1878
  advance
@@ -1942,8 +1914,7 @@ module Vinter
1942
1914
  line: line,
1943
1915
  column: column
1944
1916
  }
1945
- when :buffer_local
1946
- # Handle script-local variables/functions (like s:var)
1917
+ when :buffer_local, :window_local, :tab_local
1947
1918
  advance
1948
1919
 
1949
1920
  # Check if this is a function call
@@ -1952,16 +1923,16 @@ module Vinter
1952
1923
  end
1953
1924
 
1954
1925
  expr = {
1955
- type: :buffer_local,
1926
+ type: token[:type],
1956
1927
  name: token[:value],
1957
1928
  line: line,
1958
1929
  column: column
1959
1930
  }
1960
- when :global_variable
1931
+ when :global_variable, :environment_variable
1961
1932
  # Handle global variables (like g:var)
1962
1933
  advance
1963
1934
  expr = {
1964
- type: :global_variable,
1935
+ type: token[:type],
1965
1936
  name: token[:value],
1966
1937
  line: line,
1967
1938
  column: column
@@ -2039,19 +2010,18 @@ module Vinter
2039
2010
  if is_lambda_expression
2040
2011
  return parse_lambda_expression(line, column)
2041
2012
  else
2042
- advance # Skip '('
2043
- required_parens = 1
2044
- while current_token && required_parens > 0
2045
- if current_token[:type] == :paren_open
2046
- required_parens += 1
2047
- elsif current_token[:type] == :paren_close
2048
- 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
2049
2021
  end
2050
- advance
2022
+ expr = { type: :tuple_literal, elements: elements, line: line, column: column }
2051
2023
  end
2052
- #expr = parse_expression
2053
- #binding.pry
2054
- #expect(:paren_close) # Expect and skip ')'
2024
+ expect(:paren_close)
2055
2025
  end
2056
2026
  when :bracket_open
2057
2027
  expr = parse_list_literal(line, column)
@@ -2083,6 +2053,7 @@ module Vinter
2083
2053
  expr = parse_builtin_function_call
2084
2054
  when :type
2085
2055
  advance
2056
+ expr = { type: :identifier, name: token[:value], line: line, column: column }
2086
2057
  when :colon
2087
2058
  advance
2088
2059
  when :operator
@@ -2104,6 +2075,12 @@ module Vinter
2104
2075
  return nil
2105
2076
  end
2106
2077
 
2078
+ parse_postfix_expression(expr)
2079
+ end
2080
+
2081
+ def parse_postfix_expression(expr)
2082
+ return expr unless expr
2083
+
2107
2084
  # Now handle any chained property access or method calls
2108
2085
  while current_token
2109
2086
  # Check for property access with dot
@@ -2118,7 +2095,8 @@ module Vinter
2118
2095
  expr[:type] == :arg_variable || expr[:type] == :buffer_local ||
2119
2096
  expr[:type] == :window_local || expr[:type] == :tab_local ||
2120
2097
  expr[:type] == :local_variable || expr[:type] == :property_access ||
2121
- expr[:type] == :indexed_access)
2098
+ expr[:type] == :indexed_access || expr[:type] == :cast_expression ||
2099
+ expr[:type] == :call_expression || expr[:type] == :method_call)
2122
2100
 
2123
2101
  dot_token = advance # Skip '.'
2124
2102
  # Next token should be an identifier (property name)
@@ -2149,49 +2127,36 @@ module Vinter
2149
2127
  # Check for method call with arrow ->
2150
2128
  elsif current_token[:type] == :operator && current_token[:value] == '->'
2151
2129
  arrow_token = advance # Skip '->'
2152
- advance if current_token[:type] == :line_continuation
2130
+ advance if current_token && current_token[:type] == :line_continuation
2153
2131
 
2154
2132
  # Next token should be an identifier (method name)
2155
2133
  if current_token && current_token[:type] == :builtin_funcs
2156
- parse_builtin_function_call
2157
- elsif !current_token || current_token[:type] != :identifier
2158
- @errors << {
2159
- message: "Expected method name after '->'",
2160
- position: @position,
2161
- line: current_token ? current_token[:line] : 0,
2162
- column: current_token ? current_token[:column] : 0
2163
- }
2164
- break
2165
- end
2166
-
2167
- method_name = advance[:value] # Get method name
2168
-
2169
- # Check for arguments
2170
- args = []
2171
- if current_token && current_token[:type] == :paren_open
2172
- paren_count = 1
2173
- while current_token && paren_count > 0
2174
- if current_token[:type] == :paren_open
2175
- paren_count += 1
2176
- elsif current_token[:type] == :paren_close
2177
- paren_count -= 1
2178
- 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
2179
2139
  advance
2140
+ consume_call_arguments
2180
2141
  end
2142
+ else
2143
+ add_error("Expected method name after '->'")
2144
+ break
2181
2145
  end
2182
2146
 
2183
2147
  expr = {
2184
2148
  type: :method_call,
2185
2149
  object: expr,
2186
2150
  method: method_name,
2187
- arguments: args,
2151
+ arguments: [],
2188
2152
  line: arrow_token[:line],
2189
2153
  column: arrow_token[:column]
2190
2154
  }
2191
2155
  # Check for indexing with brackets
2192
2156
  elsif current_token[:type] == :bracket_open
2157
+ break if current_token[:line] != @tokens[@position - 1][:line]
2193
2158
  bracket_token = advance # Skip '['
2194
- index_expr = parse_expression
2159
+ index_expr = parse_expression unless current_token&.dig(:type) == :colon
2195
2160
 
2196
2161
  # Add support for list slicing with colon
2197
2162
  end_index = nil
@@ -2209,8 +2174,8 @@ module Vinter
2209
2174
  end_index = parse_expression
2210
2175
  end
2211
2176
  # handles array assignment [a,b] = split(thing)
2212
- elsif current_token[:type] == :comma
2213
- while current_token[:type] == :comma
2177
+ elsif current_token && current_token[:type] == :comma
2178
+ while current_token && current_token[:type] == :comma
2214
2179
  advance
2215
2180
  parse_expression
2216
2181
  end
@@ -2239,7 +2204,7 @@ module Vinter
2239
2204
  }
2240
2205
  end
2241
2206
  # Check for function call directly on an expression
2242
- elsif current_token[:type] == :paren_open
2207
+ elsif current_token[:type] == :paren_open && current_token[:line] == @tokens[@position - 1][:line]
2243
2208
  paren_token = advance # Skip '('
2244
2209
 
2245
2210
  args = []
@@ -2252,6 +2217,7 @@ module Vinter
2252
2217
 
2253
2218
  if current_token && current_token[:type] == :comma
2254
2219
  advance # Skip comma
2220
+ break if current_token&.dig(:type) == :paren_close
2255
2221
  else
2256
2222
  break
2257
2223
  end
@@ -2276,33 +2242,48 @@ module Vinter
2276
2242
  return expr
2277
2243
  end
2278
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
+
2279
2272
  def parse_builtin_function_call
2280
2273
  token = advance # Consume the `builtin_funcs` token
2281
2274
  line = token[:line]
2282
2275
  column = token[:column]
2283
2276
  name = token[:value]
2284
2277
 
2285
- #expect(:paren_open)
2286
- required_parens = 1
2287
- while current_token && required_parens > 0
2288
- if current_token[:type] == :paren_close
2289
- required_parens -= 1
2290
- elsif current_token[:type] == :paren_open
2291
- required_parens += 1
2292
- end
2278
+ if name.end_with?('(')
2279
+ consume_call_arguments
2280
+ elsif current_token && current_token[:type] == :paren_open
2293
2281
  advance
2282
+ consume_call_arguments
2294
2283
  end
2295
2284
 
2296
- # Collect arguments (if any) until the end of the line
2297
- #args = []
2298
- #while current_token && current_token[:type] != :comment && current_token[:value] != "\n"
2299
- #puts current_token[:value]
2300
- #args << current_token[:value]
2301
- #advance
2302
- #end
2303
-
2304
2285
  {
2305
- type: :builtin_function_call,
2286
+ type: name == 'filter(' ? :filter_command : :builtin_function_call,
2306
2287
  name: name,
2307
2288
  #arguments: args,
2308
2289
  line: line,
@@ -2311,62 +2292,33 @@ module Vinter
2311
2292
  end
2312
2293
 
2313
2294
  def is_lambda_expression
2314
- # Save the current position
2315
- original_position = @position
2316
-
2317
- # Skip the opening parenthesis
2318
- advance if current_token && current_token[:type] == :paren_open
2319
-
2320
- # Check for a parameter list followed by => or ): =>
2321
- has_params = false
2322
- has_arrow = false
2323
-
2324
- # Skip past parameters
2325
- loop do
2326
- break if !current_token
2327
-
2328
- if current_token[:type] == :identifier
2329
- has_params = true
2330
- advance
2331
-
2332
- # Either comma for another parameter, or close paren
2333
- if current_token && current_token[:type] == :comma
2334
- advance
2335
- elsif current_token && current_token[:type] == :paren_close
2336
- advance
2337
- break
2338
- else
2339
- # Not a valid lambda parameter list
2340
- break
2341
- end
2342
- elsif current_token[:type] == :paren_close
2343
- # Empty parameter list is valid
2344
- advance
2345
- break
2346
- else
2347
- # Not a valid lambda parameter list
2348
- break
2349
- end
2350
- end
2351
-
2352
- # After parameters, check for either => or ): =>
2353
- if current_token
2354
- if current_token[:type] == :operator && current_token[:value] == '=>'
2355
- has_arrow = true
2356
- elsif current_token[:type] == :colon
2357
- # There might be a return type annotation
2358
- advance
2359
- # Skip the type
2360
- advance if current_token && [:identifier, :keyword].include?(current_token[:type])
2361
- # Check for the arrow
2362
- has_arrow = current_token && current_token[:type] == :operator && current_token[:value] == '=>'
2363
- end
2364
- end
2365
-
2366
- # Reset position to where we started
2367
- @position = original_position
2368
-
2369
- 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) == '=>'
2370
2322
  end
2371
2323
 
2372
2324
  def parse_vim_lambda(line, column)
@@ -2492,13 +2444,18 @@ module Vinter
2492
2444
  param_type = parse_type
2493
2445
  end
2494
2446
 
2495
- params << {
2447
+ param = {
2496
2448
  type: :parameter,
2497
2449
  name: param_name,
2498
2450
  param_type: param_type,
2499
2451
  line: param_token[:line],
2500
2452
  column: param_token[:column]
2501
2453
  }
2454
+ if current_token&.dig(:type) == :operator && current_token[:value] == '='
2455
+ advance
2456
+ param[:default] = parse_expression
2457
+ end
2458
+ params << param
2502
2459
  else
2503
2460
  @errors << {
2504
2461
  message: "Expected parameter name in lambda expression",
@@ -2629,7 +2586,10 @@ module Vinter
2629
2586
 
2630
2587
  # Parse key (string or identifier)
2631
2588
  key = nil
2632
- 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])
2633
2593
  key = current_token[:value]
2634
2594
  advance # Skip key
2635
2595
  else
@@ -2656,7 +2616,7 @@ module Vinter
2656
2616
  # Expect colon
2657
2617
  if current_token && current_token[:type] == :colon
2658
2618
  advance # Skip colon
2659
- else
2619
+ elsif !inline_colon
2660
2620
  @errors << {
2661
2621
  message: "Expected colon after dictionary key",
2662
2622
  position: @position,
@@ -2759,7 +2719,7 @@ module Vinter
2759
2719
  # Parse list elements
2760
2720
  while @position < @tokens.length
2761
2721
  # Handle line continuation characters (backslash)
2762
- if current_token && [:backslash, :line_continuation].include?(current_token[:type])
2722
+ if current_token && [:backslash, :line_continuation, :comment].include?(current_token[:type])
2763
2723
  # Skip the backslash token
2764
2724
  advance
2765
2725
 
@@ -2834,6 +2794,18 @@ module Vinter
2834
2794
  end
2835
2795
 
2836
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
+
2837
2809
  # Check for unary operators (!, -, +)
2838
2810
  if current_token && current_token[:type] == :operator &&
2839
2811
  ['!', '-', '+'].include?(current_token[:value])
@@ -2854,27 +2826,27 @@ module Vinter
2854
2826
  return parse_primary_expression
2855
2827
  end
2856
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
+
2857
2841
  def parse_function_call(name, line, column)
2858
2842
  expect(:paren_open)
2859
2843
 
2860
- args = []
2861
-
2862
- # Parse arguments until we find a closing parenthesis
2863
-
2864
- required_parens = 1
2865
- while current_token && required_parens > 0
2866
- if current_token[:type] == :paren_open
2867
- required_parens += 1
2868
- elsif current_token[:type] == :paren_close
2869
- required_parens -= 1
2870
- end
2871
- advance
2872
- end
2844
+ consume_call_arguments
2873
2845
 
2874
2846
  {
2875
2847
  type: :function_call,
2876
2848
  name: name,
2877
- arguments: args,
2849
+ arguments: [],
2878
2850
  line: line,
2879
2851
  column: column
2880
2852
  }
@@ -2933,44 +2905,20 @@ module Vinter
2933
2905
  line = token[:line]
2934
2906
  column = token[:column]
2935
2907
 
2936
- # Handle 'import autoload'
2937
- is_autoload = false
2938
- module_name = nil
2908
+ is_autoload = current_token&.dig(:value) == 'autoload'
2909
+ module_name = advance[:value] if is_autoload
2939
2910
  path = nil
2940
2911
 
2941
- if current_token && current_token[:type] == :identifier && current_token[:value] == 'autoload'
2942
- is_autoload = true
2943
- module_name = advance[:value] # Store "autoload" as the module name
2944
-
2945
- # After "autoload" keyword, expect a string path
2946
- if current_token && current_token[:type] == :string
2947
- path = current_token[:value]
2948
- advance
2949
- else
2950
- @errors << {
2951
- message: "Expected string path after 'autoload'",
2952
- position: @position,
2953
- line: current_token ? current_token[:line] : 0,
2954
- column: current_token ? current_token[:column] : 0
2955
- }
2956
- 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)
2957
2915
  else
2958
- # Regular import with a string path
2959
- if current_token && current_token[:type] == :string
2960
- path = current_token[:value]
2961
-
2962
- # Extract module name from the path
2963
- # This is simplified logic - you might need more complex extraction
2964
- module_name = path.gsub(/['"]/, '').split('/').last.split('.').first
2965
-
2966
- 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
2967
2920
  else
2968
- # Handle other import formats
2969
- module_expr = parse_expression()
2970
- if module_expr && module_expr[:type] == :literal && module_expr[:token_type] == :string
2971
- path = module_expr[:value]
2972
- module_name = path.gsub(/['"]/, '').split('/').last.split('.').first
2973
- end
2921
+ path = expression
2974
2922
  end
2975
2923
  end
2976
2924
 
@@ -3028,6 +2976,8 @@ module Vinter
3028
2976
 
3029
2977
  if current_token[:type] == :keyword || current_token[:type] == :identifier
3030
2978
  case current_token[:value]
2979
+ when 'type'
2980
+ exported_item = parse_type_alias
3031
2981
  when 'def'
3032
2982
  exported_item = parse_def_function
3033
2983
  when 'function'
@@ -3135,18 +3085,22 @@ module Vinter
3135
3085
  attribute = attributes.pop # Take the last attribute we added
3136
3086
  advance # Skip the '='
3137
3087
 
3138
- # Get the value (number or identifier)
3139
- if current_token && (current_token[:type] == :number || current_token[:type] == :identifier) || current_token[:type] == :operator
3140
- 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
3141
3096
  attributes << attribute
3142
- advance
3143
3097
  end
3144
3098
  end
3145
3099
  end
3146
3100
 
3147
3101
  # Parse the command name
3148
3102
  command_name = nil
3149
- advance if current_token[:type] == :line_continuation
3103
+ advance if current_token && [:line_continuation, :backslash].include?(current_token[:type])
3150
3104
  if current_token && current_token[:type] == :identifier
3151
3105
  command_name = current_token[:value]
3152
3106
  advance
@@ -3161,16 +3115,26 @@ module Vinter
3161
3115
 
3162
3116
  # Parse the command implementation - collect all remaining tokens as raw parts
3163
3117
  implementation_parts = []
3164
- while @position < @tokens.length
3165
- # Break on end of line or comment
3166
- if !current_token || current_token[:type] == :comment ||
3167
- (current_token[:value] == "\n" && !current_token[:value].start_with?("\\"))
3168
- 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?
3169
3135
  end
3170
-
3171
- implementation_parts << current_token
3172
- advance
3173
3136
  end
3137
+ add_error("Expected } after command body", token) if block_body && depth.positive?
3174
3138
 
3175
3139
  {
3176
3140
  type: :command_definition,
@@ -3195,37 +3159,13 @@ module Vinter
3195
3159
  advance # Skip '!'
3196
3160
  end
3197
3161
 
3198
- # For script-local functions or other scoped functions
3199
- is_script_local = false
3200
- function_scope = nil
3201
-
3202
- # Check if we have a script-local function (s:)
3203
- if current_token && current_token[:type] == :script_local
3204
- is_script_local = true
3205
- function_scope = current_token[:value]
3206
- advance # Skip s: prefix
3207
- elsif current_token && current_token[:type] == :identifier && current_token[:value].include?(':')
3208
- # Handle other scoped functions like g: or b:
3209
- parts = current_token[:value].split(':')
3210
- if parts.length == 2
3211
- function_scope = parts[0] + ':'
3212
- advance
3213
- end
3214
- end
3215
-
3216
- # Now handle the function name, which might be separate from the scope
3217
- name = nil
3218
- if !is_script_local && function_scope.nil? && current_token && current_token[:type] == :identifier
3219
- name = current_token
3220
- advance
3221
- elsif is_script_local || !function_scope.nil?
3222
- if current_token && current_token[:type] == :identifier
3223
- name = current_token
3224
- advance
3225
- end
3226
- else
3227
- name = expect(:identifier)
3228
- 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])
3229
3169
 
3230
3170
  # Parse parameter list
3231
3171
  expect(:paren_open)
@@ -3237,7 +3177,7 @@ module Vinter
3237
3177
  while current_token
3238
3178
  if current_token[:type] == :keyword && current_token[:value] == 'abort'
3239
3179
  attributes << advance[:value]
3240
- elsif current_token[:type] == :identifier &&
3180
+ elsif [:identifier, :type].include?(current_token[:type]) &&
3241
3181
  ['range', 'dict', 'closure'].include?(current_token[:value])
3242
3182
  attributes << advance[:value]
3243
3183
  else
@@ -3252,9 +3192,6 @@ module Vinter
3252
3192
  expect_end_keyword('endfunction', 'endfunc')
3253
3193
 
3254
3194
  function_name = name ? name[:value] : nil
3255
- if function_scope
3256
- function_name = function_scope + function_name if function_name
3257
- end
3258
3195
 
3259
3196
  {
3260
3197
  type: :legacy_function,
@@ -3321,7 +3258,7 @@ module Vinter
3321
3258
 
3322
3259
  # Regular parameter parsing (existing logic)
3323
3260
  loop do
3324
- if current_token && current_token[:type] == :identifier
3261
+ if current_token && [:identifier, :type].include?(current_token[:type])
3325
3262
  param_name = advance
3326
3263
 
3327
3264
  # Check for default value
@@ -3453,14 +3390,7 @@ module Vinter
3453
3390
  pattern = ''
3454
3391
  pattern_tokens = []
3455
3392
 
3456
- # Collect all tokens until we hit a newline or a statement
3457
- while @position < @tokens.length
3458
- if !current_token ||
3459
- (current_token[:type] == :whitespace && current_token[:value].include?("\n")) ||
3460
- current_token[:type] == :comment
3461
- break
3462
- end
3463
-
3393
+ while current_token && current_token[:line] == catch_line && current_token[:type] != :comment && current_token[:value] != '|'
3464
3394
  pattern_tokens << current_token
3465
3395
  pattern += current_token[:value]
3466
3396
  advance
@@ -3533,6 +3463,10 @@ module Vinter
3533
3463
  line = token[:line]
3534
3464
  column = token[:column]
3535
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
+
3536
3470
  # Parse the function call expression that follows 'call'
3537
3471
  func_expr = nil
3538
3472
 
@@ -3817,13 +3751,27 @@ module Vinter
3817
3751
  end
3818
3752
 
3819
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
3820
3758
  token = advance # Skip 'set'
3821
3759
  line = token[:line]
3822
3760
  column = token[:column]
3823
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)
3824
3772
  # Parse option name
3825
3773
  option_name = nil
3826
- if current_token && current_token[:type] == :identifier
3774
+ if current_token && [:identifier, :type].include?(current_token[:type])
3827
3775
  option_name = current_token[:value]
3828
3776
  advance
3829
3777
  elsif current_token && current_token[:type] == :option_variable
@@ -3861,7 +3809,11 @@ module Vinter
3861
3809
  advance # Skip '='
3862
3810
 
3863
3811
  # Special handling for foldmarker and similar options that use brace notation
3864
- 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
3865
3817
  # Parse as special brace sequence value (e.g., {{{,}}})
3866
3818
  value = parse_brace_sequence_value
3867
3819
  # Special handling for comma-separated option values
@@ -3924,28 +3876,33 @@ module Vinter
3924
3876
 
3925
3877
  # Parse syntax subcommand (match, region, keyword, etc.)
3926
3878
  subcommand = nil
3927
- if current_token && current_token[:type] == :identifier
3879
+ if current_token && current_token[:line] == line && current_token[:type] == :identifier
3928
3880
  subcommand = current_token[:value]
3929
3881
  advance
3930
3882
  end
3931
3883
 
3932
3884
  # Parse syntax group name
3933
3885
  group_name = nil
3934
- if current_token && current_token[:type] == :identifier
3886
+ if current_token && current_token[:line] == line && current_token[:type] == :identifier
3935
3887
  group_name = current_token[:value]
3936
3888
  advance
3937
3889
  end
3938
3890
 
3939
3891
  # Parse pattern (regex or other pattern)
3940
3892
  pattern = nil
3941
- if current_token && current_token[:type] == :regex
3893
+ if current_token && current_token[:line] == line && current_token[:type] == :regex
3942
3894
  pattern = current_token[:value]
3943
3895
  advance
3944
3896
  end
3945
3897
 
3946
3898
  # Parse any remaining arguments as raw text until end of line
3947
3899
  args = []
3948
- 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
3949
3906
  args << current_token[:value]
3950
3907
  advance
3951
3908
  end
@@ -3966,16 +3923,19 @@ module Vinter
3966
3923
  line = token[:line]
3967
3924
  column = token[:column]
3968
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
+
3969
3929
  # Parse highlight group name
3970
3930
  group_name = nil
3971
- if current_token && current_token[:type] == :identifier
3931
+ if current_token && current_token[:line] == line && current_token[:type] == :identifier
3972
3932
  group_name = current_token[:value]
3973
3933
  advance
3974
3934
  end
3975
3935
 
3976
3936
  # Parse highlight attributes as key=value pairs
3977
3937
  attributes = {}
3978
- while current_token && current_token[:type] != :comment
3938
+ while current_token && current_token[:line] == line && current_token[:type] != :comment && current_token[:value] != '|'
3979
3939
  # Parse attribute name
3980
3940
  if current_token && current_token[:type] == :identifier
3981
3941
  attr_name = current_token[:value]