@@ -36,21 +36,30 @@ import {
36
36
const RESERVED_SYMBOLS = "#()*+/:;<=>@[\\]^`{|}~" ;
37
37
const WORDS = new RegExp ( `[^${ escapeRegex ( RESERVED_SYMBOLS ) } ]` ) ;
38
38
39
+ function lex < T > ( parser : Parser < T > ) : Parser < T > {
40
+ return parser . skip ( spaces ) ;
41
+ }
39
42
const comment = match ( / # [ ^ \n \r ] * / , "comment" ) ;
40
43
const spaces = sourceOnly ( all ( choiceOnlyOne ( match ( / \s / , "space" ) , comment ) ) ) ;
41
- const tokiPonaWord = match ( / [ a - z ] [ a - z A - Z ] * / , "word" ) ;
42
44
const backtick = matchString ( "`" , "backtick" ) ;
43
- const openParenthesis = matchString ( "(" , "open parenthesis" ) ;
44
- const closeParenthesis = matchString ( ")" , "close parenthesis" ) ;
45
- const openBracket = matchString ( "[" , "open bracket" ) ;
46
- const closeBracket = matchString ( "]" , "close bracket" ) ;
47
- const comma = matchString ( "," , "comma" ) ;
48
- const colon = matchString ( ":" , "colon" ) ;
45
+
46
+ const tokiPonaWord = lex ( match ( / [ a - z ] [ a - z A - Z ] * / , "word" ) ) ;
47
+ const openParenthesis = lex ( matchString ( "(" , "open parenthesis" ) ) ;
48
+ const closeParenthesis = lex ( matchString ( ")" , "close parenthesis" ) ) ;
49
+ const openBracket = lex ( matchString ( "[" , "open bracket" ) ) ;
50
+ const closeBracket = lex ( matchString ( "]" , "close bracket" ) ) ;
51
+ const comma = lex ( matchString ( "," , "comma" ) ) ;
52
+ const colon = lex ( matchString ( ":" , "colon" ) ) ;
49
53
const semicolon = lex ( matchString ( ";" , "semicolon" ) ) ;
54
+ const slash = lex ( matchString ( "/" , "slash" ) ) ;
50
55
51
- function lex < T > ( parser : Parser < T > ) : Parser < T > {
52
- return parser . skip ( spaces ) ;
53
- }
56
+ const keyword = memoize ( < T extends string > ( keyword : T ) : Parser < T > =>
57
+ lex ( match ( / [ a - z \- ] + / , keyword ) )
58
+ . filter ( ( that ) =>
59
+ keyword === that ||
60
+ throwError ( new UnexpectedError ( `"${ that } "` , `"${ keyword } "` ) )
61
+ ) as Parser < T >
62
+ ) ;
54
63
const unescapedWord = allAtLeastOnce (
55
64
choiceOnlyOne (
56
65
match ( WORDS , "word" ) ,
@@ -65,17 +74,8 @@ const unescapedWord = allAtLeastOnce(
65
74
word !== "" || throwError ( new ArrayResultError ( "missing word" ) )
66
75
) ;
67
76
const word = unescapedWord . map ( escapeHtml ) ;
68
- const slash = lex ( matchString ( "/" , "slash" ) ) ;
69
77
const forms = sequence ( word , all ( slash . with ( word ) ) )
70
78
. map ( ( [ first , rest ] ) => [ first , ...rest ] ) ;
71
-
72
- const keyword = memoize ( < T extends string > ( keyword : T ) : Parser < T > =>
73
- lex ( match ( / [ a - z \- ] + / , keyword ) )
74
- . filter ( ( that ) =>
75
- keyword === that ||
76
- throwError ( new UnexpectedError ( `"${ that } "` , `"${ keyword } "` ) )
77
- ) as Parser < T >
78
- ) ;
79
79
const number = choiceOnlyOne ( keyword ( "singular" ) , keyword ( "plural" ) ) ;
80
80
const optionalNumber = optionalAll ( number ) ;
81
81
const perspective = choiceOnlyOne (
@@ -84,12 +84,10 @@ const perspective = choiceOnlyOne(
84
84
keyword ( "third" ) ,
85
85
) ;
86
86
function tag < T > ( parser : Parser < T > ) : Parser < T > {
87
- return lex ( openParenthesis )
88
- . with ( parser )
89
- . skip ( lex ( closeParenthesis ) ) ;
87
+ return openParenthesis . with ( parser ) . skip ( closeParenthesis ) ;
90
88
}
91
89
function template < T > ( parser : Parser < T > ) : Parser < T > {
92
- return lex ( openBracket ) . with ( parser ) . skip ( lex ( closeBracket ) ) ;
90
+ return openBracket . with ( parser ) . skip ( closeBracket ) ;
93
91
}
94
92
const simpleUnit = memoize ( ( kind : string ) => word . skip ( tag ( keyword ( kind ) ) ) ) ;
95
93
function detectRepetition (
@@ -498,11 +496,7 @@ const definition = choiceOnlyOne<Definition>(
498
496
type : "filler" ,
499
497
} ) ) ,
500
498
) ;
501
- const singleWord = lex ( tokiPonaWord ) ;
502
- const head = sequence (
503
- all ( singleWord . skip ( lex ( comma ) ) ) ,
504
- singleWord ,
505
- )
499
+ const head = sequence ( all ( tokiPonaWord . skip ( comma ) ) , tokiPonaWord )
506
500
. skip ( colon )
507
501
. map ( ( [ init , last ] ) => [ ...init , last ] ) ;
508
502
const entry = withSource ( spaces . with ( all ( definition ) ) )
0 commit comments