1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
"""
Grammalecte: compile rules
"""
import re
import os
import traceback
import json
import colorsys
import time
import compile_rules_js_convert as jsconv
import compile_rules_graph as crg
dDEFINITIONS = {}
dDECLENSIONS = {}
|
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
"""
Grammalecte: compile rules
"""
import re
import os
import traceback
import json
import colorsys
import time
import hashlib
import compile_rules_js_convert as jsconv
import compile_rules_graph as crg
dDEFINITIONS = {}
dDECLENSIONS = {}
|
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
print(" {:>6}: {}".format(nLine, " " * nLevel + sComment))
def make (spLang, sLang, bUseCache=False):
"compile rules, returns a dictionary of values"
# for clarity purpose, don’t create any file here
if bUseCache and os.path.isfile("_build/data_cache.json"):
print("> don’t rebuild rules, use cache...")
sJSON = open("_build/data_cache.json", "r", encoding="utf-8").read()
dCacheVars = json.loads(sJSON)
print(" build made at: " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(dCacheVars.get("fBuildTime", 0))))
return dCacheVars
fBuildTime = time.time()
print("> read rules file...")
try:
lRules = open(spLang + "/rules.grx", 'r', encoding="utf-8").readlines()
except OSError:
print("Error. Rules file in project [" + sLang + "] not found.")
exit()
# removing comments, zeroing empty lines, creating definitions, storing tests, merging rule lines
print(" parsing rules...")
lRuleLine = []
lTest = []
lOpt = []
bGraph = False
lGraphRule = []
for i, sLine in enumerate(lRules, 1):
if sLine.startswith('#END'):
# arbitrary end
printBookmark(0, "BREAK BY #END", i)
break
elif sLine.startswith("#"):
# comment
pass
|
>
>
|
|
|
>
>
>
|
<
<
|
>
>
>
>
>
>
>
>
>
>
|
|
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
print(" {:>6}: {}".format(nLine, " " * nLevel + sComment))
def make (spLang, sLang, bUseCache=False):
"compile rules, returns a dictionary of values"
# for clarity purpose, don’t create any file here
dCacheVars = None
if os.path.isfile("_build/data_cache.json"):
print("> data cache found")
sJSON = open("_build/data_cache.json", "r", encoding="utf-8").read()
dCacheVars = json.loads(sJSON)
sBuildDate = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(dCacheVars.get("fBuildTime", 0)))
if bUseCache:
print("> use cache (no rebuild asked)")
print(" build made at: " + sBuildDate)
return dCacheVars
print("> read rules file...")
try:
sFileContent = open(spLang + "/rules.grx", 'r', encoding="utf-8").read()
except OSError:
print("Error. Rules file in project [" + sLang + "] not found.")
exit()
xHasher = hashlib.new("sha3_512")
xHasher.update(sFileContent.encode("utf-8"))
sFileHash = xHasher.hexdigest()
if dCacheVars and sFileHash == dCacheVars.get("sFileHash", ""):
print("> cache hash identical to file hash, use cache")
print(" build made at: " + sBuildDate)
return dCacheVars
# removing comments, zeroing empty lines, creating definitions, storing tests, merging rule lines
print(" parsing rules...")
fBuildTime = time.time()
lRuleLine = []
lTest = []
lOpt = []
bGraph = False
lGraphRule = []
for i, sLine in enumerate(sFileContent.split("\n"), 1):
if sLine.startswith('#END'):
# arbitrary end
printBookmark(0, "BREAK BY #END", i)
break
elif sLine.startswith("#"):
# comment
pass
|
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
sJSCallables += " return " + jsconv.py2js(sReturn) + ";\n"
sJSCallables += " },\n"
displayStats(lParagraphRules, lSentenceRules)
dVars = {
"fBuildTime": fBuildTime,
"callables": sPyCallables,
"callablesJS": sJSCallables,
"gctests": sGCTests,
"gctestsJS": sGCTestsJS,
"paragraph_rules": mergeRulesByOption(lParagraphRules),
"sentence_rules": mergeRulesByOption(lSentenceRules),
"paragraph_rules_JS": jsconv.writeRulesToJSArray(mergeRulesByOption(lParagraphRulesJS)),
|
>
|
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
sJSCallables += " return " + jsconv.py2js(sReturn) + ";\n"
sJSCallables += " },\n"
displayStats(lParagraphRules, lSentenceRules)
dVars = {
"fBuildTime": fBuildTime,
"sFileHash": sFileHash,
"callables": sPyCallables,
"callablesJS": sJSCallables,
"gctests": sGCTests,
"gctestsJS": sGCTestsJS,
"paragraph_rules": mergeRulesByOption(lParagraphRules),
"sentence_rules": mergeRulesByOption(lSentenceRules),
"paragraph_rules_JS": jsconv.writeRulesToJSArray(mergeRulesByOption(lParagraphRulesJS)),
|