Module: Oppen::Mixins

Included in:
Oppen, Printer, ScanStack
Defined in:
lib/oppen/mixins.rb

Overview

Mixins.

Instance Method Summary collapse

Instance Method Details

#tokens_to_wadler(tokens, base_indent: 0, printer_name: 'out') ⇒ String

Convert a list of tokens to its wadler representation.

Parameters:

  • tokens (Array[Token])
  • base_indent (Integer) (defaults to: 0)
  • printer_name (String) (defaults to: 'out')

Returns:

  • (String)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/oppen/mixins.rb', line 27

def tokens_to_wadler(tokens, base_indent: 0, printer_name: 'out')
  nb_spaces = base_indent
  out = StringIO.new

  write = ->(txt) {
    out << (' ' * nb_spaces) << txt << "\n"
  }
  display_break_token = ->(token) {
    if token.offset.positive?
      write.("#{printer_name}.nest(#{token.offset}, \"\", \"\") {")
      nb_spaces += 2
    end

    case token
    in Token::LineBreak
      write.("#{printer_name}.break(line_continuation: #{token.line_continuation.inspect})")
    in Token::Break
      write.("#{printer_name}.breakable(#{token.str.inspect}, width: #{token.width}, " \
             "line_continuation: #{token.line_continuation.inspect})")
    end

    if token.offset.positive?
      nb_spaces -= 2
      write.('}')
    end
  }

  tokens.each do |token|
    case token
    in Token::String
      write.("#{printer_name}.text(#{token.value.inspect}, width: #{token.width})")
    in Token::Break
      display_break_token.(token)
    in Token::Begin
      write.("#{printer_name}.group(#{token.offset}, \"\", \"\", #{token.break_type_name}) {")
      nb_spaces += 2
    in Token::End
      nb_spaces -= 2
      write.('}')
    in Token::EOF
      write.('') # new line
    end
  end
  out.string
end

#upsize_circular_array(arr, offset) ⇒ Array<Array, Integer, Integer>

Rotates circular array and triples its size. This method is not for public use.

Parameters:

  • arr (Array)
  • offset (Integer)

    Rotation amount

Returns:

  • (Array<Array, Integer, Integer>)

    upsized array, lhs, rhs



13
14
15
16
17
18
# File 'lib/oppen/mixins.rb', line 13

def upsize_circular_array(arr, offset)
  size = arr.size
  arr = arr.rotate(offset)
  arr.fill(nil, size, 2 * size)
  [arr, 0, size]
end