nilac 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/nilac +20 -273
- data/lib/nilac/version.rb +1 -1
- metadata +1 -1
data/bin/nilac
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# encoding: utf-8
|
3
2
|
|
4
3
|
#Nilac is the official Nila compiler. It compiles Nila into pure Javascript. Nilac is currently
|
5
4
|
#written in Ruby but will be self hosted in the upcoming years. Nila targets mostly the
|
@@ -210,83 +209,6 @@ def compile(input_file_path)
|
|
210
209
|
|
211
210
|
end
|
212
211
|
|
213
|
-
def compile_heredoc_strings(input_file_contents,temporary_nila_file)
|
214
|
-
|
215
|
-
#This method will compile all the Heredoc strings in Nila into pure
|
216
|
-
#Javascript strings. Ruby has two types of Heredocs. Currently, Nila doesn't support both of them.
|
217
|
-
#Here is an example of what Nila supports
|
218
|
-
|
219
|
-
#long_passage = <<-ipsumtext
|
220
|
-
#
|
221
|
-
#Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
|
222
|
-
#incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
|
223
|
-
#exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
|
224
|
-
#irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
|
225
|
-
#pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
|
226
|
-
#deserunt mollit anim id est laborum.
|
227
|
-
#
|
228
|
-
#ipsumtext
|
229
|
-
|
230
|
-
#Heredocs preserve all the formatting
|
231
|
-
|
232
|
-
def find_all_matching_indices(input_string,pattern)
|
233
|
-
|
234
|
-
locations = []
|
235
|
-
|
236
|
-
index = input_string.index(pattern)
|
237
|
-
|
238
|
-
while index != nil
|
239
|
-
|
240
|
-
locations << index
|
241
|
-
|
242
|
-
index = input_string.index(pattern,index+1)
|
243
|
-
|
244
|
-
|
245
|
-
end
|
246
|
-
|
247
|
-
return locations
|
248
|
-
|
249
|
-
|
250
|
-
end
|
251
|
-
|
252
|
-
joined_file_contents = input_file_contents.join
|
253
|
-
|
254
|
-
modified_file_contents = joined_file_contents.dup
|
255
|
-
|
256
|
-
heredoc_start_locations = find_all_matching_indices(joined_file_contents,"<<-")
|
257
|
-
|
258
|
-
heredoc_start_locations.each do |location|
|
259
|
-
|
260
|
-
string_extract = joined_file_contents[location..-1]
|
261
|
-
|
262
|
-
end_of_first_line = string_extract.index("\n")
|
263
|
-
|
264
|
-
name_of_heredoc = string_extract[3...end_of_first_line].rstrip
|
265
|
-
|
266
|
-
heredoc_start,heredoc_end = find_all_matching_indices(string_extract,name_of_heredoc)
|
267
|
-
|
268
|
-
heredoc = string_extract[heredoc_start..heredoc_end+name_of_heredoc.length-1]
|
269
|
-
|
270
|
-
heredoc_content = heredoc.split(name_of_heredoc)
|
271
|
-
|
272
|
-
heredoc_content = heredoc_content[1].lstrip.rstrip.gsub("\n","\\n").gsub("\t","\\t")
|
273
|
-
|
274
|
-
modified_file_contents = modified_file_contents.sub(string_extract[heredoc_start-3..heredoc_end+name_of_heredoc.length-1],"\""+heredoc_content+"\"")
|
275
|
-
|
276
|
-
end
|
277
|
-
|
278
|
-
file_id = open(temporary_nila_file, 'w')
|
279
|
-
|
280
|
-
file_id.write(modified_file_contents)
|
281
|
-
|
282
|
-
file_id.close()
|
283
|
-
|
284
|
-
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
285
|
-
|
286
|
-
return line_by_line_contents
|
287
|
-
|
288
|
-
end
|
289
|
-
|
290
212
|
def compile_interpolated_strings(input_file_contents)
|
291
213
|
|
292
214
|
modified_file_contents = input_file_contents.dup
|
@@ -1361,38 +1283,6 @@ def compile(input_file_path)
|
|
1361
1283
|
|
1362
1284
|
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
1363
1285
|
|
1364
|
-
var_declaration_index = 0
|
1365
|
-
|
1366
|
-
for x in 0...line_by_line_contents.length
|
1367
|
-
|
1368
|
-
current_row = line_by_line_contents[x]
|
1369
|
-
|
1370
|
-
if current_row.lstrip.include?("var ")
|
1371
|
-
|
1372
|
-
var_declaration_index = x
|
1373
|
-
|
1374
|
-
break
|
1375
|
-
|
1376
|
-
end
|
1377
|
-
|
1378
|
-
end
|
1379
|
-
|
1380
|
-
empty_check = line_by_line_contents[x+1..x+4]
|
1381
|
-
|
1382
|
-
empty_check.each_with_index do |content,index|
|
1383
|
-
|
1384
|
-
empty_check[index] = content.lstrip
|
1385
|
-
|
1386
|
-
end
|
1387
|
-
|
1388
|
-
empty_check = empty_check.delete_if {|content| content.empty?}
|
1389
|
-
|
1390
|
-
if empty_check.empty?
|
1391
|
-
|
1392
|
-
line_by_line_contents.delete_at(x+1);line_by_line_contents.delete_at(x+2)
|
1393
|
-
|
1394
|
-
end
|
1395
|
-
|
1396
1286
|
return line_by_line_contents
|
1397
1287
|
|
1398
1288
|
end
|
@@ -1411,7 +1301,7 @@ def compile(input_file_path)
|
|
1411
1301
|
|
1412
1302
|
def create_self_invoking_function(input_file_contents)
|
1413
1303
|
|
1414
|
-
# A feature imported from Coffeescript. This makes all the function private by default
|
1304
|
+
# A feature imported from Coffeescript 1.6.1. This makes all the function private by default
|
1415
1305
|
# and prevents global variables from leaking.
|
1416
1306
|
|
1417
1307
|
modified_file_contents = ["(function() {\n\n",input_file_contents,"\n\n}).call(this);\n"].flatten
|
@@ -1420,170 +1310,15 @@ def compile(input_file_path)
|
|
1420
1310
|
|
1421
1311
|
end
|
1422
1312
|
|
1423
|
-
def compile_license_functions(input_file_contents,temporary_nila_file)
|
1424
|
-
|
1425
|
-
#This method will make it easier for developers to add open source licensing to their projects
|
1426
|
-
|
1427
|
-
#Currently the following licenses are supported
|
1428
|
-
|
1429
|
-
#1. MIT License
|
1430
|
-
|
1431
|
-
#Example of MIT License => mit_license(year,authors)
|
1432
|
-
|
1433
|
-
#mit_license("2013","Adhithya Rajasekaran, Sri Madhavi Rajasekaran")
|
1434
|
-
|
1435
|
-
def find_all_matching_indices(input_string,pattern)
|
1436
|
-
|
1437
|
-
locations = []
|
1438
|
-
|
1439
|
-
index = input_string.index(pattern)
|
1440
|
-
|
1441
|
-
while index != nil
|
1442
|
-
|
1443
|
-
locations << index
|
1444
|
-
|
1445
|
-
index = input_string.index(pattern,index+1)
|
1446
|
-
|
1447
|
-
|
1448
|
-
end
|
1449
|
-
|
1450
|
-
return locations
|
1451
|
-
|
1452
|
-
|
1453
|
-
end
|
1454
|
-
|
1455
|
-
license_functions = ["mit_license(","apache_license"]
|
1456
|
-
|
1457
|
-
mit_license = <<-licensetext
|
1458
|
-
|
1459
|
-
/*
|
1460
|
-
|
1461
|
-
Copyright yer, <auth>
|
1462
|
-
|
1463
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”),
|
1464
|
-
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
1465
|
-
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
1466
|
-
|
1467
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
1468
|
-
|
1469
|
-
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
1470
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
1471
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
1472
|
-
IN THE SOFTWARE.
|
1473
|
-
|
1474
|
-
*/
|
1475
|
-
|
1476
|
-
|
1477
|
-
licensetext
|
1478
|
-
|
1479
|
-
apache_license = <<-licensetext
|
1480
|
-
|
1481
|
-
/*
|
1482
|
-
|
1483
|
-
Copyright [yer] [auth]
|
1484
|
-
|
1485
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
1486
|
-
you may not use this file except in compliance with the License.
|
1487
|
-
You may obtain a copy of the License at
|
1488
|
-
|
1489
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
1490
|
-
|
1491
|
-
Unless required by applicable law or agreed to in writing, software
|
1492
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
1493
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
1494
|
-
See the License for the specific language governing permissions and
|
1495
|
-
limitations under the License.
|
1496
|
-
|
1497
|
-
*/
|
1498
|
-
|
1499
|
-
|
1500
|
-
licensetext
|
1501
|
-
|
1502
|
-
license_texts = [mit_license,apache_license]
|
1503
|
-
|
1504
|
-
license_params = [2,2]
|
1505
|
-
|
1506
|
-
license_replaceables = ["yer","auth","orgg"]
|
1507
|
-
|
1508
|
-
joined_file_contents = input_file_contents.join
|
1509
|
-
|
1510
|
-
license_functions.each_with_index do |function,index|
|
1511
|
-
|
1512
|
-
function_locations = find_all_matching_indices(joined_file_contents,function)
|
1513
|
-
|
1514
|
-
function_locations.each do |location|
|
1515
|
-
|
1516
|
-
string_extract = joined_file_contents[location..-1]
|
1517
|
-
|
1518
|
-
function_end = string_extract.index(")")
|
1519
|
-
|
1520
|
-
function_call = string_extract[0..function_end]
|
1521
|
-
|
1522
|
-
parameters = function_call.split(function)[1][0...-1]
|
1523
|
-
|
1524
|
-
parameters = parameters.split(":")
|
1525
|
-
|
1526
|
-
parameters.each_with_index do |param,index|
|
1527
|
-
|
1528
|
-
parameters[index] = param[1...-1]
|
1529
|
-
|
1530
|
-
end
|
1531
|
-
|
1532
|
-
replacement_string = license_texts[index]
|
1533
|
-
|
1534
|
-
for x in 0...parameters.length
|
1535
|
-
|
1536
|
-
replacement_string = replacement_string.sub(license_replaceables[x],parameters[x])
|
1537
|
-
|
1538
|
-
end
|
1539
|
-
|
1540
|
-
replacement_array = replacement_string.split("\n")
|
1541
|
-
|
1542
|
-
modified_replacement_array = replacement_array.dup
|
1543
|
-
|
1544
|
-
replacement_array.each_with_index do |line,index|
|
1545
|
-
|
1546
|
-
modified_replacement_array[index] = line.lstrip
|
1547
|
-
|
1548
|
-
end
|
1549
|
-
|
1550
|
-
replacement_string = replacement_array.join("\n")
|
1551
|
-
|
1552
|
-
if joined_file_contents.include?(function_call+";\n")
|
1553
|
-
|
1554
|
-
joined_file_contents = joined_file_contents.sub(function_call+";\n","")
|
1555
|
-
|
1556
|
-
else
|
1557
|
-
|
1558
|
-
joined_file_contents = joined_file_contents.sub(function_call+";","")
|
1559
|
-
|
1560
|
-
end
|
1561
|
-
|
1562
|
-
joined_file_contents = replacement_string.lstrip + "\n\n" + joined_file_contents
|
1563
|
-
|
1564
|
-
end
|
1565
|
-
|
1566
|
-
end
|
1567
|
-
|
1568
|
-
file_id = open(temporary_nila_file, 'w')
|
1569
|
-
|
1570
|
-
file_id.write(joined_file_contents)
|
1571
|
-
|
1572
|
-
file_id.close()
|
1573
|
-
|
1574
|
-
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
1575
|
-
|
1576
|
-
return line_by_line_contents
|
1577
|
-
|
1578
|
-
end
|
1579
|
-
|
1580
1313
|
def output_javascript(file_contents,output_file,temporary_nila_file)
|
1581
1314
|
|
1582
1315
|
file_id = open(output_file, 'w')
|
1583
1316
|
|
1584
1317
|
File.delete(temporary_nila_file)
|
1585
1318
|
|
1586
|
-
file_id.write("//
|
1319
|
+
file_id.write("//Written in Nila and compiled into Javascript.Have fun!\n\n")
|
1320
|
+
|
1321
|
+
file_id.write("//Visit http://adhithyan15.github.com/nila to know more!\n\n")
|
1587
1322
|
|
1588
1323
|
file_id.write(file_contents.join)
|
1589
1324
|
|
@@ -1599,7 +1334,7 @@ def compile(input_file_path)
|
|
1599
1334
|
|
1600
1335
|
file_contents = split_semicolon_seperated_expressions(file_contents)
|
1601
1336
|
|
1602
|
-
file_contents = compile_heredoc_strings(file_contents,temp_file)
|
1337
|
+
#file_contents = compile_heredoc_strings(file_contents,temp_file)
|
1603
1338
|
|
1604
1339
|
file_contents = compile_interpolated_strings(file_contents)
|
1605
1340
|
|
@@ -1629,8 +1364,6 @@ def compile(input_file_path)
|
|
1629
1364
|
|
1630
1365
|
file_contents = create_self_invoking_function(file_contents)
|
1631
1366
|
|
1632
|
-
file_contents = compile_license_functions(file_contents,temp_file)
|
1633
|
-
|
1634
1367
|
file_contents = pretty_print_javascript(file_contents,temp_file)
|
1635
1368
|
|
1636
1369
|
output_javascript(file_contents,output_js_file,temp_file)
|
@@ -1640,6 +1373,18 @@ end
|
|
1640
1373
|
|
1641
1374
|
def create_executable(input_file)
|
1642
1375
|
|
1376
|
+
def read_file_line_by_line(input_path)
|
1377
|
+
|
1378
|
+
file_id = open(input_path)
|
1379
|
+
|
1380
|
+
file_line_by_line = file_id.readlines()
|
1381
|
+
|
1382
|
+
file_id.close
|
1383
|
+
|
1384
|
+
return file_line_by_line
|
1385
|
+
|
1386
|
+
end
|
1387
|
+
|
1643
1388
|
windows_output = `ocra --add-all-core #{input_file}`
|
1644
1389
|
|
1645
1390
|
end
|
@@ -1714,7 +1459,9 @@ OptionParser.new do |opts|
|
|
1714
1459
|
|
1715
1460
|
opts.on("-b", "--build", "Builds Itself") do
|
1716
1461
|
|
1717
|
-
|
1462
|
+
file_path = Dir.pwd + "/nilac.rb"
|
1463
|
+
|
1464
|
+
create_executable(file_path)
|
1718
1465
|
|
1719
1466
|
puts "Build Successful!"
|
1720
1467
|
|
data/lib/nilac/version.rb
CHANGED