Method: Ppr::Preprocessor#initialize

Defined in:
lib/ppr/ppr_core.rb

#initialize(params = {}, apply: ".do", applyR: ".doR", define: ".def", defineR: ".defR", assign: ".assign", loadm: ".load", requirem: ".require", ifm: ".if", elsem: ".else", endifm: ".endif", endm: ".end", expand: ":<", separator: /^|[^\w]|$/, glue: "##", escape: "\\") ⇒ Preprocessor

Creates a new preprocessor, where apply, applyR, define, defineR, assign, loadm, requirem, ifm, elsem and endm are the keywords defining the beginings and end of a macro definitions, and where separator is the regular expression used for separating macro references to the remaining of the code, expand is the string representing the expansion operator of the macro, glue is string used for glueing a macro expension to the text, escape is the escape character.

Assigned parameters can be added through param to be used within the macros of the preprocessed text.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/ppr/ppr_core.rb', line 386

def initialize(params = {},
               apply: ".do", applyR: ".doR", 
               define: ".def", defineR: ".defR",
               assign: ".assign",
               loadm: ".load", requirem: ".require",
               ifm: ".if", elsem: ".else", endifm: ".endif",
               endm: ".end",
               expand: ":<",
               separator: /^|[^\w]|$/, glue: "##",
               escape: "\\")
    # Check and Initialize the keywords
    # NOTE: since there are a lot of checks, use a generic but
    # harder to read code.
    keys = [ "apply", "applyR", "define", "defineR", "assign",
             "loadm", "requirem", "ifm", "elsem", "endifm", "endm"]
    # Sort the keywords by string content to quickly find erroneously
    # identical ones.
    keys.sort_by! {|key| eval(key) }
    # Check for identical keywords.
    keys.each_with_index do |key,i|
        value = eval(key)
        if i+1 < keys.size then
            # Check if the next keyword has the same string.
            nvalue = eval(keys[i+1])
            if value == nvalue then
                # Two keywords with same string.
                raise "'#{key}:#{value}' and '#{keys[i+1]}:#{nvalue}' keywords must be different."
            end
        end
    end

    # Seperate the begin of macro keywords from the others (they
    # are used differently).
    other_keys = ["elsem", "endifm", "endm"]
    begin_keys = keys - other_keys
    # Assign the begin of macro keywords to the corresponding attributes.
    begin_keys.each do |key|
        eval("@#{key} = #{key}.to_s")
    end
    # Generates the structures used for detecting the keywords.
    # For the begining of macros.
    @macro_keys = (begin_keys - other_keys).map do
        |key| self.instance_variable_get("@#{key}")
    end.sort!.reverse
    # For the other keywords.
    other_keys.each do |key|
        eval('@'+key+' = Regexp.new("^\s*#{Regexp.escape('+key+')}\s*$")')
    end

    # Sets the expand command.
    @expand = expand.to_s
    # Check and set the separator, the glue and the escape.
    @separator = Regexp.new("(?:#{separator}|#{glue})")
    @glue = glue.to_s

    # Initialize the current line number to 0.
    @number = LineNumber.new(0)
    # Initialize the macros.
    @macros = KeywordSearcher.new(@separator)

    # Initialize the stack for handling the if macros.
    @if_mode = []

    # Create the execution context for the macros.
    @generator = SaferGenerator.new
    @context = Object.new
    # Process the preprocessing parameters.
    params.each do |k,v|
        parameter_set(k,v)
    end
end