Class: CBS::Compiler
- Inherits:
-
Object
- Object
- CBS::Compiler
- Defined in:
- lib/cbs.rb
Instance Attribute Summary collapse
-
#bindings ⇒ Object
readonly
Returns the value of attribute bindings.
Instance Method Summary collapse
- #format ⇒ Object
-
#initialize(io) ⇒ Compiler
constructor
A new instance of Compiler.
- #match(string) ⇒ Object
- #parse ⇒ Object
- #parseBlock(terminator, level, code = '') ⇒ Object
- #parseClass(metaclass, initializer, switchNames) ⇒ Object
- #parseClassBlock ⇒ Object
- #parseFunction(name) ⇒ Object
- #parseString(terminator, string) ⇒ Object
- #readUntil(delimiters) ⇒ Object
Constructor Details
#initialize(io) ⇒ Compiler
Returns a new instance of Compiler.
11 12 13 14 |
# File 'lib/cbs.rb', line 11 def initialize(io) io = StringIO.new(io) if io.is_a? String @io = io end |
Instance Attribute Details
#bindings ⇒ Object (readonly)
Returns the value of attribute bindings.
9 10 11 |
# File 'lib/cbs.rb', line 9 def bindings @bindings end |
Instance Method Details
#format ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/cbs.rb', line 167 def format code = "" code << @classes.inject('') do |c, klass| c << klass.format c end code << "\n" code << "Event.addBehavior({\n" code << @bindings.inject('') do |c, binding| c << binding.format c << ",\n" unless @bindings.last === binding c end code << "\n});\n" code end |
#match(string) ⇒ Object
160 161 162 163 164 165 |
# File 'lib/cbs.rb', line 160 def match(string) pos = @io.pos return true if @io.read(string.length) == string @io.pos = pos return false end |
#parse ⇒ Object
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 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cbs.rb', line 16 def parse @selectors = Array.new @bindings = Array.new @classes = Array.new selector = '' while (char = @io.getc) case char when ?, @selectors << selector.strip selector = '' when ?{ @selectors << selector.strip selector = '' @bindings << BlockBinding.new(@selectors, parseBlock(?}, 1, "{")) @selectors = Array.new when ?% @selectors << selector.strip selector = '' @bindings << ObjectBinding.new(@selectors, parseBlock(?;, 0).sub(/\s*;$/, '')) @selectors = Array.new when ?@ if match('behavior') @classes << parseClass('Behavior', 'create', false) elsif match('class') @classes << parseClass('Class', 'create', false) elsif match('extention') @classes << parseClass('_empty_', 'addMethods', true) end else selector << char end end end |
#parseBlock(terminator, level, code = '') ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cbs.rb', line 112 def parseBlock(terminator, level, code='') eob = false while (!eob) and (char = @io.getc) case char when ?' code << parseString(?', "'") when ?" code << parseString(?", '"') when ?{ level += 1 code << char when ?} level -= 1 code << char else code << char end eob = true if char == terminator and level == 0 end code.strip.gsub(/\n/, "\n ") end |
#parseClass(metaclass, initializer, switchNames) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cbs.rb', line 57 def parseClass(, initializer, switchNames) name = readUntil(':{').strip superclass = nil functions = nil char = @io.getc if char == ?: superclass = readUntil('{').strip char = @io.getc end if char == ?{ functions = parseClassBlock end if switchNames = name superclass = nil name = nil end Container.new(name, superclass, , initializer, functions) end |
#parseClassBlock ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cbs.rb', line 77 def parseClassBlock eob = false functions = [] until eob readUntil('}\':abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$') case (char = @io.getc) when nil then eob = true when ?} then eob = true else functions << parseFunction("%c" % char) end end return functions end |
#parseFunction(name) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/cbs.rb', line 92 def parseFunction(name) name += readUntil('{(').strip arguments = '' block = nil char = @io.getc if char == ?( arguments += '(' arguments += readUntil(')') arguments += @io.read(1) readUntil('{') char = @io.getc else arguments = '()' end if char == ?{ block = parseBlock(?}, 1, '{') end Function.new(name, arguments, block) end |
#parseString(terminator, string) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/cbs.rb', line 134 def parseString(terminator, string) eob = false while (!eob) and (char = @io.getc) case char when ?\\ string << @io.getc else string << char end eob = true if char == terminator end string end |
#readUntil(delimiters) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/cbs.rb', line 148 def readUntil(delimiters) string = '' while char = @io.getc if delimiters.include? char @io.pos = @io.pos - 1 return string end string << char end string end |