conlangers-suite-test 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +71 -0
  3. package/dist/collator.d.ts +5 -0
  4. package/dist/conlangers-suite-test.cjs.js +771 -0
  5. package/dist/conlangers-suite-test.es.js +6073 -0
  6. package/dist/escape_mapper.d.ts +11 -0
  7. package/dist/generata/supra_builder.d.ts +14 -0
  8. package/dist/generata/word_builder.d.ts +23 -0
  9. package/dist/index.d.ts +6 -0
  10. package/dist/logger.d.ts +45 -0
  11. package/dist/main.d.ts +22 -0
  12. package/dist/parser.d.ts +79 -0
  13. package/dist/resolvers/canon_graphemes_resolver.d.ts +14 -0
  14. package/dist/resolvers/category_resolver.d.ts +26 -0
  15. package/dist/resolvers/feature_resolver.d.ts +23 -0
  16. package/dist/resolvers/generation_resolver.d.ts +34 -0
  17. package/dist/resolvers/nesca_grammar_stream.d.ts +15 -0
  18. package/dist/resolvers/trans_category_resolver.d.ts +15 -0
  19. package/dist/resolvers/transform_resolver.d.ts +63 -0
  20. package/dist/text_builder.d.ts +31 -0
  21. package/dist/transforma/carryover_associator.d.ts +11 -0
  22. package/dist/transforma/chance_mapper.d.ts +15 -0
  23. package/dist/transforma/greek.d.ts +2 -0
  24. package/dist/transforma/hangul.d.ts +3 -0
  25. package/dist/transforma/lettercase_mapper.d.ts +12 -0
  26. package/dist/transforma/reference_mapper.d.ts +14 -0
  27. package/dist/transforma/transformer.d.ts +66 -0
  28. package/dist/transforma/xsampa.d.ts +3 -0
  29. package/dist/utils/picker_utilities.d.ts +4 -0
  30. package/dist/utils/types.d.ts +161 -0
  31. package/dist/utils/utilities.d.ts +12 -0
  32. package/dist/word.d.ts +19 -0
  33. package/dist/word_bank.d.ts +27 -0
  34. package/package.json +92 -0
@@ -0,0 +1,771 @@
1
+ /*! Program: conlangers-suite-test -- version: 1.0.6 -- license: MIT -- author: Neonnaut
2
+ Repository: git+https://github.com/Neonnaut/the-conlangers-suite.git */
3
+ /*! conlangers-suite-test v1.0.6 | MIT */
4
+ "use strict"
5
+ var e=Object.defineProperty,t=(t,s,i)=>((t,s,i)=>s in t?e(t,s,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[s]=i)(t,"symbol"!=typeof s?s+"":s,i)
6
+ const s="[A-ZÁĆÉǴÍḰĹḾŃÓṔŔŚÚẂÝŹÄËḦÏÖÜẄẌŸǍČĎĚǦȞǏǨĽŇǑŘŠŤǓŽÀÈÌǸÒÙẀỲΓΔΘΛΞΠΣΦΨΩ]",i=e=>null==e?void 0:e[e.length-1]
7
+ function r(e){if(e.length>=2){const t=e[0],s=e.length-1,i=e[s]
8
+ e[0]=i,e[s]=t}return e}function n(e){const t=e.length
9
+ if(0===t)return""
10
+ if(1===t)return e[0]
11
+ return`${e.slice(0,t-1).join(", ")} and ${e[t-1]}`}function o(e,t,s=!1){const i=[...t.keys()].sort((e,t)=>t.length-e.length),r=(e,n=[])=>{let o="",a=0
12
+ for(;a<e.length;){let l=!1
13
+ for(const h of i)if(e.startsWith(h,a)){if(n.includes(h))o+="�"
14
+ else{const e=t.get(h),i=r((null==e?void 0:e.content)||"",[...n,h])
15
+ o+=s?`{${i}}`:i}a+=h.length,l=!0
16
+ break}l||(o+=e[a++])}return o}
17
+ return r(e)}function a(e,t){const s=[]
18
+ let i=0
19
+ for(;i<e.length;){let r=!1
20
+ for(const n of t.sort((e,t)=>t.length-e.length))if(e.startsWith(n,i)){s.push(n),i+=n.length,r=!0
21
+ break}r||(s.push(e[i]),i++)}return s}const l=["categories","words","units","alphabet","invisible","graphemes","syllable-boundaries","features","feature-field","stage","letter-case-field","schema"],h=["<",">","@","⇒","→","->",">>","_","{","}","[","]","(",")","0","/","!","#","$","+","?",":","*","&","%","|","~","=","1","2","3","4","5","6","7","8","9"],c=[...h,"^"]
22
+ class _{constructor(e,s,i,r,n,o,a,l,h,c,_,u){t(this,"logger"),t(this,"escape_mapper"),t(this,"lettercase_mapper"),t(this,"chance_mapper"),t(this,"num_of_words"),t(this,"output_mode"),t(this,"remove_duplicates"),t(this,"force_word_limit"),t(this,"sort_words"),t(this,"input_divider"),t(this,"output_divider"),t(this,"directive","none"),t(this,"disable_directive",!1),t(this,"directive_name"),t(this,"category_distribution"),t(this,"category_pending"),t(this,"units"),t(this,"optionals_weight"),t(this,"wordshape_distribution"),t(this,"wordshape_pending"),t(this,"feature_pending"),t(this,"schema_input"),t(this,"schema_output"),t(this,"stages_pending",[]),t(this,"substages_pending",[]),t(this,"graphemes"),t(this,"syllable_boundaries"),t(this,"graphemes_pending",""),t(this,"alphabet"),t(this,"invisible"),t(this,"file_line_num",0),t(this,"app"),t(this,"current_stage_name"),this.logger=e,this.app=s,this.escape_mapper=i,this.lettercase_mapper=r,this.chance_mapper=n,""===o&&(o="100")
23
+ let p=Number(o)
24
+ isNaN(p)?(this.logger.warn(`Number of words '${p}' was not a number. Genearating 100 words instead`),p=100):Number.isInteger(p)||(this.logger.warn(`Number of words '${p}' was rounded to the nearest whole number`),p=Math.ceil(p)),(p>1e5||p<1)&&(this.logger.warn(`Number of words '${p}' was not between 1 and 100,000. Genearating 100 words instead`),p=100),this.num_of_words=p,this.output_mode=a,this.sort_words=l,this.remove_duplicates=h,this.force_word_limit=c,this.input_divider=""===_?"\\n":_,this.input_divider=this.input_divider.replace(/\\n/g,"\n"),this.output_divider="vocabug"===s?""===u?" ":u:""===u?"\n":u,this.output_divider=this.output_divider.replace(/\\n/g,"\n"),"paragraph"===this.output_mode?(this.sort_words=!1,this.remove_duplicates=!1,this.force_word_limit=!1,this.output_divider=" "):"debug"===this.output_mode&&(this.sort_words=!1,this.remove_duplicates=!1,this.force_word_limit=!1,this.output_divider="\n\n"),this.category_distribution="gusein-zade",this.category_pending=new Map,this.optionals_weight=10,this.units=new Map,this.wordshape_distribution="zipfian",this.wordshape_pending={content:"",line_num:0},this.stages_pending=[],this.substages_pending=[],this.feature_pending=new Map,this.alphabet=[],this.invisible=[],this.graphemes_pending="",this.graphemes=[],this.syllable_boundaries=[],this.disable_directive=!1,this.directive_name="",this.current_stage_name="",this.schema_input={fields:[],delimiters:[]},this.schema_output={fields:[],delimiters:[]}}get_line(e){let t=e[this.file_line_num]
25
+ return t=this.escape_mapper.escape_backslash_pairs(t),t=t.replace(/;.*/u,"").trim(),t=this.escape_mapper.escape_named_escape(t),t.includes("&[")&&this.logger.validation_error("Invalid named escape",this.file_line_num),t}parse_file(e){const t=e.split("\n")
26
+ let s="none",i="none",r="none",n=[],o=[],a=""
27
+ for(;this.file_line_num<t.length;++this.file_line_num){let e=this.get_line(t)
28
+ if(""===e)continue
29
+ if(e.startsWith("@")&&(s=this.parse_decorator(e,s),"none"!=s)){n=[]
30
+ continue}const l=this.parse_directive(e,s)
31
+ if("none"==l){if(!this.disable_directive){if("none"===i&&this.logger.validation_error("Invalid syntax -- expected a decorator or directive",this.file_line_num),"categories"===i){const[t,s,i]=this.get_cat_seg_fea(e,"category")
32
+ i||this.logger.validation_error(e+" is not a category declaration",this.file_line_num),this.category_pending.set(t,{content:s,line_num:this.file_line_num})}if("words"!==i){if("units"===i){"vocabug"!==this.app&&this.logger.validation_error("Units directive is only valid in Vocabug",this.file_line_num)
33
+ const[t,s,i]=this.get_cat_seg_fea(e,"unit")
34
+ i||this.logger.validation_error(e+" is not a unit declaration",this.file_line_num),this.validate_unit(s)||this.logger.validation_error(`The unit '${t}' had separator(s) outside sets -- expected separators for units to appear only in sets`,this.file_line_num),this.valid_words_brackets(s)||this.logger.validation_error(`The unit '${t}' had missmatched brackets`,this.file_line_num),this.units.set(`<${t}>`,{content:""+s,line_num:this.file_line_num})}if("schema"===i){"vocabug"===this.app&&this.logger.validation_error("Schema directive is not valid in Vocabug",this.file_line_num)
35
+ const[t,s,i]=this.get_schema(e)
36
+ "input"===t?(s.includes("word")||this.logger.validation_error("Input schema must include a 'word' field",this.file_line_num),this.schema_input={fields:s,delimiters:i}):"output"===t&&(this.schema_output={fields:s,delimiters:i})}if("features"===i){const[t,s,i]=this.get_cat_seg_fea(e,"feature")
37
+ i||this.logger.validation_error(e+" is not a feature declaration",this.file_line_num)
38
+ const r=s.split(/[,\s]+/).filter(Boolean)
39
+ 0==r.length&&this.logger.validation_error(`Feature ${t} had no graphemes`,this.file_line_num),this.feature_pending.set(t,{content:r.join(","),line_num:this.file_line_num})}if("feature-field"===i){if(0===n.length){const t=e.split(/[\s]+/).filter(Boolean)
40
+ t.length<2&&this.logger.validation_error("Feature-field header too short",this.file_line_num),n=t
41
+ continue}this.parse_featurefield(e,n)}if("letter-case-field"===i){if(0===n.length){const t=e.split(/[\s]+/).filter(Boolean)
42
+ t.length<2&&this.logger.validation_error("letter-case-field header too short",this.file_line_num),n=t
43
+ continue}this.parse_lettercasefield(e,n)}if("alphabet"===i){const t=e.split(/[,\s]+/).filter(Boolean)
44
+ for(let e=0;e<t.length;e++)t[e]=this.escape_mapper.restore_escaped_chars(t[e]).trim()
45
+ this.alphabet.push(...t)}if("invisible"===i){const t=e.split(/[,\s]+/).filter(Boolean)
46
+ for(let e=0;e<t.length;e++)t[e]=this.escape_mapper.restore_escaped_chars(t[e]).trim()
47
+ this.invisible.push(...t)}if("graphemes"!==i){if("syllable-boundaries"===i){const t=e.split(/[,\s]+/).filter(Boolean)
48
+ for(let e=0;e<t.length;e++)t[e]=this.escape_mapper.restore_escaped_chars(t[e]).trim()
49
+ this.syllable_boundaries.push(...t)}if("stage"===i){if("clusterfield"===r){if(e.startsWith(">")){for(const e of o)this.push_transform_to_stage(e)
50
+ r="none",n=[],o=[]
51
+ continue}o=this.parse_clusterfield(e,n,o)
52
+ continue}if("<"===e)this.logger.validation_error("Feature-field header was empty",this.file_line_num)
53
+ else{if(e.startsWith("< ")){0!=a.length&&this.logger.validation_error("Wrapped rule was not completed before starting cluster-field",this.file_line_num),o.push({t_type:"cluster-field",target:"",result:"",conditions:[],exceptions:[],chance:this.chance_mapper.get_last_chance(),line_num:this.file_line_num}),e=e.substring(2).trim()
54
+ const t=e.split(/[\s]+/).filter(Boolean)
55
+ t.length<2&&this.logger.validation_error("Feature-field header too short",this.file_line_num),r="clusterfield",n=t
56
+ continue}if(e.startsWith("<routine")){0!=a.length&&this.logger.validation_error("Wrapped rule was not completed before starting routine",this.file_line_num)
57
+ const t=this.parse_routine(e)
58
+ this.push_transform_to_stage({t_type:t,target:"\\",result:"\\",conditions:[],exceptions:[],chance:this.chance_mapper.get_last_chance(),line_num:this.file_line_num})
59
+ continue}if(e.startsWith("<@chance")){const t=e.match(/^<@chance\s*=\s*(\d+(?:\.\d+)?)%$/)
60
+ if(this.chance_mapper.check_parsing&&this.logger.validation_error("Cannot start a new chance while another chance is being parsed",this.file_line_num),t){const e=t[1]
61
+ this.chance_mapper.add_chance(Number(e)),this.chance_mapper.check_parsing=!0}else this.logger.validation_error("Invalid chance syntax",this.file_line_num)
62
+ continue}if(!e.startsWith(">")){if(/(->|=>|>>|⇒|→|\/|!)$/.test(e)){a+=" "+e
63
+ continue}e=a+" "+e,a=""
64
+ const[t,s,i,r]=this.get_transform(e)
65
+ this.push_transform_to_stage({t_type:"rule",target:t,result:s,conditions:i,exceptions:r,chance:this.chance_mapper.get_last_chance(),line_num:this.file_line_num})
66
+ continue}this.chance_mapper.check_parsing=!1}}}else this.graphemes_pending+=" "+e}else"vocabug"!==this.app&&this.logger.validation_error("Words directive is only valid in Vocabug",this.file_line_num),this.valid_words_brackets(e)||this.logger.validation_error("Wordshapes had missmatched brackets",this.file_line_num),this.wordshape_pending.content+=" "+e,this.wordshape_pending.line_num=this.file_line_num}}else if("none"!=r&&this.logger.validation_error(r+" was not closed before directive change",this.file_line_num),i=l,s="none",!0===this.disable_directive?this.disable_directive=!1:"p"===this.disable_directive&&(this.disable_directive=!0),"stage"===i){const e={transforms_pending:[],name:this.current_stage_name}
67
+ this.current_stage_name="",this.stages_pending.push(e)}}"none"!=s&&this.logger.validation_error(`Decorator '${s}' was not followed by a directive`,this.file_line_num)}push_transform_to_stage(e){let t=i(this.stages_pending)
68
+ t||(t={transforms_pending:[],name:"default"},this.stages_pending.push(t)),t.transforms_pending.push(e)}get_cat_seg_fea(e,t){if(""===e)return["","",!1]
69
+ const i=e.split("=")
70
+ if(2!==i.length)return[e,"",!1]
71
+ const r=i[0].trim(),n=i[1].trim()
72
+ if(""===r||""===n)return[e,"",!1]
73
+ const o=RegExp(`^${s}$`),a=/^[A-Za-z+$-]+$/,l=/^(\+|-|>)[a-zA-Z+-]+$/
74
+ if("category"===t){if(o.test(r))return[r,n,!0]}else if("unit"===t){if(a.test(r))return[r,n,!0]}else if("feature"===t&&l.test(r))return[r,n,!0]
75
+ return[e,"",!1]}parse_distribution(e){return e.toLowerCase().startsWith("g")?"gusein-zade":e.toLowerCase().startsWith("z")?"zipfian":e.toLowerCase().startsWith("s")?"shallow":"flat"}validate_unit(e){let t=!1,s=!1
76
+ for(let i=0;i<e.length;i++){const r=e[i]
77
+ if("{"===r?t=!0:"}"===r?t=!1:"("===r?s=!0:")"===r&&(s=!1),!(","!==r&&" "!==r||t||s))return!1}return!0}parse_decorator(e,t){let s="none"
78
+ e=e.substring(1)
79
+ const i=((e=this.escape_mapper.restore_preserve_escaped_chars(e)).match(/\./g)||[]).length,r=(e.match(/=/g)||[]).length
80
+ 1!==i&&this.logger.validation_error("Invalid decorator format1",this.file_line_num)
81
+ const[n,o]=e.split(/\.(.+)/).filter(Boolean)
82
+ if(1===r){let[e,t]=o.split("=")
83
+ if(e=e.trim(),t=t.trim(),"words"===n){if("distribution"===e)this.wordshape_distribution=this.parse_distribution(t),s="words"
84
+ else if("optionals-weight"===e){t.endsWith("%")||this.logger.validation_error(`Invalid optionals-weight '${t}' -- expected a percentage value ending with '%'`,this.file_line_num),t=t.slice(0,-1).trim()
85
+ const e=(e=>{const t=Number(e)
86
+ return Number.isInteger(t)&&t>=1&&t<=100?t:null})(t)
87
+ null==e&&this.logger.validation_error(`Invalid optionals-weight '${t}' -- expected a number between 1 and 100`,this.file_line_num),this.optionals_weight=e,s="words"}}else"categories"===n?"distribution"===e&&(this.category_distribution=this.parse_distribution(t),s="categories"):"stage"===n&&"name"===e&&(this.current_stage_name=t,s="stage")}else"disabled"===o?l.includes(n)?(s=n,this.disable_directive="p"):this.logger.validation_error("Invalid directive name on decorator "+n,this.file_line_num):this.logger.validation_error("Invalid decorator format2",this.file_line_num)
88
+ return"none"===s?this.logger.validation_error("Invalid decorator",this.file_line_num):"none"!==t&&t!==s&&this.logger.validation_error(`Decorator mismatch -- expected '${t}' decorator after '${t}' decorator`,this.file_line_num),s}parse_directive(e,t){let s="none"
89
+ return"categories:"===e?s="categories":"words:"===e?s="words":"units:"===e?s="units":"alphabet:"===e?s="alphabet":"invisible:"===e?s="invisible":"graphemes:"===e?s="graphemes":"syllable-boundaries:"===e?s="syllable-boundaries":"features:"===e?s="features":"feature-field:"===e?s="feature-field":"stage:"===e?s="stage":"letter-case-field:"===e?s="letter-case-field":"schema:"===e&&(s="schema"),"none"===s?"none":("none"!=t&&s!=t&&this.logger.validation_error(`Directive mismatch -- expected '${t}' directive after '${t}' decorator`,this.file_line_num),s)}valid_words_brackets(e){const t=[],s={")":"(",">":"<","}":"{"}
90
+ for(const i of e)if(Object.values(s).includes(i))t.push(i)
91
+ else if(Object.keys(s).includes(i)&&(0===t.length||t.pop()!==s[i]))return!1
92
+ return 0===t.length}parse_clusterfield(e,t,s){0===s.length&&this.logger.validation_error("Clusterfield transform not started properly",this.file_line_num)
93
+ const i=s[0]
94
+ i.target+=", ",i.result+=", "
95
+ const r=e.split(/[\s]+/).filter(Boolean),n=r.shift()
96
+ r.length===t.length&&void 0!==n||this.logger.validation_error(`Cluster-field row length mismatch with header length -- expected row length of ${t.length} but got lenght of ${r.length}`,this.file_line_num)
97
+ const o=[],a=[]
98
+ for(let l=0;l<t.length;++l)"+"!==r[l]&&(o.push(n+t[l]),a.push(r[l]))
99
+ return i.target+=o.join(", "),i.result+=a.join(", "),[i]}parse_routine(e){1!==((e=this.escape_mapper.restore_preserve_escaped_chars(e)).match(/=/g)||[]).length&&this.logger.validation_error(`Invalid routine format1 '${e}'`,this.file_line_num)
100
+ let[,t]=e.split("=")
101
+ t=t.trim()
102
+ 1!==(t.match(/>/g)||[]).length&&this.logger.validation_error(`Invalid routine format '${e}'`,this.file_line_num)
103
+ let[s]=t.split(">")
104
+ switch(s=s.trim(),s=s.replace(/\bcapitalize\b/g,"capitalise"),s=s.replace(/\blatin-to-hangeul\b/g,"latin-to-hangul"),s){case"reverse":case"compose":case"decompose":case"capitalise":case"decapitalise":case"to-uppercase":case"to-lowercase":case"latin-to-hangul":case"hangul-to-latin":case"greek-to-latin":case"latin-to-greek":case"xsampa-to-ipa":case"ipa-to-xsampa":return s}this.logger.validation_error(`Invalid routine '${s}'`,this.file_line_num)}get_transform(e){""===e&&this.logger.validation_error("No input",this.file_line_num)
105
+ const t=(e=e.replace(/\/\//g,"!")).split(/>>|->|→|=>|⇒/)
106
+ 1===t.length&&this.logger.validation_error("No arrows in transform",this.file_line_num),2!==t.length&&this.logger.validation_error("Too many arrows in transform",this.file_line_num)
107
+ const s=t[0].trim()
108
+ ""===s&&this.logger.validation_error("Target is empty in transform",this.file_line_num),this.valid_transform_brackets(s)||this.logger.validation_error("Target had missmatched brackets",this.file_line_num)
109
+ const i=t[1].indexOf("/"),r=t[1].indexOf("!"),n=Math.min(-1===i?1/0:i,-1===r?1/0:r),o=n===1/0?t[1].trim():t[1].slice(0,n).trim()
110
+ ""==o&&this.logger.validation_error("Result is empty in transform",this.file_line_num),this.valid_transform_brackets(o)||this.logger.validation_error("Result had missmatched brackets",this.file_line_num)
111
+ const a=n===1/0?"":t[1].slice(n).trim(),{conditions:l,exceptions:h}=this.get_environment(a)
112
+ return[s,o,l,h]}get_schema(e){""===e&&this.logger.validation_error("Schema declaration cannot be empty",this.file_line_num)
113
+ const t=e.split("=")
114
+ 2!==t.length&&this.logger.validation_error("Schema declaration was invalid",this.file_line_num)
115
+ const s=t[0].trim()
116
+ "input"!==s&&"output"!==s&&this.logger.validation_error("Schema declaration was not for 'input' or 'output'",this.file_line_num)
117
+ const i=t[1].trim(),r=[],n=[]
118
+ let o=0
119
+ const a=i.length
120
+ for(i.startsWith("<")&&n.push("");o<a;){if("<"===i[o]){const e=o+1
121
+ let t=e
122
+ for(;t<a&&">"!==i[t];)t++
123
+ t>=a&&this.logger.validation_error("unterminated field",this.file_line_num)
124
+ const s=i.slice(e,t)
125
+ r.push(s),o=t+1
126
+ continue}const e=o
127
+ for(;o<a&&"<"!==i[o];)o++
128
+ const t=i.slice(e,o)
129
+ n.push(t)}return n.length<r.length+1&&n.push(""),[s,r,n]}get_environment(e){const t=[],s=[]
130
+ let i="",r="condition"
131
+ for(let n=0;n<e.length;n++){const o=e[n]
132
+ if("/"===o){if(i.trim()){const e=this.validate_environment(i.trim(),r);("condition"===r?t:s).push(e)}i="",r="condition"}else if("!"===o){if(i.trim()){const e=this.validate_environment(i.trim(),r);("condition"===r?t:s).push(e)}i="",r="exception"}else i+=o}if(i.trim()){const e=i.trim(),n=this.validate_environment(e,r);("condition"===r?t:s).push(n)}return{conditions:t,exceptions:s}}validate_environment(e,t){const s=e.split("_")
133
+ 2!==s.length&&this.logger.validation_error(`${t} "${e}" must contain exactly one underscore`,this.file_line_num)
134
+ const[i,r]=s
135
+ return i||r||this.logger.validation_error(`${t} "${e}" must have content on at least one side of '_'`,this.file_line_num),`${i}_${r}`}parse_featurefield(e,t){const s=e.split(/[\s]+/).filter(Boolean),i=s.shift()
136
+ s.length===t.length&&void 0!==i||this.logger.validation_error(`Feature-field row length mismatch with header length -- expected row length of ${t.length} but got lenght of ${s.length}`,this.file_line_num);/^[a-zA-Z+-]+$/.test(i)||this.logger.validation_error("A feature in a feature-field must be of lowercase letters only.",this.file_line_num)
137
+ const r=[],n=[],o=t.length
138
+ for(let a=0;a<o;++a)"."!==s[a]&&("+"===s[a]?r.push(t[a]):"-"===s[a]?n.push(t[a]):this.logger.validation_error(`Feature-field values must be either '+', '-', or '.' -- found '${s[a]}' instead.`,this.file_line_num))
139
+ r.length>0&&this.feature_pending.set("+"+i,{content:r.join(","),line_num:this.file_line_num}),n.length>0&&this.feature_pending.set("-"+i,{content:n.join(","),line_num:this.file_line_num})}parse_lettercasefield(e,t){const s=e.split(/[\s]+/).filter(Boolean),i=s.shift()
140
+ "uppercase"!==i&&this.logger.validation_error("Letter-case-field first column must be 'uppercase'",this.file_line_num),s.length===t.length&&void 0!==i||this.logger.validation_error(`Feature-field row length mismatch with header length -- expected row length of ${t.length} but got lenght of ${s.length}`,this.file_line_num)
141
+ const r=new Map(t.map((e,t)=>[e,s[t]]))
142
+ this.lettercase_mapper.create_map(r)}valid_transform_brackets(e){const t=[],s={")":"(","}":"{","]":"["}
143
+ for(const i of e)if(Object.values(s).includes(i))t.push(i)
144
+ else if(Object.keys(s).includes(i)&&(0===t.length||t.pop()!==s[i]))return!1
145
+ return 0===t.length}}const u=class e{constructor(e,s){t(this,"current_form"),t(this,"rejected"),t(this,"num_of_transformations"),t(this,"steps"),t(this,"field_values"),this.rejected=!1,this.current_form=s.word,this.num_of_transformations=0,this.steps=[],this.field_values=s,null===e?this.steps.push({action:null,form:s.word,line_num:null}):this.steps.push({action:e,form:s.word,line_num:null})}get_last_form(){return this.current_form}get_word(){const t=[]
146
+ if("debug"==e.output_mode){for(let e=0;e<this.steps.length;e++){const s=this.steps[e]
147
+ let i=s.form||"",r=s.action||"",n=s.line_num||""
148
+ i&&(i=`⟨${i}⟩`),i&&r&&(r+=" ➤ "),n&&(n=" @ ln:"+n),t.push(`${r}${i}${n}`)}return t.join("\n")}if("old-to-new"==e.output_mode){const e=this.steps[0]
149
+ let s=""
150
+ return e&&(s=e.form||""),t.push(`${s} => ${this.current_form}`),t.join("")}return t.push(""+this.current_form),t.join("")}recombine_word_by_schema(t){let s="",i=0
151
+ for(s+=e.field_delimiters[0]||"";i<e.fields.length;){const r=e.fields[i]
152
+ let n=""
153
+ n="word"===r?this.current_form:t[r]||"�",s+=n
154
+ s+=e.field_delimiters[i+1]||"�",i++}return s}record_transformation(e,t,s){this.steps.push({action:e,form:t,line_num:s+1}),this.num_of_transformations++}record_output(){let t=""
155
+ t=e.field_delimiters.length>0?this.recombine_word_by_schema(this.field_values):this.get_last_form(),this.steps.push({form:t,action:null,line_num:null}),this.current_form=t}record_step(e,t,s){this.steps.push({action:e,form:t,line_num:s})}}
156
+ t(u,"output_mode","word-list"),t(u,"fields",[]),t(u,"field_delimiters",[])
157
+ let p=u
158
+ function d(e,t){const s=t.reduce((e,t)=>e+t,0)
159
+ let i=Math.random()*s
160
+ for(let r=0;r<e.length;r++){if(i<t[r])return e[r]
161
+ i-=t[r]}return""}function g(e,t){return 1==e?[1]:"zipfian"===t?function(e){const t=[]
162
+ for(let s=0;s<e;++s)t.push(10/Math.pow(s+1,.9))
163
+ return t}(e):"gusein-zade"===t?function(e){const t=[]
164
+ for(let s=0;s<e;++s)t.push(Math.log(e+1)-Math.log(s+1))
165
+ return t}(e):"shallow"===t?function(e){const t=[]
166
+ for(let s=0;s<e;++s){const i=s+1,r=.5-s/(e-1)*.07
167
+ t.push(1/Math.pow(i,r))}return t}(e):function(e){const t=[]
168
+ for(let s=0;s<e;++s)t.push(1)
169
+ return t}(e)}class m{constructor(e,s,i,r,n,o,a){t(this,"escape_mapper"),t(this,"supra_builder"),t(this,"categories"),t(this,"wordshapes"),t(this,"category_distribution"),t(this,"optionals_weight"),this.escape_mapper=e,this.supra_builder=s,this.categories=i,this.wordshapes=r,this.category_distribution=n,this.optionals_weight=o,p.output_mode=a}make_word(){let e=d(this.wordshapes.items,this.wordshapes.weights)
170
+ const t=this.resolve_wordshape_sets(e,this.category_distribution,this.optionals_weight)
171
+ let s=t
172
+ if(1!=this.supra_builder.id_counter){const[e,i]=this.supra_builder.extract_letters_and_weights(t),r=function(e,t){for(let r=0;r<e.length;r++)if("s"===t[r])return e[r]
173
+ const s=t.reduce((e,t)=>"number"==typeof t&&t>0?e+t:e,0)
174
+ if(0===s)return""
175
+ let i=Math.random()*s
176
+ for(let r=0;r<e.length;r++){const s=t[r]
177
+ if(!("number"!=typeof s||s<=0)){if(i<s)return e[r]
178
+ i-=s}}return""}(e,i)
179
+ s=this.supra_builder.replace_letter_and_clean(t,Number(r))}let i=""
180
+ for(let n=0;n<s.length;n++){let e=s[n]
181
+ for(const[t,s]of this.categories)if(t==e){e=d(s.graphemes,s.weights)
182
+ break}i+=e}let r=i.replace(/\^/g,"")
183
+ return 0!=this.escape_mapper.counter&&(e=this.escape_mapper.restore_escaped_chars(e),r=this.escape_mapper.restore_escaped_chars(r)),new p(e,{word:r})}resolve_wordshape_sets(e,t,s){const i=/\{[^{}]*\}/g,r=/\([^()]*\)/g
184
+ let n,o,a=[]
185
+ for(;null!==(n=e.match(r));){const i=n[n.length-1],r=i.slice(1,-1).split(/[,\s]+/).filter(Boolean)
186
+ if(100*Math.random()<s&&r.length>0){const s=r.some(e=>e.includes("*"))?"flat":t
187
+ o=this.extract_value_and_weight(r,s)
188
+ const n=d(o[0],o[1])
189
+ e=e.replace(i,n)}else e=e.replace(i,"")}for(;null!==(n=e.match(i));){const s=n[n.length-1]
190
+ if(a=s.slice(1,-1).split(/[,\s]+/).filter(Boolean),0===a.length)a=["^"]
191
+ else{const e=a.some(e=>e.includes("*"))?"flat":t
192
+ o=this.extract_value_and_weight(a,e)
193
+ a=[d(o[0],o[1])]}e=e.replace(s,a[0])}return e}extract_value_and_weight(e,t){let s=[],i=[]
194
+ return e.every(e=>!e.includes("*"))?(s=e,i=g(e.length,t),[s,i]):(e.forEach(e=>{const[t,r]=e.split("*"),n=r&&!isNaN(Number(r))?parseFloat(r):1
195
+ s.push(t),i.push(n)}),[s,i])}}class f{constructor(){t(this,"map",new Map),t(this,"capture_stream_index",null),t(this,"capture_stream",[]),t(this,"is_capturing_sequence",!1),this.map=new Map}reset_capture_stream_index(){this.capture_stream_index=null}set_capture_stream_index(e){this.capture_stream_index=e}capture_reference(e,t){if(null===this.capture_stream_index){const s=i(t)
196
+ s?this.map.set(e,[s]):this.map.set(e,[""])}else{const s=t.slice(this.capture_stream_index)
197
+ this.map.set(e,s)}}get_captured_reference(e){return this.map.get(e)??[e]}clone(){const e=new f
198
+ return e.map=new Map(this.map),e.capture_stream_index=this.capture_stream_index,e}absorb(e){for(const[t,s]of e.map.entries())this.map.set(t,s)}}const w={"b_<":595,"d_<":599,"d`":598,"g_<":608,"h\\":614,"j\\":669,"l\\":634,"l`":621,"n`":627,"p\\":632,"r\\":633,"r\\`":635,"r`":637,"s\\":597,"s`":642,"t`":648,"x\\":615,"z\\":657,"z`":656,A:593,B:946,"B\\":665,C:231,D:240,E:603,F:625,G:611,"G\\":610,"G\\_<":667,H:613,"H\\":668,I:618,J:626,"J\\":607,"J\\_<":644,K:620,"K\\":622,L:654,"L\\":671,M:623,"M\\":624,N:331,"N\\":628,O:596,"O\\":664,"v\\":651,P:651,Q:594,R:641,"R\\":640,S:643,T:952,U:650,V:652,W:653,X:967,"X\\":295,Y:655,Z:658,'"':712,"%":716,":":720,":\\":721,"@":601,"@\\":600,"@`":602,"{":230,"}":649,1:616,2:248,3:604,"3\\":606,4:638,5:619,6:592,7:612,8:629,9:339,"&":630,"?":660,"?\\":661,"<\\":674,">\\":673,"^":42779,"!":42780,"!\\":451,"|":124,"|\\":448,"||":8214,"|\\|\\":449,"=\\":450,"-\\":8255,'_"':776,"_+":799,"_-":800,_0:805,"_>":700,"_?\\":740,"_^":815,"_}":794,"`":734,"=":809,"_=":809,"~":771,"_~)":771,_A:792,_a:826,_B:783,_B_L:7622,_c:796,_d:810,_e:820,"<F>":8600,_G:736,_H:769,_H_T:7620,_h:688,_j:690,"'":690,_k:816,_L:768,_l:737,_M:772,_m:827,_N:828,_n:8319,_O:825,_o:798,_q:793,"<R>":8599,_R_F:7624,_r:797,_T:779,_t:804,_v:812,_w:695,_X:774,_x:829,_F:770,"_\\":770,_R:780,"_/":780,"b\\":11377,"!\\!\\":8252,_f:846,_i:8595,_s:845,_u:840,_W:841,_z:842},v=Object.keys(w).sort((e,t)=>t.length-e.length),b={595:"b_<",599:"d_<",598:"d`",608:"g_<",614:"h\\",669:"j\\",634:"l\\",621:"l`",627:"n`",632:"p\\",633:"r\\",635:"r\\`",637:"r`",597:"s\\",642:"s`",648:"t`",615:"x\\",657:"z\\",656:"z`",593:"A",946:"B",665:"B\\",231:"C",240:"D",603:"E",625:"F",611:"G",610:"G\\",667:"G\\_<",613:"H",668:"H\\",618:"I",626:"J",607:"J\\",644:"J\\_<",620:"K",622:"K\\",654:"L",671:"L\\",623:"M",624:"M\\",331:"N",628:"N\\",596:"O",664:"O\\",651:"v\\",594:"Q",641:"R",640:"R\\",643:"S",952:"T",650:"U",652:"V",653:"W",967:"X",295:"X\\",655:"Y",658:"Z",712:'"',716:"%",720:":",721:":\\",601:"@",600:"@\\",602:"@`",230:"{",649:"}",616:"1",248:"2",604:"3",606:"3\\",638:"4",619:"5",592:"6",612:"7",629:"8",339:"9",630:"&",660:"?",661:"?\\",674:"<\\",673:">\\",42779:"^",42780:"!",451:"!\\",124:"|",448:"|\\",8214:"||",449:"|\\|\\",450:"=\\",8255:"-\\",776:'_"',799:"_+",800:"_-",805:"_0",700:"_>",740:"_?\\",815:"_^",794:"_}",734:"`",809:"=",771:"~",792:"_A",826:"_a",783:"_B",7622:"_B_L",796:"_c",810:"_d",820:"_e",8600:"<F>",736:"_G",769:"_H",7620:"_H_T",688:"_h",690:"_j",816:"_k",768:"_L",737:"_l",772:"_M",827:"_m",828:"_N",8319:"_n",825:"_O",798:"_o",793:"_q",8599:"<R>",7624:"_R_F",797:"_r",779:"_T",804:"_t",812:"_v",695:"_w",774:"_X",829:"_x",770:"_F",780:"_R",11377:"b\\",8252:"!\\!\\",846:"_f",8595:"_i",845:"_s",840:"_u",841:"_W",842:"_z"}
199
+ const y={gk:1,dt:4,bp:8,ch:13,kh:14,th:15,ph:17,k:0,n:2,t:3,r:5,m:6,p:7,s:9,z:10,c:12,j:13,x:18},$={gk:2,gn:21,ch:23,kh:24,th:25,ph:26,k:1,n:4,t:7,r:8,m:16,p:17,s:19,z:20,c:22,x:27},k={"uí":16,"ụí":19,yo:12,yu:17,"yẹ":3,ya:2,ye:7,"yọ":6,wa:9,"wẹ":10,"wọ":14,we:15,wi:11,o:8,u:13,"ẹ":1,a:0,"ọ":4,e:5,"ụ":18,i:20},x=[12593,12594,12596,12599,12600,12601,12609,12610,12611,12613,12614,12615,12616,12617,12618,12619,12620,12621,12622]
200
+ function j(e,t,s){return String.fromCharCode(44032+588*(e>=0?e:0)+28*(t>=0?t:0)+(s>=0?s:0))}const M={}
201
+ for(const[X,K]of Object.entries(y))M[K]=X
202
+ const T={}
203
+ for(const[X,K]of Object.entries(k))T[K]=X
204
+ const E={}
205
+ for(const[X,K]of Object.entries($))E[K]=X
206
+ const A={a:"α","á":"ά","à":"ὰ",e:"ε","é":"έ","è":"ὲ","ẹ":"η","ẹ́":"ή","ẹ̀":"ὴ",i:"ι","í":"ί","ì":"ὶ",o:"ο","ó":"ό","ò":"ὸ","ọ":"ω","ọ́":"ώ","ọ̀":"ὼ",u:"υ","ú":"ύ","ù":"ὺ",b:"β",d:"δ",f:"φ",g:"γ",k:"κ",l:"λ",m:"μ",n:"ν",p:"π",r:"ρ",s:"σ",t:"τ",x:"χ",z:"ζ",h:"ͱ","č":"ͷ",c:"ϛ",q:"ξ","þ":"θ","ṕ":"ψ","š":"ϸ",w:"ϝ",j:"ϳ"},B=Object.fromEntries(Object.entries(A).map(([e,t])=>[t,e]))
207
+ class R{constructor(){t(this,"caryover_list"),this.caryover_list=[]}set_item(e,t){this.caryover_list.push({entry_id:e,variant_id:t})}get_result_associateme(e,t){const s=this.find_first_item()
208
+ if(!s)return null
209
+ const[i,r]=s,n=e.base_id,o=this.find_grapheme(i,n,r,t)
210
+ return o?i!=e.entry_id?null:(this.remove_first_item(),o):null}find_first_item(){const e=null==(t=this.caryover_list)?void 0:t[0]
211
+ var t
212
+ return e?[e.entry_id,e.variant_id]:void 0}remove_first_item(){this.caryover_list.shift()}find_grapheme(e,t,s,i){if(e<0||e>=i.length)return null
213
+ const r=i[e]
214
+ if(s<0||s>=r.variants.length)return null
215
+ const n=r.variants[s]
216
+ return t<0||t>=n.length?null:n[t]}}class W{constructor(e,s,i,r,n,o,a,l,h){t(this,"logger"),t(this,"stages",[]),t(this,"substages",[]),t(this,"graphemes"),t(this,"lettercase_mapper"),t(this,"chance_mapper"),t(this,"syllable_boundaries"),t(this,"debug",!1),t(this,"associateme_mapper"),this.logger=e,this.graphemes=s,this.lettercase_mapper=i,this.chance_mapper=r,this.syllable_boundaries=n,this.associateme_mapper=h,this.debug="debug"===l,this.stages=o,this.substages=a}run_routine(e,t,s,i){const r=s.join("")
217
+ let n=""
218
+ switch(e){case"decompose":n=r.normalize("NFD")
219
+ break
220
+ case"compose":n=r.normalize("NFC")
221
+ break
222
+ case"capitalise":n=this.lettercase_mapper.capitalise(r)
223
+ break
224
+ case"decapitalise":n=this.lettercase_mapper.decapitalise(r)
225
+ break
226
+ case"to-uppercase":n=this.lettercase_mapper.to_uppercase(r)
227
+ break
228
+ case"to-lowercase":n=this.lettercase_mapper.to_lowercase(r)
229
+ break
230
+ case"reverse":n=(o=s,o.slice().reverse()).join("")
231
+ break
232
+ case"xsampa-to-ipa":n=function(e){let t="",s=0
233
+ for(;s<e.length;){let i=!1
234
+ for(const r of v)if(e.startsWith(r,s)){t+=String.fromCharCode(w[r]),s+=r.length,i=!0
235
+ break}i||(t+=e[s],s++)}return t}(r)
236
+ break
237
+ case"ipa-to-xsampa":n=function(e){let t=""
238
+ for(let s=0;s<e.length;s++){const i=e.charCodeAt(s),r=b[i]
239
+ t+=void 0!==r?r:e[s]}return t}(r)
240
+ break
241
+ case"latin-to-hangul":n=function(e){let t=""
242
+ const s=Object.keys(y),i=Object.keys(k),r=Object.keys($)
243
+ for(;e.length>0;){let n=0,o=null
244
+ for(const t of s)if(e.startsWith(t)){o=y[t],n+=t.length
245
+ break}let a=null
246
+ for(const t of i)if(e.slice(n).startsWith(t)){a=k[t],n+=t.length
247
+ break}if(null===o&&null===a){t+=e[0],e=e.slice(1)
248
+ continue}if(null!==o&&null===a){t+=String.fromCharCode(x[o]),e=e.slice(n)
249
+ continue}let l=null,h=null
250
+ for(const t of r)if(e.slice(n).startsWith(t)){const s=e.slice(n+t.length)
251
+ i.some(e=>s.startsWith(e))||(l=$[t],h=t)
252
+ break}null!==h&&(n+=h.length),t+=j(null!==o?o:11,a,l??0),e=e.slice(n)}return t}(r)
253
+ break
254
+ case"hangul-to-latin":n=function(e){let t=""
255
+ for(const s of e){const e=s.charCodeAt(0)
256
+ if(e<44032||e>55203){t+=s
257
+ continue}const i=e-44032,r=Math.floor(i/588),n=Math.floor(i%588/28),o=i%28
258
+ t+=(11===r?"":M[r]??"")+(T[n]??"")+(0===o?"":E[o]??"")}return t}(r)
259
+ break
260
+ case"latin-to-greek":n=function(e){let t=""
261
+ for(const s of e)t+=A[s]??s
262
+ return t}(r)
263
+ break
264
+ case"greek-to-latin":n=function(e){let t=""
265
+ for(const s of e)t+=B[s]??s
266
+ return t}(r)
267
+ break
268
+ default:this.logger.validation_error("This should not have happened")}var o
269
+ return t.record_transformation(`<routine = ${e}>`,n,i),a(n,this.graphemes)}target_to_word_match(e,t,s,i){for(let r=0;r<=e.length;r++){const n=this.match_pattern_at(e,t,r,s,i,e.length)
270
+ if(null!==n)return[n.start,n.end-n.start,n.matched]}return[0,0,[]]}result_former(e,t,s,i){const n=[]
271
+ for(let o=0;o<e.length;o++){const a=e[o]
272
+ if("grapheme"===a.type)if(a.association){const e=i.get_result_associateme(a.association,this.associateme_mapper)
273
+ if(null===e){a.max===1/0&&this.logger.validation_error("This should not have happened: infinite max grapheme??")
274
+ for(let e=0;e<a.max;e++)n.push(a.base)}else for(let t=0;t<a.min;t++)n.push(e)}else for(let e=0;e<a.min;e++)n.push(a.base)
275
+ else if("target-mark"===a.type)for(let e=0;e<t.length;e++)n.push(t[e])
276
+ else if("metathesis-mark"===a.type){const e=r([...t])
277
+ n.push(...e)}else if("reference-start-capture"===a.type)s.set_capture_stream_index(n.length)
278
+ else if("reference-capture"===a.type)s.capture_reference(a.key,n)
279
+ else if("reference-mark"===a.type){const e=s.get_captured_reference(a.key)
280
+ n.push(...e)}}return s.reset_capture_stream_index(),n}resolve_association(e,t){for(let s=0;s<e.length;s++){const i=e[s]
281
+ for(let e=0;e<i.variants.length;e++){const r=i.variants[e]
282
+ for(let i=0;i<r.length;i++)if(r[i]===t)return{entry_id:s,base_id:i,variant_id:e}}}return null}get_variant_id_for_base(e,t,s,i){if(t<0||t>=e.length)return null
283
+ const r=e[t]
284
+ if(s<0||s>=r.bases.length)return null
285
+ for(let n=0;n<r.variants.length;n++)if(r.variants[n][s]===i)return n
286
+ return null}match_pattern_at(e,t,s,i,n,o,a){let l=s,h=0
287
+ const c=[]
288
+ for(;h<t.length;){const s=t[h]
289
+ if("grapheme"!==s.type&&"wildcard"!==s.type&&"anythings-mark"!==s.type&&"target-mark"!==s.type&&"metathesis-mark"!==s.type&&"syllable-boundary"!==s.type&&"word-boundary"!==s.type&&"empty-mark"!==s.type&&"reference-capture"!==s.type&&"reference-mark"!==s.type&&"reference-start-capture"!==s.type){h++
290
+ continue}const _=s.min,u=s.max,p=void 0!==o?Math.min(u,o-l):u
291
+ if("grapheme"===s.type)if(s.association){let t=0
292
+ const i=s.association.entry_id,r=s.association.base_id
293
+ for(;t<s.max&&l+t<e.length;){const o=e[l+t],a=this.get_variant_id_for_base(this.associateme_mapper,i,r,o)
294
+ if(null===a)break
295
+ s.association.is_target&&n&&n.set_item(i,a),t++}if(t<s.min)return null
296
+ c.push(...e.slice(l,l+t)),l+=t}else{let t=0
297
+ for(;t<p&&e[l+t]===s.base;)t++
298
+ if(t<_)return null
299
+ c.push(...e.slice(l,l+t)),l+=t}else if("target-mark"===s.type){a&&0!==a.length||this.logger.validation_error("Target-mark requires a non-empty target_stream")
300
+ const t=a,i=t.length,r=s.min,n=s.max,h=void 0!==o?Math.min(n,Math.floor((o-l)/i)):n
301
+ let _=0
302
+ for(;_<h&&e.slice(l+_*i,l+(_+1)*i).every((e,s)=>e===t[s]);)_++
303
+ if(_<r)return null
304
+ const u=_*i
305
+ c.push(...e.slice(l,l+u)),l+=u}else if("metathesis-mark"===s.type){a&&0!==a.length||this.logger.validation_error("Metathesis-mark requires a non-empty target_stream")
306
+ const t=r([...a]),i=t.length,n=s.min,h=s.max,_=void 0!==o?Math.min(h,Math.floor((o-l)/i)):h
307
+ let u=0
308
+ for(;u<_&&e.slice(l+u*i,l+(u+1)*i).every((e,s)=>e===t[s]);)u++
309
+ if(u<n)return null
310
+ const p=u*i
311
+ c.push(...e.slice(l,l+p)),l+=p}else if("empty-mark"===s.type)c.push(""),l+=0
312
+ else if("wildcard"===s.type){const t=Math.min(p,e.length-l)
313
+ if(t<_)return null
314
+ c.push(...e.slice(l,l+t)),l+=t}else if("syllable-boundary"===s.type){let t=0
315
+ if(this.syllable_boundaries.includes(e[l])){for(;t<p&&e[l+t]===e[l];)t++
316
+ if(t<_)return null
317
+ c.push(...e.slice(l,l+t)),l+=t}else{if(0!==l&&l!==e.length)return null
318
+ if(_>1)return null
319
+ c.push("$")}}else if("word-boundary"===s.type){if(0!==l&&l!==e.length)return null
320
+ if(_>1)return null
321
+ c.push("#")}else if("anythings-mark"===s.type){const t=s.blocked_by??[],i=s.consume??[]
322
+ let r=0
323
+ e:for(;r<p&&void 0!==e[l+r];){for(const s of t){const t=s.length,i=e.slice(l+r,l+r+t)
324
+ if(i.length===t&&i.every((e,t)=>e===s[t]))break e}for(const t of i){const s=t.length,i=e.slice(l+r,l+r+s)
325
+ if(i.length===s&&i.every((e,s)=>e===t[s])){r+=s
326
+ break e}}r++}if(r<s.min)return null
327
+ c.push(...e.slice(l,l+r)),l+=r}else if("reference-start-capture"===s.type)i.set_capture_stream_index(c.length)
328
+ else if("reference-capture"===s.type)i.capture_reference(s.key,c)
329
+ else if("reference-mark"===s.type){const t=i.get_captured_reference(s.key),r=t.length
330
+ if(0===r)return null
331
+ const n=void 0!==o?Math.min(s.max,Math.floor((o-l)/r)):s.max
332
+ let a=0
333
+ for(;a<n&&e.slice(l+a*r,l+(a+1)*r).every((e,s)=>e===t[s]);)a++
334
+ if(a<s.min)return null
335
+ const h=a*r
336
+ c.push(...e.slice(l,l+h)),l+=h}h++}return i.reset_capture_stream_index(),{start:s,end:l,matched:c}}environment_match(e,t,s,i,r,n,o){const a=[" / "],l=i.length,h=r
337
+ let c=!1
338
+ for(let d=0;d<=s;d++){const i=this.match_pattern_at(e,h,d,o,null,s,t)
339
+ if(null!==i&&i.end===s){c=!0,a.push(...i.matched)
340
+ break}}if(!c)return[!1,""]
341
+ a.push("_")
342
+ const _=n,u=s+l,p=this.match_pattern_at(e,_,u,o,null,e.length,t)
343
+ return null===p?[!1,""]:(a.push(...p.matched),[!0,a.join("")])}replacementa(e,t,s,i,r){t.sort((e,t)=>e.index_span-t.index_span)
344
+ const n=new Set,o=new Map,a=new Map
345
+ for(const p of t)0===p.length_span?(o.has(p.index_span)||o.set(p.index_span,[]),o.get(p.index_span).push(...p.replacement_stream)):a.set(p.index_span,{length_span:p.length_span,replacement_stream:p.replacement_stream})
346
+ const l=[],h=[],c=[]
347
+ let _=0
348
+ for(;_<e.length;){if(o.has(_))for(const e of o.get(_))h.push("^"),c.push(e),l.push(e)
349
+ const t=a.get(_)
350
+ if(t&&![...Array(t.length_span).keys()].some(e=>n.has(_+e))){const s=e.slice(_,_+t.length_span)
351
+ t.replacement_stream.length>0&&l.push(...t.replacement_stream),h.push(s.join("")),c.push(0===t.replacement_stream.length?"∅":t.replacement_stream.join(""))
352
+ for(let e=0;e<t.length_span;e++)n.add(_+e)
353
+ _+=t.length_span}else l.push(e[_]),_++}if(o.has(e.length))for(const p of o.get(e.length))h.push("^"),c.push(p),l.push(p)
354
+ const u=l
355
+ if(h.length>0){let e=""
356
+ for(const t of i){e+=` ! ${t.before.map(e=>e.base).join("")}_${t.after.map(e=>e.base).join("")}`}let n=""
357
+ for(const s of t)if(0!=s.matched_conditions.length)for(const e of s.matched_conditions)n+=e
358
+ const o=`${h.join(", ")} → ${c.join(", ")}`
359
+ s.record_transformation(`${o}${n}${e}`,u.join(" "),r)}return u}apply_transform(e,t,s){const{t_type:i,target:r,result:n,conditions:o,exceptions:a,line_num:l}=s
360
+ if("rule"!=i&&"cluster-field"!=i)return t=this.run_routine(i,e,t,l)
361
+ r.length!==n.length&&this.logger.validation_error("Mismatched target/result concurrent set lengths in a transform",l)
362
+ const h=[]
363
+ for(let c=0;c<r.length;c++){const s=new f,i=new R,_=r[c],u=n[c]
364
+ let p="replacement"
365
+ if("deletion"===u[0].type?p="deletion":"reject"===u[0].type?p="reject":this.target_to_word_match(t,_,s,i),"insertion"===_[0].type){"deletion"!==p&&"reject"!==p||this.logger.validation_error(`Inserion of ${p} is not valid`,l),0===o.length&&this.logger.validation_error("Insertion without a condition is not valid",l),p="insertion"
366
+ for(let e=0;e<=t.length;e++){const r=this.result_former(u,t,s,i),n=[]
367
+ let l=0===o.length
368
+ for(const i of o){const o=s.clone(),[a,h]=this.environment_match(t,r,e,[],i.before,i.after,o)
369
+ a&&(n.push(h),s.absorb(o),l=!0)}const c=a.some(i=>{const n=s.clone(),[o]=this.environment_match(t,r,e,[],i.before,i.after,n)
370
+ return o})
371
+ if(!l||c)continue
372
+ const _=this.result_former(u,t,s,i)
373
+ h.push({index_span:e,length_span:0,target_stream:["^"],replacement_stream:_,matched_conditions:n})}}else{let r=0
374
+ for(;r<=t.length;){const[n,c,d]=this.target_to_word_match(t.slice(r),_,s,i)
375
+ if(0===c){r++
376
+ continue}const g=r+n,m=[]
377
+ let f=0===o.length
378
+ for(const e of o){const i=s.clone(),[r,n]=this.environment_match(t,d,g,d,e.before,e.after,i)
379
+ r&&(m.push(n),s.absorb(i),f=!0)}const w=a.some(e=>{const i=s.clone(),[r]=this.environment_match(t,d,g,d,e.before,e.after,i)
380
+ return r})
381
+ if(f&&!w){if("reject"===p)return e.rejected=!0,e.record_transformation(d.join("")+" → 0","∅",l),t
382
+ if("deletion"===p)h.push({index_span:g,length_span:c,target_stream:d,replacement_stream:[],matched_conditions:m})
383
+ else{const e=this.result_former(u,d,s,i)
384
+ h.push({index_span:g,length_span:c,target_stream:d,replacement_stream:e,matched_conditions:m})}r=g+c}else r=g+1}}}return t=this.replacementa(t,h,e,a,l)}do_transforms(e,t){if(""==e.get_last_form())return e.rejected=!0,e
385
+ if(0==t.length)return e
386
+ let s=a(e.get_last_form(),this.graphemes)
387
+ for(const i of t){if(e.rejected)break
388
+ null==i.chance||!1!==this.chance_mapper.get_is_success(i.chance)?(0!=i.target.length||"rule"!==i.t_type&&"cluster-field"!==i.t_type)&&(s=this.apply_transform(e,s,i),e.current_form=s.join(""),0===s.length&&(e.rejected=!0,this.debug&&e.record_step("REJECT-NULL-WORD",null,null))):e.record_step("CHANCE FAILED - SKIPPED transform",null,i.line_num)}return e}do_stages(e){this.chance_mapper.roll_all()
389
+ for(const t of this.stages)t.name&&e.record_step("stage = "+t.name,null,null),e=this.do_transforms(e,t.transforms)
390
+ return e.rejected||e.record_output(),e}get_variant_id(e,t,s){const{entry_id:i,base_id:r}=s
391
+ if(i<0||i>=e.length)return null
392
+ const n=e[i]
393
+ if(r<0||r>=n.bases.length)return null
394
+ for(let o=0;o<n.variants.length;o++)if(n.variants[o][r]===t)return o
395
+ return null}}function I(e,t,s,i){const r=[],n=new Map
396
+ for(let l=0;l<t.length;l++){const e=t[l],s=e.current_form
397
+ r.push(s)
398
+ const i=n.get(s)
399
+ i?i.push(e):n.set(s,[e])}const o=function(e,t,s,i=[]){if(0===s.length){if(0==i.length)return t.sort(Intl.Collator().compare)
400
+ {const e=new Set(i),s=Intl.Collator()
401
+ return t.map(t=>({original:t,stripped:h(t,e)})).sort((e,t)=>s.compare(e.stripped,t.stripped)).map(e=>e.original)}}s.push("�")
402
+ const r=new Map
403
+ s.forEach((e,t)=>r.set(e,t))
404
+ const n=new Set(i),o=new Set
405
+ function a(e){const t=[],s=Array.from(r.keys()).concat(Array.from(n)).sort((e,t)=>t.length-e.length)
406
+ let i=0
407
+ for(;i<e.length;){let r=!1
408
+ for(const n of s)if(e.startsWith(n,i)){t.push(n),i+=n.length,r=!0
409
+ break}r||(t.push(e[i]),i+=1)}return t}function l(e,t){const s=a(e).filter(e=>!n.has(e)),i=a(t).filter(e=>!n.has(e))
410
+ for(let n=0;n<Math.max(s.length,i.length);n++){const e=s[n],t=i[n]
411
+ if(void 0===e)return-1
412
+ if(void 0===t)return 1
413
+ const a=r.get(e),l=r.get(t)
414
+ if(void 0===a&&o.add(e),void 0===l&&o.add(t),(a??1/0)!==(l??1/0))return(a??1/0)-(l??1/0)}return 0}function h(e,t){const s=Array.from(t).sort((e,t)=>t.length-e.length)
415
+ let i="",r=0
416
+ for(;r<e.length;){let t=!1
417
+ for(const i of s)if(e.startsWith(i,r)){r+=i.length,t=!0
418
+ break}t||(i+=e[r],r+=1)}return i}const c=[...t].sort(l)
419
+ if(o.size>0)return e.warn(`The custom order stated in 'alphabet' was ignored because words had unknown graphemes: '${Array.from(o).join(", ")}' missing from 'alphabet'`),t.sort(Intl.Collator().compare)
420
+ return c}(e,r,s,i),a=[]
421
+ for(let l=0;l<o.length;l++){const e=o[l],t=n.get(e)
422
+ if(!t||0===t.length)continue
423
+ const s=t.shift()
424
+ a.push(s)}return a}class C{constructor(e,s,i,r,n,o,a,l,h,c,_){t(this,"logger"),t(this,"lettercase_mapper"),t(this,"build_start"),t(this,"num_of_words"),t(this,"output_mode"),t(this,"remove_duplicates"),t(this,"force_word_limit"),t(this,"sort_words"),t(this,"output_divider"),t(this,"alphabet"),t(this,"invisible"),t(this,"terminated"),t(this,"words"),t(this,"num_of_duplicates"),t(this,"num_of_rejects"),t(this,"num_of_duds"),t(this,"upper_gen_limit"),this.logger=e,this.lettercase_mapper=s,this.build_start=i,this.num_of_words=r,this.output_mode=a,this.remove_duplicates=n,this.force_word_limit=o,this.sort_words=h,this.output_divider=l,this.alphabet=c,this.invisible=_,this.terminated=!1,this.words=[],this.num_of_duplicates=0,this.num_of_rejects=0,this.num_of_duds=0,this.upper_gen_limit=5*r,this.upper_gen_limit>1e6&&(this.upper_gen_limit=1e6),"debug"===this.output_mode&&this.show_debug()}add_word(e){let t=!1
425
+ if(e.rejected&&"debug"!==p.output_mode)this.num_of_rejects++,this.num_of_duds++
426
+ else if(this.remove_duplicates){let s=!1
427
+ const i=e.get_last_form()
428
+ for(let e=0;e<this.words.length;e++)if(this.words[e].get_last_form()===i){s=!0
429
+ break}s?(this.num_of_duplicates++,this.num_of_duds++):t=!0}else t=!0
430
+ t&&this.words.push(e),this.words.length>=this.num_of_words?this.terminated=!0:(Date.now()-this.build_start>=3e4||this.num_of_duds>=this.upper_gen_limit&&!this.force_word_limit)&&(this.terminated=!0,this.remove_duplicates?this.logger.warn("Could not generate the requested amount of words. Try adding more unique word-shapes or remove some reject transforms"):this.logger.warn("Could not generate the requested amount of words. Try adding more word-shapes or remove some reject transforms"))}create_record(){const e=Date.now()-this.build_start,t=Math.ceil(e/100)/10,s=t.toFixed(t%1==0?0:1),i="1"===s?s+" second":s+" seconds",r=[]
431
+ 1==this.words.length?r.push("1 word generated"):this.words.length>1?r.push(this.words.length+" words generated"):0==this.words.length&&r.push("Zero words generated"),1==this.num_of_duplicates?r.push("1 duplicate word removed"):this.num_of_duplicates>1&&r.push(this.num_of_duplicates+" duplicate words removed"),1==this.num_of_rejects?r.push("1 word rejected"):this.num_of_rejects>1&&r.push(this.num_of_rejects+" words rejected"),this.logger.info(`${n(r)} -- in ${i}`)}make_text(){this.create_record(),this.sort_words&&(this.words=I(this.logger,this.words,this.alphabet,this.invisible))
432
+ const e=[]
433
+ for(let t=0;t<this.words.length;t++)e.push(this.words[t].get_word())
434
+ return"paragraph"===this.output_mode?this.paragraphify(e):e.join(this.output_divider)}paragraphify(e){if(0===e.length)return""
435
+ if(1===e.length)return this.lettercase_mapper.capitalise(e[0])+this.random_end_punctuation()
436
+ const t=[]
437
+ let s=!0
438
+ for(let r=0;r<e.length;r++){let i=e[r]
439
+ if(s&&(i=this.lettercase_mapper.capitalise(i),s=!1),r===e.length-1)t.push(i)
440
+ else if(r%7==0&&0!==r){const e=this.random_end_punctuation()
441
+ t.push(i+e),s=!0}else r%6==0&&0!==r?t.push(i+","):t.push(i)}let i=t.join(" ")
442
+ return i=i.replace(/[,\s]*$/,""),i+=this.random_end_punctuation(),i}random_end_punctuation(){const e=Math.random()
443
+ return e<.005?"...":e<.03?"!":e<.08?"?":"."}show_debug(){const e="Num of words: "+this.num_of_words+"\nOutput mode: "+this.output_mode+"\nRemove duplicates: "+this.remove_duplicates+"\nForce word limit: "+this.force_word_limit+"\nSort words: "+this.sort_words+'\nOutput divider: "'+this.output_divider+'"\nAlphabet: '+this.alphabet.join(", ")+"\nInvisible: "+this.invisible.join(", ")
444
+ this.logger.diagnostic(e)}}class N{constructor(){t(this,"errors"),t(this,"warnings"),t(this,"infos"),t(this,"diagnostics"),t(this,"payload"),t(this,"Uncaught_Error",class extends Error{constructor(e){super(e.message),this.name=e.name||"Error",Object.setPrototypeOf(this,new.target.prototype),e.stack&&(this.stack=e.stack)}}),t(this,"Validation_Error",class extends Error{constructor(e){super(e),this.name="Validation_Error",Object.setPrototypeOf(this,new.target.prototype)}}),this.errors=[],this.warnings=[],this.infos=[],this.diagnostics=[],this.payload=""}uncaught_error(e){const t=new this.Uncaught_Error(e),s=this.extract_location(t.stack),i=`${t.name}: ${t.message}${s?" @ "+s:""}`
445
+ this.errors.push(i)}extract_location(e){if(!e)return null
446
+ const t=e.split("\n")
447
+ for(const s of t){const e=s.match(/(?:\(|\bat\s+)?(.*?):(\d+):(\d+)\)?/)
448
+ if(e){let t=e[1].replace(/\?.*$/,"")
449
+ return t=t.replace(/^.*\/src\//,"modules/"),t=t.replace(/(\bmodules\b\/)\1/,"$1"),`${t}:${e[2]}`}}return null}validation_error(e,t=null){const s=new this.Validation_Error(e)
450
+ throw t||0===t?this.errors.push(`Error: ${e} @ line ${t+1}.`):this.errors.push(`Error: ${e}.`),s}warn(e,t=null){t||0===t?this.warnings.push(`Warning: ${e} @ line ${t+1}.`):this.warnings.push(`Warning: ${e}.`)}info(e){this.infos.push(e+".")}diagnostic(e){this.diagnostics.push(e)}set_payload(e){this.payload=e}create_log(){return{payload:this.payload,errors:this.errors,warnings:this.warnings,infos:this.infos,diagnostics:this.diagnostics}}}const O={"&[Space]":" ","&[Tab]":"\t","&[Newline]":"\n","&[Acute]":"́","&[DoubleAcute]":"̋","&[Grave]":"̀","&[DoubleGrave]":"̏","&[Circumflex]":"̂","&[Caron]":"̌","&[Breve]":"̆","&[BreveBelow]":"̮","&[InvertedBreve]":"̑","&[InvertedBreveBelow]":"̯","&[TildeAbove]":"̃","&[TildeBelow]":"̰","&[Macron]":"̄","&[MacronBelow]":"̱","&[MacronBelowStandalone]":"˗","&[Dot]":"̇","&[DotBelow]":"̣","&[Diaeresis]":"̈","&[DiaeresisBelow]":"̤","&[Ring]":"̊","&[RingBelow]":"̥","&[Horn]":"̛","&[Hook]":"̉","&[CommaAbove]":"̓","&[CommaBelow]":"̦","&[Cedilla]":"̧","&[Ogonek]":"̨","&[VerticalLineBelow]":"̩","&[VerticalLineAbove]":"̍","&[DoubleVerticalLineBelow]":"͈","&[PlusSignBelow]":"̟","&[PlusSignStandalone]":"˖","&[uptackBelow]":"̝","&[UpTackStandalone]":"˔","&[LeftTackBelow]":"̘","&[rightTackBelow]":"̙","&[DownTackBelow]":"̞","&[DownTackStandalone]":"˕","&[BridgeBelow]":"̪","&[BridgeAbove]":"͆","&[InvertedBridgeBelow]":"̺","&[SquareBelow]":"̻","&[SeagullBelow]":"̼","&[LeftBracketBelow]":"͉"}
451
+ class S{constructor(){t(this,"map"),t(this,"counter"),this.map=new Map,this.map.set("\0"," "),this.counter=1}escape_backslash_pairs(e){const t=new Map
452
+ return e.replace(/\\(.)/g,(e,s)=>{if(t.has(s))return t.get(s)
453
+ const i=String.fromCharCode(57344+this.counter)
454
+ return t.set(s,i),this.map.set(i,s),this.counter++,i})}escape_special_chars(e){const t=new Set(h),s=new Map
455
+ return e.split("").map(e=>{if(t.has(e)){if(s.has(e))return s.get(e)
456
+ const t=`&[${this.counter}]`
457
+ return s.set(e,t),this.map.set(t,e),this.counter++,t}return e}).join("")}escape_named_escape(e){return e.replace(/&\[[A-Za-z]+\]/g,e=>O[e]??e)}restore_escaped_chars(e){return e.split("").map(e=>this.map.has(e)?this.map.get(e):e).join("")}restore_preserve_escaped_chars(e){return e.split("").map(e=>this.map.has(e)?"\\"+this.map.get(e):e).join("")}}class F{constructor(){t(this,"map"),t(this,"reverse_map"),this.map=new Map,this.reverse_map=new Map}create_map(e){const t=Array.from(e.entries()).sort(([e],[t])=>t.length-e.length)
458
+ this.map=new Map(t)
459
+ const s=t.slice().sort(([,e],[,t])=>t.length-e.length).map(([e,t])=>[t,e])
460
+ this.reverse_map=new Map(s)}tokenise(e){const t=[]
461
+ let s=0
462
+ for(;s<e.length;){let i=!1
463
+ for(const[r]of this.map)if(r&&e.startsWith(r,s)){t.push(r),s+=r.length,i=!0
464
+ break}i||(t.push(e[s]),s++)}return t}capitalise(e){if(!e)return e
465
+ const t=this.tokenise(e),s=t[0]??""
466
+ return(this.map.get(s)??(s?s[0].toUpperCase()+s.slice(1):""))+t.slice(1).join("")}decapitalise(e){if(!e)return e
467
+ const t=this.tokenise(e),s=t[0]??""
468
+ return(this.reverse_map.get(s)??(s?s[0].toUpperCase()+s.slice(1):""))+t.slice(1).join("")}to_uppercase(e){return e?this.tokenise(e).map(e=>this.map.get(e)??e.toUpperCase()).join(""):e}to_lowercase(e){return e?this.tokenise(e).map(e=>this.reverse_map.get(e)??e.toLowerCase()).join(""):e}}class L{constructor(e){t(this,"logger"),t(this,"weights"),t(this,"letters"),t(this,"id_counter"),this.logger=e,this.weights={},this.letters={},this.id_counter=1}process_string(e,t){const i=RegExp(`^(\\^|${s})(?:\\*((\\d+(?:\\.\\d+)?)|s))?$`)
469
+ return e.replace(/\[([^\]]*)\]/g,(e,s)=>{const r=i.exec(s)
470
+ r||this.logger.validation_error(`Invalid supra-set item '${e}' -- expected all supra-set items to look like '[A]', '[^]' or '[A*2]'`,t)
471
+ const n=r[1],o=r[2],a="s"===o?"s":o?Number(o):1,l=this.id_counter++
472
+ return this.weights[l]=a,this.letters[l]=n,`[${l}]`})}extract_letters_and_weights(e){const t=/\[(\d+)\]/g,s=[],i=[]
473
+ let r
474
+ for(;null!==(r=t.exec(e));){const e=Number(r[1])
475
+ e in this.letters&&e in this.weights||this.logger.validation_error(`Missing data for ID '${e}'`,null),s.push(e.toString()),i.push(this.weights[e])}return[s,i]}replace_letter_and_clean(e,t){return e.replace(/\[(\d+)\]/g,(e,s)=>{const i=Number(s)
476
+ return i in this.letters||this.logger.validation_error(`Unknown ID '${i}' found in input.`,null),i===t?""+this.letters[i]:""})}get_weights(){return this.weights}get_letters(){return this.letters}}class U{constructor(e,s,i,r,n,o,a,l){t(this,"logger"),t(this,"output_mode"),t(this,"nesca_grammar_stream"),t(this,"categories"),t(this,"stages_pending"),t(this,"stages"),t(this,"substages_pending"),t(this,"substages"),t(this,"syllable_boundaries"),t(this,"features",new Map),t(this,"line_num"),this.logger=e,this.output_mode=s,this.nesca_grammar_stream=i,this.categories=r,this.stages_pending=n,this.substages_pending=o,this.stages=[],this.substages=[],this.features=a,this.syllable_boundaries=0===l.length?["."]:l,this.line_num=0,this.resolve_stages(),"debug"===this.output_mode&&this.show_debug()}resolve_stages(){for(const e of this.stages_pending)this.stages.push({transforms:this.resolve_transforms(e.transforms_pending),name:e.name})}resolve_transforms(e){const t=[]
477
+ for(let s=0;s<e.length;s++){if(this.line_num=e[s].line_num,"cluster-field"===e[s].t_type){t.push({t_type:e[s].t_type,target:this.get_cluser_field_graphemes(e[s].target,"TARGET"),result:this.get_cluser_field_graphemes(e[s].result,"RESULT"),conditions:[],exceptions:[],chance:null,line_num:this.line_num})
478
+ continue}if("rule"!==e[s].t_type){t.push({t_type:e[s].t_type,target:[],result:[],conditions:[],exceptions:[],chance:null,line_num:this.line_num})
479
+ continue}const i=e[s].target,r=this.categories_into_transform(i),n=this.features_into_transform(r),o=this.resolve_alt_opt(n),a=e[s].result,l=this.categories_into_transform(a),h=this.features_into_transform(l),c=this.resolve_alt_opt(h),{result_array:_,target_array:u}=this.normaliseTransformLength(o,c),p=_.flat(),d=u.flat(),g=[]
480
+ for(let e=0;e<d.length;e++)g.push(this.nesca_grammar_stream.main_parser(d[e],"TARGET",this.line_num))
481
+ const m=[]
482
+ for(let e=0;e<p.length;e++)m.push(this.nesca_grammar_stream.main_parser(p[e],"RESULT",this.line_num))
483
+ const f=e[s].chance,w=[],v=[]
484
+ for(let t=0;t<e[s].conditions.length;t++){let i=e[s].conditions[t]
485
+ i=this.categories_into_transform(i),i=this.features_into_transform(i),this.valid_transform_brackets(i)||this.logger.validation_error(`Invalid brackets in condition "${i}"`,this.line_num),this.valid_environment(i)||this.logger.validation_error(`Found separators outside sets in condition "${i}"`,this.line_num)
486
+ const r=this.resolve_alt_opt(i)
487
+ for(let e=0;e<r[0].length;e++){const[t,s]=this.environment_helper(r[0][e]),i=this.nesca_grammar_stream.main_parser(t,"BEFORE",this.line_num),n=this.nesca_grammar_stream.main_parser(s,"AFTER",this.line_num)
488
+ w.push({before:i,after:n})}}for(let t=0;t<e[s].exceptions.length;t++){let i=e[s].exceptions[t]
489
+ i=this.categories_into_transform(i),i=this.features_into_transform(i),this.valid_transform_brackets(i)||this.logger.validation_error(`Invalid brackets in exception "${i}"`,this.line_num),this.valid_environment(i)||this.logger.validation_error(`Found separators outside sets in condition "${i}"`,this.line_num)
490
+ const r=this.resolve_alt_opt(i)
491
+ for(let e=0;e<r[0].length;e++){const[t,s]=this.environment_helper(r[0][e]),i=this.nesca_grammar_stream.main_parser(t,"BEFORE",this.line_num),n=this.nesca_grammar_stream.main_parser(s,"AFTER",this.line_num)
492
+ v.push({before:i,after:n})}}t.push({t_type:e[s].t_type,target:g,result:m,conditions:w,exceptions:v,chance:f,line_num:this.line_num})}return t}environment_helper(e){const[t="",s=""]=e.split("_",2)
493
+ return[t.trim(),s.trim()]}split_top_level(e){const t=[]
494
+ let s=0,i=""
495
+ for(let r=0;r<e.length;r++){const n=e[r]
496
+ "["===n||"("===n||"{"===n?s++:"]"!==n&&")"!==n&&"}"!==n||s--,","!==n&&!/\s/.test(n)||0!==s?i+=n:(i.trim()&&t.push(i.trim()),i="")}return i.trim()&&t.push(i.trim()),t}check_grammar_rules(e){const t=[]
497
+ for(let s=0;s<e.length;s++){const i=e[s]
498
+ if("{"!==i&&"("!==i||(t.length>=1&&this.logger.validation_error("Nested alternator / optionalator not allowed",this.line_num),t.push({char:i,index:s})),"}"===i||")"===i){0===t.length&&this.logger.validation_error("Mismatched closing bracket",this.line_num)
499
+ const{char:r,index:n}=t.pop()
500
+ "{"===r&&"}"===i||"("===r&&")"===i||this.logger.validation_error("Mismatched bracket types",this.line_num)
501
+ const o=e.slice(n+1,s).trim();/[^\s,]/.test(o)||this.logger.validation_error("Alternator / optionalator must not be empty",this.line_num)
502
+ const a=e.slice(0,n).trim(),l=e.slice(s+1).trim();/[^\s,]/.test(a)||/[^\s,]/.test(l)||")"!==i||this.logger.validation_error("Optionalator must be part of a larger sequence",this.line_num)}}0!==t.length&&this.logger.validation_error("Unclosed bracket",this.line_num)}expand_chunk(e){this.check_grammar_rules(e)
503
+ return[...e.matchAll(/([^{(})]+)|(\{[^}]+\})|(\([^)]+\))/g)].map(e=>e[0]).map(e=>{if(e.startsWith("{"))return e.slice(1,-1).split(/[\s,]+/)
504
+ if(e.startsWith("(")){return["",...e.slice(1,-1).split(/[\s,]+/)]}return[e]}).reduce((e,t)=>{const s=[]
505
+ for(const i of e)for(const e of t)s.push(i+e)
506
+ return s},[""])}resolve_alt_opt(e){return this.split_top_level(e).map(e=>this.expand_chunk(e))}getTransformLengths(e,t){return 1===t.length&&e.length>1&&(t=Array(e.length).fill(t[0])),t.length!==e.length&&this.logger.validation_error(`Concurrent change length mismatch: target has ${e.length}, result has ${t.length}`,this.line_num),t.map((t,s)=>{const i=e[s]
507
+ return 1===t.length&&i.length>1&&(t=Array(i.length).fill(t[0])),t.length!==i.length&&this.logger.validation_error(`Alternator / optionalator length mismatch at index ${s}: target has ${i.length}, result has ${t.length}`,this.line_num),t})}categories_into_transform(e){let t=""
508
+ const s=e.length
509
+ for(let i=0;i<s;i++){const s=e[i]
510
+ if("<"===s&&/^[A-Z]$/.test(e[i+1]))t+=s+e[i+1],i+=1
511
+ else{if(this.categories.has(s)){const r=this.categories.get(s).filter(e=>!e.includes("^")).join(", ")
512
+ if(this.check_bracket_context(e,i,i,"category")){t+=""+r
513
+ continue}t+=`{${r}}`
514
+ continue}t+=s}}return t}features_into_transform(e){const t=e.length,s=[]
515
+ let i=!1,r="",n=0
516
+ for(let o=0;o<t;o++){const t=e[o]
517
+ if(i){if("]"===t){if(i=!1,0!==r.length){const t=this.get_graphemes_from_matrix(r)
518
+ if(this.check_bracket_context(e,n,o,"feature")){s.push(""+t)
519
+ continue}s.push(`{${t}}`)
520
+ continue}r=""
521
+ continue}r+=t}else{if("["===t){n=o
522
+ const t=e[o+1]
523
+ if("+"===t||"-"===t){i=!0
524
+ continue}}s.push(t)}}return i&&this.logger.validation_error("Unclosed feature-matrix missing ']'",this.line_num),s.join("")}check_bracket_context(e,t,s,i){const r=e.length,n=[]
525
+ for(let p=0;p<r;p++){const s=e[p]
526
+ if("("===s||"{"===s?n.push({kind:s,index:p}):")"!==s&&"}"!==s||n.pop(),p===t)break}const o=n.at(-1)
527
+ if(!o)return!1
528
+ const a=o.index,l=this.find_matching_bracket(e,a),h=t-1,c=s+1,_=h<=a||","===e[h]||" "===e[h],u=c>=l||","===e[c]||" "===e[c]
529
+ return _&&u||this.logger.validation_error(`A ${i} is adjacent to other content inside a set`,this.line_num),!0}find_matching_bracket(e,t){const s=e[t],i="("===s?")":"{"===s?"}":""
530
+ let r=0
531
+ for(let n=t;n<e.length;n++)if(e[n]===s?r++:e[n]===i&&r--,0===r)return n
532
+ return-1}get_graphemes_from_matrix(e){const t=e.split(",").map(e=>e.trim()),s=[]
533
+ for(const i of t){const e=this.features.get(i)
534
+ e||this.logger.validation_error(`Unknown feature '${i}'`,this.line_num),s.push(e.graphemes)}if(0===s.length)return""
535
+ return s.slice(1).reduce((e,t)=>e.filter(e=>t.includes(e)),s[0]).join(", ")}normaliseTransformLength(e,t){1===t.length&&e.length>1&&(t=Array(e.length).fill(t[0])),t.length!==e.length&&this.logger.validation_error(`Concurrent change length mismatch: target has ${e.length}, result has ${t.length}`,this.line_num),t=t.map((t,s)=>{const i=e[s]
536
+ return 1===t.length&&i.length>1&&(t=Array(i.length).fill(t[0])),t.length!==i.length&&this.logger.validation_error(`An alternator / optionalator length mismatch occured: target has ${i.length}, result has ${t.length}`,this.line_num),t})
537
+ return{target_array:e,result_array:t}}valid_transform_brackets(e){const t=[],s={")":"(","}":"{","]":"["}
538
+ for(const i of e)if(Object.values(s).includes(i))t.push(i)
539
+ else if(Object.keys(s).includes(i)&&(0===t.length||t.pop()!==s[i]))return!1
540
+ return 0===t.length}valid_environment(e){let t=0
541
+ for(let s=0;s<e.length;s++){const i=e[s]
542
+ if("{(".includes(i)?t++:"})".includes(i)&&t--,0===t){if(" "===i){const t=e[s-1],i=e[s+1]
543
+ if(!("_"===t||"_"===i))return!1}if(","===i)return!1}}return!0}valid_cat_fea(e){const t=[],s=[],i=e.length
544
+ let r=!1,n="",o=0
545
+ for(let a=0;a<i;a++){const i=e[a]
546
+ if(!r){if("("===i||"{"===i){s.push(i),t.push(i)
547
+ continue}if(")"===i||"}"===i){s.pop(),t.push(i)
548
+ continue}}if(r||"["!==i)if(r){if("]"===i){r=!1
549
+ const i=s.at(-1)
550
+ if("("===i||"{"===i){const s=e[o-1],r=e[a+1]
551
+ "("===i&&"("===s&&")"===r||this.logger.validation_error(`Square bracket set "[${n}]" is not allowed inside ${i}`,this.line_num),t.push(n)
552
+ continue}t.push(`{${n}}`)
553
+ continue}n+=i}else t.push(i)
554
+ else r=!0,n="",o=a}return r&&this.logger.validation_error("Unclosed square bracket set",this.line_num),t.join("")}format_tokens(e){return e.map(e=>{let t=e.base
555
+ if("anythings-mark"===e.type){if("consume"in e&&e.consume){t+=`[${e.consume.map(e=>e.join("")).join(", ")}]`}if("blocked_by"in e&&e.blocked_by){t+=`|[${e.blocked_by.map(e=>e.join("")).join(", ")}]`}}return"escaped"in e&&e.escaped&&(t="\\"+t),"min"in e&&1===e.min&&e.max===1/0?t+="+":"min"in e&&e.max===1/0?t+=`+[${e.min},]`:"min"in e&&e.min==e.max?1==e.min||(t+=`+[${e.min}]`):"min"in e&&(t+=`+[${e.min}${e.max!==1/0?","+e.max:""}]`),"association"in e&&(t+="~"),t}).join(" ")}get_cluser_field_graphemes(e,t){const s=e.split(/[,]+/).filter(Boolean),i=[]
556
+ for(let r=0;r<s.length;r++){const e=s[r].trim(),n=this.nesca_grammar_stream.cluster_parser(e,t,this.line_num)
557
+ i.push(n)}return i}show_debug(){const e=[]
558
+ for(const n of this.stages){const t=[],s=n.transforms
559
+ for(let e=0;e<s.length;e++){const i=s[e]
560
+ if("rule"!=i.t_type&&"cluster-field"!=i.t_type){t.push(` <routine = ${i.t_type}> @ ln:${i.line_num+1}`)
561
+ continue}const r=[]
562
+ for(let e=0;e<i.target.length;e++)r.push(this.format_tokens(i.target[e]))
563
+ const n=[]
564
+ for(let e=0;e<i.result.length;e++)n.push(this.format_tokens(i.result[e]))
565
+ const o=i.chance?" CHANCE "+i.chance:""
566
+ let a=""
567
+ for(let e=0;e<i.exceptions.length;e++)a+=` ! ${this.format_tokens(i.exceptions[e].before)}_${this.format_tokens(i.exceptions[e].after)}`
568
+ let l=""
569
+ for(let e=0;e<i.conditions.length;e++)l+=` / ${this.format_tokens(i.conditions[e].before)}_${this.format_tokens(i.conditions[e].after)}`
570
+ t.push(` ${r.join(", ")} → ${n.join(", ")}${l}${a}${o} @ ln:${i.line_num+1}`)}e.push({transforms:t,name:n.name})}const t=[]
571
+ for(const[n,o]of this.features)t.push(` ${n} = ${o.graphemes.join(", ")}`)
572
+ const s=[]
573
+ for(const n of this.nesca_grammar_stream.associateme_mapper){const e=" "+n.variants.map(e=>`{${e.join(",")}}`).join("<")
574
+ s.push(e)}const i=s.join("\n"),r="Graphemes: "+this.nesca_grammar_stream.graphemes.join(", ")+"\nSyllable Boundaries: "+this.syllable_boundaries.join(", ")+"\nAssociatemes: \n"+i+"\nFeatures {\n"+t.join("\n")+"\n}\n"+e.map(e=>`stage "${e.name}" {\n `+e.transforms.join("\n ")).join("\n")+"\n}"
575
+ this.logger.diagnostic(r)}}class z{constructor(e,s,i,r){t(this,"logger"),t(this,"graphemes"),t(this,"associateme_mapper"),t(this,"escape_mapper"),this.logger=e,this.graphemes=s,this.associateme_mapper=i,this.escape_mapper=r}main_parser(e,t,s){let i=0
576
+ const r=[]
577
+ if(e.startsWith("@routine")){const t=e.slice(8)
578
+ return[{type:"routine",base:t,routine:t}]}if("^"===e){if("RESULT"===t)return[{type:"deletion",base:"^"}]
579
+ if("TARGET"===t)return[{type:"insertion",base:"^"}]
580
+ this.logger.validation_error(`Unexpected character '${e}' in mode '${t}'`,s)}else if("0"===e)return"RESULT"!==t&&this.logger.validation_error(`Reject not allowed in '${t}'`,s),[{type:"reject",base:"0"}]
581
+ for(;i<e.length;){let n={type:"pending",base:"",min:1,max:1}
582
+ const o=e[i]
583
+ if(/\s/.test(o))i++
584
+ else{if("%"===o){"RESULT"===t&&this.logger.validation_error(`Anythings-mark not allowed in '${t}'`,s),n={type:"anythings-mark",base:"%",min:1,max:1/0}
585
+ let r=i+1
586
+ if("["!==e[r])this.logger.validation_error("Expected '[' after '%' for anythings-mark",s)
587
+ else{r++
588
+ let t=""
589
+ for(;r<e.length;){const s=e[r]
590
+ if("]"===s)break
591
+ t+=s,r++}(r>=e.length||"]"!==e[r])&&this.logger.validation_error("Unclosed blocker",s)
592
+ const o=[],l=[],h=t.split("|").map(e=>e.trim())
593
+ if(h.length>2)throw Error("Invalid garde_stream: more than one '|' found")
594
+ const[c,_]=h
595
+ if(c){const e=c.split(",").map(e=>e.trim()).filter(Boolean)
596
+ for(const t of e){const e=a(t,this.graphemes).map(e=>this.escape_mapper.restore_escaped_chars(e)).filter(Boolean)
597
+ e.length>0&&o.push(e)}}if(_){const e=_.split(",").map(e=>e.trim()).filter(Boolean)
598
+ for(const t of e){const e=a(t,this.graphemes).map(e=>this.escape_mapper.restore_escaped_chars(e)).filter(Boolean)
599
+ e.length>0&&l.push(e)}}0!==o.length&&(n.consume=o),0!==l.length&&(n.blocked_by=l),r++,i=r}}else if("*"===o)"RESULT"==t&&this.logger.validation_error(`Wildcard not allowed in '${t}'`,s),n={type:"wildcard",base:"*",min:1,max:1},i++
600
+ else{if("#"==o){"BEFORE"!==t&&"AFTER"!==t&&this.logger.validation_error(`Word-boundary not allowed in '${t}'`,s),0!==i&&i+1!==e.length&&this.logger.validation_error(`Hash must be at the start or end of '${t}'`,s),n={type:"word-boundary",base:"#",min:1,max:1},r.push(n),i++
601
+ continue}if("$"==o){"BEFORE"!==t&&"AFTER"!==t&&this.logger.validation_error(`Syllable-boundary not allowed in '${t}'`,s),n={type:"syllable-boundary",base:"$",min:1,max:1},r.push(n),i++
602
+ continue}if("&"==o){const o=i+1
603
+ if("T"===e[o])"TARGET"===t&&this.logger.validation_error(`Target-mark not allowed in '${t}'`,s),n={type:"target-mark",base:"&T",min:1,max:1},i=o
604
+ else if("M"===e[o])"TARGET"===t&&this.logger.validation_error(`Metathesis-mark not allowed in '${t}'`,s),n={type:"metathesis-mark",base:"&M",min:1,max:1},i=o
605
+ else if("E"===e[o])"TARGET"!==t&&this.logger.validation_error("Empty-mark only allowed in 'TARGET'",s),n={type:"empty-mark",base:"&E",min:1,max:1},i=o
606
+ else{if("="===e[o]){n={type:"reference-start-capture",base:"&=",min:1,max:1},i=o+1,r.push(n)
607
+ continue}this.logger.validation_error(`A 'T', 'M' or '=' did not follow '&' in '${t}'`,s)}i++}else if("="===o){const o=i+1,a=e[o]
608
+ if(/^[1-9]$/.test(a)){n={type:"reference-capture",base:"="+a,key:a,min:1,max:1},r.push(n),i=o+1
609
+ continue}this.logger.validation_error(`Invalid reference capture syntax in '${t}'`,s)}else if(/^[1-9]$/.test(o))"TARGET"===t&&this.logger.validation_error("Reference-mark not allowed in 'TARGET'",s),n={type:"reference-mark",base:o,key:o,min:1,max:1},i++
610
+ else if("~"===o)i++
611
+ else if(c.includes(o))this.logger.validation_error(`Unexpected syntax character '${o}' in ${t}`,s)
612
+ else{const t=this.escape_mapper.restore_escaped_chars(e)
613
+ let s=!1
614
+ t[i]!==e[i]&&(s=!0)
615
+ let r=!1
616
+ for(const e of this.graphemes.sort((e,t)=>t.length-e.length))if(t.startsWith(e,i)){n={type:"grapheme",base:e,min:1,max:1},i+=e.length,r=!0
617
+ break}r||(n={type:"grapheme",base:t[i],min:1,max:1},i++),s&&"grapheme"===n.type&&(n.escaped=!0)}}if(":"===e[i])n.min=2,n.max=2,i++
618
+ else if("+"===e[i])"RESULT"===t&&this.logger.validation_error(`Quantifier not allowed in '${t}'`,s),n.min=1,n.max=1/0,i++
619
+ else if("?"===e[i]){let r=i+1
620
+ if("["!==e[r])this.logger.validation_error("Expected '[' after '?' for quantifier",s)
621
+ else{r+=1
622
+ let o=""
623
+ for(;r<e.length&&"]"!==e[r];)o+=e[r],r++
624
+ "]"!==e[r]&&this.logger.validation_error("Unclosed quantifier",s)
625
+ const a=o.split(",")
626
+ if(1===a.length){const e=parseInt(a[0],10)
627
+ isNaN(e)&&this.logger.validation_error(`Invalid quantifier value: "${a[0]}"`,s),n.min=e,n.max=e}else if(2===a.length){const[e,i]=a,r=""===e?1:parseInt(e,10),o=""===i?1/0:parseInt(i,10)
628
+ ""!==e&&isNaN(r)&&this.logger.validation_error(`Invalid min value: "${e}"`,s),""!==i&&null!==o&&isNaN(o)&&this.logger.validation_error(`Invalid max value: "${i}"`,s),o===1/0&&"RESULT"===t&&this.logger.validation_error(`In '${t}', '${n.base}' cannot be reproduced an infinite amount of times`,s),n.min=r,n.max=o}else this.logger.validation_error(`Invalid quantifier format: "${o}"`,s)
629
+ i=r+1}n.max!=1/0&&n.min>n.max&&this.logger.validation_error(`Invalid quantifier: min '${n.min}' cannot be greater than max '${n.max}'`,s)}if("~"===e[i]){"grapheme"!==n.type&&this.logger.validation_error("Based-mark only allowed after grapheme token",s)
630
+ const e=this.find_base_location(this.associateme_mapper,n.base)
631
+ e||this.logger.validation_error(`Grapheme "${n.base}" with a based-mark was not an associateme base`,s)
632
+ const[r,o]=e
633
+ n.association={entry_id:r,base_id:o,variant_id:0,is_target:"TARGET"===t},i++}"pending"!==n.type&&r.push(n)}}return r}cluster_parser(e,t,s){let i=0
634
+ const r=[]
635
+ if("^"===e){if("RESULT"===t)return[{type:"deletion",base:"^"}]
636
+ this.logger.validation_error(`Unexpected character '${e}' in mode '${t}'`,s)}else if("0"===e)return"RESULT"!==t&&this.logger.validation_error(`Reject not allowed in '${t}'`,s),[{type:"reject",base:"0"}]
637
+ for(;i<e.length;){let t={type:"pending",base:"",min:1,max:1}
638
+ const n=e[i]
639
+ if(/\s/.test(n)){i++
640
+ continue}"^"!==n&&"0"!==n||this.logger.validation_error(`Unexpected character '${n}' in cluster-field`,s)
641
+ const o=this.escape_mapper.restore_escaped_chars(e)
642
+ let a=!1
643
+ o[i]!==e[i]&&(a=!0)
644
+ let l=!1
645
+ for(const e of this.graphemes.sort((e,t)=>t.length-e.length))if(o.startsWith(e,i)){t={type:"grapheme",base:e,min:1,max:1},i+=e.length,l=!0
646
+ break}l||(t={type:"grapheme",base:o[i],min:1,max:1},i++),a&&"grapheme"===t.type&&(t.escaped=!0),"pending"!==t.type&&r.push(t)}return r}find_base_location(e,t){for(let s=0;s<e.length;s++){const i=e[s]
647
+ for(let e=0;e<i.bases.length;e++)if(i.bases[e]===t)return[s,e]}return null}}let G=class{constructor(e,s,i,r,n){t(this,"logger"),t(this,"escape_mapper"),t(this,"output_mode"),t(this,"category_distribution"),t(this,"category_pending"),t(this,"categories"),t(this,"trans_categories"),this.logger=e,this.output_mode=s,this.escape_mapper=i,this.category_distribution=r,this.category_pending=n,this.categories=new Map,this.trans_categories=new Map,this.resolve_categories(),this.get_trans_categories(),"debug"===this.output_mode&&this.show_debug()}get_trans_categories(){for(const[e,t]of this.categories)this.trans_categories.set(e,t.graphemes)}resolve_categories(){for(const[e,t]of this.category_pending){this.valid_category_brackets(t.content)||this.logger.validation_error(`Category '${e}' had missmatched brackets`,t.line_num),this.valid_category_weights(t.content)||this.logger.validation_error(`Category '${e}' had invalid weights -- expected weights to follow an item and look like '*NUMBER' followed by either ',', a bracket, or ' '`,t.line_num)
648
+ for(const[e,t]of this.category_pending.entries()){const s=o(t.content,this.category_pending,!0)
649
+ this.category_pending.set(e,{content:s,line_num:t.line_num})}}for(const[e,t]of this.category_pending){const s=this.resolve_nested_categories(t.content,this.category_distribution)
650
+ for(let e=0;e<s.graphemes.length;e++)s.graphemes[e]=this.escape_mapper.escape_special_chars(s.graphemes[e])
651
+ this.categories.set(e,s)}}valid_category_brackets(e){const t=[],s={"}":"{"}
652
+ for(const i of e)if(Object.values(s).includes(i))t.push(i)
653
+ else if(Object.keys(s).includes(i)&&(0===t.length||t.pop()!==s[i]))return!1
654
+ return 0===t.length}valid_category_weights(e){return!(/\*(?!\d+(\.\d+)?)/g.test(e)||/^\*/.test(e)||/[ ,{}]\*/g.test(e)||/\*(\d+\.\d+|\d+)(?=[^.\d]|$)(?![ ,\]\n]|$)/g.test(e))}resolve_nested_categories(e,t){const s=function e(s,i=1){const r=function(e){const t=[]
655
+ let s=0,i=""
656
+ for(;s<e.length;)if("{"===e[s]){i.trim()&&(t.push(i.trim()),i="")
657
+ let r=1,n=s+1
658
+ for(;n<e.length&&r>0;)"{"===e[n]?r++:"}"===e[n]&&r--,n++
659
+ const o=e.slice(s+1,n-1)
660
+ s=n
661
+ let a=1
662
+ if("*"===e[s]){s++
663
+ let t=""
664
+ for(;s<e.length&&/[\d.]/.test(e[s]);)t+=e[s++]
665
+ a=parseFloat(t||"1")}t.push({group:o,weight:a})}else/[,\s]/.test(e[s])?(i.trim()&&(t.push(i.trim()),i=""),s++):i+=e[s++]
666
+ return i.trim()&&t.push(i.trim()),t}(s),n=r.some(e=>"string"==typeof e&&e.includes("*"))?Array(r.length).fill(1):g(r.length,t),o=[]
667
+ for(let t=0;t<r.length;t++){const s=r[t],a=n[t]*i
668
+ if("string"==typeof s){const[e,t]=s.split("*"),r=void 0!==t&&""!==t,n=r?(r?parseFloat(t):1)*i:a
669
+ o.push({key:e.trim(),weight:n})}else{const t=e(s.group,1),i=t.reduce((e,t)=>e+t.weight,0)
670
+ for(const{key:e,weight:r}of t){const t=r/i*s.weight*a
671
+ o.push({key:e,weight:t})}}}return o}(e)
672
+ return{graphemes:s.map(e=>e.key),weights:s.map(e=>e.weight)}}show_debug(){const e=[]
673
+ for(const[s,i]of this.categories){const t=[]
674
+ for(let e=0;e<i.graphemes.length;e++)t.push(`${i.graphemes[e]}*${i.weights[e]}`)
675
+ const r=""+t.join(", ")
676
+ e.push(` ${s} = ${r}`)}const t="Category-distribution: "+this.category_distribution+"\nCategories {\n"+e.join("\n")+"\n}"
677
+ this.logger.diagnostic(t)}}
678
+ class D{constructor(e,s,i){t(this,"logger"),t(this,"output_mode"),t(this,"category_pending"),t(this,"trans_categories"),this.logger=e,this.output_mode=s,this.category_pending=i,this.trans_categories=new Map,this.resolve_categories(),"debug"===this.output_mode&&this.show_debug()}resolve_categories(){for(const[e,t]of this.category_pending.entries()){const s=o(t.content,this.category_pending)
679
+ this.category_pending.set(e,{content:s,line_num:t.line_num})}for(const[e,t]of this.category_pending){const s=t.content.split(/[,\s]+/).filter(Boolean)
680
+ this.trans_categories.set(e,s)}}show_debug(){const e=[]
681
+ for(const[s,i]of this.trans_categories){const t=[]
682
+ for(let e=0;e<i.length;e++)t.push(""+i[e])
683
+ const r=""+t.join(", ")
684
+ e.push(` ${s} = ${r}`)}const t="~ CATEGORIES ~\n\nCategories {\n"+e.join("\n")+"\n}"
685
+ this.logger.diagnostic(t)}}class q{constructor(e,s,i,r,n,o,a){t(this,"logger"),t(this,"supra_builder"),t(this,"output_mode"),t(this,"optionals_weight"),t(this,"units"),t(this,"wordshape_distribution"),t(this,"wordshape_pending"),t(this,"wordshapes"),this.logger=e,this.output_mode=s,this.supra_builder=i,this.optionals_weight=a,this.units=n,this.wordshape_distribution=r,this.wordshape_pending=o,this.wordshapes={items:[],weights:[]},this.expand_units(),this.expand_wordshape_units(),this.set_wordshapes(),"debug"===this.output_mode&&this.show_debug()}set_wordshapes(){const e=[]
686
+ let t="",s=0
687
+ 0==this.wordshape_pending.content.length&&this.logger.validation_error("No word-shapes to choose from -- expected 'words: wordshape1 wordshape2 ...'",this.wordshape_pending.line_num),this.wordshape_pending.content=this.supra_builder.process_string(this.wordshape_pending.content,this.wordshape_pending.line_num),this.valid_words_brackets(this.wordshape_pending.content)||this.logger.validation_error("Word-shapes had missmatched brackets",this.wordshape_pending.line_num),this.valid_words_weights(this.wordshape_pending.content)||this.logger.validation_error("Word-shapes had invalid weights -- expected weights to follow an item and look like '*NUMBER' followed by either ',' a bracket, or ' '",this.wordshape_pending.line_num)
688
+ for(let n=0;n<this.wordshape_pending.content.length;n++){const i=this.wordshape_pending.content[n]
689
+ "{"===i||"("===i?s++:"}"!==i&&")"!==i||s--," "!==i&&","!==i||0!==s?t+=i:t.length>0&&(e.push(t),t="")}t.length>0&&e.push(t)
690
+ const[i,r]=this.extract_wordshape_value_and_weight(e,this.wordshape_distribution)
691
+ for(let n=0;n<i.length;n++)this.wordshapes.items.push(i[n]),this.wordshapes.weights.push(r[n])}valid_words_brackets(e){const t=[],s={")":"(",">":"<","}":"{"}
692
+ for(const i of e)if(Object.values(s).includes(i))t.push(i)
693
+ else if(Object.keys(s).includes(i)&&(0===t.length||t.pop()!==s[i]))return!1
694
+ return 0===t.length}extract_wordshape_value_and_weight(e,t){const s=[],i=[],r=e.flatMap(e=>{const t=[]
695
+ let s="",i=0,r=0
696
+ for(let n=0;n<e.length;n++){const o=e[n]
697
+ s+=o,"{"===o&&i++,"}"===o&&i--,"("===o&&r++,")"===o&&r--
698
+ const a=n===e.length-1;(","===o&&0===i&&0===r||a)&&(","!==o&&a||(s=s.slice(0,-1)),s.trim()&&t.push(s.trim()),s="")}return t})
699
+ if(r.every(e=>!/^(?:\{.*\}|[^*]+)\*[\d.]+$/.test(e))){const e=r.map(e=>e.trim()),n=g(e.length,t)
700
+ return s.push(...e),i.push(...n),[s,i]}for(const n of r){const e=n.trim(),t=e.match(/^(.*)\*([\d.]+)$/)
701
+ if(t&&!/\{.*\*.*\}$/.test(t[1]))s.push(t[1]),i.push(parseFloat(t[2]))
702
+ else if(/^\{.*\}\*[\d.]+$/.test(e)){const t=e.lastIndexOf("*")
703
+ s.push(e.slice(0,t)),i.push(parseFloat(e.slice(t+1)))}else s.push(e),i.push(1)}return[s,i]}valid_words_weights(e){return!(/\*(?!\d+(\.\d+)?)/g.test(e)||/^\*/.test(e)||/[ ,]\*/g.test(e)||/\*(\d+\.\d+|\d+)(?=[^.\d]|$)(?![ ,}\])\n]|$)/g.test(e))}expand_wordshape_units(){this.wordshape_pending.content=o(this.wordshape_pending.content,this.units)
704
+ const e=this.wordshape_pending.content.match(/<[A-Za-z+$-]+>/)
705
+ e&&this.logger.validation_error(`Nonexistent unit detected: '${e[0]}'`,this.wordshape_pending.line_num)}expand_units(){for(const[e,t]of this.units.entries()){const s=o(t.content,this.units)
706
+ this.units.set(e,{content:s,line_num:t.line_num})}}show_debug(){const e=[]
707
+ for(const[i,r]of this.units)e.push(` ${i.slice(1,-1)} = ${r.content}`)
708
+ const t=[]
709
+ for(let i=0;i<this.wordshapes.items.length;i++)t.push(` ${this.wordshapes.items[i]}*${this.wordshapes.weights[i]}`)
710
+ const s="Wordshape-distribution: "+this.wordshape_distribution+"\nOptionals-weight: "+this.optionals_weight+"\nUnits {\n"+e.join("\n")+"\n}\nWordshapes {\n"+t.join("\n")+"\n}"
711
+ this.logger.diagnostic(s)}}class H{constructor(e,s,i,r,n){t(this,"logger"),t(this,"escape_mapper"),t(this,"output_mode"),t(this,"feature_pending"),t(this,"features"),t(this,"graphemes"),this.logger=e,this.output_mode=s,this.escape_mapper=i,this.graphemes=n,this.feature_pending=r,this.features=new Map,this.resolve_features(),"debug"===this.output_mode&&this.show_debug()}resolve_features(){for(const[e,t]of this.feature_pending)if(e.startsWith(">")){this.feature_pending.delete(e)
712
+ const s=t.content.split(",").map(e=>"^"+e).join(",")+this.graphemes.join(",")
713
+ this.feature_pending.set(e.replace(">","-"),{content:s,line_num:t.line_num}),this.feature_pending.set(e.replace(">","+"),{content:t.content,line_num:t.line_num})}for(const[e,t]of this.feature_pending){const s=o(t.content,this.feature_pending)
714
+ this.feature_pending.set(e,{content:s,line_num:t.line_num})}for(const[e,t]of this.feature_pending){const s=Array.from(new Set(t.content.split(","))),i=[],r=[]
715
+ for(const e of s){if(e.startsWith("^")){const t=e.slice(1)
716
+ r.push(t)
717
+ continue}e.includes("^")&&this.logger.validation_error(`Invalid grapheme '${e}' has a misplaced caret`,t.line_num),(e.startsWith("+")||e.startsWith("-")||e.startsWith(">"))&&this.logger.validation_error(`Referenced feature '${e}' not found`,t.line_num),i.push(e)}const n=i.filter(e=>!r.includes(e))
718
+ 0===n.length&&this.logger.validation_error(`Feature '${e}' had zero graphemes`,t.line_num)
719
+ for(let e=0;e<n.length;e++)n[e]=this.escape_mapper.escape_special_chars(n[e])
720
+ this.features.set(e,{graphemes:n})}}show_debug(){const e=[]
721
+ for(const[s,i]of this.features)e.push(` ${s} = ${i.graphemes.join(", ")}`)
722
+ const t="Features {\n"+e.join("\n")+"\n}"
723
+ this.logger.diagnostic(t)}}class V{constructor(e,s,i){t(this,"logger"),t(this,"escape_mapper"),t(this,"graphemes_pending"),t(this,"graphemes"),t(this,"associateme_mapper"),this.logger=e,this.escape_mapper=s,this.graphemes_pending=i,this.graphemes=[],this.associateme_mapper=[],this.resolve_canon_graphemes(),this.resolve_associatemes()}resolve_canon_graphemes(){const e=this.graphemes_pending.replace(/(<\{|\})/g,",").split(/[,\s]+/).filter(Boolean)
724
+ for(let t=0;t<e.length;t++)e[t]=this.escape_mapper.restore_escaped_chars(e[t])
725
+ this.graphemes=Array.from(new Set(e))}resolve_associatemes(){const e=[],t=this.graphemes_pending??"",s=[...t.matchAll(/\{[^}]+\}(?:\s*<\s*\{[^}]+\})*/g)]
726
+ let i=t
727
+ for(const r of s)i=i.replace(r[0],"")
728
+ i.includes("<")&&this.logger.validation_error('Stray "<" found outside of a valid associateme entry')
729
+ for(const r of s){const t=r[0],s=t.split("<").map(e=>e.replace(/[{}]/g,"").trim().split(/[,\s]+/).map(e=>e.trim()).filter(e=>e.length>0))
730
+ 0===s.length&&this.logger.validation_error("A base associateme was empty in the graphemes directive")
731
+ const i=s[0]
732
+ 0===i.length&&this.logger.validation_error("A base associateme was empty in the graphemes directive")
733
+ const n=i.length
734
+ for(let e=0;e<s.length;e++){const i=s[e]
735
+ if(i.length!==n){const s=0===e?"bases":"variant "+e
736
+ this.logger.validation_error(`Mismatched associateme entry variant group length in "${t}": ${s} had a length of ${i.length} -- expected length of ${n}`)}}const o=[...s]
737
+ e.push({bases:i,variants:o})}this.associateme_mapper=e}}class Z{constructor(e,s,i,r,n,o,a,l,h,c,_,u){t(this,"logger"),t(this,"lettercase_mapper"),t(this,"build_start"),t(this,"schema_input"),t(this,"schema_output"),t(this,"words"),t(this,"input_divider"),t(this,"output_divider"),t(this,"num_of_rejects",0),t(this,"num_of_transformed",0),t(this,"num_of_passed",0),t(this,"output_mode"),t(this,"sort_words"),t(this,"alphabet"),t(this,"invisible"),this.logger=e,this.lettercase_mapper=s,this.build_start=i,this.schema_input=r,this.schema_output=n,this.num_of_rejects=0,this.input_divider=a,this.output_divider=h,this.words=[],this.output_mode=l,p.output_mode=l,p.fields=n.fields,p.field_delimiters=n.delimiters,""==o&&this.logger.validation_error("No input words to transform"),this.sort_words=c,this.alphabet=_,this.invisible=u
738
+ const d=o.split(this.input_divider)
739
+ for(let t=0;t<d.length;t++){if(""===d[t])continue
740
+ let e
741
+ e=0===this.schema_input.fields.length?{word:d[t].trim()}:this.split_word_by_schema(d[t].trim())
742
+ const s=new p(null,e)
743
+ this.words.push(s)}}make_text(){const e=[]
744
+ this.sort_words&&(this.words=I(this.logger,this.words,this.alphabet,this.invisible))
745
+ for(let t=0;t<this.words.length;t++){const s=this.words[t]
746
+ s.rejected?(this.num_of_rejects++,"debug"===this.output_mode&&e.push(s.get_word())):(e.push(s.get_word()),s.num_of_transformations>0?this.num_of_transformed++:this.num_of_passed++)}if(this.create_record(),"debug"===this.output_mode&&this.show_debug(),"paragraph"===this.output_mode){const t=[]
747
+ let s=!0
748
+ for(let i=0;i<e.length;i++){let r=e[i]
749
+ s&&(r=this.lettercase_mapper.capitalise(r),s=!1),t.push(r)
750
+ const n=r.charAt(r.length-1);[".","!","?"].includes(n)&&(s=!0)}return t.join(" ")}return e.join(this.output_divider)}split_word_by_schema(e){const t={},s=this.schema_input.fields,i=this.schema_input.delimiters
751
+ let r=0,n=0
752
+ const o=i[0]
753
+ for(""!==o&&e.startsWith(o)&&(r=o.length);n<s.length;){const o=s[n],a=i[n+1]||""
754
+ let l
755
+ ""===a?l=e.length:(l=e.indexOf(a,r),-1===l&&(l=e.length)),t[o]=e.slice(r,l),r=l+a.length,n++}return t}create_record(){const e=Date.now()-this.build_start,t=Math.ceil(e/100)/10,s=t.toFixed(t%1==0?0:1),i="1"===s?s+" second":s+" seconds",r=[]
756
+ 1==this.num_of_transformed?r.push("1 word had transformations"):this.num_of_transformed>1&&r.push(this.num_of_transformed+" words had transformations"),1==this.num_of_passed?r.push("1 word unchanged"):this.words.length>1&&r.push(this.num_of_passed+" words unchanged"),1==this.num_of_rejects?r.push("1 word was rejected"):this.num_of_rejects>1&&r.push(this.num_of_rejects+" words rejected"),this.logger.info(`${n(r)} -- in ${i}`)}show_debug(){const e="~ CREATING TEXT ~\n\nNum of words: "+this.words.length+`\nSchema {\n input fields = "${this.schema_input.fields.join('", "')}"\n input delimiters = "${this.schema_input.delimiters.join('", "')}"\n output fields = "${this.schema_output.fields.join('", "')}"\n output delimiters = "${this.schema_output.delimiters.join('", "')}"\n}\n\nMode: `+this.output_mode
757
+ this.logger.diagnostic(e)}}class P{constructor(){t(this,"chances",[]),t(this,"check_parsing"),this.chances=[{id:1,percent:50,rolled:null}],this.check_parsing=!1}add_chance(e){const t=Math.max(0,Math.min(100,e))
758
+ this.chances.push({id:this.chances.length+1,percent:t,rolled:null})}get_is_success(e){const t=this.chances.find(t=>t.id===e)
759
+ return t?t.rolled:null}reset(){for(const e of this.chances)e.rolled=null}roll_all(){for(const e of this.chances){const t=100*Math.random()
760
+ e.rolled=t<e.percent}return{}}get_last_chance(){return this.check_parsing&&this.chances.length>0?this.chances[this.chances.length-1].id:null}}function vocabug({file:e,num_of_words:t=100,output_mode:s="word-list",remove_duplicates:i=!0,force_word_limit:r=!1,sort_words:n=!0,output_divider:o=" "}){const a=new N
761
+ try{const l=Date.now(),h=new S,c=new L(a),u=new F,p=new P,d=new _(a,"vocabug",h,u,p,t,s,n,i,r," ",o)
762
+ d.parse_file(e)
763
+ const g=new G(a,d.output_mode,h,d.category_distribution,d.category_pending),f=new q(a,d.output_mode,c,d.wordshape_distribution,d.units,d.wordshape_pending,d.optionals_weight),w=new V(a,h,d.graphemes_pending),v=new H(a,d.output_mode,h,d.feature_pending,w.graphemes),b=new z(a,w.graphemes,w.associateme_mapper,h),y=new U(a,d.output_mode,b,g.trans_categories,d.stages_pending,d.substages_pending,v.features,d.syllable_boundaries),$=new m(h,c,g.categories,f.wordshapes,g.category_distribution,f.optionals_weight,d.output_mode),k=new W(a,w.graphemes,d.lettercase_mapper,d.chance_mapper,y.syllable_boundaries,y.stages,y.substages,d.output_mode,w.associateme_mapper),x=new C(a,d.lettercase_mapper,l,d.num_of_words,d.remove_duplicates,d.force_word_limit,d.output_mode,d.output_divider,d.sort_words,d.alphabet,d.invisible)
764
+ for(;!x.terminated;){let e=$.make_word()
765
+ e=k.do_stages(e),x.add_word(e)}a.set_payload(x.make_text())}catch(l){l instanceof a.Validation_Error||a.uncaught_error(l)}return a.create_log()}function nesca({file:e,input_words:t,output_mode:s="word-list",input_divider:i="\n",output_divider:r="\n",sort_words:n=!0}){const o=new N
766
+ try{const a=Date.now(),l=new S,h=new F,c=new P,u=new _(o,"nesca",l,h,c,1,s,n,!1,!1,i,r)
767
+ u.parse_file(e)
768
+ const p=new D(o,u.output_mode,u.category_pending),d=new V(o,l,u.graphemes_pending),g=new H(o,u.output_mode,l,u.feature_pending,d.graphemes),m=new z(o,d.graphemes,d.associateme_mapper,l),f=new U(o,u.output_mode,m,p.trans_categories,u.stages_pending,u.substages_pending,g.features,u.syllable_boundaries),w=new W(o,d.graphemes,u.lettercase_mapper,u.chance_mapper,f.syllable_boundaries,f.stages,f.substages,u.output_mode,d.associateme_mapper),v=new Z(o,u.lettercase_mapper,a,u.schema_input,u.schema_output,t,u.input_divider,u.output_mode,u.output_divider,u.sort_words,u.alphabet,u.invisible)
769
+ for(let e=0;e<v.words.length;e++)v.words[e]=w.do_stages(v.words[e])
770
+ o.set_payload(v.make_text())}catch(a){a instanceof o.Validation_Error||o.uncaught_error(a)}return o.create_log()}const J={vocabug,nesca}
771
+ module.exports=J