Class: MilkTea::CST::SourceFile

Inherits:
Data
  • Object
show all
Defined in:
lib/milk_tea/core/cst.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



5
6
7
# File 'lib/milk_tea/core/cst.rb', line 5

def source
  @source
end

#tokensObject (readonly)

Returns the value of attribute tokens

Returns:

  • (Object)

    the current value of tokens



5
6
7
# File 'lib/milk_tea/core/cst.rb', line 5

def tokens
  @tokens
end

#triviaObject (readonly)

Returns the value of attribute trivia

Returns:

  • (Object)

    the current value of trivia



5
6
7
# File 'lib/milk_tea/core/cst.rb', line 5

def trivia
  @trivia
end

Instance Method Details

#append_trivia(result, entries, normalize_spaces:, leading:) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/milk_tea/core/cst.rb', line 24

def append_trivia(result, entries, normalize_spaces:, leading:)
  entries.each do |entry|
    if normalize_spaces && entry.kind == :space && (!leading || entry.column > 1)
      result << " "
    else
      result << segment_text(entry.start_offset, entry.end_offset, entry.text)
    end
  end
end

#reconstructObject



6
7
8
# File 'lib/milk_tea/core/cst.rb', line 6

def reconstruct
  reconstruct_from_tokens
end

#reconstruct_from_tokens(normalize_spaces: false) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/milk_tea/core/cst.rb', line 14

def reconstruct_from_tokens(normalize_spaces: false)
  return "" if tokens.empty?

  tokens.each_with_object(+"") do |token, result|
    append_trivia(result, token.leading_trivia, normalize_spaces:, leading: true)
    result << segment_text(token.start_offset, token.end_offset, token.lexeme) unless token.eof?
    append_trivia(result, token.trailing_trivia, normalize_spaces:, leading: false)
  end
end

#reconstruct_normalizedObject



10
11
12
# File 'lib/milk_tea/core/cst.rb', line 10

def reconstruct_normalized
  reconstruct_from_tokens(normalize_spaces: true)
end

#segment_text(start_offset, end_offset, fallback) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/milk_tea/core/cst.rb', line 34

def segment_text(start_offset, end_offset, fallback)
  return fallback unless source
  return "" if end_offset <= start_offset

  fallback_text = fallback.dup.force_encoding(source.encoding)
  segment = source.byteslice(start_offset, end_offset - start_offset)
  return fallback_text unless segment
  segment = segment.dup.force_encoding(source.encoding)
  return fallback_text if segment != fallback_text

  segment
end