-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_parser.rb
135 lines (128 loc) · 3.64 KB
/
js_parser.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!ruby
# coding: utf-8
#
# js_parser.rb
#
# Created by Erik Österlund on 1/14/10.
# Copyright 2010 Växjö Universitet. All rights reserved.
#
require 'table_generator'
module LALR
class InterpreterGenerator
def generate_parser
sm = symbol_to_index
token_to_num = {}
@tokens.size.times do |i|
token_to_num[@tokens[i]] = i + 1 # Start symbol is 0
end
result = <<-end.here_with_pipe
|var index = 0;
|var jndex = 0;
|var tok;
|var numbers = [];
|var tokens = null;
|var t = null;
|var o = [];
|var s = [0];
|var ruleLength = [#{@rules.map{|rule|rule.productions.size.to_s}.join(",")}];
|var ruleSym = [#{@rules.map{|rule|sm[rule.name]}.join(",")}];
|var goto = [#{
@gotos.size.times.map do |i|
"{" + @gotos[i].each.map do |k, v|
"#{sm[k]}:#{v}"
end.join(",") + "}"
end.join(",")
}];
|var shift = [#{
@actions.size.times.map do |i|
"{" + @actions[i].select do |k, v|
v.action == :shift
end.map do |k, v|
"#{k.is_a?(EOF) ? 0 : token_to_num[k]}:#{v.number}"
end.join(",") + "}"
end.join(",")
}];
|var reduce = [#{
@actions.size.times.map do |i|
"{" + @actions[i].select do |k, v|
v.action == :reduce
end.map do |k, v|
"#{k.is_a?(EOF) ? 0 : token_to_num[k]}:#{v.number}"
end.join(",") + "}"
end.join(",")
}];
|var accept = {#{
@actions.size.times.select do |i|
@actions[i].select{|k, v| v.action == :accept}.size > 0
end.map do |i|
"#{i}:{" + @actions[i].select{|k, v| v.action == :accept}.map do |k, v|
"#{k.is_a?(EOF) ? 0 : token_to_num[k]}:true"
end.join(",") + "}"
end.join(",")
}};
|function u()
|{
|if(index == tokens.length)
|{
|index = 0;
|if(++jndex == numbers.length){tokens = null;return false;}
|tokens = numbers[jndex];
|}
|tok = tokens[index++];
|return true;
|}
|function se()
|{
|postMessage('Unexpected: "' + t[index-1] + '"');
|return;
|}
|onmessage = function(message)
|{
|var data = JSON.parse(message.data);
|if(t == null)
|{
|t = data;
|return;
|}
|numbers.push(data);
|if (tokens == null)
|{
|tokens = data;
|index = 0;
|} else
|return;
|derive();
|}
|
|function derive()
|{
|if(!u()){return;};
|var symbol = null;
|while(true){
|var state = s[s.length-1];
|var temp;
|if (symbol != null)
|{
|s.push(goto[state][symbol]);
|symbol = null;
|} else if(temp = shift[state][tok])
|{
|s.push(temp);if(!u()){return;};
|} else if(temp = reduce[state][tok]) {
|o.push(temp);
|s = s.slice(0, s.length - ruleLength[temp]);
|symbol = ruleSym[temp];
|} else if(accept[state] && accept[state][tok]) {
|tokens = null;
|o.push(0);
|compile();
|return;
|} else {se();return;}
|}
|}
end
result += generate_compiler
result
end
end
end