1
1
var rd = require ( './rd_parser' ) ;
2
2
3
- function SPEG_actions_visitor ( ) {
3
+ function SPEG_module_visitor ( ) {
4
4
this . actions = new SPEG_actions ( ) ;
5
5
}
6
- SPEG_actions_visitor . prototype . visit = function ( node ) {
6
+ SPEG_module_visitor . prototype . visit = function ( node ) {
7
+ this . firstRule = null ;
7
8
if ( node . children ) {
8
9
node . children = node . children . map ( function ( child ) {
9
10
return this . visit ( child ) ;
@@ -22,26 +23,47 @@ SPEG_actions.prototype.noop = function(node) {
22
23
} ;
23
24
24
25
SPEG_actions . prototype . peg = function ( node ) {
25
- return node . children [ 3 ] ;
26
+ var module_source =
27
+ 'var simplepeg = require(\'simplepeg\');\n' +
28
+ 'var rd = simplepeg.rd;\n' +
29
+ 'function SPEG() {\n' +
30
+ ' this.state = null;\n' +
31
+ '}\n' +
32
+ 'SPEG.prototype.parse_text = function(text) {\n' +
33
+ ' this.state = { text: text, position: 0 };\n' +
34
+ ' var ast = ' + this . firstRule + '()(this.state);\n' +
35
+ ' if (ast) {\n' +
36
+ ' return ast;\n' +
37
+ ' } else {\n' +
38
+ ' throw new simplepeg.TextParseError(\'Failed to parse text: \\n\\n\' + rd.get_last_error(this.state))\n' +
39
+ ' }\n' +
40
+ '};\n' +
41
+ 'module.exports = {\n' +
42
+ ' SPEG: SPEG\n' +
43
+ '};\n\n' + node . children [ 3 ] . join ( '\n' ) ;
44
+
45
+ return module_source ;
26
46
} ;
27
47
28
48
SPEG_actions . prototype . parsing_body = function ( node ) {
29
49
node . children = node . children . map ( function ( child ) {
30
50
return child . children [ 0 ] ;
31
51
} ) ;
32
- return node ;
52
+ return node . children ;
33
53
} ;
34
54
35
55
SPEG_actions . prototype . parsing_rule = function ( node ) {
36
56
var rule = node . children [ 4 ] ;
37
57
var ruleName = node . children [ 0 ] . match ;
38
- return '{\n' +
39
- 'name: ' + ruleName + ',\n' +
40
- 'parser: function(state) {\n' +
58
+ if ( ! this . firstRule ) {
59
+ this . firstRule = ruleName ;
60
+ }
61
+ return 'function ' + ruleName + '() {\n' +
62
+ 'return function(state) {\n' +
41
63
'var start = state.position;\n' +
42
- 'var ast = ' + rule + '(state);\n' +
64
+ 'var ast = ( ' + rule + ') (state);\n' +
43
65
'if (ast) {\n' +
44
- 'ast.rule = ' + ruleName + ';\n' +
66
+ 'ast.rule = " ' + ruleName + '" ;\n' +
45
67
'if (!state.succesfullRules) {\n' +
46
68
'state.succesfullRules = [];\n' +
47
69
'}\n' +
@@ -56,13 +78,13 @@ SPEG_actions.prototype.parsing_rule = function(node) {
56
78
'state.failedRules = [];\n' +
57
79
'}\n' +
58
80
'state.failedRules.push({\n' +
59
- 'rule: ' + ruleName + ',\n' +
81
+ 'rule: " ' + ruleName + '" ,\n' +
60
82
'start_position: start\n' +
61
83
'});\n' +
62
84
'}\n' +
63
85
'return ast;\n' +
64
86
'}\n' +
65
- '}'
87
+ '}' ;
66
88
} ;
67
89
68
90
SPEG_actions . prototype . parsing_expression = function ( node ) {
@@ -140,24 +162,21 @@ SPEG_actions.prototype.parsing_optional = function(node) {
140
162
} ;
141
163
142
164
SPEG_actions . prototype . parsing_string = function ( node ) {
143
- return 'rd.string(' + node . children [ 1 ] . match + ')' ;
144
- //.replace(/\\\\/g, '\\')
145
- //.replace(/\\"/g, '"')
146
- //);
165
+ return 'rd.string("' + node . children [ 1 ] . match + '")' ;
147
166
} ;
148
167
149
168
SPEG_actions . prototype . parsing_regex_char = function ( node ) {
150
- return 'rd.regex_char(' + node . children [ 0 ] . match + ')' ;
169
+ return 'rd.regex_char(/ ' + node . children [ 0 ] . match + '/ )' ;
151
170
} ;
152
171
153
172
SPEG_actions . prototype . parsing_rule_call = function ( node ) {
154
- return 'rd.call_rule_by_name (' + node . match + ')' ;
173
+ return 'rd.rec (' + node . match + ')' ;
155
174
} ;
156
175
157
176
SPEG_actions . prototype . parsing_end_of_file = function ( ) {
158
177
return 'rd.end_of_file()' ;
159
178
} ;
160
179
161
180
module . exports = {
162
- SPEG_actions_visitor : SPEG_actions_visitor
181
+ SPEG_module_visitor : SPEG_module_visitor
163
182
} ;
0 commit comments