11import { memoize } from "@std/cache/memoize" ;
2+ import {
3+ lazy as lazyEval ,
4+ nullableAsArray ,
5+ throwError ,
6+ } from "../../misc/misc.ts" ;
27import {
38 contentWordSet ,
49 fillerSet ,
@@ -7,7 +12,6 @@ import {
712 preverbSet ,
813 tokiPonaWordSet ,
914} from "../dictionary.ts" ;
10- import { nullableAsArray , throwError } from "../../misc/misc.ts" ;
1115import {
1216 Clause ,
1317 ContextClause ,
@@ -58,7 +62,6 @@ import {
5862 UnrecognizedError ,
5963} from "./parser_lib.ts" ;
6064import { describe , Token } from "./token.ts" ;
61- import { lazy as lazyEval } from "../../misc/misc.ts" ;
6265
6366const spaces = match ( / \s * / , "spaces" ) ;
6467
@@ -125,6 +128,15 @@ const emphasis = choice<Emphasis>(
125128 specificWord ( "a" ) . map ( ( word ) => ( { type : "word" , word } ) ) ,
126129) ;
127130const optionalEmphasis = optional ( emphasis ) ;
131+ const alaXLongGlyph = memoize ( ( word : string ) =>
132+ specificWord ( word )
133+ . skip ( specificToken ( "headless long glyph end" ) )
134+ . map ( ( ) => ( { type : "x ala x" , word } ) as const )
135+ ) ;
136+ const alaX = memoize ( ( word : string ) =>
137+ sequence ( specificWord ( "ala" ) , specificWord ( word ) )
138+ . map ( ( ) => ( { type : "x ala x" , word } ) as const )
139+ ) ;
128140function xAlaX (
129141 useWord : Set < string > ,
130142 description : string ,
@@ -136,34 +148,30 @@ function xAlaX(
136148 specificToken ( "inside long glyph" )
137149 . filter ( ( { words } ) => filterCombinedGlyphs ( words , "ala" ) ) ,
138150 )
139- . then ( ( word ) =>
140- specificWord ( word )
141- . skip ( specificToken ( "headless long glyph end" ) )
142- . map ( ( ) => ( { type : "x ala x" , word } ) )
143- ) ,
151+ . then ( alaXLongGlyph ) ,
144152 specificToken ( "x ala x" )
145153 . map ( ( { word } ) => ( { type : "x ala x" , word } ) ) ,
146154 word
147- . then ( ( word ) =>
148- sequence ( specificWord ( "ala" ) , specificWord ( word ) )
149- . map ( ( ) => ( { type : "x ala x" , word } ) )
150- ) ,
155+ . then ( alaX ) ,
151156 ) ;
152157}
158+ const reduplicateRest = memoize ( ( word : string ) =>
159+ count ( manyAtLeastOnce ( specificWord ( word ) ) )
160+ . map ( ( count ) =>
161+ ( {
162+ type : "reduplication" ,
163+ word,
164+ count : count + 1 ,
165+ } ) as const
166+ )
167+ ) ;
153168function simpleWordUnit (
154169 word : Set < string > ,
155170 description : string ,
156171) : Parser < SimpleHeadedWordUnit > {
157172 return choice < SimpleHeadedWordUnit > (
158173 wordFrom ( word , description )
159- . then ( ( word ) =>
160- count ( manyAtLeastOnce ( specificWord ( word ) ) )
161- . map ( ( count ) => ( {
162- type : "reduplication" ,
163- word,
164- count : count + 1 ,
165- } ) )
166- ) ,
174+ . then ( reduplicateRest ) ,
167175 xAlaX ( word , description ) ,
168176 wordFrom ( word , description )
169177 . map ( ( word ) => ( { type : "default" , word } ) ) ,
0 commit comments