expressir 2.4.82 → 2.4.83
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.
- checksums.yaml +4 -4
- data/.github/workflows/lane-parity.yml +6 -1
- data/.github/workflows/rust-ext.yml +8 -1
- data/README.adoc +130 -1
- data/docs/_guides/formatter/formatter-architecture.adoc +169 -1
- data/docs/_guides/ruby-api/formatting-schemas.adoc +64 -0
- data/lib/expressir/express/builders/entity_decl_builder.rb +3 -0
- data/lib/expressir/express/builders/function_decl_builder.rb +1 -0
- data/lib/expressir/express/builders/procedure_decl_builder.rb +1 -0
- data/lib/expressir/express/builders/rule_decl_builder.rb +1 -0
- data/lib/expressir/express/builders/subtype_constraint_builder.rb +7 -1
- data/lib/expressir/express/formatter.rb +4 -3
- data/lib/expressir/express/formatters/data_types_formatter.rb +39 -21
- data/lib/expressir/express/formatters/declarations_formatter.rb +69 -57
- data/lib/expressir/express/formatters/remark_formatter.rb +53 -1
- data/lib/expressir/express/formatters/statements_formatter.rb +1 -0
- data/lib/expressir/express/formatters/supertype_expressions_formatter.rb +66 -1
- data/lib/expressir/express/line_map.rb +8 -0
- data/lib/expressir/express/node_position_index.rb +74 -4
- data/lib/expressir/express/pretty_formatter.rb +22 -5
- data/lib/expressir/express/remark_attacher.rb +665 -29
- data/lib/expressir/express/schema_compare.rb +2 -1
- data/lib/expressir/express/shtolo.rb +3 -0
- data/lib/expressir/model/declarations/entity.rb +5 -0
- data/lib/expressir/model/declarations/function.rb +4 -0
- data/lib/expressir/model/declarations/procedure.rb +4 -0
- data/lib/expressir/model/declarations/remark_item.rb +4 -0
- data/lib/expressir/model/declarations/rule.rb +4 -0
- data/lib/expressir/model/references/simple_reference.rb +1 -0
- data/lib/expressir/model/remark_placement.rb +6 -0
- data/lib/expressir/version.rb +1 -1
- metadata +2 -2
|
@@ -49,6 +49,7 @@ module Expressir
|
|
|
49
49
|
@node_index = nil
|
|
50
50
|
@owner_map = nil
|
|
51
51
|
@active_scope_map = nil
|
|
52
|
+
@code_lines = nil
|
|
52
53
|
end
|
|
53
54
|
|
|
54
55
|
def attach(model)
|
|
@@ -77,8 +78,11 @@ module Expressir
|
|
|
77
78
|
@line_map = nil
|
|
78
79
|
@owner_map = nil
|
|
79
80
|
@active_scope_map = nil
|
|
81
|
+
@code_lines = nil
|
|
80
82
|
@source_lines_for_where_clause = nil
|
|
81
83
|
@where_clause_line_index = nil
|
|
84
|
+
@supertype_operands = nil
|
|
85
|
+
@code_without_remarks = nil
|
|
82
86
|
end
|
|
83
87
|
|
|
84
88
|
# Extend each node's captured slice through the remarks attached
|
|
@@ -250,13 +254,27 @@ module Expressir
|
|
|
250
254
|
target = ip.remark_items&.find { |ri| ri.id == remark.tag } || ip
|
|
251
255
|
end
|
|
252
256
|
end
|
|
253
|
-
|
|
254
|
-
source_offset: remark.position)
|
|
257
|
+
add_tagged_remark(target, remark)
|
|
255
258
|
@attached_spans << remark.position
|
|
256
259
|
end
|
|
257
260
|
end
|
|
258
261
|
end
|
|
259
262
|
|
|
263
|
+
# Stores a tagged remark. A `--IPn:` comment line of the source also
|
|
264
|
+
# lists its text (empty for the bare marker) in `source_remarks` of the
|
|
265
|
+
# proposition's remark item, so the plain form can write it while a text
|
|
266
|
+
# that only a tagged description supplied is not written there.
|
|
267
|
+
def add_tagged_remark(target, remark)
|
|
268
|
+
add_remark(target, remark.text, format: remark.format, tag: remark.tag,
|
|
269
|
+
source_offset: remark.position)
|
|
270
|
+
return unless remark.format == Model::RemarkFormat::TAIL
|
|
271
|
+
return unless target.is_a?(Model::Declarations::RemarkItem)
|
|
272
|
+
return unless target.id == remark.tag && remark.tag.match?(/^IP\d+$/)
|
|
273
|
+
|
|
274
|
+
target.source_remarks ||= []
|
|
275
|
+
target.source_remarks << remark.text.to_s
|
|
276
|
+
end
|
|
277
|
+
|
|
260
278
|
# ----- Untagged remark attachment -----
|
|
261
279
|
|
|
262
280
|
def attach_untagged_remarks(remarks)
|
|
@@ -266,7 +284,10 @@ module Expressir
|
|
|
266
284
|
untagged.each do |remark|
|
|
267
285
|
next if @attached_spans.include?(remark.position)
|
|
268
286
|
|
|
269
|
-
|
|
287
|
+
# What a remark says in its own text, such as `-- before
|
|
288
|
+
# END_FUNCTION`, is not code: the END-line test reads the code of
|
|
289
|
+
# the line (`code_lines`).
|
|
290
|
+
line_content = code_lines[remark.line - 1].to_s
|
|
270
291
|
if end_scope_line?(line_content)
|
|
271
292
|
matched_node = @node_index.node_for_end_scope_at(remark.line, line_content) ||
|
|
272
293
|
scope_owner_for_end_line(remark.line, line_content)
|
|
@@ -342,6 +363,12 @@ module Expressir
|
|
|
342
363
|
# here means the remark is an inline tail (code; -- note). The
|
|
343
364
|
# end-line check is restricted to statements: container end_lines are
|
|
344
365
|
# child-derived approximations that can collide with comment lines.
|
|
366
|
+
label_block = where_label_block_target(remark)
|
|
367
|
+
return label_block if label_block.first
|
|
368
|
+
|
|
369
|
+
operand = supertype_operand_target(remark)
|
|
370
|
+
return operand if operand.first
|
|
371
|
+
|
|
345
372
|
if inline_remark?(remark)
|
|
346
373
|
# The candidate pool carries both the nodes STARTING on the line
|
|
347
374
|
# (the remark trails a statement's own text) and the nodes ENDING
|
|
@@ -373,15 +400,27 @@ module Expressir
|
|
|
373
400
|
# closes that body. Without this check the comment would instead be
|
|
374
401
|
# read as leading the next statement of an OUTER region, which is
|
|
375
402
|
# where it would wrongly render.
|
|
403
|
+
clause = block_before_clause_target(remark)
|
|
404
|
+
return clause if clause.first
|
|
405
|
+
|
|
376
406
|
closing = closing_region_target(line, own_line: own_line_remark?(remark))
|
|
377
407
|
return closing if closing.first
|
|
378
408
|
|
|
409
|
+
# An own-line comment in the declaration part of a FUNCTION,
|
|
410
|
+
# PROCEDURE or RULE leads the declaration written below it. The
|
|
411
|
+
# statement lookup below would hand it to the first statement.
|
|
412
|
+
head = head_declaration_target(remark)
|
|
413
|
+
return head if head.first
|
|
414
|
+
|
|
379
415
|
enclosing, region, = statement_region_for(line)
|
|
380
416
|
if region
|
|
381
417
|
following = region
|
|
382
418
|
.select { |n| n[:line] > line }
|
|
383
419
|
.min_by { |n| n[:position] }
|
|
384
|
-
|
|
420
|
+
if following
|
|
421
|
+
return [following[:node], Model::RemarkPlacement::LEADING,
|
|
422
|
+
before_case_label_region(following[:node], remark)]
|
|
423
|
+
end
|
|
385
424
|
|
|
386
425
|
# No following statement and no closing keyword above: the comment is
|
|
387
426
|
# not demonstrably inside this body (it may sit after the whole
|
|
@@ -395,26 +434,137 @@ module Expressir
|
|
|
395
434
|
# placement (#696). Attributes carry no source spans, so a
|
|
396
435
|
# comment inside an entity body finds nothing here and keeps
|
|
397
436
|
# the legacy attachment.
|
|
437
|
+
operand = supertype_operand_lead(remark)
|
|
438
|
+
return operand if operand
|
|
439
|
+
|
|
398
440
|
declaration = declaration_level_target(remark)
|
|
399
441
|
return declaration if declaration.first
|
|
400
442
|
|
|
401
|
-
|
|
443
|
+
schema_end_target(remark)
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# A comment on a line of its own whose next code line is END_SCHEMA
|
|
447
|
+
# closes the declarations of the schema that END_SCHEMA closes, the way
|
|
448
|
+
# a comment before END_FUNCTION closes the statements. Blank lines and
|
|
449
|
+
# further comments between the two are skipped; so is the rest of a
|
|
450
|
+
# block comment, which may name END_SCHEMA in its text.
|
|
451
|
+
def schema_end_target(remark)
|
|
452
|
+
return [nil, nil, nil] unless line_content_for(remark.line).to_s.lstrip.start_with?("--", "(*")
|
|
453
|
+
|
|
454
|
+
probe = remark.line
|
|
455
|
+
while probe <= source_line_count
|
|
456
|
+
content = line_content_for(probe).to_s.strip
|
|
457
|
+
if content.start_with?("(*")
|
|
458
|
+
probe += 1 until probe > source_line_count || line_content_for(probe).to_s.include?("*)")
|
|
459
|
+
elsif !(content.empty? || content.start_with?("--"))
|
|
460
|
+
break
|
|
461
|
+
end
|
|
462
|
+
probe += 1
|
|
463
|
+
end
|
|
464
|
+
return [nil, nil, nil] unless line_content_for(probe).to_s.strip.match?(/\AEND_SCHEMA\b/i)
|
|
465
|
+
|
|
466
|
+
schema = @node_index.containing_node_of_type(Model::Declarations::Schema, probe)
|
|
467
|
+
return [nil, nil, nil] unless schema
|
|
468
|
+
|
|
469
|
+
[schema, Model::RemarkPlacement::TRAILING, "declarations"]
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
# A leading remark of the statement of a CASE action that was written
|
|
473
|
+
# before the label of that action (between two actions, or before the
|
|
474
|
+
# first) has the region BEFORE_LABEL: the formatter writes it above the
|
|
475
|
+
# label. One written after the label has no region. The text between
|
|
476
|
+
# the remark and the statement tells them apart: only remarks and
|
|
477
|
+
# white space means the label is already behind the remark. (A label
|
|
478
|
+
# that is a reference carries no source offset to compare.)
|
|
479
|
+
def before_case_label_region(statement, remark)
|
|
480
|
+
return unless statement.parent.is_a?(Model::Statements::CaseAction)
|
|
481
|
+
return unless statement.source_offset
|
|
482
|
+
|
|
483
|
+
gap = @source.b.byteslice(remark.position,
|
|
484
|
+
statement.source_offset - remark.position)
|
|
485
|
+
return if gap.nil? || only_remarks_and_space?(gap)
|
|
486
|
+
|
|
487
|
+
Model::RemarkPlacement::BEFORE_LABEL_REGION
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
# Whether the bytes hold nothing but white space and remarks: a line
|
|
491
|
+
# remark runs to the end of the line, a block remark to its matching
|
|
492
|
+
# `*)` (they nest), and an unterminated block remark swallows the rest.
|
|
493
|
+
def only_remarks_and_space?(gap)
|
|
494
|
+
i = 0
|
|
495
|
+
size = gap.bytesize
|
|
496
|
+
while i < size
|
|
497
|
+
byte = gap.getbyte(i)
|
|
498
|
+
pair = gap.byteslice(i, 2)
|
|
499
|
+
if [32, 9, 10, 12, 13].include?(byte)
|
|
500
|
+
i += 1
|
|
501
|
+
elsif pair == "--"
|
|
502
|
+
i += 1 until i >= size || gap.getbyte(i) == 10
|
|
503
|
+
elsif pair == "(*"
|
|
504
|
+
depth = 1
|
|
505
|
+
i += 2
|
|
506
|
+
while i < size && depth.positive?
|
|
507
|
+
case gap.byteslice(i, 2)
|
|
508
|
+
when "(*" then depth += 1
|
|
509
|
+
i += 2
|
|
510
|
+
when "*)" then depth -= 1
|
|
511
|
+
i += 2
|
|
512
|
+
else i += 1
|
|
513
|
+
end
|
|
514
|
+
end
|
|
515
|
+
else
|
|
516
|
+
return false
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
true
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
# A block comment at the start of a line, before the USE FROM or
|
|
523
|
+
# REFERENCE FROM clause that begins on that line, leads the clause,
|
|
524
|
+
# as an own-line comment above it does. The clause shares the line,
|
|
525
|
+
# so it is neither "the next node after the line" nor outside the
|
|
526
|
+
# scope the lookup measures; and a `--` comment at the end of the
|
|
527
|
+
# line must not make the remark read as a trailing one.
|
|
528
|
+
def block_before_clause_target(remark)
|
|
529
|
+
content = line_content_for(remark.line).to_s
|
|
530
|
+
return [nil, nil, nil] unless content.match?(CLAUSE_AFTER_BLOCKS)
|
|
531
|
+
|
|
532
|
+
clause = @node_index.starting_at(remark.line).find do |n|
|
|
533
|
+
n[:node].is_a?(Model::Declarations::Interface) &&
|
|
534
|
+
n[:position] && n[:position] > remark.position
|
|
535
|
+
end
|
|
536
|
+
return [nil, nil, nil] unless clause
|
|
537
|
+
|
|
538
|
+
[clause[:node], Model::RemarkPlacement::LEADING, nil]
|
|
402
539
|
end
|
|
403
540
|
|
|
541
|
+
CLAUSE_AFTER_BLOCKS = /\A(?:\s*\(\*.*?\*\))+\s*(?:USE|REFERENCE)\s+FROM\b/i
|
|
542
|
+
|
|
404
543
|
# The nearest semantic node starting after the remark's line but
|
|
405
544
|
# still inside the scope that contains the remark — the next
|
|
406
545
|
# declaration the comment introduces.
|
|
407
546
|
def declaration_level_target(remark)
|
|
408
547
|
line = remark.line
|
|
548
|
+
# An item of a SELECT list encloses nothing.
|
|
409
549
|
scope = @node_index.spanning(line)
|
|
410
|
-
.reject
|
|
550
|
+
.reject do |n|
|
|
551
|
+
n[:node].is_a?(Model::ExpFile) ||
|
|
552
|
+
n[:collection] == NodePositionIndex::SELECT_ITEMS
|
|
553
|
+
end
|
|
411
554
|
.min_by { |n| [n[:end_line] - n[:line], n[:line]] }
|
|
412
555
|
limit = scope ? scope[:end_line] : line
|
|
413
556
|
next_line = @node_index.next_start_after(line, limit)
|
|
414
557
|
return [nil, nil, nil] unless next_line
|
|
415
558
|
|
|
416
|
-
|
|
417
|
-
|
|
559
|
+
following = @node_index.starting_at(next_line).first
|
|
560
|
+
# A remark between `SELECT` and the parenthesis, or after the code
|
|
561
|
+
# of another line, does not lead an item of the list.
|
|
562
|
+
if following[:collection] == NodePositionIndex::SELECT_ITEMS &&
|
|
563
|
+
!leads_select_item?(remark)
|
|
564
|
+
return [nil, nil, nil]
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
[following[:node], Model::RemarkPlacement::LEADING, nil]
|
|
418
568
|
end
|
|
419
569
|
|
|
420
570
|
# Whether the remark starts its line: nothing but blanks before it.
|
|
@@ -434,6 +584,55 @@ module Expressir
|
|
|
434
584
|
!content[0...opener].strip.empty?
|
|
435
585
|
end
|
|
436
586
|
|
|
587
|
+
# The columns (byte indexes in the line) of the `(* ... *)` comments
|
|
588
|
+
# that follow the label of a WHERE rule on this line:
|
|
589
|
+
# `wr1 : (* a *) (* b *) size > 0;`. The line starts with the label, so
|
|
590
|
+
# no string comes before it; white space may separate the label's colon
|
|
591
|
+
# and the comments; a comment nests and ends at its own `*)`. A comment
|
|
592
|
+
# that does not end on the line is the last.
|
|
593
|
+
def label_block_columns(content)
|
|
594
|
+
match = content.b.match(/\A\s*[A-Za-z_]\w*\s*:(?!=)/n)
|
|
595
|
+
return [] unless match
|
|
596
|
+
|
|
597
|
+
bytes = content.b
|
|
598
|
+
i = match.end(0)
|
|
599
|
+
columns = []
|
|
600
|
+
loop do
|
|
601
|
+
i += 1 while bytes[i]&.match?(/\s/n)
|
|
602
|
+
return columns unless bytes[i, 2] == "(*"
|
|
603
|
+
|
|
604
|
+
columns << i
|
|
605
|
+
depth = 0
|
|
606
|
+
while i < bytes.bytesize
|
|
607
|
+
case bytes[i, 2]
|
|
608
|
+
when "(*" then depth += 1
|
|
609
|
+
i += 2
|
|
610
|
+
when "*)" then depth -= 1
|
|
611
|
+
i += 2
|
|
612
|
+
break if depth.zero?
|
|
613
|
+
else i += 1
|
|
614
|
+
end
|
|
615
|
+
end
|
|
616
|
+
return columns unless depth.zero?
|
|
617
|
+
end
|
|
618
|
+
end
|
|
619
|
+
|
|
620
|
+
# A `(* block *)` comment right after the label of a WHERE rule belongs
|
|
621
|
+
# to that rule, written after the label like a `--` comment there.
|
|
622
|
+
# Asked only on the first line of a WHERE rule.
|
|
623
|
+
def where_label_block_target(remark)
|
|
624
|
+
line = remark.line
|
|
625
|
+
columns = label_block_columns(line_content_for(line).to_s)
|
|
626
|
+
return [nil, nil, nil] unless columns.include?(remark.position - @line_map.line_start(line).to_i)
|
|
627
|
+
|
|
628
|
+
rule = @node_index.starting_at(line)
|
|
629
|
+
.find { |n| n[:node].is_a?(Model::Declarations::WhereRule) }
|
|
630
|
+
return [nil, nil, nil] unless rule
|
|
631
|
+
|
|
632
|
+
[rule[:node], Model::RemarkPlacement::INLINE,
|
|
633
|
+
Model::RemarkPlacement::OPENER_REGION]
|
|
634
|
+
end
|
|
635
|
+
|
|
437
636
|
# A comment trailing code on its line belongs to the statement it
|
|
438
637
|
# follows: `x := 1; -- why`, or the opener of a compound one, as in
|
|
439
638
|
# `IF x THEN -- why`.
|
|
@@ -445,7 +644,7 @@ module Expressir
|
|
|
445
644
|
# it to, and without that region the old appending behaviour stands.
|
|
446
645
|
def inline_target(remark, nodes)
|
|
447
646
|
started_here = nodes.select do |n|
|
|
448
|
-
|
|
647
|
+
takes_inline_remark?(n) &&
|
|
449
648
|
n[:line] == remark.line &&
|
|
450
649
|
n[:position] && n[:position] < remark.position
|
|
451
650
|
end
|
|
@@ -463,6 +662,39 @@ module Expressir
|
|
|
463
662
|
opener_region(owner, remark.line)]
|
|
464
663
|
end
|
|
465
664
|
|
|
665
|
+
# Whether an indexed node takes a remark written after it on its
|
|
666
|
+
# line: a TakesInlineRemark, or an item of a SELECT list. The item is
|
|
667
|
+
# a reference, and a reference takes such a remark nowhere else (the
|
|
668
|
+
# FOR items of a RULE are indexed too), so the index entry is asked,
|
|
669
|
+
# not the class.
|
|
670
|
+
def takes_inline_remark?(entry)
|
|
671
|
+
entry[:node].is_a?(Model::TakesInlineRemark) ||
|
|
672
|
+
entry[:collection] == NodePositionIndex::SELECT_ITEMS
|
|
673
|
+
end
|
|
674
|
+
|
|
675
|
+
# Whether the remark stands where it can lead an item of a SELECT
|
|
676
|
+
# list: the code before it, on its line or on the lines above, ends
|
|
677
|
+
# with the parenthesis that opens the list or with the comma of the
|
|
678
|
+
# item before, and no code follows it on its line.
|
|
679
|
+
def leads_select_item?(remark)
|
|
680
|
+
code = keyword_scannable(line_content_for(remark.line).to_s)
|
|
681
|
+
before, _, block = code.partition("(*")
|
|
682
|
+
return false unless block.partition("*)").last.strip.empty?
|
|
683
|
+
|
|
684
|
+
before = last_code_before(remark.line) if before.strip.empty?
|
|
685
|
+
before.rstrip.end_with?("(", ",")
|
|
686
|
+
end
|
|
687
|
+
|
|
688
|
+
# The code of the nearest line above `line` that holds code.
|
|
689
|
+
def last_code_before(line)
|
|
690
|
+
(line - 1).downto(1) do |number|
|
|
691
|
+
code = keyword_scannable(line_content_for(number).to_s)
|
|
692
|
+
.gsub(/\(\*.*?\*\)/, "").strip
|
|
693
|
+
return code unless code.empty? || code.start_with?("(*")
|
|
694
|
+
end
|
|
695
|
+
""
|
|
696
|
+
end
|
|
697
|
+
|
|
466
698
|
# A node beginning and ending on the remark's line. Ranked by where it
|
|
467
699
|
# ends, so of several sharing the line the outermost complete one wins:
|
|
468
700
|
# `CASE n OF 1 : x := 2; END_CASE; -- why` trails the CASE, not its
|
|
@@ -495,7 +727,7 @@ module Expressir
|
|
|
495
727
|
def closing_owner(nodes, remark)
|
|
496
728
|
nodes
|
|
497
729
|
.select do |n|
|
|
498
|
-
|
|
730
|
+
takes_inline_remark?(n) &&
|
|
499
731
|
n[:end_line] == remark.line && n[:line] &&
|
|
500
732
|
n[:line] < remark.line &&
|
|
501
733
|
n[:position] && n[:position] < remark.position
|
|
@@ -543,6 +775,95 @@ module Expressir
|
|
|
543
775
|
Model::RemarkPlacement::LABEL_REGION],
|
|
544
776
|
}.freeze
|
|
545
777
|
|
|
778
|
+
# A remark (a line or a block comment) after an operand of a
|
|
779
|
+
# SUBTYPE_CONSTRAINT's supertype expression, on the line the operand
|
|
780
|
+
# ends on: inline on the nearest operand that ends before it, when
|
|
781
|
+
# nothing but what the formatter writes after that operand stands
|
|
782
|
+
# between them. That is the comma of an operand of a ONEOF list, or,
|
|
783
|
+
# for the last operand of the whole expression, the parentheses and
|
|
784
|
+
# the `;` that end it.
|
|
785
|
+
def supertype_operand_target(remark)
|
|
786
|
+
operand = supertype_operands.select { |n| n[:finish] <= remark.position }.max_by { |n| n[:finish] }
|
|
787
|
+
return [nil, nil, nil] unless operand && operand[:finish] >= line_start_of(remark.position)
|
|
788
|
+
|
|
789
|
+
finish = operand[:finish]
|
|
790
|
+
between = @source.byteslice(finish, remark.position - finish).to_s.gsub(/\s/, "")
|
|
791
|
+
allowed = case operand[:collection]
|
|
792
|
+
when NodePositionIndex::SUPERTYPE_LISTED then between.match?(/\A\)*,\z/)
|
|
793
|
+
when NodePositionIndex::SUPERTYPE_FINAL then between.match?(/\A\)*;?\z/)
|
|
794
|
+
else false
|
|
795
|
+
end
|
|
796
|
+
allowed ? [operand[:node], Model::RemarkPlacement::INLINE, nil] : [nil, nil, nil]
|
|
797
|
+
end
|
|
798
|
+
|
|
799
|
+
# A remark with no code after it on its line, when the next code
|
|
800
|
+
# starts with an operand of a SUBTYPE_CONSTRAINT's supertype
|
|
801
|
+
# expression: it leads that operand if the last code before the remark
|
|
802
|
+
# is a comma or the `(` that opens a ONEOF list, and nothing but white
|
|
803
|
+
# space and comments lies between the remark and the operand's name.
|
|
804
|
+
# A comment that stands inside the own parentheses of an operand is
|
|
805
|
+
# not taken. Nil when the next code is not such an operand.
|
|
806
|
+
def supertype_operand_lead(remark)
|
|
807
|
+
following = supertype_operands.select { |n| n[:position] >= remark.position }.min_by { |n| n[:position] }
|
|
808
|
+
# Only white space and a new line lie between the remark and the
|
|
809
|
+
# operand. The span of an operand in parentheses starts at its `(`,
|
|
810
|
+
# that of the Rust parser at its name, so a `(` before the name is
|
|
811
|
+
# found in both ways.
|
|
812
|
+
gap = following && code_without_remarks.byteslice(remark.position, following[:position] - remark.position)
|
|
813
|
+
blank_gap = gap&.include?("\n") && gap.match?(/\A[ \t\r\n\f]*\z/)
|
|
814
|
+
unless blank_gap && code_without_remarks.getbyte(following[:position]) != 40
|
|
815
|
+
# Other code first: when the next node to start is an operand all
|
|
816
|
+
# the same, the remark is not the declaration's to lead.
|
|
817
|
+
next_line = @node_index.next_start_after(remark.line, source_line_count)
|
|
818
|
+
next_entry = next_line && @node_index.starting_at(next_line).first
|
|
819
|
+
return next_entry && NodePositionIndex::SUPERTYPE_OPERANDS.include?(next_entry[:collection]) ? [nil, nil, nil] : nil
|
|
820
|
+
end
|
|
821
|
+
|
|
822
|
+
# The last code before the remark: a comma, or the `(` of a ONEOF.
|
|
823
|
+
last = remark.position.positive? && code_without_remarks.rindex(/[^ \t\r\n\f]/, remark.position - 1)
|
|
824
|
+
listed = last && (code_without_remarks.getbyte(last) == 44 ||
|
|
825
|
+
(code_without_remarks.getbyte(last) == 40 && oneof_before?(last)))
|
|
826
|
+
return [nil, nil, nil] unless listed && following[:collection] != NodePositionIndex::SUPERTYPE_OTHER
|
|
827
|
+
|
|
828
|
+
[following[:node], Model::RemarkPlacement::LEADING, nil]
|
|
829
|
+
end
|
|
830
|
+
|
|
831
|
+
# Whether the word before the `(` at `index` is ONEOF.
|
|
832
|
+
def oneof_before?(index)
|
|
833
|
+
word_end = code_without_remarks.rindex(/[^ \t\r\n\f]/, index - 1)
|
|
834
|
+
return false unless word_end && word_end >= 4
|
|
835
|
+
|
|
836
|
+
code_without_remarks.byteslice(word_end - 4, 5).casecmp?("oneof") &&
|
|
837
|
+
(word_end == 4 || code_without_remarks.byteslice(word_end - 5, 1).match?(/\W/))
|
|
838
|
+
end
|
|
839
|
+
|
|
840
|
+
# The source with every comment overwritten by spaces (new lines
|
|
841
|
+
# kept), as bytes, so that its positions are the source's.
|
|
842
|
+
def code_without_remarks
|
|
843
|
+
@code_without_remarks ||= @source.b.gsub(/'[^']*'|\(\*.*?\*\)|--[^\n]*/m) do |text|
|
|
844
|
+
text.start_with?("'") ? text : text.gsub(/[^\n]/n, " ")
|
|
845
|
+
end
|
|
846
|
+
end
|
|
847
|
+
|
|
848
|
+
# The byte position where the line of `position` starts.
|
|
849
|
+
def line_start_of(position)
|
|
850
|
+
(code_without_remarks.rindex("\n", position - 1) || -1) + 1
|
|
851
|
+
end
|
|
852
|
+
|
|
853
|
+
# The references of every supertype expression, each with where its
|
|
854
|
+
# name ends: the own closing parentheses of an operand, and the white
|
|
855
|
+
# space and comments before them, are not part of it, so that the
|
|
856
|
+
# span of an operand in parentheses as the Ruby builders give it and
|
|
857
|
+
# the span of the Rust parser (the name) are read alike.
|
|
858
|
+
def supertype_operands
|
|
859
|
+
@supertype_operands ||= @node_index.nodes.filter_map do |n|
|
|
860
|
+
next unless NodePositionIndex::SUPERTYPE_OPERANDS.include?(n[:collection]) && n[:position]
|
|
861
|
+
|
|
862
|
+
span_end = n[:position] + NodePositionIndex.span_length(n[:node])
|
|
863
|
+
n.merge(finish: code_without_remarks.rindex(/[^\s)]/, span_end - 1) + 1)
|
|
864
|
+
end
|
|
865
|
+
end
|
|
866
|
+
|
|
546
867
|
# Attach `ELSE -- why` / `OTHERWISE : -- why` to the innermost
|
|
547
868
|
# enclosing IF / CASE with INLINE placement and the keyword's region.
|
|
548
869
|
# Returns [nil, nil, nil] when the line head is no mid-construct
|
|
@@ -565,6 +886,151 @@ module Expressir
|
|
|
565
886
|
[innermost[:node], Model::RemarkPlacement::INLINE, region]
|
|
566
887
|
end
|
|
567
888
|
|
|
889
|
+
# The nested declarations of a callable: its own collections, before
|
|
890
|
+
# LOCAL and the statements.
|
|
891
|
+
HEAD_COLLECTIONS = %i[
|
|
892
|
+
types entities subtype_constraints functions procedures constants
|
|
893
|
+
].freeze
|
|
894
|
+
|
|
895
|
+
# A comment on its own line in the declaration part of a FUNCTION,
|
|
896
|
+
# PROCEDURE or RULE. Before a nested declaration it leads that
|
|
897
|
+
# declaration; before LOCAL or END_LOCAL it closes the part above
|
|
898
|
+
# (TRAILING, region declarations or variables); before a LOCAL
|
|
899
|
+
# variable it leads the variable. The variables carry no source spans,
|
|
900
|
+
# so the next code line names the one (as in local_variable_target).
|
|
901
|
+
# Nothing else follows the declaration part but the statements and
|
|
902
|
+
# WHERE rules, so a body that starts above the line found means the
|
|
903
|
+
# comment is not in the declaration part. Only `--` comments and
|
|
904
|
+
# one-line `(* *)` comments take part: a comment of several lines is
|
|
905
|
+
# left to the lookups that follow.
|
|
906
|
+
def head_declaration_target(remark)
|
|
907
|
+
line = remark.line
|
|
908
|
+
return [nil, nil, nil] unless only_remark?(line_content_for(line))
|
|
909
|
+
|
|
910
|
+
callable = innermost_candidate(@node_index.spanning(line).select do |n|
|
|
911
|
+
n[:line] < line && function_rule_procedure?(n[:node])
|
|
912
|
+
end)
|
|
913
|
+
return [nil, nil, nil] unless callable
|
|
914
|
+
|
|
915
|
+
owner = callable[:node]
|
|
916
|
+
body = @node_index.children_in(owner, STATEMENT_REGIONS + [:where_rules])
|
|
917
|
+
.select { |n| n[:line] > line }.map { |n| n[:line] }.min
|
|
918
|
+
in_head = ->(l) { body.nil? || l < body }
|
|
919
|
+
|
|
920
|
+
nested = @node_index.children_in(owner, HEAD_COLLECTIONS)
|
|
921
|
+
.select { |n| n[:line] > line }.min_by { |n| n[:position] }
|
|
922
|
+
if nested
|
|
923
|
+
return [nested[:node], Model::RemarkPlacement::LEADING, nil] if in_head.(nested[:line])
|
|
924
|
+
|
|
925
|
+
return [nil, nil, nil]
|
|
926
|
+
end
|
|
927
|
+
|
|
928
|
+
code_line = next_code_line(line)
|
|
929
|
+
return [nil, nil, nil] unless code_line && in_head.(code_line[0]) && owner.variables&.any?
|
|
930
|
+
|
|
931
|
+
if code_line[1].match?(/\ALOCAL\b/i)
|
|
932
|
+
return [owner, Model::RemarkPlacement::TRAILING, "declarations"]
|
|
933
|
+
elsif code_line[1].match?(/\AEND_LOCAL\b/i)
|
|
934
|
+
return [owner, Model::RemarkPlacement::TRAILING, "variables"]
|
|
935
|
+
end
|
|
936
|
+
|
|
937
|
+
# A variable is led only from inside the LOCAL block of this
|
|
938
|
+
# callable, after the end of a declaration: a comment between the
|
|
939
|
+
# names of one declaration, or in a statement body, is not.
|
|
940
|
+
text = (callable[:line]...line).map { |n| line_content_for(n) }.join("\n").b
|
|
941
|
+
inside, last = local_block_state(text)
|
|
942
|
+
return [nil, nil, nil] unless inside && (last == ";" || last.casecmp?("LOCAL"))
|
|
943
|
+
|
|
944
|
+
names = code_line[1][/\A\s*([A-Za-z_]\w*(?:\s*,\s*[A-Za-z_]\w*)*)\s*:(?!=)/, 1]&.split(/\s*,\s*/)
|
|
945
|
+
variable = names && owner.variables.find do |v|
|
|
946
|
+
names.any? { |name| name.casecmp?(v.id) }
|
|
947
|
+
end
|
|
948
|
+
return [nil, nil, nil] unless variable
|
|
949
|
+
|
|
950
|
+
[variable, Model::RemarkPlacement::LEADING, nil]
|
|
951
|
+
end
|
|
952
|
+
|
|
953
|
+
# Whether a source line holds nothing but remarks: `--` comments and
|
|
954
|
+
# one-line `(* *)` blocks, which nest. A block left open at the end of
|
|
955
|
+
# the line, or code, makes it false.
|
|
956
|
+
def only_remark?(content)
|
|
957
|
+
i = 0
|
|
958
|
+
depth = 0
|
|
959
|
+
while i < content.length
|
|
960
|
+
pair = content[i, 2]
|
|
961
|
+
if depth.zero? && pair == "--"
|
|
962
|
+
return true
|
|
963
|
+
elsif pair == "(*"
|
|
964
|
+
depth += 1
|
|
965
|
+
i += 2
|
|
966
|
+
elsif depth.positive? && pair == "*)"
|
|
967
|
+
depth -= 1
|
|
968
|
+
i += 2
|
|
969
|
+
elsif depth.zero? && !content[i].match?(/\s/)
|
|
970
|
+
return false
|
|
971
|
+
else
|
|
972
|
+
i += 1
|
|
973
|
+
end
|
|
974
|
+
end
|
|
975
|
+
depth.zero?
|
|
976
|
+
end
|
|
977
|
+
|
|
978
|
+
# The first line after `line` that holds code, as [line number, text]:
|
|
979
|
+
# blank lines and lines of remarks only are skipped.
|
|
980
|
+
def next_code_line(line)
|
|
981
|
+
probe = line + 1
|
|
982
|
+
while probe <= source_line_count
|
|
983
|
+
content = line_content_for(probe).to_s.strip
|
|
984
|
+
return [probe, content] unless content.empty? || only_remark?(content)
|
|
985
|
+
|
|
986
|
+
probe += 1
|
|
987
|
+
end
|
|
988
|
+
nil
|
|
989
|
+
end
|
|
990
|
+
|
|
991
|
+
# Whether the end of `text` stands inside the LOCAL block of the
|
|
992
|
+
# callable that `text` starts, and the last code token before it:
|
|
993
|
+
# [inside, token]. Remarks and string literals are skipped; LOCAL opens
|
|
994
|
+
# the block and END_LOCAL closes it.
|
|
995
|
+
def local_block_state(text)
|
|
996
|
+
inside = false
|
|
997
|
+
last = ""
|
|
998
|
+
i = 0
|
|
999
|
+
while i < text.bytesize
|
|
1000
|
+
pair = text.byteslice(i, 2)
|
|
1001
|
+
char = text.byteslice(i, 1)
|
|
1002
|
+
if pair == "--"
|
|
1003
|
+
i += 1 while i < text.bytesize && text.getbyte(i) != 10
|
|
1004
|
+
elsif pair == "(*"
|
|
1005
|
+
depth = 0
|
|
1006
|
+
while i < text.bytesize
|
|
1007
|
+
pair = text.byteslice(i, 2)
|
|
1008
|
+
depth += 1 if pair == "(*"
|
|
1009
|
+
depth -= 1 if pair == "*)"
|
|
1010
|
+
i += ["(*", "*)"].include?(pair) ? 2 : 1
|
|
1011
|
+
break if depth.zero?
|
|
1012
|
+
end
|
|
1013
|
+
elsif ["'", '"'].include?(char)
|
|
1014
|
+
i += 1
|
|
1015
|
+
i += (text.byteslice(i, 2) == "''" ? 2 : 1) until i >= text.bytesize || (text.byteslice(i, 1) == char && text.byteslice(i, 2) != "''")
|
|
1016
|
+
i += 1
|
|
1017
|
+
last = "'"
|
|
1018
|
+
elsif char.match?(/[A-Za-z_]/n)
|
|
1019
|
+
word = text.match(/\G\w+/n, i)[0]
|
|
1020
|
+
last = word
|
|
1021
|
+
inside = true if word.casecmp?("LOCAL")
|
|
1022
|
+
inside = false if word.casecmp?("END_LOCAL")
|
|
1023
|
+
i += word.bytesize
|
|
1024
|
+
elsif char.match?(/\s/n)
|
|
1025
|
+
i += 1
|
|
1026
|
+
else
|
|
1027
|
+
last = char
|
|
1028
|
+
i += 1
|
|
1029
|
+
end
|
|
1030
|
+
end
|
|
1031
|
+
[inside, last]
|
|
1032
|
+
end
|
|
1033
|
+
|
|
568
1034
|
# A remark on a LOCAL variable declaration line — `v : INTEGER; -- why`.
|
|
569
1035
|
# Local variables carry no source spans, so the line's declared name is
|
|
570
1036
|
# matched against the enclosing scope's variables (#693). A multi-name
|
|
@@ -629,6 +1095,12 @@ module Expressir
|
|
|
629
1095
|
# So do END_ENTITY and END_TYPE.
|
|
630
1096
|
/\AEND_ENTITY\b/i => [Model::Declarations::Entity, :where_rules],
|
|
631
1097
|
/\AEND_TYPE\b/i => [Model::Declarations::Type, :where_rules],
|
|
1098
|
+
# DERIVE, INVERSE and UNIQUE close the section before them. So does
|
|
1099
|
+
# the WHERE of an ENTITY or a TYPE, resolved in closing_region_target
|
|
1100
|
+
# from the construct open at that line (the entry above is a RULE's).
|
|
1101
|
+
/\ADERIVE\b/i => [Model::Declarations::Entity, :attributes],
|
|
1102
|
+
/\AINVERSE\b/i => [Model::Declarations::Entity, :derived_attributes],
|
|
1103
|
+
/\AUNIQUE\b/i => [Model::Declarations::Entity, :inverse_attributes],
|
|
632
1104
|
}.freeze
|
|
633
1105
|
|
|
634
1106
|
# Declarations whose bodies hold no statements. Only a comment on a
|
|
@@ -638,6 +1110,14 @@ module Expressir
|
|
|
638
1110
|
Model::Declarations::Entity, Model::Declarations::Type
|
|
639
1111
|
].freeze
|
|
640
1112
|
|
|
1113
|
+
# Whether an entity has attributes of that kind, wherever the model
|
|
1114
|
+
# keeps them: the Ruby builders put every kind in `attributes`.
|
|
1115
|
+
ENTITY_HAS_KIND = lambda do |entity, kind|
|
|
1116
|
+
[entity.attributes, entity.derived_attributes, entity.inverse_attributes]
|
|
1117
|
+
.flat_map { |items| Array(items).flatten }
|
|
1118
|
+
.any? { |attribute| attribute.respond_to?(:kind) && attribute.kind == kind }
|
|
1119
|
+
end
|
|
1120
|
+
|
|
641
1121
|
# Regions whose owner may not have that body, in which case the
|
|
642
1122
|
# keyword closes the earlier region instead.
|
|
643
1123
|
REGION_FALLBACKS = {
|
|
@@ -647,10 +1127,17 @@ module Expressir
|
|
|
647
1127
|
[:action_statements, ->(n) { !n.otherwise_statement.nil? }],
|
|
648
1128
|
[Model::Declarations::Rule, :where_rules] =>
|
|
649
1129
|
[:statements, ->(n) { n.where_rules&.length&.positive? }],
|
|
650
|
-
# END_ENTITY
|
|
651
|
-
# the
|
|
1130
|
+
# END_ENTITY and the section keywords close the last section the
|
|
1131
|
+
# entity has before them, END_TYPE the definition when the type
|
|
1132
|
+
# has no WHERE section.
|
|
652
1133
|
[Model::Declarations::Entity, :where_rules] =>
|
|
653
|
-
[:
|
|
1134
|
+
[:unique_rules, ->(n) { n.where_rules&.length&.positive? }],
|
|
1135
|
+
[Model::Declarations::Entity, :unique_rules] =>
|
|
1136
|
+
[:inverse_attributes, ->(n) { n.unique_rules&.length&.positive? }],
|
|
1137
|
+
[Model::Declarations::Entity, :inverse_attributes] =>
|
|
1138
|
+
[:derived_attributes, ->(n) { ENTITY_HAS_KIND.call(n, Model::Declarations::Attribute::INVERSE) }],
|
|
1139
|
+
[Model::Declarations::Entity, :derived_attributes] =>
|
|
1140
|
+
[:attributes, ->(n) { ENTITY_HAS_KIND.call(n, Model::Declarations::Attribute::DERIVED) }],
|
|
654
1141
|
[Model::Declarations::Type, :where_rules] =>
|
|
655
1142
|
[:underlying_type, ->(n) { n.where_rules&.length&.positive? }],
|
|
656
1143
|
}.freeze
|
|
@@ -663,6 +1150,8 @@ module Expressir
|
|
|
663
1150
|
def closing_region_target(line, own_line: true)
|
|
664
1151
|
keyword_owner, region, keyword_line = closing_keyword_after(line)
|
|
665
1152
|
return [nil, nil, nil] unless keyword_owner
|
|
1153
|
+
|
|
1154
|
+
keyword_owner, region = declaration_where(keyword_owner, region, keyword_line)
|
|
666
1155
|
return [nil, nil, nil] if !own_line && DECLARATION_BODY_OWNERS.include?(keyword_owner)
|
|
667
1156
|
|
|
668
1157
|
# The owner is the construct the keyword actually closes — the
|
|
@@ -673,25 +1162,48 @@ module Expressir
|
|
|
673
1162
|
opener_line = active_opener_line(keyword_line, keyword_owner)
|
|
674
1163
|
return [nil, nil, nil] unless opener_line
|
|
675
1164
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
1165
|
+
# Several constructs of the class may start on that line: the one
|
|
1166
|
+
# still open reaches the keyword line.
|
|
1167
|
+
starting = @node_index.starting_at(opener_line).select { |n| n[:node].is_a?(keyword_owner) }
|
|
1168
|
+
owner = starting.reverse.find { |n| n[:end_line] && n[:end_line] >= keyword_line } || starting.first
|
|
679
1169
|
return [nil, nil, nil] unless owner
|
|
680
1170
|
|
|
681
1171
|
# END_IF closes the THEN body when there is no ELSE; END_CASE closes
|
|
682
1172
|
# the last action when there is no OTHERWISE.
|
|
683
|
-
|
|
684
|
-
|
|
1173
|
+
loop do
|
|
1174
|
+
fallback, present = REGION_FALLBACKS[[keyword_owner, region]]
|
|
1175
|
+
break if fallback.nil? || present.call(owner[:node])
|
|
1176
|
+
|
|
1177
|
+
region = fallback
|
|
1178
|
+
end
|
|
685
1179
|
|
|
686
1180
|
[owner[:node], Model::RemarkPlacement::TRAILING, region.to_s]
|
|
687
1181
|
end
|
|
688
1182
|
|
|
1183
|
+
# The WHERE keyword of a RULE closes its statements. Where an ENTITY or a
|
|
1184
|
+
# TYPE is open at that line it closes the unique rules (or the section
|
|
1185
|
+
# before them) or the definition.
|
|
1186
|
+
def declaration_where(owner, region, keyword_line)
|
|
1187
|
+
return [owner, region] unless owner == Model::Declarations::Rule && region == :statements
|
|
1188
|
+
|
|
1189
|
+
klass, opener_line = active_scope_map[keyword_line]
|
|
1190
|
+
return [owner, region] unless klass == :other
|
|
1191
|
+
|
|
1192
|
+
declaration = @node_index.starting_at(opener_line).map { |n| n[:node] }
|
|
1193
|
+
.find { |n| DECLARATION_BODY_OWNERS.any? { |c| n.is_a?(c) } }
|
|
1194
|
+
case declaration
|
|
1195
|
+
when Model::Declarations::Entity then [Model::Declarations::Entity, :unique_rules]
|
|
1196
|
+
when Model::Declarations::Type then [Model::Declarations::Type, :underlying_type]
|
|
1197
|
+
else [owner, region]
|
|
1198
|
+
end
|
|
1199
|
+
end
|
|
1200
|
+
|
|
689
1201
|
# The first non-blank, non-comment source line after `line`.
|
|
690
1202
|
# Source keywords that open a nestable construct, paired with the class
|
|
691
1203
|
# of node they produce. Used to find which construct a closing keyword
|
|
692
1204
|
# actually belongs to.
|
|
693
1205
|
OPENERS = [
|
|
694
|
-
[/\bIF\b
|
|
1206
|
+
[/\bIF\b/i, Model::Statements::If],
|
|
695
1207
|
[/\bCASE\b.*?\bOF\b/i, Model::Statements::Case],
|
|
696
1208
|
[/\bREPEAT\b/i, Model::Statements::Repeat],
|
|
697
1209
|
[/\bALIAS\b/i, Model::Statements::Alias],
|
|
@@ -742,8 +1254,8 @@ module Expressir
|
|
|
742
1254
|
stack = []
|
|
743
1255
|
(1..source_line_count).each do |ln|
|
|
744
1256
|
map[ln] = stack.last
|
|
745
|
-
content =
|
|
746
|
-
next if content.empty?
|
|
1257
|
+
content = code_lines[ln - 1].to_s.strip
|
|
1258
|
+
next if content.empty?
|
|
747
1259
|
|
|
748
1260
|
line_events(content).each do |_offset, kind, klass|
|
|
749
1261
|
kind == :open ? stack << [klass, ln] : stack.pop
|
|
@@ -752,6 +1264,53 @@ module Expressir
|
|
|
752
1264
|
map
|
|
753
1265
|
end
|
|
754
1266
|
|
|
1267
|
+
# The code of each source line, for the scope map only: strings
|
|
1268
|
+
# blanked, a `--` tail cut and every part inside a `(* ... *)` comment
|
|
1269
|
+
# removed. The comment state runs from line to line, so a remark of
|
|
1270
|
+
# several lines, or one that nests, hides the keywords written in its
|
|
1271
|
+
# prose; a `--` inside a block remark ends nothing, and a `(*` inside a
|
|
1272
|
+
# string or after `--` opens nothing.
|
|
1273
|
+
def code_lines
|
|
1274
|
+
@code_lines ||= scan_code_lines
|
|
1275
|
+
end
|
|
1276
|
+
|
|
1277
|
+
def scan_code_lines
|
|
1278
|
+
depth = 0
|
|
1279
|
+
source_lines_for_where_clause.map do |raw|
|
|
1280
|
+
chars = raw.chars
|
|
1281
|
+
code = +""
|
|
1282
|
+
i = 0
|
|
1283
|
+
while i < chars.length
|
|
1284
|
+
pair = chars[i, 2].join
|
|
1285
|
+
if depth.positive?
|
|
1286
|
+
if pair == "(*"
|
|
1287
|
+
depth += 1
|
|
1288
|
+
i += 2
|
|
1289
|
+
elsif pair == "*)"
|
|
1290
|
+
depth -= 1
|
|
1291
|
+
code << " " if depth.zero?
|
|
1292
|
+
i += 2
|
|
1293
|
+
else
|
|
1294
|
+
i += 1
|
|
1295
|
+
end
|
|
1296
|
+
elsif chars[i] == "'"
|
|
1297
|
+
close = chars[(i + 1)..].index("'")
|
|
1298
|
+
code << "''"
|
|
1299
|
+
i = close ? i + close + 2 : chars.length
|
|
1300
|
+
elsif pair == "--"
|
|
1301
|
+
break
|
|
1302
|
+
elsif pair == "(*"
|
|
1303
|
+
depth = 1
|
|
1304
|
+
i += 2
|
|
1305
|
+
else
|
|
1306
|
+
code << chars[i]
|
|
1307
|
+
i += 1
|
|
1308
|
+
end
|
|
1309
|
+
end
|
|
1310
|
+
code
|
|
1311
|
+
end
|
|
1312
|
+
end
|
|
1313
|
+
|
|
755
1314
|
# Opener/closer events on one line, ordered by where they appear.
|
|
756
1315
|
# EVERY occurrence is collected, not just the first: a line holding two
|
|
757
1316
|
# complete IF blocks contributes two openers and two closers, and
|
|
@@ -769,8 +1328,17 @@ module Expressir
|
|
|
769
1328
|
events.sort_by(&:first)
|
|
770
1329
|
end
|
|
771
1330
|
|
|
1331
|
+
# The first code line after the remark that starts on `line`, matched
|
|
1332
|
+
# against CLOSING_KEYWORDS. The scan starts after the LAST line of that
|
|
1333
|
+
# remark (a `(* ... *)` comment may span several lines and may nest),
|
|
1334
|
+
# then skips whole block comments, `--` lines and blank lines.
|
|
772
1335
|
def closing_keyword_after(line)
|
|
773
|
-
|
|
1336
|
+
last, after = comment_end(line)
|
|
1337
|
+
# Code after the `*)` on the last line of a comment of several
|
|
1338
|
+
# lines: the comment is not on lines of its own before a keyword.
|
|
1339
|
+
return [nil, nil, nil] if last > line && code_follows?(line_content_for(last).to_s[after..].to_s)
|
|
1340
|
+
|
|
1341
|
+
probe = last + 1
|
|
774
1342
|
# Skip further comment lines AND blank lines: a comment separated
|
|
775
1343
|
# from its closing keyword by an empty line still closes that body.
|
|
776
1344
|
while probe <= source_line_count
|
|
@@ -779,13 +1347,13 @@ module Expressir
|
|
|
779
1347
|
# emits between a trailing comment and the closing keyword:
|
|
780
1348
|
# skip the whole block, the keyword lies beyond it (#609).
|
|
781
1349
|
if content.start_with?("(*")
|
|
782
|
-
|
|
783
|
-
#
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
probe
|
|
1350
|
+
ending, after = comment_end(probe)
|
|
1351
|
+
# After a remark of several lines, code after that block on its
|
|
1352
|
+
# last line is the next code: no closing keyword at the start of
|
|
1353
|
+
# a line. (A remark of one line is read as before.)
|
|
1354
|
+
return [nil, nil, nil] if last > line && code_follows?(line_content_for(ending).to_s[after..].to_s)
|
|
1355
|
+
|
|
1356
|
+
probe = ending + 1
|
|
789
1357
|
next
|
|
790
1358
|
end
|
|
791
1359
|
break unless content.empty? || content.start_with?("--")
|
|
@@ -799,6 +1367,74 @@ module Expressir
|
|
|
799
1367
|
[nil, nil, nil]
|
|
800
1368
|
end
|
|
801
1369
|
|
|
1370
|
+
# Where the comment starting on `line` ends: the line (`line` itself
|
|
1371
|
+
# for a `--` comment or a block closed on its line, else the line of the
|
|
1372
|
+
# `*)` that closes the outermost `(*`; blocks nest) and the offset in
|
|
1373
|
+
# that line just after the comment.
|
|
1374
|
+
def comment_end(line)
|
|
1375
|
+
depth = 0
|
|
1376
|
+
probe = line
|
|
1377
|
+
while probe <= source_line_count
|
|
1378
|
+
text = line_content_for(probe).to_s
|
|
1379
|
+
i = probe == line ? comment_start_index(text) : 0
|
|
1380
|
+
return [probe, text.length] if i.nil? || (depth.zero? && text[i, 2] == "--")
|
|
1381
|
+
|
|
1382
|
+
while i < text.length
|
|
1383
|
+
pair = text[i, 2]
|
|
1384
|
+
if pair == "(*"
|
|
1385
|
+
depth += 1
|
|
1386
|
+
i += 2
|
|
1387
|
+
elsif pair == "*)" && depth.positive?
|
|
1388
|
+
depth -= 1
|
|
1389
|
+
i += 2
|
|
1390
|
+
return [probe, i] if depth.zero?
|
|
1391
|
+
else
|
|
1392
|
+
i += 1
|
|
1393
|
+
end
|
|
1394
|
+
end
|
|
1395
|
+
probe += 1
|
|
1396
|
+
end
|
|
1397
|
+
[source_line_count, line_content_for(source_line_count).to_s.length]
|
|
1398
|
+
end
|
|
1399
|
+
|
|
1400
|
+
# Whether `rest`, the text after a comment on its line, holds code:
|
|
1401
|
+
# further `(* ... *)` comments closed on the line are passed over; a
|
|
1402
|
+
# comment left open, a `--` comment and white space are not code.
|
|
1403
|
+
def code_follows?(rest)
|
|
1404
|
+
i = 0
|
|
1405
|
+
loop do
|
|
1406
|
+
i += 1 while i < rest.length && rest[i].match?(/[ \t\r\n\f]/)
|
|
1407
|
+
return i < rest.length && rest[i, 2] != "--" unless rest[i, 2] == "(*"
|
|
1408
|
+
|
|
1409
|
+
depth = 0
|
|
1410
|
+
while i < rest.length
|
|
1411
|
+
pair = rest[i, 2]
|
|
1412
|
+
if pair == "(*"
|
|
1413
|
+
depth += 1
|
|
1414
|
+
i += 2
|
|
1415
|
+
elsif pair == "*)"
|
|
1416
|
+
depth -= 1
|
|
1417
|
+
i += 2
|
|
1418
|
+
break if depth.zero?
|
|
1419
|
+
else
|
|
1420
|
+
i += 1
|
|
1421
|
+
end
|
|
1422
|
+
end
|
|
1423
|
+
return false if depth.positive?
|
|
1424
|
+
end
|
|
1425
|
+
end
|
|
1426
|
+
|
|
1427
|
+
# Where the first comment of a source line starts, skipping the quoted
|
|
1428
|
+
# strings before it; nil when the line holds none.
|
|
1429
|
+
def comment_start_index(text)
|
|
1430
|
+
quoted = false
|
|
1431
|
+
text.length.times do |i|
|
|
1432
|
+
quoted = !quoted if text[i] == "'"
|
|
1433
|
+
return i if !quoted && ["(*", "--"].include?(text[i, 2])
|
|
1434
|
+
end
|
|
1435
|
+
nil
|
|
1436
|
+
end
|
|
1437
|
+
|
|
802
1438
|
def statement_region_for(line, nodes = nil)
|
|
803
1439
|
candidates = (nodes || @node_index.spanning(line)).select do |n|
|
|
804
1440
|
n[:line] && n[:end_line] && n[:line] <= line && n[:end_line] >= line &&
|