Class: Ruco::Group
- Inherits:
-
Object
- Object
- Ruco::Group
- Defined in:
- lib/ruco.rb
Direct Known Subclasses
Instance Method Summary collapse
- #convert_thing(thing) ⇒ Object
- #either(*args) ⇒ Object
- #generate(indent = 0) ⇒ Object
- #grammar_tree ⇒ Object
- #group(&block) ⇒ Object
- #group_type ⇒ Object
-
#initialize(type = :normal, prodset = {}) ⇒ Group
constructor
A new instance of Group.
- #many(thing, options = nil) ⇒ Object
- #maybe(thing) ⇒ Object
- #maybemany(thing, options = nil) ⇒ Object
- #multiply ⇒ Object
- #one(thing) ⇒ Object
- #sync ⇒ Object
Constructor Details
#initialize(type = :normal, prodset = {}) ⇒ Group
Returns a new instance of Group.
69 70 71 72 73 |
# File 'lib/ruco.rb', line 69 def initialize(type=:normal, prodset={}) @type = type @prodset = prodset @stuff = [] end |
Instance Method Details
#convert_thing(thing) ⇒ Object
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 |
# File 'lib/ruco.rb', line 79 def convert_thing(thing) if thing.is_a? String return LitString.new(thing, @prodset) end if thing.is_a? Identifier or thing.is_a? Variation thing.prodset = @prodset if !@prodset[thing.name] @prodset[thing.name] = {count: 0, type: :id} end @prodset[thing.name][:count] += 1 @prodset[thing.name][:count] += 1 unless @type == :normal or @type == :either end if thing.is_a? Token if !@prodset[thing.name] @prodset[thing.name] = {count: 0, type: :token} end @prodset[thing.name][:count] += 1 @prodset[thing.name][:count] += 1 unless @type == :normal or @type == :either end return thing end |
#either(*args) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ruco.rb', line 133 def either(*args) g = Group.new :either, @prodset g.instance_eval do args.each do |x| one x end end one g end |
#generate(indent = 0) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/ruco.rb', line 189 def generate(indent=0) result = [] @stuff.each do |x| result << "#{x.generate(indent+1)}" end openbrace = "(" closebrace = ")" divider = "" openbrace = "[" if @type == :maybe closebrace = "]" if @type == :maybe openbrace = "{" if @type == :multiple closebrace = "}" if @type == :multiple divider = "|" if @type == :either ("\t"*indent) + openbrace + "\n" + result.join("#{divider}\n") + "\n" + ("\t"*indent) + closebrace end |
#grammar_tree ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ruco.rb', line 110 def grammar_tree @stuff.map do |x| if x.is_a? LitString {type: "terminal", token: x.str} elsif x.is_a? Group {type: "group", sort: x.group_type, inner: x.grammar_tree} elsif x.is_a? Identifier or x.is_a? Variation or x.is_a? Token {type: "nonterminal", ident: x.name} else {type: "unknown", x: "*****************"} end end end |
#group(&block) ⇒ Object
183 184 185 186 187 |
# File 'lib/ruco.rb', line 183 def group(&block) g = Group.new :normal, @prodset g.instance_eval(&block) g end |
#group_type ⇒ Object
106 107 108 |
# File 'lib/ruco.rb', line 106 def group_type @type end |
#many(thing, options = nil) ⇒ Object
153 154 155 156 |
# File 'lib/ruco.rb', line 153 def many(thing, =nil) one thing maybemany thing, end |
#maybe(thing) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/ruco.rb', line 144 def maybe(thing) g = Group.new :maybe, @prodset g.instance_eval do one thing end g.multiply one g end |
#maybemany(thing, options = nil) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/ruco.rb', line 158 def maybemany(thing, =nil) g = Group.new :multiple, @prodset g.instance_eval do if [:separator].is_a? String one [:separator] else puts "Separator needs to be a simple string" end if one thing end g.multiply one g end |
#multiply ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/ruco.rb', line 172 def multiply @stuff.each do |x| if x.is_a? Group x.multiply elsif x.is_a? LitString else @prodset[x.name][:count] += 1 end end end |
#one(thing) ⇒ Object
128 129 130 131 |
# File 'lib/ruco.rb', line 128 def one(thing) thing = convert_thing(thing) @stuff << thing end |
#sync ⇒ Object
124 125 126 |
# File 'lib/ruco.rb', line 124 def sync @stuff << Sync.new end |