@@ -36,21 +36,30 @@ import {
3636const RESERVED_SYMBOLS = "#()*+/:;<=>@[\\]^`{|}~" ;
3737const WORDS = new RegExp ( `[^${ escapeRegex ( RESERVED_SYMBOLS ) } ]` ) ;
3838
39+ function lex < T > ( parser : Parser < T > ) : Parser < T > {
40+ return parser . skip ( spaces ) ;
41+ }
3942const comment = match ( / # [ ^ \n \r ] * / , "comment" ) ;
4043const spaces = sourceOnly ( all ( choiceOnlyOne ( match ( / \s / , "space" ) , comment ) ) ) ;
41- const tokiPonaWord = match ( / [ a - z ] [ a - z A - Z ] * / , "word" ) ;
4244const 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" ) ) ;
4953const semicolon = lex ( matchString ( ";" , "semicolon" ) ) ;
54+ const slash = lex ( matchString ( "/" , "slash" ) ) ;
5055
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+ ) ;
5463const unescapedWord = allAtLeastOnce (
5564 choiceOnlyOne (
5665 match ( WORDS , "word" ) ,
@@ -65,17 +74,8 @@ const unescapedWord = allAtLeastOnce(
6574 word !== "" || throwError ( new ArrayResultError ( "missing word" ) )
6675 ) ;
6776const word = unescapedWord . map ( escapeHtml ) ;
68- const slash = lex ( matchString ( "/" , "slash" ) ) ;
6977const forms = sequence ( word , all ( slash . with ( word ) ) )
7078 . 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- ) ;
7979const number = choiceOnlyOne ( keyword ( "singular" ) , keyword ( "plural" ) ) ;
8080const optionalNumber = optionalAll ( number ) ;
8181const perspective = choiceOnlyOne (
@@ -84,12 +84,10 @@ const perspective = choiceOnlyOne(
8484 keyword ( "third" ) ,
8585) ;
8686function 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 ) ;
9088}
9189function template < T > ( parser : Parser < T > ) : Parser < T > {
92- return lex ( openBracket ) . with ( parser ) . skip ( lex ( closeBracket ) ) ;
90+ return openBracket . with ( parser ) . skip ( closeBracket ) ;
9391}
9492const simpleUnit = memoize ( ( kind : string ) => word . skip ( tag ( keyword ( kind ) ) ) ) ;
9593function detectRepetition (
@@ -498,11 +496,7 @@ const definition = choiceOnlyOne<Definition>(
498496 type : "filler" ,
499497 } ) ) ,
500498) ;
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 )
506500 . skip ( colon )
507501 . map ( ( [ init , last ] ) => [ ...init , last ] ) ;
508502const entry = withSource ( spaces . with ( all ( definition ) ) )
0 commit comments