Skip to content

Commit 50619d1

Browse files
committed
module visitor
1 parent 5a81c59 commit 50619d1

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

src/speg_module_visitor.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
var rd = require('./rd_parser');
2+
3+
function SPEG_actions_visitor() {
4+
this.actions = new SPEG_actions();
5+
}
6+
SPEG_actions_visitor.prototype.visit = function(node) {
7+
if (node.children) {
8+
node.children = node.children.map(function(child){
9+
return this.visit(child);
10+
}, this);
11+
}
12+
if (this.actions && node.action) {
13+
return this.actions[node.action](node);
14+
}
15+
return node;
16+
};
17+
18+
function SPEG_actions() {}
19+
20+
SPEG_actions.prototype.noop = function(node) {
21+
return node;
22+
};
23+
24+
SPEG_actions.prototype.peg = function(node) {
25+
return node.children[3];
26+
};
27+
28+
SPEG_actions.prototype.parsing_body = function(node) {
29+
node.children = node.children.map(function(child){
30+
return child.children[0];
31+
});
32+
return node;
33+
};
34+
35+
SPEG_actions.prototype.parsing_rule = function(node) {
36+
var rule = node.children[4];
37+
var ruleName = node.children[0].match;
38+
return '{\n' +
39+
'name: ' + ruleName + ',\n' +
40+
'parser: function(state) {\n' +
41+
'var start = state.position;\n' +
42+
'var ast = ' + rule + '(state);\n' +
43+
'if (ast) {\n' +
44+
'ast.rule = ' + ruleName + ';\n' +
45+
'if (!state.succesfullRules) {\n' +
46+
'state.succesfullRules = [];\n' +
47+
'}\n' +
48+
'state.succesfullRules.push({\n' +
49+
'rule: ast.rule,\n' +
50+
'match: ast.match,\n' +
51+
'start_position: ast.start_position,\n' +
52+
'end_position: ast.end_position\n' +
53+
'});\n' +
54+
'} else {\n' +
55+
'if (!state.failedRules) {\n' +
56+
'state.failedRules = [];\n' +
57+
'}\n' +
58+
'state.failedRules.push({\n' +
59+
'rule: ' + ruleName + ',\n' +
60+
'start_position: start\n' +
61+
'});\n' +
62+
'}\n' +
63+
'return ast;\n' +
64+
'}\n' +
65+
'}'
66+
};
67+
68+
SPEG_actions.prototype.parsing_expression = function(node) {
69+
return node.children[0];
70+
};
71+
72+
SPEG_actions.prototype.parsing_sequence = function(node) {
73+
var head = [node.children[0].children[0]];
74+
var tail = node.children[1].children.map(function(child){
75+
return child.children[1].children[0];
76+
});
77+
return 'rd.sequence([' + head.concat(tail) + '])';
78+
};
79+
80+
SPEG_actions.prototype.parsing_ordered_choice = function(node) {
81+
var head = [node.children[0]];
82+
var tail = node.children[1].children.map(function(child){
83+
return child.children[3];
84+
});
85+
return 'rd.ordered_choice([' + head.concat(tail) + '])';
86+
};
87+
88+
SPEG_actions.prototype.parsing_sub_expression = function(node) {
89+
var parser = node.children[1].children[0];
90+
var tags = node.children[0].children.map(function(tag_node){
91+
return tag_node.children[0].match;
92+
});
93+
return 'function(state) {\n' +
94+
'var ast = ' + parser + '.call(this, state);\n' +
95+
'var tags = [' + tags + '];\n' +
96+
'if (ast) {\n' +
97+
' if (tags.length > 0) {\n' +
98+
' if (ast.tags) {\n' +
99+
' ast.tags = tags.concat(ast.tags);\n' +
100+
' } else {\n' +
101+
' ast.tags = tags;\n' +
102+
' }\n' +
103+
' }\n' +
104+
'} else {\n' +
105+
' if (!state.failedTags) {\n' +
106+
' state.failedTags = [];\n' +
107+
' }\n' +
108+
' state.failedTags.push.apply(state.failedTags, tags);\n' +
109+
'}\n' +
110+
'return ast;\n' +
111+
'}'
112+
};
113+
114+
SPEG_actions.prototype.parsing_group = function(node) {
115+
return node.children[2];
116+
};
117+
118+
SPEG_actions.prototype.parsing_atomic_expression = function(node) {
119+
return node.children[0];
120+
};
121+
122+
SPEG_actions.prototype.parsing_not_predicate = function(node) {
123+
return 'rd.not_predicate(' + node.children[1].children[0] + ')';
124+
};
125+
126+
SPEG_actions.prototype.parsing_and_predicate = function(node) {
127+
return 'rd.and_predicate(' + node.children[1].children[0] + ')';
128+
};
129+
130+
SPEG_actions.prototype.parsing_zero_or_more = function(node) {
131+
return 'rd.zero_or_more(' + node.children[0].children[0] + ')';
132+
};
133+
134+
SPEG_actions.prototype.parsing_one_or_more = function(node) {
135+
return 'rd.one_or_more(' + node.children[0].children[0] + ')';
136+
};
137+
138+
SPEG_actions.prototype.parsing_optional = function(node) {
139+
return 'rd.optional(' + node.children[0].children[0] + ')';
140+
};
141+
142+
SPEG_actions.prototype.parsing_string = function(node) {
143+
return 'rd.string(' + node.children[1].match + ')';
144+
//.replace(/\\\\/g, '\\')
145+
//.replace(/\\"/g, '"')
146+
//);
147+
};
148+
149+
SPEG_actions.prototype.parsing_regex_char = function(node) {
150+
return 'rd.regex_char(' + node.children[0].match + ')';
151+
};
152+
153+
SPEG_actions.prototype.parsing_rule_call = function(node) {
154+
return 'rd.call_rule_by_name(' + node.match + ')';
155+
};
156+
157+
SPEG_actions.prototype.parsing_end_of_file = function() {
158+
return 'rd.end_of_file()';
159+
};
160+
161+
module.exports = {
162+
SPEG_actions_visitor: SPEG_actions_visitor
163+
};

0 commit comments

Comments
 (0)