11// this code is Deno only
22
3- import { unreachable } from "@std/assert/unreachable" ;
43import { extractResultError , ResultError } from "../compound.ts" ;
54import { PositionedError } from "../parser/parser_lib.ts" ;
6- import { HEADS , parseDictionary } from "./parser .ts" ;
5+ import { parseDictionary } from "./parallel_parser .ts" ;
76import { Dictionary } from "./type.ts" ;
87
98const SOURCE = new URL ( "../../dictionary.txt" , import . meta. url ) ;
@@ -28,69 +27,27 @@ export const dictionary: Dictionary = new Map(Object.entries(json));
2827` ;
2928 await Deno . writeTextFile ( DESTINATION , code ) ;
3029}
31- function buildOffloaded ( src : string ) : Promise < Dictionary > {
32- return new Promise ( ( resolve , reject ) => {
33- const worker = new Worker (
34- new URL ( "./worker.ts" , import . meta. url ) ,
35- { type : "module" } ,
36- ) ;
37- worker . postMessage ( src ) ;
38- worker . onmessage = ( event ) => {
39- resolve ( event . data as Dictionary ) ;
40- worker . terminate ( ) ;
41- } ;
42- worker . onerror = ( event ) => {
43- reject ( event . error ) ;
44- } ;
45- } ) ;
46- }
4730export async function build ( ) : Promise < boolean > {
4831 // deno-lint-ignore no-console
4932 console . log (
5033 `Building dictionary with ${ navigator . hardwareConcurrency } threads...` ,
5134 ) ;
5235 const start = performance . now ( ) ;
5336 const text = await Deno . readTextFile ( SOURCE ) ;
54- const heads = [ ...text . matchAll ( HEADS ) ] . map ( ( match ) => match . index ) ;
55- const regionIndices = [ ...new Array ( navigator . hardwareConcurrency ) . keys ( ) ]
56- . map ( ( index ) => {
57- const start = index * text . length / navigator . hardwareConcurrency ;
58- for ( const head of heads ) {
59- if ( start <= head ) {
60- return head ;
61- }
62- }
63- } ) ;
64- const regions = regionIndices . map ( ( index , i ) =>
65- text . slice ( index , regionIndices [ i + 1 ] ?? text . length )
66- ) ;
67- const dictionary : Dictionary = new Map ( ) ;
37+ let dictionary : Dictionary ;
6838 try {
69- const entries = await Promise . all (
70- regions . map ( ( region ) => buildOffloaded ( region ) ) ,
71- ) ;
72- for ( const entry of entries ) {
73- for ( const [ name , definition ] of entry ) {
74- if ( dictionary . has ( name ) ) {
75- throw new Error ( ) ;
76- }
77- dictionary . set ( name , definition ) ;
78- }
79- }
80- } catch ( _ ) {
81- try {
82- parseDictionary ( text ) ;
83- } catch ( error ) {
84- displayError ( `${ SOURCE } ` , extractResultError ( error ) ) ;
85- return false ;
86- }
87- unreachable ( ) ;
39+ dictionary = await parseDictionary ( text ) ;
40+ } catch ( error ) {
41+ displayError ( text , extractResultError ( error ) ) ;
42+ return false ;
8843 }
8944 await buildWithDictionary ( dictionary ) ;
9045 const end = performance . now ( ) ;
9146 const total = Math . floor ( end - start ) ;
9247 // deno-lint-ignore no-console
93- console . log ( `Building dictionary done in ${ total } ms` ) ;
48+ console . log (
49+ `Building dictionary done in ${ total } ms` ,
50+ ) ;
9451 return true ;
9552}
9653function displayError ( source : string , errors : ReadonlyArray < ResultError > ) {
0 commit comments