Module: PFM::Message::Parser

Included in:
State, UI::Message::Window
Defined in:
scripts/01450 Systems/00004 Message/00001 Logic/00001 Parser.rb

Overview

Module parsing a message to a set of instruction the message displayer can show

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.code_replacerArray<Proc> (readonly)

Get the list of code replacer

Returns:

  • (Array<Proc>)


33
34
35
# File 'scripts/01450 Systems/00004 Message/00001 Logic/00001 Parser.rb', line 33

def code_replacer
  @code_replacer
end

Class Method Details

.register_marker(regexp, code) {|captures| ... }

Note:

If no block is given, the function will assume the regexp has 1 match

Register a text marker to make text easier to parse

Parameters:

  • regexp (Regexp)

    regexp to parse

  • code (Integer)

    code to use for easier parsing (must be positive integer)

Yield Parameters:

  • captures (Array<String>)

    list of all captures from the regexp

Yield Returns:

  • (Array)

    array of element not containing “[” or “]”



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'scripts/01450 Systems/00004 Message/00001 Logic/00001 Parser.rb', line 41

def register_marker(regexp, code)
  if block_given?
    @code_replacer[code] = proc do |text|
      text.gsub!(regexp) do
        parameters = yield(Regexp.last_match.captures)
        parameters = [parameters] unless parameters.is_a?(Array)
        next "#{code.chr}[#{parameters.join(',')}]"
      end
    end
  else
    @code_replacer[code] = proc { |text| text.gsub!(regexp, "#{code.chr}[\\1]") }
  end
end

Instance Method Details

#convert_text_to_properties(text) ⇒ Message::Properties

Function that parses the message text so it's easier to work with it

Parameters:

  • text (String)

    original message text

Returns:



14
15
16
17
18
# File 'scripts/01450 Systems/00004 Message/00001 Logic/00001 Parser.rb', line 14

def convert_text_to_properties(text)
  parsed_text = Text.parse_string_for_messages(text.dup).dup
  Parser.code_replacer.each { |replacer| replacer&.call(parsed_text) }
  return Properties.new(parsed_text)
end

#make_instructions(properties, width, width_computer)

Function that generate the instructions based on properties & text surface

Parameters:

  • properties (Properties)

    Message box properties

  • width (Integer)

    width of the surface used to draw the message

  • width_computer (WidthComputer)

    object helping to compute the width of the words



24
25
26
27
28
# File 'scripts/01450 Systems/00004 Message/00001 Logic/00001 Parser.rb', line 24

def make_instructions(properties, width, width_computer)
  instructions = Instructions.new(properties, width, width_computer)
  instructions.parse
  return instructions
end