forked from daKuleMune/nodebnf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Language.js
143 lines (122 loc) · 3.92 KB
/
Language.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const BnfLine = require( "./BnfLine" ).BnfLine;
const types = require( "./BnfLine" ).Types;
exports.Language = class Language{
constructor( abnfSyntax, languageId ){
this.name = languageId;
this._lineIndex = 0;
this._charPosition = 0;
this._rawSyntaxArray = [];
this._literalMap = [];
this._CreateSyntaxLines( abnfSyntax );
this._rawSyntax = this._rawSyntaxArray.join( "\n" );
try{
this._CreateRuleList();
}
catch( ex ){
console.error( "Unable to compile language (a)bnf." );
console.error( ex );
console.error( "\nSYNTAX:\n" + this._rawSyntax.trim() );
}
}
_CreateRuleList(){
this.rules = {};
let ruleBuilder = [];
let match;
let namePattern = /<*([A-z_][A-z_\-\d]*)>*[\s\n]*:{0,2}=/mg;
while ((match = namePattern.exec(this._rawSyntax)) != null) {
let name = match[1];
ruleBuilder.push( {
name,
startAt : match.index,
endAt : this._rawSyntax.length
});
if( ruleBuilder.length != 1 ){
ruleBuilder[ruleBuilder.length - 2].endAt = ruleBuilder[ruleBuilder.length - 1].startAt - 1;
}
}
ruleBuilder.map( x => this._AddRule( x.name, this._BuildSyntax( this._rawSyntax.substring( x.startAt, x.endAt ).replace( /\n/g, " " ).trim() ) ) );
}
_AddRule( ruleName, ruleSyntax ){
if( this.rules[ruleName] === undefined ){
this.rules[ruleName] = ruleSyntax;
}
else{
if( ruleSyntax[0].type !== types.alt ){
throw "Duplicate definition of rule: '" + ruleName + "'";
}
else{
this.rules[ruleName] = this.rules[ruleName].concat( ruleSyntax );
}
}
}
_BuildSyntax( rawSyntax ){
let ruleSyntax = rawSyntax.split( "=" )[1].trim();
let rule = { rawSyntax : ruleSyntax };
try{
let bnfLine = new BnfLine( ruleSyntax, this._literalMap );
rule.syntax = bnfLine.GetTokenMap();
}
catch( ex ){
throw "Unable to build bnf line syntax for rule " + rawSyntax + "\nerror: " + ex;
}
return rule.syntax;
}
_MapLiterals( syntax ){
let literals = syntax.raw.match( /['"][^'"]*['"]/gi );
if( literals === null ){
return [];
}
else{
let ret = [];
literals.map( ( x ) => {
let literal = x.substring( 1, x.length - 1 );
if( literal === "" ){
syntax.raw = syntax.raw.replace( x, "<BLANK>" );
}
else{
syntax.raw = syntax.raw.replace( x, "@" + ( ret.length + this._literalMap.length ) );
}
ret.push( literal );//Trim to quotes away //Turn into a buffer//
} );
return ret;
}
}
_PrepareLine( rawSyntax ){
let syntax = {
raw : rawSyntax
};
let literals = this._MapLiterals( syntax );
if( syntax.raw.indexOf( ";" ) !== -1 ){
syntax.raw = syntax.raw.substring( 0, syntax.raw.indexOf( ";" ) );
}
let literalMatch = syntax.raw.match( /@\d+/g ) || [];
if( literals.length > literalMatch.length ){
literals.splice( literalMatch.length, literals.length - literalMatch.length );
}
this._literalMap = this._literalMap.concat( literals );
let preparedLine = {
line : this._lineIndex++,
charPosition : this._charPosition,
original : rawSyntax,
syntax : this._TrimSyntax( syntax ),
}
this._rawSyntaxArray.push( preparedLine.syntax );
this._charPosition += preparedLine.syntax.length + 1;
return preparedLine;
}
_TrimSyntax( syntax ){
syntax.raw = syntax.raw.trim();
if( syntax.raw === "" ){
return "";
}
if( syntax.raw.startsWith( ';' ) ){
return "";
}
return syntax.raw;
}
_CreateSyntaxLines( abnfSyntax ){
let literalIndex = 0;
this._syntaxLines = abnfSyntax.replace( /\r/gi, "" ).split( "\n" )
.map( ( x ) => this._PrepareLine( x ) );
}
}