Class: Nodepile::Pragmas

Inherits:
Object
  • Object
show all
Defined in:
lib/nodepile/pragmas.rb

Overview

Pragmas is a parser and organizer class used to interpret the meaning of pragmas that may appear in source files. By default, pragmas are coded instructions for rendering, parsing, or layout that may be embedded in input files. They are often instructions stored in the “_id” field of an input file that begin with a specific indicating string ‘#pragma ’ Pragmas may be used to control things like the specific layout engine that is used to visualize a graph (e.g. dot versus neato) Example pragma lines are.

#pragma neato
#pragma unflatten

Create an instance of a Nodepile::Pragmas object in order to track and interpret the collective pragmas of a given graph. Note that the most common rule in #pragma interpretation is that if two pragmas contradict each other, the furthest down in the file/parsing stream dominates.

Constant Summary collapse

DEFAULT_PRAGMA_MARKER =
"#pragma "
@@indicator_patterns =

array of [pragma_sym,regexp]

Array.new

Instance Method Summary collapse

Constructor Details

#initialize(pragma_marker: DEFAULT_PRAGMA_MARKER) ⇒ Pragmas

Returns a new instance of Pragmas.



26
27
28
29
30
# File 'lib/nodepile/pragmas.rb', line 26

def initialize(pragma_marker: DEFAULT_PRAGMA_MARKER)
    @marker = pragma_marker.freeze
    @indicators = Hash.new  #name mapped to value
    # if you make this method more complex, remember to update #dup
end

Instance Method Details

#[](pragma_sym) ⇒ Object



38
39
40
# File 'lib/nodepile/pragmas.rb', line 38

def [](pragma_sym)  
    @indicators[pragma_sym]
end

#dupObject



32
33
34
35
36
# File 'lib/nodepile/pragmas.rb', line 32

def dup
    c = self.class.new(pragma_marker: @marker)
    c._indicators.merge!(@indicators)  
    return c
end

#each_setting_pair {|pragma_sym, pragma_val| ... } ⇒ Object

Yields:

  • (pragma_sym, pragma_val)

    provides pairs of name values as they have been set. Only set values will appear in the yielded set.



44
45
46
47
48
# File 'lib/nodepile/pragmas.rb', line 44

def each_setting_pair
    return enum_for(__method__) unless block_given
    @indicators.each_pair{|k,v| yield(k,v) }
    return @indicators.length
end

#parse(pragma_string) ⇒ void

This method returns an undefined value.

Parse the given pragma and store the meaning for access via square bracket method or the #each_setting_pair method.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nodepile/pragmas.rb', line 53

def parse(pragma_string)
    raise "Expecting pragma_string to start with [#{@marker}]" unless pragma_string.start_with?(@marker)
    
    #TODO: I there are more complicated parsing rules, they should go before
    #      the simple fallthrough case of whitespace separated indicators
    
    # Simple indicators are single "word" values where each value is delimited
    # by whitespace.  If two indicators apply to the same pragma_sym,
    pragma_string[@marker.length..-1].split(/\s+/).each{|s| 
                            prag_sym,_ = @@indicator_patterns.find{|(prag_sym,rx)| rx.match(s) }
                            if prag_sym
                                @indicators[prag_sym] = $1
                            else
                                raise "Unrecognized pragma encountered [#{s}]"
                            end
                           }
    return nil
end