-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for runtime context object? #666
Comments
This might be a good place for a 'use strict'
const pegjs = require('pegjs')
const src = `
authorized
= perm:perm rest:(_ o:op _ p:perm { return [o, p]})* { rest.unshift(perm); return rest }
perm = pn:permname _ "(" _ n:name & { return options[pn].includes(n) } _")" {
return { [pn]: n }
}
permname
= "role"i { return "roles" }
/ "adgroup"i { return "adgroups" }
name = $[a-z_]i+
op
= $"||"
/ $"&&"
_ = $[ \t]*
`
const parser = pegjs.generate(src)
for (const a of process.argv.slice(2)) {
console.log(parser.parse(a, {
roles: ['admin'],
adgroups: ['ACME_Developer']
}))
} |
Thanks @hildjj. Are you saying that the That's not clear from the docs. Is it a "hack" or is it going to be working in future versions of PEG? I ended up creating a grammar that outputs and AST that then is interpreted by my code. {
"any": [
{
"all": [
{
"adgroup": "ACME_Developer"
},
{
"role": "admin"
}
]
},
{
"adgroup": "supergod"
}
]
} |
Yes, the docs are a little unclear about this, but it's supported behavior. I'll create a bug on peggyjs/peggy to improve the docs. |
Issue type
Prerequisites
Description
Is there any reason that context data can only be set at compile time?
I see many situations where the compiled grammar would need to access a context that changes at runtime.
Compiling the grammar for every use case doesn't seem to be a viable solution.
Unless I'm missing something....
Example code:
I'm using PEG for a decorator in ExpressJS that controls access to endpoints:
The grammar relies on a
profile
variable that is supposed to contain the current user's profile containing roles and adgroups.The profile is only available when the endpoint is hit.
I'd expect the compiled grammar to work something like this:
Software
The text was updated successfully, but these errors were encountered: