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
PSL: define the grammar of string literals #4167
Comments
I am wondering if this is spec material, or something engineering should take care of and have documented. |
There is initial work towards addressing the most important limitations here: prisma/prisma-engines#738 I will update this issue with once that is reviewed and merged. |
Current state with prisma/prisma-engines#738 : the only changes to the previous state (no escaping implemented) is that |
Moving this issue to prisma/prisma since it is about the Prisma Schema Language. This is not critical for the migration engine at the moment, but it would be nice to have a grammar. |
Agreed. Here's a non-official grammar, but probably a good starting point: https://github.com/prisma/prismafile/blob/master/src/parser/index.pegjs It supports escape sequences in the same way that JSON does: https://github.com/prisma/prismafile/blob/23098e1febebf0aa6ca790aff41faac71dcb233a/src/parser/index.pegjs#L392 Currently it drives the prisma/upgrade tool. |
Relevant: prisma/prisma-engines#1973 |
Is this unofficial grammar present anywhere for viewing ? Right now, https://github.com/prisma/prismafile/blob/master/src/parser/index.pegjs leads to a 404 (which probably means the repo doesn't exist or is private) |
Simplify pest grammar of string literals We want to tokenize invalid escape sequences and report them as such later in validation. This results in better error messages across the board. closes prisma/prisma#4167
Simplify pest grammar of string literals We want to tokenize invalid escape sequences and report them as such later in validation. This results in better error messages across the board. closes prisma/prisma#4167
Simplify pest grammar of string literals We want to tokenize invalid escape sequences and report them as such later in validation. This results in better error messages across the board. closes prisma/prisma#4167
@tomhoule Sorry for the ping but anything on this ? Is the PSL grammar file available for others to view ? |
Hi @Ultra-Instinct-05 , sorry I missed your previous comment. The grammar used inside Prisma is https://github.com/prisma/prisma-engines/blob/main/libs/datamodel/schema-ast/src/parser/datamodel.pest — note however that we apply a bunch of validations on top, so this file defines a grammar that is more permissive than what we actually allow (for example: trailing commas in attributes). Notably, the new grammar for string literals is meant to be an exact implementation of the grammar for string literals from the JSON spec (https://datatracker.ietf.org/doc/html/rfc8259). |
There is no spec-style well defined grammar beyond what the implementation defines, but if this something you would like to have, the best course of action would be to open an issue so we can track interest into that and make prioritization decisions. |
@tomhoule I maintain a Sublime Text package to highlight schema files https://github.com/Sublime-Instincts/PrismaHighlight. It would be nice if there was documentation on the official grammar, so that the package highlights stuff correctly. Right now it works, but I just looked at various example schema files to make the package. I am pretty sure even VS Code can take advantage of it 🙂 |
This is mostly an issue in
@default("some string here")
directives.At the moment, escaping has an effect on parsing, but not on migrations, nor on what gets inserted by the query engine. The default string the query engine will insert is exactly what is inside the quotes in the schema, including the backslashes used for escaping.
Illustration
Given the following schema:
if you create one user with just the id, leaving the fields with defaults to be inserted by prisma, the response will be (I tested this, on both postgres and sqlite):
Proposed solution
Similar to most languages and formats with string literals (e.g. JSON), define
\
as an escaping character that will be removed from the string during parsing.Some languages allow a
\
before any character, some (like JSON) only as the first token of a recognized escape sequence.Regarding escape sequences: some string literals cannot be expressed as literals within the current schema parser. The most common example is probably strings containing a newline. Therefore common escape sequences, like
\n
, could be a worthwhile addition. Same goes for a "bare" double quote.Related issues
#1888
The text was updated successfully, but these errors were encountered: