1
1
import { memoize } from "@std/cache/memoize" ;
2
+ import {
3
+ lazy as lazyEval ,
4
+ nullableAsArray ,
5
+ throwError ,
6
+ } from "../../misc/misc.ts" ;
2
7
import {
3
8
contentWordSet ,
4
9
fillerSet ,
@@ -7,7 +12,6 @@ import {
7
12
preverbSet ,
8
13
tokiPonaWordSet ,
9
14
} from "../dictionary.ts" ;
10
- import { nullableAsArray , throwError } from "../../misc/misc.ts" ;
11
15
import {
12
16
Clause ,
13
17
ContextClause ,
@@ -58,7 +62,6 @@ import {
58
62
UnrecognizedError ,
59
63
} from "./parser_lib.ts" ;
60
64
import { describe , Token } from "./token.ts" ;
61
- import { lazy as lazyEval } from "../../misc/misc.ts" ;
62
65
63
66
const spaces = match ( / \s * / , "spaces" ) ;
64
67
@@ -125,6 +128,15 @@ const emphasis = choice<Emphasis>(
125
128
specificWord ( "a" ) . map ( ( word ) => ( { type : "word" , word } ) ) ,
126
129
) ;
127
130
const 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
+ ) ;
128
140
function xAlaX (
129
141
useWord : Set < string > ,
130
142
description : string ,
@@ -136,34 +148,30 @@ function xAlaX(
136
148
specificToken ( "inside long glyph" )
137
149
. filter ( ( { words } ) => filterCombinedGlyphs ( words , "ala" ) ) ,
138
150
)
139
- . then ( ( word ) =>
140
- specificWord ( word )
141
- . skip ( specificToken ( "headless long glyph end" ) )
142
- . map ( ( ) => ( { type : "x ala x" , word } ) )
143
- ) ,
151
+ . then ( alaXLongGlyph ) ,
144
152
specificToken ( "x ala x" )
145
153
. map ( ( { word } ) => ( { type : "x ala x" , word } ) ) ,
146
154
word
147
- . then ( ( word ) =>
148
- sequence ( specificWord ( "ala" ) , specificWord ( word ) )
149
- . map ( ( ) => ( { type : "x ala x" , word } ) )
150
- ) ,
155
+ . then ( alaX ) ,
151
156
) ;
152
157
}
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
+ ) ;
153
168
function simpleWordUnit (
154
169
word : Set < string > ,
155
170
description : string ,
156
171
) : Parser < SimpleHeadedWordUnit > {
157
172
return choice < SimpleHeadedWordUnit > (
158
173
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 ) ,
167
175
xAlaX ( word , description ) ,
168
176
wordFrom ( word , description )
169
177
. map ( ( word ) => ( { type : "default" , word } ) ) ,
0 commit comments