forked from daKuleMune/nodebnf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compiler.js
54 lines (47 loc) · 1.65 KB
/
Compiler.js
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
const Language = require( "./Language" ).Language;
const Parser = require( "./Parser" ).Parser;
exports.Compiler = class Compiler{
constructor(){
this.languages = {};
this.parsers = {};
this.events = {};
this.languageEvents = {};
this.defaultLanguage = null;
}
AddLanguage( abnfSyntax, languageId, setAsDefault = false ){
this.languages[languageId] = new Language( abnfSyntax, languageId );
this.languageEvents[languageId] = {};
if( this.defaultLanguage === null || setAsDefault ){
this.defaultLanguage = languageId;
}
this.parsers[languageId] = new Parser( this.languages[languageId], this );
}
Trigger( languageId, token, dataObject, tokenTree ){
if( this.languageEvents[languageId][token.name] ){
this.languageEvents[languageId][token.name]( token, dataObject, tokenTree );
}
if( this.events[token.name] ){
this.events[token.name]( token, dataObject, tokenTree );
}
}
SetRuleEvents( ruleEventMap, languageId = null ){
if( languageId !== null ){
this.languageEvents[languageId] = Object.assign( this.languageEvents[languageId], ruleEventMap );
}
else{
this.events = Object.assign( this.events, ruleEventMap );
}
}
AddRuleEvent( ruleName, event, languageId = null ){
if( languageId !== null ){
this.languageEvents[languageId][ruleName] = event;
}
else{
this.events[ruleName] = event;
}
}
ParseScript( script, dataObject = {}, languageId = null ){
languageId = languageId || this.defaultLanguage;
return this.parsers[languageId].ParseScript( script, dataObject );
}
}