metadevpro/ts-pegjs

Wrong documentation

Ahryman40k opened this issue · 5 comments

Hi,

your documentation says to use tspegjs as a pegjs plugin like
pegjs --plugin ./src/tspegjs -o examples/arithmetics.ts --cache examples/arithmetics.pegjs

But looking at your sources show why it doesn't work. In fact, tspegjs does the job
../node_modules/.bin/tspegjs -o parser.ts --cache ./arithmetics..pegjs

Did I missed something ?
Thanks

Hi @Ahryman40k :

  • ts-pegjs is a plugin for pegjs in the way it implements the plugin API exposed by pegjs: see pegjs docs for it.
  • On the other hand, all plugins for pegjs are also the main entrypoint and you will be calling them all directly from CLI in a way pegjs is internalized as a hidden dependency.
  • All pegjs parameters and functionality is already there as are bypassed by the plugins to pegjs.
  • In the case for ts-pegjs the 95% of the generation work is still done by pegjs. The other 5% corresponds to wrapping ts code and types.

I understand thank you.

Why when I'm using CLI, I've got the following error ? What Am I doing wrong ?

pegjs --plugin ../../node_modules/.bin/tspegjs -o ./bulk-opening.ts --cache BulkOpening.pegjs
p.use is not a function

The call looks good so far, I will need to look to your BulkOpening.pegjs to be check.
Can you share a gist with a minimal reproduction sample?

I don't really have a project. Only a pegjs file tested online and some utilities installed with npm then I ran the cli command.

../../node_modules/.bin/tspegjs
tspegjs v.0.3.1      TS target for pegjs
Usage:
  tspegjs [-o outFile.ts] [--allowed-start-rules <rule1,rule2>] [--trace] [--cache] [--no-tslint] [--t

associated pegjs version ( from your package I guess ) is

../../node_modules/.bin/pegjs -v
PEG.js 0.10.0
BulkOpening "bulk_opening" = BulkStartKey _ clientId:Integer _ date:Date _ hour:Hour _ bulkId:Integer _ financialYear:Integer _ period:Integer _ appVersion:StrangeKey {
 const d = new Date(date);
 const h = new Date(hour);
 
 d.setHours( h.getHours() ) 
 d.setMinutes( h.getMinutes() )
 d.setSeconds( h.getSeconds() )
 
 return  {
    type: 'BulkHeader',
    clientId,
    bulkId,
    date: d,
    financialYear,
    period,
    appVersion
  }
}

BulkStartKey = "0000"

Date "date"  = $([0-9]+) { 
    const date = text();
    return Date.parse(date.slice(0, 4) + "-" + date.slice(4, 6) + "-" + date.slice(6, 8));
}

Hour "hour"  = $([0-9]+) { 
    const h = text();
    return new Date().setHours( h.slice(0,2), h.slice(2,4), h.slice(4,6));
}

StrangeKey "id"  = ([0-9][0-9].[0-9][0-9].[0-9][0-9].[0-9][0-9]) { 
    return text(); 
}

Integer "integer"  = [0-9]+ { 
    return parseInt(text(), 10); 
}

_ "whitespace" = [ \t\n\r]*

and finally a sample line

0000 827 20180216 170216 0012 2019 01   04.04.00.41

Testing with the latest peggy and ts-pegjs I got:

ts-pegjs@1.2.2 compile:samples

output/bulkOpening.ts:313:35 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

313       return new Date().setHours( h.slice(0,2), h.slice(2,4), h.slice(4,6));

It's a type error on your code.
Can be fixed changing your sample from:

   h.slice(0,2), h.slice(2,4), h.slice(4,6)

to

  +h.slice(0,2), +h.slice(2,4), +h.slice(4,6)