@@ -2,7 +2,7 @@ import { unicodeIdContinueReg, unicodeIdStartReg } from './unicode-regex.js';
22
33/** @see https://github.com/GregRos/parjs/issues/59 */
44/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/consistent-type-imports */
5- const { anyCharOf, string, stringLen, noCharOf, anyStringOf, regexp, float, whitespace, eof } =
5+ const { rest , anyCharOf, string, stringLen, noCharOf, anyStringOf, regexp, float, whitespace, eof } =
66 require ( 'parjs' ) as typeof import ( 'parjs' ) ;
77const { map, qthen, or, many, between, then, thenq, manySepBy, stringify } =
88 require ( 'parjs/combinators' ) as typeof import ( 'parjs/combinators' ) ;
@@ -76,8 +76,8 @@ const pParams = pPair.pipe(manySepBy(',')).pipe(map((pair) => Object.assign({},
7676// Command
7777const pCommand = string ( '.' ) . pipe ( qthen ( pIdent ) ) . pipe ( between ( whitespace ( ) ) ) ;
7878
79- // Program
80- const pProgram = pCommand . pipe ( then ( pParams . pipe ( or ( eof ( ) ) ) ) ) . pipe ( map ( ( v ) => ( { command : v [ 0 ] , params : v [ 1 ] } ) ) ) ;
79+ // // Program
80+ // const pProgram = pCommand.pipe(then(pParams.pipe(or(eof())))).pipe(map((v) => ({ command: v[0], params: v[1] })));
8181
8282// Main Parser
8383export type Params = Record < string , string | number | boolean | null > ;
@@ -108,17 +108,27 @@ export const parse = (input: string): ParseResult => {
108108 } ;
109109 }
110110
111- const result = pProgram . parse ( _input ) ;
112- if ( ! result . isOk ) {
111+ const command = pCommand . pipe ( then ( rest ( ) ) ) . parse ( _input ) ;
112+ if ( ! command . isOk ) {
113+ return {
114+ command : null ,
115+ params : { } ,
116+ error : null ,
117+ } ;
118+ }
119+
120+ const params = pParams . pipe ( or ( eof ( ) ) ) . parse ( command . value [ 1 ] ) ;
121+ if ( ! params . isOk ) {
113122 return {
114123 command : null ,
115124 params : null ,
116- error : result . toString ( ) ,
125+ error : params . toString ( ) ,
117126 } ;
118127 }
119128
120129 return {
121130 error : null ,
122- ...result . value ,
131+ command : command . value [ 0 ] ,
132+ params : params . value ,
123133 } ;
124134} ;
0 commit comments