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: ' ', margin: 80, new_line: "\n", out: StringIO.new) ⇒ 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.

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

    should have a write and string method



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

def initialize(config: Config.wadler, space: ' ',
               margin: 80, new_line: "\n", out: StringIO.new)
  @config = config
  @current_indent = 0
  @space = space
  @margin = margin
  @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

#marginObject (readonly)

Returns the value of attribute margin.



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

def margin
  @margin
end

#new_lineObject (readonly)

Returns the value of attribute new_line.



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

def new_line
  @new_line
end

#outObject (readonly)

Returns the value of attribute out.



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

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.



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

def tokens
  @tokens
end

Instance Method Details

#break(offset = 0) ⇒ Nil

Parameters:

  • offset (Integer) (defaults to: 0)

    indentation of the break

Returns:

  • (Nil)


108
109
110
# File 'lib/wadler/print.rb', line 108

def break(offset = 0)
  tokens << Oppen.line_break(offset:)
end

#breakable(str = ' ') ⇒ Nil

Parameters:

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

Returns:

  • (Nil)


101
102
103
# File 'lib/wadler/print.rb', line 101

def breakable(str = ' ')
  tokens << Oppen.break(str, 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 oppening 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)


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

def group(indent = 0, open_obj = '', close_obj = '',
          break_type = Oppen::Token::BreakType::CONSISTENT, &)
  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, break_type = Oppen::Token::BreakType::CONSISTENT, &block) ⇒ Nil

Parameters:

  • indent (Integer)

    nest indentation

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

    nest breaking type

Returns:

  • (Nil)


81
82
83
84
85
86
87
88
89
# File 'lib/wadler/print.rb', line 81

def nest(indent, break_type = Oppen::Token::BreakType::CONSISTENT, &block)
  @current_indent += indent

  raise LocalJumpError if !block_given?

  block.call
ensure
  @current_indent -= indent
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:, margin:, new_line:, config:, space:, out:)
end

#text(value) ⇒ Nil

Parameters:

  • value (String)

Returns:

  • (Nil)


94
95
96
# File 'lib/wadler/print.rb', line 94

def text(value)
  tokens << Oppen.string(value)
end