Index: gc_core/js/lang_core/gc_engine.js ================================================================== --- gc_core/js/lang_core/gc_engine.js +++ gc_core/js/lang_core/gc_engine.js @@ -278,18 +278,18 @@ }, listRules: function (sFilter=null) { // generator: returns tuple (sOption, sLineId, sRuleId) try { - for ([sOption, lRuleGroup] of _getRules(true)) { + for ([sOption, lRuleGroup] of this._getRules(true)) { for ([_, _, sLineId, sRuleId, _, _] of lRuleGroup) { if (!sFilter || sRuleId.test(sFilter)) { yield [sOption, sLineId, sRuleId]; } } } - for ([sOption, lRuleGroup] of _getRules(false)) { + for ([sOption, lRuleGroup] of this._getRules(false)) { for ([_, _, sLineId, sRuleId, _, _] of lRuleGroup) { if (!sFilter || sRuleId.test(sFilter)) { yield [sOption, sLineId, sRuleId]; } } Index: gc_core/js/tests.js ================================================================== --- gc_core/js/tests.js +++ gc_core/js/tests.js @@ -1,11 +1,11 @@ // JavaScript "use strict"; -if (typeof(exports) !== 'undefined') { +if (typeof(require) !== 'undefined') { var helpers = require("resource://grammalecte/helpers.js"); } class TestGrammarChecking { @@ -15,12 +15,12 @@ this._aRuleTested = new Set(); }; * testParse (bDebug=false) { const t0 = Date.now(); - const aData = JSON.parse(helpers.loadFile("resource://grammalecte/"+this.gce.lang+"/tests_data.json")).aData; - //const aData = require("resource://grammalecte/"+this.gce.lang+"/tests_data.js").aData; + let sURL = (typeof(browser) !== 'undefined') ? browser.extension.getURL("grammalecte/"+this.gce.lang+"/tests_data.json") : "resource://grammalecte/"+this.gce.lang+"/tests_data.json"; + const aData = JSON.parse(helpers.loadFile(sURL)).aData; let nInvalid = 0 let nTotal = 0 let sErrorText; let sSugg; let sExpectedErrors; @@ -78,11 +78,11 @@ helpers.logerror(e); } if (bShowUntested) { i = 0; - for (let [sOpt, sLineId, sRuleId] of gce.listRules()) { + for (let [sOpt, sLineId, sRuleId] of this.gce.listRules()) { if (!this._aRuleTested.has(sLineId) && !/^[0-9]+[sp]$|^[pd]_/.test(sRuleId)) { sUntestedRules += sRuleId + ", "; i += 1; } } @@ -124,13 +124,13 @@ _getFoundErrors (sLine, bDebug, sOption) { try { let aErrs = []; if (sOption) { - gce.setOption(sOption, true); + this.gce.setOption(sOption, true); aErrs = this.gce.parse(sLine, "FR", bDebug); - gce.setOption(sOption, false); + this.gce.setOption(sOption, false); } else { aErrs = this.gce.parse(sLine, "FR", bDebug); } let sRes = " ".repeat(sLine.length); let sListErr = ""; Index: gc_lang/fr/webext/gce_worker.js ================================================================== --- gc_lang/fr/webext/gce_worker.js +++ gc_lang/fr/webext/gce_worker.js @@ -19,108 +19,88 @@ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export */ -helpers.echo("START"); - -helpers.echo(conj.getConj("devenir", ":E", ":2s")); - -helpers.echo(mfsp.getMasForm("emmerdeuse", true)); -helpers.echo(mfsp.getMasForm("pointilleuse", false)); - -helpers.echo(phonet.getSimil("est")); - -let oDict = new IBDAWG("French.json"); -helpers.echo(oDict.getMorph("merde")); - -gc_engine.load("JavaScript"); -let aRes = gc_engine.parse("Je suit..."); -for (let oErr of aRes) { - helpers.echo(text.getReadableError(oErr)); -} + let oTokenizer = null; let oLxg = null; function loadGrammarChecker (sGCOptions="", sContext="JavaScript") { - if (gce === null) { + if (gc_engine === null) { try { - gce = require("resource://grammalecte/fr/gc_engine.js"); + gc_engine = require("resource://grammalecte/fr/gc_engine.js"); helpers = require("resource://grammalecte/helpers.js"); text = require("resource://grammalecte/text.js"); tkz = require("resource://grammalecte/tokenizer.js"); lxg = require("resource://grammalecte/fr/lexicographe.js"); oTokenizer = new tkz.Tokenizer("fr"); helpers.setLogOutput(console.log); - gce.load(sContext); - oDict = gce.getDictionary(); + gc_engine.load(sContext); + oDict = gc_engine.getDictionary(); oLxg = new lxg.Lexicographe(oDict); if (sGCOptions !== "") { - gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); + gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); } - // we always retrieve options from the gce, for setOptions filters obsolete options - return gce.getOptions()._toString(); + // we always retrieve options from the gc_engine, for setOptions filters obsolete options + return gc_engine.getOptions()._toString(); } catch (e) { console.log("# Error: " + e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message); } } } function parse (sText, sLang, bDebug, bContext) { - let aGrammErr = gce.parse(sText, sLang, bDebug, bContext); + let aGrammErr = gc_engine.parse(sText, sLang, bDebug, bContext); return JSON.stringify(aGrammErr); } function parseAndSpellcheck (sText, sLang, bDebug, bContext) { - let aGrammErr = gce.parse(sText, sLang, bDebug, bContext); + let aGrammErr = gc_engine.parse(sText, sLang, bDebug, bContext); let aSpellErr = oTokenizer.getSpellingErrors(sText, oDict); return JSON.stringify({ aGrammErr: aGrammErr, aSpellErr: aSpellErr }); } function getOptions () { - return gce.getOptions()._toString(); + return gc_engine.getOptions()._toString(); } function getDefaultOptions () { - return gce.getDefaultOptions()._toString(); + return gc_engine.getDefaultOptions()._toString(); } function setOptions (sGCOptions) { - gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); - return gce.getOptions()._toString(); + gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); + return gc_engine.getOptions()._toString(); } function setOption (sOptName, bValue) { - gce.setOptions(new Map([ [sOptName, bValue] ])); - return gce.getOptions()._toString(); + gc_engine.setOptions(new Map([ [sOptName, bValue] ])); + return gc_engine.getOptions()._toString(); } function resetOptions () { - gce.resetOptions(); - return gce.getOptions()._toString(); + gc_engine.resetOptions(); + return gc_engine.getOptions()._toString(); } -function fullTests (sGCOptions="") { - if (!gce || !oDict) { +function fullTests (sGCOptions='{"nbsp":true, "esp":true, "unit":true, "num":true}') { + if (!gc_engine || !oDict) { return "# Error: grammar checker or dictionary not loaded." } - let dMemoOptions = gce.getOptions(); + let dMemoOptions = gc_engine.getOptions(); if (sGCOptions) { - gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); + gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions))); } - let tests = require("resource://grammalecte/tests.js"); - let oTest = new tests.TestGrammarChecking(gce); - let sAllRes = ""; + let oTest = new TestGrammarChecking(gc_engine); for (let sRes of oTest.testParse()) { - dump(sRes+"\n"); - sAllRes += sRes+"\n"; + helpers.echo(sRes+"\n"); } - gce.setOptions(dMemoOptions); - return sAllRes; + gc_engine.setOptions(dMemoOptions); } // Lexicographer @@ -148,6 +128,24 @@ sendResponse({response: "response from background script"}); } browser.runtime.onMessage.addListener(handleMessage); +helpers.echo("START"); + +helpers.echo(conj.getConj("devenir", ":E", ":2s")); + +helpers.echo(mfsp.getMasForm("emmerdeuse", true)); +helpers.echo(mfsp.getMasForm("pointilleuse", false)); + +helpers.echo(phonet.getSimil("est")); + +let oDict = new IBDAWG("French.json"); +helpers.echo(oDict.getMorph("merde")); + +gc_engine.load("JavaScript"); +let aRes = gc_engine.parse("Je suit..."); +for (let oErr of aRes) { + helpers.echo(text.getReadableError(oErr)); +} +//fullTests(); Index: gc_lang/fr/webext/manifest.json ================================================================== --- gc_lang/fr/webext/manifest.json +++ gc_lang/fr/webext/manifest.json @@ -41,10 +41,11 @@ "grammalecte/fr/phonet.js", "grammalecte/fr/cregex.js", "grammalecte/fr/gc_options.js", "grammalecte/fr/gc_rules.js", "grammalecte/fr/gc_engine.js", + "grammalecte/tests.js", "gce_worker.js" ] }, "web_accessible_resources": [ "grammalecte/_dictionaries/French.json",