Skip to content

Commit defbbdc

Browse files
committed
SPEG_actions is now part of a visitor
1 parent d60c23c commit defbbdc

File tree

3 files changed

+97
-96
lines changed

3 files changed

+97
-96
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ jspm_packages
3232

3333
# IDE
3434
.idea/
35+
.vscode/

src/speg.js

+1-94
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var ex = require('./exceptions');
55

66
function SPEG() {
77
this.parser = new SPEG_parser();
8-
this.visitor = new SPEG_actions_visitor(new SPEG_actions());
8+
this.visitor = new SPEG_actions_visitor();
99
this.speg_parser = null;
1010
}
1111

@@ -55,99 +55,6 @@ SPEG.prototype.parse = function(grammar, text) {
5555
}
5656
};
5757

58-
function SPEG_actions() {}
59-
60-
SPEG_actions.prototype.noop = function(node) {
61-
return node;
62-
};
63-
64-
SPEG_actions.prototype.peg = function(node) {
65-
return node.children[3];
66-
};
67-
68-
SPEG_actions.prototype.parsing_body = function(node) {
69-
node.children = node.children.map(function(child){
70-
return child.children[0];
71-
});
72-
return node;
73-
};
74-
75-
SPEG_actions.prototype.parsing_rule = function(node) {
76-
var rule = node.children[4];
77-
return {
78-
name: node.children[0].match,
79-
parser: rule
80-
}
81-
};
82-
83-
SPEG_actions.prototype.parsing_expression = function(node) {
84-
return node.children[0];
85-
};
86-
87-
SPEG_actions.prototype.parsing_sequence = function(node) {
88-
var head = [node.children[0].children[0]];
89-
var tail = node.children[1].children.map(function(child){
90-
return child.children[1].children[0];
91-
});
92-
return rd.sequence(head.concat(tail));
93-
};
94-
95-
SPEG_actions.prototype.parsing_ordered_choice = function(node) {
96-
var head = [node.children[0]];
97-
var tail = node.children[1].children.map(function(child){
98-
return child.children[3];
99-
});
100-
return rd.ordered_choice(head.concat(tail));
101-
};
102-
103-
SPEG_actions.prototype.parsing_sub_expression = function(node) {
104-
return node.children[0];
105-
};
106-
107-
SPEG_actions.prototype.parsing_group = function(node) {
108-
return node.children[2];
109-
};
110-
111-
SPEG_actions.prototype.parsing_atomic_expression = function(node) {
112-
return node.children[0];
113-
};
114-
115-
SPEG_actions.prototype.parsing_not_predicate = function(node) {
116-
return rd.not_predicate(node.children[1].children[0]);
117-
};
118-
119-
SPEG_actions.prototype.parsing_and_predicate = function(node) {
120-
return rd.and_predicate(node.children[1].children[0]);
121-
};
122-
123-
SPEG_actions.prototype.parsing_zero_or_more = function(node) {
124-
return rd.zero_or_more(node.children[0].children[0]);
125-
};
126-
127-
SPEG_actions.prototype.parsing_one_or_more = function(node) {
128-
return rd.one_or_more(node.children[0].children[0]);
129-
};
130-
131-
SPEG_actions.prototype.parsing_optional = function(node) {
132-
return rd.optional(node.children[0].children[0]);
133-
};
134-
135-
SPEG_actions.prototype.parsing_string = function(node) {
136-
return rd.string(node.children[1].match);
137-
};
138-
139-
SPEG_actions.prototype.parsing_regex_char = function(node) {
140-
return rd.regex_char(node.children[0].match);
141-
};
142-
143-
SPEG_actions.prototype.parsing_rule_call = function(node) {
144-
return rd.call_rule_by_name(node.match);
145-
};
146-
147-
SPEG_actions.prototype.parsing_end_of_file = function(node) {
148-
return rd.end_of_file();
149-
};
150-
15158
module.exports = {
15259
SPEG: SPEG
15360
};

src/speg_visitor.js

+95-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ PEGJS_visitor.prototype.end_of_file = function(node) {
3434
return null;
3535
};
3636

37-
function SPEG_actions_visitor(actions) {
38-
this.actions = actions;
37+
function SPEG_actions_visitor() {
38+
this.actions = new SPEG_actions();
3939
}
4040
SPEG_actions_visitor.prototype.visit = function(node) {
4141
if (node.children) {
@@ -49,6 +49,99 @@ SPEG_actions_visitor.prototype.visit = function(node) {
4949
return node;
5050
};
5151

52+
function SPEG_actions() {}
53+
54+
SPEG_actions.prototype.noop = function(node) {
55+
return node;
56+
};
57+
58+
SPEG_actions.prototype.peg = function(node) {
59+
return node.children[3];
60+
};
61+
62+
SPEG_actions.prototype.parsing_body = function(node) {
63+
node.children = node.children.map(function(child){
64+
return child.children[0];
65+
});
66+
return node;
67+
};
68+
69+
SPEG_actions.prototype.parsing_rule = function(node) {
70+
var rule = node.children[4];
71+
return {
72+
name: node.children[0].match,
73+
parser: rule
74+
}
75+
};
76+
77+
SPEG_actions.prototype.parsing_expression = function(node) {
78+
return node.children[0];
79+
};
80+
81+
SPEG_actions.prototype.parsing_sequence = function(node) {
82+
var head = [node.children[0].children[0]];
83+
var tail = node.children[1].children.map(function(child){
84+
return child.children[1].children[0];
85+
});
86+
return rd.sequence(head.concat(tail));
87+
};
88+
89+
SPEG_actions.prototype.parsing_ordered_choice = function(node) {
90+
var head = [node.children[0]];
91+
var tail = node.children[1].children.map(function(child){
92+
return child.children[3];
93+
});
94+
return rd.ordered_choice(head.concat(tail));
95+
};
96+
97+
SPEG_actions.prototype.parsing_sub_expression = function(node) {
98+
return node.children[0];
99+
};
100+
101+
SPEG_actions.prototype.parsing_group = function(node) {
102+
return node.children[2];
103+
};
104+
105+
SPEG_actions.prototype.parsing_atomic_expression = function(node) {
106+
return node.children[0];
107+
};
108+
109+
SPEG_actions.prototype.parsing_not_predicate = function(node) {
110+
return rd.not_predicate(node.children[1].children[0]);
111+
};
112+
113+
SPEG_actions.prototype.parsing_and_predicate = function(node) {
114+
return rd.and_predicate(node.children[1].children[0]);
115+
};
116+
117+
SPEG_actions.prototype.parsing_zero_or_more = function(node) {
118+
return rd.zero_or_more(node.children[0].children[0]);
119+
};
120+
121+
SPEG_actions.prototype.parsing_one_or_more = function(node) {
122+
return rd.one_or_more(node.children[0].children[0]);
123+
};
124+
125+
SPEG_actions.prototype.parsing_optional = function(node) {
126+
return rd.optional(node.children[0].children[0]);
127+
};
128+
129+
SPEG_actions.prototype.parsing_string = function(node) {
130+
return rd.string(node.children[1].match);
131+
};
132+
133+
SPEG_actions.prototype.parsing_regex_char = function(node) {
134+
return rd.regex_char(node.children[0].match);
135+
};
136+
137+
SPEG_actions.prototype.parsing_rule_call = function(node) {
138+
return rd.call_rule_by_name(node.match);
139+
};
140+
141+
SPEG_actions.prototype.parsing_end_of_file = function(node) {
142+
return rd.end_of_file();
143+
};
144+
52145
module.exports = {
53146
SPEG_actions_visitor: SPEG_actions_visitor,
54147
PEGJS_visitor: PEGJS_visitor

0 commit comments

Comments
 (0)