You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The returnTypes option is ignored for expressions with actions. Currently I do think all actions always have a return type of any no matter what is being specified in returnTypes. This is important to me because I would like to make the parser generated with plantuml-parser internally typed.
To illustrate what is going on consider the following very simple rule:
Line = .*
{
return "A string!"
}
when compiling a parser using this rule and the following returnTypes declaration:
returnTypes: {'Line': 'number'}
I would expect the type script compilation to fail. Because the return type of Line should be number but is always string.
Full blown example
Consider the following file:
// genparser.jsconstpegjs=require('pegjs');consttspegjs=require('ts-pegjs');letparser=pegjs.generate(` Line = .* { return "A string!" } `,{format: 'commonjs',output: 'source',plugins: [tspegjs],trace: true,tspegjs: {customHeader: ` // Hacky way to make the generated // parser executable. // This is only here for the sake of // this demonstration const Tracer = require('pegjs-backtrace'); const parserInput = ''; console.log( peg$parse( parserInput, { tracer: new Tracer( parserInput, { showTrace: true, useColor: false, } ) } ) ) `},returnTypes: {'Line': 'number'}})console.log(parser)
The current implementation is limited in the way ts-pegjs is a decorator over pegjs and not a complete TS generator.
As commented before, to fully handling types we need some changes inside pegjs to support it.
What the current implementation does for returnTypes is to type only the functions of the form:
function peg$parse<<Rule>>(): <<Type>> {.
These functions expose a public façade for start-rules only (aka public-API).
In your sample @Enteee, the type is injected in peg$parseLine():
The
returnTypes
option is ignored for expressions with actions. Currently I do think all actions always have a return type ofany
no matter what is being specified inreturnTypes
. This is important to me because I would like to make the parser generated with plantuml-parser internally typed.To illustrate what is going on consider the following very simple rule:
when compiling a parser using this rule and the following
returnTypes
declaration:I would expect the type script compilation to fail. Because the return type of
Line
should benumber
but is alwaysstring
.Full blown example
Consider the following file:
generating
parser.ts
Give me the following
`parser.ts`
Then, compilation works just fine:
$ npx tsc parser.ts # this should fail
and even running the parser
Looking at the generated typescript output (
parser.ts
). I do think the following lines:should be:
If I change this manually, I get the expected typescript compilation error:
The text was updated successfully, but these errors were encountered: