Module: Tracery

Included in:
TraceryNode, TraceryTests
Defined in:
lib/tracery.rb

Instance Method Summary collapse

Instance Method Details

#createGrammar(raw) ⇒ Object

Parses a plaintext rule in the tracery syntax



6
7
8
# File 'lib/tracery.rb', line 6

def createGrammar(raw)
    return Grammar.new(raw)
end

#createSection(start, finish, type, results, lastEscapedChar, escapedSubstring, rule, errors) ⇒ Object

TODO_: needs heavy refactoring – no nesting in ruby (ie. move entire parser to another class w/ shared state)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tracery.rb', line 45

def createSection(start, finish, type, results, lastEscapedChar, escapedSubstring, rule, errors)
    if(finish - start < 1) then
        if(type == 1) then
            errors << "#{start}: empty tag"
        else
            if(type == 2) then
                errors << "#{start}: empty action"
            end
        end
    end
    rawSubstring = ""
    if(!lastEscapedChar.nil?) then
        rawSubstring = escapedSubstring + "\\" + rule[(lastEscapedChar+1)..-1]
    else
        rawSubstring = rule[start...finish]
    end
    
    results[:sections] << {
            type: type,
            raw: rawSubstring
        }
end

#parse(rule) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tracery.rb', line 68

def parse(rule)
    depth = 0
    inTag = false
    results = {errors: [], sections: []}
    escaped = false
    
    errors = []
    start = 0
    
    escapedSubstring = ""
    lastEscapedChar = nil

    if(rule.nil?) then
        sections = {errors: errors, sections: []}
        return sections
    end
    
    rule.each_char.with_index do |c, i|
        if(!escaped) then
            case(c)
                when '[' then
                    # Enter a deeper bracketed section
                    if(depth == 0 && !inTag) then
                        if(start < i) then
                            createSection(start, i, 0, results, lastEscapedChar, escapedSubstring, rule, errors)
                            lastEscapedChar = nil
                            escapedSubstring = ""
                        end
                        start = i + 1
                    end
                    depth += 1
                when ']' then
                    depth -= 1
                    # End a bracketed section
                    if(depth == 0 && !inTag) then
                        createSection(start, i, 2, results, lastEscapedChar, escapedSubstring, rule, errors)
                        lastEscapedChar = nil
                        escapedSubstring = ""
                        start = i + 1
                    end
                when '#' then
                    # Hashtag
                    #   ignore if not at depth 0, that means we are in a bracket
                    if(depth == 0) then
                        if(inTag) then
                            createSection(start, i, 1, results, lastEscapedChar, escapedSubstring, rule, errors)
                            lastEscapedChar = nil
                            escapedSubstring = ""
                            start = i + 1
                        else
                            if(start < i) then
                                createSection(start, i, 0, results, lastEscapedChar, escapedSubstring, rule, errors)
                                lastEscapedChar = nil
                                escapedSubstring = ""
                            end
                            start = i + 1
                        end
                        inTag = !inTag
                    end
                when '\\' then
                    escaped = true;
                    escapedSubstring = escapedSubstring + rule[start...i];
                    start = i + 1;
                    lastEscapedChar = i;
            end
        else
            escaped = false
        end
    end #each character in rule
    
    if(start < rule.length) then
        createSection(start, rule.length, 0, results, lastEscapedChar, escapedSubstring, rule, errors)
        lastEscapedChar = nil
        escapedSubstring = ""
    end
    
    errors << ("Unclosed tag") if inTag
    errors << ("Too many [") if depth > 0
    errors << ("Too many ]") if depth < 0

    # Strip out empty plaintext sections
    results[:sections].select! {|section| 
        if(section[:type] == 0 && section[:raw].empty?) then
            false
        else
            true
        end
    }
    results[:errors] = errors;
    return results
end

#parseTag(tagContents) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tracery.rb', line 10

def parseTag(tagContents)
    parsed = {
            symbol: nil,
            preactions: [],
            postactions: [],
            modifiers: []
        }
    
    sections = parse(tagContents)[:sections]
    symbolSection = nil;
    sections.each do |section|
        if(section[:type] == 0) then
            if(symbolSection.nil?) then
                symbolSection = section[:raw]
            else
                raise "multiple main sections in #{tagContents}"
            end
        else
            parsed[:preactions].push(section)
        end
    end
    
    if(symbolSection.nil?) then
        # raise "no main section in #{tagContents}"
    else
        
        components = symbolSection.split(".");
        parsed[:symbol] = components.first
        parsed[:modifiers] = components.drop(1)
    end

    return parsed
end