forked from SimplePEG/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeg.js
More file actions
75 lines (69 loc) · 2.49 KB
/
speg.js
File metadata and controls
75 lines (69 loc) · 2.49 KB
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
var SPEG_actions_visitor = require('./speg_visitor').SPEG_actions_visitor;
var SPEG_module_visitor = require('./speg_module_visitor').SPEG_module_visitor;
var SPEG_parser = require('./speg_parser');
var rd = require('./rd_parser');
var ex = require('./exceptions');
function SPEG() {
this.parser = new SPEG_parser();
this.visitor = new SPEG_actions_visitor();
this.speg_parser = null;
this.state = null;
}
SPEG.prototype.parse_grammar = function(grammar) {
this.speg_parser = null;
var speg_ast = this.parser.parse(grammar);
if (speg_ast) {
this.speg_parser = this.visitor.visit(speg_ast);
} else {
throw new ex.GrammarParseError('Failed to parse grammar: \n\n' + this.parser.get_last_error());
}
};
SPEG.prototype.parse_text = function(text) {
if (this.speg_parser) {
var rules = this.speg_parser.children;
var first_rule = rules[0];
var first_rule_parser = first_rule.parser;
this.state = { text: text, position: 0, rules: rules };
var ast = first_rule_parser(this.state);
if (ast) {
return ast;
} else {
throw new ex.TextParseError('Failed to parse text: \n\n' + rd.get_last_error(this.state))
}
} else {
throw Error('You need grammar to parse text. Call parseGrammar first');
}
};
SPEG.prototype.parse = function(grammar, text) {
var speg_ast = this.parser.parse(grammar);
if (speg_ast) {
var generated_parser = this.visitor.visit(speg_ast);
var rules = generated_parser.children;
var first_rule = rules[0];
var first_rule_parser = first_rule.parser;
this.state = { text: text, position: 0, rules: rules };
var ast = first_rule_parser(this.state);
if (ast) {
return ast;
} else {
throw new ex.TextParseError('Failed to parse text: \n\n' + rd.get_last_error(this.state))
}
} else {
throw new ex.GrammarParseError('Failed to parse grammar: \n\n' + this.parser.get_last_error())
}
};
SPEG.prototype.parse_module = function(grammar) {
this.module_visitor = new SPEG_module_visitor();
var speg_ast = this.parser.parse(grammar);
if (speg_ast) {
return this.module_visitor.visit(speg_ast);
} else {
throw new ex.GrammarParseError('Failed to parse grammar: \n\n' + this.parser.get_last_error());
}
}
module.exports = {
SPEG: SPEG,
rd: rd,
TextParseError: ex.TextParseError,
GrammarParseError: ex.GrammarParseError
};