module Ribosome

The initial part of this file belongs to the ribosome project.

Copyright © 2014 Martin Sustrik All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Public Class Methods

add(line, bind) click to toggle source

Adds one . line from the DNA file.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 197
def Ribosome.add(line, bind)

    # If there is no previous line, add one.
    if(@stack.last.empty?)
        @stack.last << Block.new("")
    end

    # In this block we will accumulate the expanded line.
    block = @stack.last.last

    # Traverse the line and convert it into a block.
    i = 0
    while true
        j = line.index(/[@&][1-9]?\{/, i)
        j = line.size if j == nil

        # Process constant block of text.
        if (i != j)
            block.add_right(Block.new(line[i..j - 1]))
        end

        break if line.size == j

        # Process an embedded expression.
        i = j
        j += 1
        level = 0
        if (line[j] >= ?1 && line[j] <= ?9)
            level = line[j].to_i
            j += 1
        end

        # Find corresponding }.
        par = 0;
        while true
            if(line[j] == ?{)
                par += 1
            elsif(line[j] == ?})
                par -= 1
            end
            break if par == 0
            j += 1
            if j >= line.size
                raise SyntaxError.new("Unmatched {")
            end
        end

        # Expression of higher indirection levels are simply brought
        # down by one level.
        if(level > 0)
            if line [i + 1] == ?1
                block.add_right(Block.new("@" + line[i + 2..j]))
            else
                line[i + 1] = (line [i + 1].to_i - 1).chr
                block.add_right(Block.new(line[i..j]))
            end
            i = j + 1
            next
        end

        # We are at the lowest level of embeddedness so we have to
        # evaluate the embedded expression straight away.
        expr = line[(level == 0 ? i + 2 : i + 3)..j - 1]
        @stack.push([])
        val = eval(expr, bind)
        top = @stack.pop()
        if(top.empty?)
            val = Block.new(val.to_s)
        else
            val = Block.new("")
            for b in top
                val.add_bottom(b)
            end
        end
        val.trim if line[i] == ?@
        block.add_right(val)
        i = j + 1
    end
end
align(line, bind) click to toggle source

Adds newline followed by leading whitespace copied from the previous line and one line from the DNA file.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 285
def Ribosome.align(line, bind)
    if @stack.last.empty?
        n = 0
    else
        n = @stack.last.last.last_offset
    end
    @stack.last << Block.new("")
    add(" " * n, nil)
    add(line, bind)
end
append(filename) click to toggle source

Redirects output to the specified file. New stuff is added to the existing content of the file.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 169
def Ribosome.append(filename)
    close()
    @outisafile = true
    @out = File.open(filename, "a")
end
close() click to toggle source

Flush the data to the currently open file and close it.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 188
def Ribosome.close()
    for b in @stack.last
        b.write(@out, @tabsize)
    end
    @stack = [[]]
    @out.close() if @outisafile
end
dot(line, bind) click to toggle source

Adds newline followed by one . line from the DNA file.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 278
def Ribosome.dot(line, bind)
    @stack.last << Block.new("")
    add(line, bind)
end
output(filename) click to toggle source

Redirects output to the specified file.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 161
def Ribosome.output(filename)
    close()
    @outisafile = true
    @out = File.open(filename, "w")
end
rethrow(e, rnafile, linemap) click to toggle source

Report an error that happened when executing RNA file.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 297
def Ribosome.rethrow(e, rnafile, linemap)
    i = 0
    for i in 0..e.backtrace.size - 1
        l = e.backtrace[i]
        if l.start_with?(rnafile + ":")
            stop = l.index(":", rnafile.size + 1) || l.size
            num = l[rnafile.size + 1..stop - 1].to_i
            for j in 0..linemap.size - 1
                break if linemap[j][0] == nil || linemap[j][0] > num
            end
            j -= 1
            num = num - linemap[j][0] + linemap[j][2]
            l = "#{linemap[j][1]}:#{num}#{l[stop..-1]}"
            e.backtrace[i] = l
        end
    end
    raise e
end
stdout() click to toggle source

Redirects output to the stdout.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 176
def Ribosome.stdout()
    close()
    @outisafile = false
    @out = $stdout
end
tabsize(size) click to toggle source

Sets the size of the tab.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 183
def Ribosome.tabsize(size)
    @tabsize = size
end