Class: Component

Inherits:
Object
  • Object
show all
Defined in:
lib/mulparse/component.rb

Overview

TODO:

* Expand as needed to fit expanding lang config and
 parser.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(next_start = nil, parent = nil, name: nil, start: nil, finish: nil, unestable: nil, hash: nil) ⇒ Component



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mulparse/component.rb', line 42

def initialize(
    next_start=nil,
    parent=nil,
    name: nil,
    start: nil,
    finish: nil,
    unestable: nil,
    hash: nil )

    #   Initialize the ComponentNode with the

    #   data provided.


    @start_match = next_start # Store the component's opening match


    # Set up lang config attributes


    @name = name
    
    @start = start
    @finish = finish
    
    @unestable = unestable

    @hash = hash

    @parent = parent # Set the ComponentNode's parent

    @parent.children << self if parent

    @children = []; # Set the children of this ComponentNode to an empty array


end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



29
30
31
# File 'lib/mulparse/component.rb', line 29

def children
  @children
end

#finishObject (readonly)

Returns the value of attribute finish.



36
37
38
# File 'lib/mulparse/component.rb', line 36

def finish
  @finish
end

#finish_matchObject

Returns the value of attribute finish_match.



24
25
26
# File 'lib/mulparse/component.rb', line 24

def finish_match
  @finish_match
end

#hashObject (readonly)

Returns the value of attribute hash.



40
41
42
# File 'lib/mulparse/component.rb', line 40

def hash
  @hash
end

#nameObject (readonly)

Lang config



33
34
35
# File 'lib/mulparse/component.rb', line 33

def name
  @name
end

#parentObject (readonly)

Hierarchial



28
29
30
# File 'lib/mulparse/component.rb', line 28

def parent
  @parent
end

#startObject (readonly)

Returns the value of attribute start.



35
36
37
# File 'lib/mulparse/component.rb', line 35

def start
  @start
end

#start_matchObject

Delimiters



23
24
25
# File 'lib/mulparse/component.rb', line 23

def start_match
  @start_match
end

#unestableObject (readonly)

Returns the value of attribute unestable.



38
39
40
# File 'lib/mulparse/component.rb', line 38

def unestable
  @unestable
end

Instance Method Details

#get_src(src) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mulparse/component.rb', line 92

def get_src( src )

    # Return the matches and the content between

    # them in the src provided.


    begin
        return src[ ( @start_match.begin( 0 )...@finish_match.end( 0 ) ) ]
    rescue NoMethodError
        return @start_match[0]
    end

end

#has_sibling_at?(offset = 1) ⇒ Boolean



83
84
85
86
87
88
89
90
# File 'lib/mulparse/component.rb', line 83

def has_sibling_at?( offset=1 )
    
    # Returns true if a call to sibling() with the

    # same parameter would run without error.


    return @parent.children.index( self ) + offset < @parent.children.length

end

#sibling(offset = 1) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/mulparse/component.rb', line 74

def sibling( offset=1 )

    # Returns the sibling offset by the

    # provided parameter.


    return @parent.children[ @parent.children.index( self ) + offset ]

end

#to_sObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mulparse/component.rb', line 105

def to_s()

    # Return a string representation of this Component


    if @start_match then

        abbreviated_start_match = @start_match[0][0..10]
        abbreviated_start_match += "..." if @start_match.length > 11

    end

    return "Component(\n" \
        "    start_match=%p,\n" \
        "    parent.name=%p,\n" \
        "    name=%p,\n" \
        "    start=%s,\n" \
        "    finish=%s,\n" \
        "    unestable=%p\n" \
        ")" % [
            abbreviated_start_match,
            @parent&.name,
            @name,
            @start,
            @finish || "nil",
            @unestable
        ]

end