|
| 1 | +#!ruby |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# js_interpreter_generator.rb |
| 5 | +# |
| 6 | +# Created by Erik Österlund on 1/14/10. |
| 7 | +# Copyright 2010 Växjö Universitet. All rights reserved. |
| 8 | +# |
| 9 | + |
| 10 | +require 'table_generator' |
| 11 | +require 'parser' |
| 12 | +require 'set' |
| 13 | +require 'digest' |
| 14 | +require 'js_compiler' |
| 15 | +require 'js_parser' |
| 16 | +require 'js_lexer' |
| 17 | +require 'js_interpreter_generator_single' |
| 18 | + |
| 19 | +class String |
| 20 | + def here_with_pipe |
| 21 | + lines = self.split("\n") |
| 22 | + lines.map! {|c| c.sub!(/\s*\|/, '')} |
| 23 | + new_string = lines.join("\n") |
| 24 | + self.replace(new_string) |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +module LALR |
| 29 | + |
| 30 | + class InterpreterGenerator |
| 31 | + include Marshal |
| 32 | + |
| 33 | + def compiler(arg) |
| 34 | + @compiler = yield.gsub(/\n\s*/m, "") |
| 35 | + @compiler_name = arg |
| 36 | + end |
| 37 | + |
| 38 | + def header() |
| 39 | + @header = yield |
| 40 | + end |
| 41 | + |
| 42 | + def footer() |
| 43 | + @footer = yield |
| 44 | + end |
| 45 | + |
| 46 | + def comment(arg) |
| 47 | + @comments << arg |
| 48 | + end |
| 49 | + |
| 50 | + def commentAmbiguity(arg) |
| 51 | + @commentAmbiguities << arg |
| 52 | + end |
| 53 | + |
| 54 | + def token(arg, literal = nil) |
| 55 | + reg = fix_regex arg if arg.is_a? Regexp |
| 56 | + @token_lit[literal] = @tokens.length if literal |
| 57 | + end |
| 58 | + |
| 59 | + def rule(arg) |
| 60 | + arg.each do |k,v| |
| 61 | + if v.is_a? Array |
| 62 | + v.size.times do |i| |
| 63 | + v[i] = fix_regex v[i] if v[i].is_a? Regexp |
| 64 | + end |
| 65 | + @rules << Rule.new(k, v) |
| 66 | + else |
| 67 | + v = fix_regex v if v.is_a? Regexp |
| 68 | + @rules << Rule.new(k, [v]) |
| 69 | + end |
| 70 | + end |
| 71 | + @rule_evaluators << yield.gsub(/\$(\d+)/m){|m|"this.$#{m[1].to_i}.$0"}. |
| 72 | + gsub(/\${2}/m, 'this.$0').gsub(/\@(\d+)/m){|m|"this.$#{m[1].to_i}"}. |
| 73 | + gsub(/\@{2}/m, 'this').gsub(/\n\s*/m, "") |
| 74 | + end |
| 75 | + |
| 76 | + def start(arg) |
| 77 | + @start = arg |
| 78 | + end |
| 79 | + |
| 80 | + def initialize(arg, file) |
| 81 | + @rules = [] |
| 82 | + @rule_evaluators = [] |
| 83 | + @tokens = [] |
| 84 | + @token_lit = {} |
| 85 | + @comments = [] |
| 86 | + @commentAmbiguities = [] |
| 87 | + @name, @file = arg, file |
| 88 | + @compiler = "" |
| 89 | + @compiler_name = nil |
| 90 | + end |
| 91 | + |
| 92 | + def symbol_to_index |
| 93 | + symbols = Set.new |
| 94 | + @rules.each do |rule| |
| 95 | + symbols << rule.name |
| 96 | + end |
| 97 | + symbols = symbols.to_a |
| 98 | + sm = {} |
| 99 | + symbols.size.times do |i| |
| 100 | + sm[symbols[i]] = i |
| 101 | + end |
| 102 | + sm |
| 103 | + end |
| 104 | + |
| 105 | + def generate_interpreter |
| 106 | + our_dump = dump(@rules.map{|rule|rule.name.to_s + rule.productions.to_s}) + |
| 107 | + dump(@start) + dump(@tokens.sort{|a, b|a.source <=> b.source}) + dump(@comments.sort{|a, b|a.source <=> b.source}) |
| 108 | + current_dump = "" |
| 109 | + File.open(@file + ".grammar", "r"){|f| |
| 110 | + current_dump = f.read |
| 111 | + } if File.exists?(@file + ".grammar") |
| 112 | + |
| 113 | + if Digest::SHA256.digest(our_dump) == Digest::SHA256.digest(current_dump) and |
| 114 | + File.exists?(@file + ".gotos") and File.exists?(@file + ".actions") |
| 115 | + File.open(@file + ".gotos", "r"){|f| |
| 116 | + @gotos = Marshal.restore(f.read()) |
| 117 | + } |
| 118 | + File.open(@file + ".actions", "r"){|f| |
| 119 | + @actions = Marshal.restore(f.read()) |
| 120 | + } |
| 121 | + @rules.insert 0, Rule.new(:S, [@start]) |
| 122 | + else |
| 123 | + generator = TableGenerator.new(@rules, @start) |
| 124 | + @actions, @gotos = generator.generate_tables |
| 125 | + |
| 126 | + File.open(@file + ".grammar", "w"){|f| |
| 127 | + f.write(our_dump) |
| 128 | + } |
| 129 | + File.open(@file + ".gotos", "w"){|f| |
| 130 | + f.write(Marshal.dump(@gotos)) |
| 131 | + } |
| 132 | + File.open(@file + ".actions", "w"){|f| |
| 133 | + f.write(Marshal.dump(@actions)) |
| 134 | + } |
| 135 | + end |
| 136 | + |
| 137 | + File.open(@file + ".js", "w"){|f| |
| 138 | + f.write generate_interface |
| 139 | + } |
| 140 | + File.open(@file + "_lexer.js", "w"){|f| |
| 141 | + f.write generate_lexer |
| 142 | + } |
| 143 | + File.open(@file + "_parser.js", "w"){|f| |
| 144 | + f.write generate_parser |
| 145 | + } |
| 146 | + File.open(@file + "_single.js", "w"){|f| |
| 147 | + f.write generate_interface_single |
| 148 | + } |
| 149 | + end |
| 150 | + |
| 151 | + private |
| 152 | + def fix_regex(reg) |
| 153 | + r = Regexp.new("(" + reg.source + ")", Regexp::MULTILINE) |
| 154 | + @tokens << r unless @tokens.include? r |
| 155 | + r |
| 156 | + end |
| 157 | + |
| 158 | + def generate_interface |
| 159 | + <<-end.here_with_pipe |
| 160 | + |function #{@name}_compiler(str, path, bundle, callback) |
| 161 | + |{ |
| 162 | + |this._lexer = new Worker('#{@file + "_lexer.js"}'); |
| 163 | + |this._parser = new Worker('#{@file + "_parser.js"}'); |
| 164 | + |var parser = this._parser; |
| 165 | + |this._executable = null; |
| 166 | + |this._code = str; |
| 167 | + |this._dependencies = []; |
| 168 | + |var dependencies = this._dependencies; |
| 169 | + |this._bundle = bundle; |
| 170 | + |this._path = path; |
| 171 | + | |
| 172 | + |this._lexer.onmessage = function(message) |
| 173 | + |{ |
| 174 | + |parser.postMessage(message.data); |
| 175 | + |} |
| 176 | + | |
| 177 | + |this._parser.onmessage = function(message) |
| 178 | + |{ |
| 179 | + |callback(new Executable(message.data, dependencies)); |
| 180 | + |} |
| 181 | + |} |
| 182 | + |#{@name}_compiler.prototype.parse = function() |
| 183 | + |{ |
| 184 | + |this._start = new Date(); |
| 185 | + |this._startLexing = this._start; |
| 186 | + |this._lexer.postMessage(this._code); |
| 187 | + |} |
| 188 | + |function preprocess(str, path, bundle, callback) |
| 189 | + |{ |
| 190 | + |new #{@name}_compiler(str, path, bundle, callback).parse(); |
| 191 | + |} |
| 192 | + end |
| 193 | + end |
| 194 | + |
| 195 | + end |
| 196 | + |
| 197 | +end |
0 commit comments