Module: Tracery
- Included in:
- TraceryNode, TraceryTests
- Defined in:
- lib/tracery.rb,
lib/version.rb
Constant Summary collapse
- VERSION =
"0.7.7"
- @@internal_rnd =
handle random and replacement random
Random.new
- @@rnd_proc =
lambda { @@internal_rnd.rand }
Class Method Summary collapse
Instance Method Summary collapse
-
#createGrammar(raw) ⇒ Object
Parses a plaintext rule in the tracery syntax.
-
#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).
- #parse(rule) ⇒ Object
- #parseTag(tagContents) ⇒ Object
Class Method Details
.random ⇒ Object
21 22 23 |
# File 'lib/tracery.rb', line 21 def self.random return @@rnd_proc.call end |
.resetRnd ⇒ Object
17 18 19 |
# File 'lib/tracery.rb', line 17 def self.resetRnd setRnd(lambda { @@internal_rnd.rand }) end |
.setRnd(lambdaproc) ⇒ Object
13 14 15 |
# File 'lib/tracery.rb', line 13 def self.setRnd(lambdaproc) @@rnd_proc = lambdaproc end |
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)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tracery.rb', line 60 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)...finish] else rawSubstring = rule[start...finish] end results[:sections] << { type: type, raw: rawSubstring } end |
#parse(rule) ⇒ Object
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/tracery.rb', line 83 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tracery.rb', line 25 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 |