Module: Oppen

Extended by:
Mixins
Defined in:
lib/oppen.rb,
lib/oppen/token.rb,
lib/oppen/mixins.rb,
lib/wadler/print.rb,
lib/oppen/printer.rb,
lib/oppen/version.rb,
lib/oppen/scan_stack.rb,
lib/oppen/print_stack.rb

Overview

Oppen.

Defined Under Namespace

Modules: Mixins Classes: Config, PrintStack, Printer, ScanStack, Token, Wadler

Constant Summary collapse

VERSION =

Oppen version

Returns:

  • (String)

    current version

'0.9.7'

Class Method Summary collapse

Methods included from Mixins

tokens_to_wadler, upsize_circular_array

Class Method Details

.begin_consistent(offset: 2) ⇒ Oppen::Token::Begin

Returns a new consistent Begin token.

Parameters:

  • offset (Integer) (defaults to: 2)

Returns:



146
147
148
# File 'lib/oppen.rb', line 146

def self.begin_consistent(offset: 2)
  Token::Begin.new(break_type: Token::BreakType::CONSISTENT, offset:)
end

.begin_inconsistent(offset: 2) ⇒ Oppen::Token::Begin

Returns a new inconsistent Begin token.

Parameters:

  • offset (Integer) (defaults to: 2)

Returns:



153
154
155
# File 'lib/oppen.rb', line 153

def self.begin_inconsistent(offset: 2)
  Token::Begin.new(break_type: Token::BreakType::INCONSISTENT, offset:)
end

.break(str = ' ', line_continuation: '', offset: 0, width: str.length) ⇒ Oppen::Token::Break

Returns a new Break token.

Parameters:

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

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

  • offset (Integer) (defaults to: 0)
  • width (Integer) (defaults to: str.length)

    token width that defaults to str.length

Returns:



131
132
133
# File 'lib/oppen.rb', line 131

def self.break(str = ' ', line_continuation: '', offset: 0, width: str.length)
  Token::Break.new(str, width:, line_continuation:, offset:)
end

.endOppen::Token::End

Returns a new End token.

Returns:



158
159
160
# File 'lib/oppen.rb', line 158

def self.end
  Token::End.new
end

.eofOppen::Token::EOF

Returns a new EOF token.

Returns:



163
164
165
# File 'lib/oppen.rb', line 163

def self.eof
  Token::EOF.new
end

.line_break(line_continuation: '', offset: 0) ⇒ Oppen::Token::LineBreak

Returns a new LineBreak token.

Parameters:

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

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

  • offset (Integer) (defaults to: 0)

Returns:



139
140
141
# File 'lib/oppen.rb', line 139

def self.line_break(line_continuation: '', offset: 0)
  Token::LineBreak.new(line_continuation:, offset:)
end

Entry point of the pretty printer.

Parameters:

  • config (Config) (defaults to: Config.oppen)
  • 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")

    the delimiter between lines

  • out (Object) (defaults to: StringIO.new)

    should have a write and string method

  • tokens (Array[Token]) (defaults to: [])

    the list of tokens to be printed

  • width (Integer) (defaults to: 80)

    maximum line width desired

Returns:

  • (String)

    output of the pretty printer



30
31
32
33
34
35
36
37
# File 'lib/oppen.rb', line 30

def self.print(config: Config.oppen, space: ' ',
               new_line: "\n", out: StringIO.new, tokens: [], width: 80)
  printer = Printer.new width, new_line, config, space, out
  tokens.each do |token|
    printer.print token
  end
  printer.output
end

.string(value, width: value.length) ⇒ Oppen::Token::String

Returns a new String token.

Parameters:

  • value (String)
  • width (Integer) (defaults to: value.length)

    token width that defaults to value.length

Returns:



114
115
116
# File 'lib/oppen.rb', line 114

def self.string(value, width: value.length)
  Token::String.new(value, width:)
end

.whitespace(value) ⇒ Oppen::Token::Whitespace

Returns a new Whitespace token.

Returns:

See Also:



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

def self.whitespace(value)
  Token::Whitespace.new(value, width: value.bytesize)
end