-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_interpreter_generator_single.rb
100 lines (93 loc) · 3.08 KB
/
js_interpreter_generator_single.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!ruby
# coding: utf-8
#
# js_interpreter_generator.rb
#
# Created by Erik Österlund on 1/14/10.
# Copyright 2010 Växjö Universitet. All rights reserved.
#
require 'table_generator'
require 'parser'
require 'set'
require 'digest'
require 'js_compiler_single'
require 'js_parser_single'
require 'js_lexer_single'
class String
def here_with_pipe
lines = self.split("\n")
lines.map! {|c| c.sub!(/\s*\|/, '')}
new_string = lines.join("\n")
self.replace(new_string)
end
end
module LALR
class InterpreterGenerator
def generate_interface_single
result = <<-end.here_with_pipe
|#{@header.gsub(/\n\s*/, "")}
|function Preprocessor(str, url, flags)
|{
|this._start = new Date().getTime();
|this._lexer = new #{@name}_lexer(str);
|this._parser = new #{@name}_parser();
|this._executable = null;
|this._code = str;
|this._dependencies = [];
|this._flags = flags;
|this._URL = url;
|}
|Preprocessor.prototype.parse = function()
|{
|var start = new Date().getTime();
|var matches = this._lexer.matches();
|#{
#console.log("Matching: " + (new Date().getTime() - start));
}
|start = new Date().getTime();
|var tokens = this._lexer.tokens();
|#{
#console.log("Lexing: " + (new Date().getTime() - start));
}
|try {
|start = new Date().getTime();
|var derivation = this._parser.parse(tokens);
|
#{
#console.log("Parsing: " + (new Date().getTime() - start));
}
|} catch (e) {
|var index = e - 1;
|var error = "Unexpected token: " + matches[index] + "\\n\\n";
|for (var i = Math.max(index - 20, 0); i < Math.min(matches.length, index + 10); i++)
|{
|error += i == index ? (" " + matches[i] + " ") : matches[i];
|}
|error += "\\n\\nRegex matched: " + tokens[index];
|throw new SyntaxError(this.error_message(error));
|}
|start = new Date().getTime();
|var result = this._parser.compile(matches, derivation);
|
#{
#console.log("Compiling: " + (new Date().getTime() - start));
#console.log("Time: " + (new Date().getTime() - this._start));
#console.log(result);
}
|return new Executable(result[0], result[1], this._URL);
|}
|exports.preprocess = function(str, url, flags)
|{
|return new Preprocessor(str, url, flags).parse();
|};
|Preprocessor.prototype.error_message = function(errorMessage)
|{
|return errorMessage + " <Context File: "+ this._URL +
|(this._currentClass ? " Class: "+this._currentClass : "") +
|(this._currentSelector ? " Method: "+this._currentSelector : "") +">";
|};
end
result + generate_lexer_single + generate_parser_single + "\n" + @footer
end
end
end