Class: Oppen::Wadler

Inherits:
Object
  • Object
show all
Defined in:
lib/wadler/print.rb

Overview

Wadler.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Config.wadler, space: ' ', new_line: "\n", out: StringIO.new, width: 80) ⇒ Wadler

Returns a new instance of Wadler.

Parameters:

  • config (Oppen::Config) (defaults to: Config.wadler)
  • space (String, Proc) (defaults to: ' ')

    could be a String or a callable. If it's a string, spaces will be generated with the the lambda ->(n){ n * space }, where n is the number of columns to indent. If it's a callable, it will receive n and it needs to return a string.

  • new_line (String) (defaults to: "\n")
  • out (Object) (defaults to: StringIO.new)

    should have a write and string method

  • width (Integer) (defaults to: 80)


25
26
27
28
29
30
31
32
33
34
# File 'lib/wadler/print.rb', line 25

def initialize(config: Config.wadler, space: ' ',
               new_line: "\n", out: StringIO.new, width: 80)
  @config = config
  @current_indent = 0
  @space = space
  @width = width
  @new_line = new_line
  @out = out
  @tokens = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/wadler/print.rb', line 7

def config
  @config
end

#current_indentObject (readonly)

Returns the value of attribute current_indent.



8
9
10
# File 'lib/wadler/print.rb', line 8

def current_indent
  @current_indent
end

#new_lineObject (readonly)

Returns the value of attribute new_line.



10
11
12
# File 'lib/wadler/print.rb', line 10

def new_line
  @new_line
end

#outObject (readonly)

Returns the value of attribute out.



11
12
13
# File 'lib/wadler/print.rb', line 11

def out
  @out
end

#spaceObject (readonly)

Returns the value of attribute space.



9
10
11
# File 'lib/wadler/print.rb', line 9

def space
  @space
end

#tokensObject (readonly)

Returns the value of attribute tokens.



12
13
14
# File 'lib/wadler/print.rb', line 12

def tokens
  @tokens
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/wadler/print.rb', line 13

def width
  @width
end

Instance Method Details

#break(line_continuation: '') ⇒ Nil

Parameters:

  • line_continuation (String) (defaults to: '')

    If a new line is needed display this string before the new line

Returns:

  • (Nil)


128
129
130
# File 'lib/wadler/print.rb', line 128

def break(line_continuation: '')
  tokens << Oppen.line_break(line_continuation:, offset: current_indent)
end

#breakable(str = ' ', width: str.length, line_continuation: '') ⇒ Nil

Parameters:

  • str (String) (defaults to: ' ')
  • line_continuation (String) (defaults to: '')

    If a new line is needed display this string before the new line

Returns:

  • (Nil)


121
122
123
# File 'lib/wadler/print.rb', line 121

def breakable(str = ' ', width: str.length, line_continuation: '')
  tokens << Oppen.break(str, width:, line_continuation:, offset: current_indent)
end

#group(indent = 0, open_obj = '', close_obj = '', break_type = Oppen::Token::BreakType::CONSISTENT) { ... } ⇒ Nil

Parameters:

  • indent (Integer) (defaults to: 0)

    group indentation

  • open_obj (String) (defaults to: '')

    group opening delimiter

  • close_obj (String) (defaults to: '')

    group closing delimiter

  • break_type (Oppen::Token::BreakType) (defaults to: Oppen::Token::BreakType::CONSISTENT)

    group breaking type

Yields:

  • the block of text in a group

Returns:

  • (Nil)

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wadler/print.rb', line 52

def group(indent = 0, open_obj = '', close_obj = '',
          break_type = Oppen::Token::BreakType::CONSISTENT)
  raise ArgumentError, "#{open_obj.nil? ? 'open_obj' : 'close_obj'} cannot be nil" \
    if open_obj.nil? || close_obj.nil?

  tokens <<
    case break_type
    in Oppen::Token::BreakType::CONSISTENT
      Oppen.begin_consistent(offset: indent)
    in Oppen::Token::BreakType::INCONSISTENT
      Oppen.begin_inconsistent(offset: indent)
    end

  if !open_obj.empty?
    self.break
    text(open_obj)
  end

  yield

  if !close_obj.empty?
    self.break
    text(close_obj)
  end

  tokens << Oppen.end
end

#nest(indent, open_obj = '', close_obj = '', break_type = Oppen::Token::BreakType::CONSISTENT) ⇒ Nil

Parameters:

  • indent (Integer)

    nest indentation

  • open_obj (String) (defaults to: '')

    nest opening delimiter

  • close_obj (String) (defaults to: '')

    nest closing delimiter

  • break_type (Oppen::Token::BreakType) (defaults to: Oppen::Token::BreakType::CONSISTENT)

    nest breaking type

Returns:

  • (Nil)

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wadler/print.rb', line 86

def nest(indent, open_obj = '', close_obj = '',
         break_type = Oppen::Token::BreakType::CONSISTENT)
  raise ArgumentError, "#{open_obj.nil? ? 'open_obj' : 'close_obj'} cannot be nil" \
    if open_obj.nil? || close_obj.nil?

  @current_indent += indent

  if !open_obj.empty?
    text(open_obj)
    self.break
  end

  begin
    yield
  ensure
    @current_indent -= indent
  end

  return if close_obj.empty?

  self.break
  text(close_obj)
end

#outputString

Returns:

  • (String)


37
38
39
40
41
42
# File 'lib/wadler/print.rb', line 37

def output
  if !tokens.last.is_a? Oppen::Token::EOF
    tokens << Oppen.eof
  end
  Oppen.print(tokens:, new_line:, config:, space:, out:, width:)
end

#text(value, width: value.length) ⇒ Nil

Parameters:

  • value (String)

Returns:

  • (Nil)


113
114
115
# File 'lib/wadler/print.rb', line 113

def text(value, width: value.length)
  tokens << Oppen.string(value, width:)
end