Skip to content
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

Fix defaultFormatFailMsg at compile time #301

Merged

Conversation

GallaFrancesco
Copy link
Contributor

Trying to generate a grammar whose PEG syntax is wrong at compile time with pegged v0.4.5 and DMD v2.095.0 or v2.096.1 fails with:

./../pegged/peg.d(284,30): Error: static variable `defaultFormatFailMsg` cannot be read at compile time
../../pegged/peg.d(284,30):        called from here: `this.failMsg(cast(string delegate(Position, string, string, const(ParseTree)))defaultFormatFailMsg, "Sucess")`
../../pegged/peg.d(271,40):        called from here: `this.toStringThisNode(allChildrenSuccessful)`
../../pegged/peg.d(266,60):        called from here: `child.toString(tabs ~ (i < this.children.length - 1LU ? " | " : "   "))`
../../pegged/peg.d(257,12):        13 recursive calls to function `toString`
../../pegged/grammar.d(106,75):        called from here: `defAsParseTree.toString("")`
src/pegged/examples/simple_arithmetic.d(11,14):        called from here: `grammar("\x0aArithmetic:\x0a    Term     < Factor (Add / Sub)*\x0a    Add      <-- \"+\" Factor\x0a    Sub      < \"-\" Factor\x0a    Factor   < Primary (Mul / Div)*\x0a    Mul      < \"*\" Primary\x0a    Div      < \"/\" Primary\x0a    Primary  < Parens / Neg / Number / Variable\x0a    Parens   < :\"(\" Term :\")\"\x0a    Neg      < \"-\" Primary\x0a    Number   < ~([0-9]+)\x0a    Variable <- identifier\x0a
  • To reproduce, introduce a syntax error, simple as changing a < into <-- (or any symbol not recognized by pegged which is not an identifier) in one of the example/ grammars, such as the simple_arithmetic above. Then run ./ci.sh
  • Proposed fix: change defaultFormatFailMsg declaration from auto to immutable. The result is a properly formatted fail msg:
/home/fra/_progs/forks/Pegged/examples/simple_arithmetic/src/pegged/examples/simple_arithmetic.d-mixin-11(11,1): Error: static assert:  "Pegged (failure)
 +-Pegged.Grammar (failure)
    +-Pegged.GrammarName[1, 17]["Arithmetic"]
    |  +-Pegged.Identifier[1, 11]["Arithmetic"]
  [full parse tree dump omitted]

@veelo
Copy link
Collaborator

veelo commented May 18, 2021

I have trouble understanding this code (like why is this a delegate) and why this change fixes the problem. Good job figuring this out! I'd be interested if you can share more clarity, but I'll merge this anyway because it clearly works better this way. Thanks!

@veelo veelo merged commit 9084c49 into dlang-community:master May 18, 2021
@Zardoz89
Copy link
Contributor

I have trouble understanding this code (like why is this a delegate) and why this change fixes the problem.

Perhaps, because this :

 string failMsg(string delegate(Position, string, string, const ParseTree) formatFailMsg = defaultFormatFailMsg,
        string successMsg = "Sucess") const @property
{

failMsg expects a delegate to format fail messages.

Why this change fixed? I don't know, but kudos to @GallaFrancesco for finding and fixing it.

@GallaFrancesco
Copy link
Contributor Author

failMsg expects a delegate because delegates can be passed down as function arguments, see https://tour.dlang.org/tour/en/basics/delegates and https://dlang.org/spec/function.html#closures

I'm no expert, but the reason why using auto does not work at compile time could be that failMsg accepts a delegate which should be initialized at compile time. immutable global declaration with explicit initialization are treated as constants (which allows for CTFE) while auto global declarations are not. See the following example:

  • this does not compile:
auto deg = delegate(string str) { return "deg: "~str;};
shared static this() {
  pragma(msg, deg("ciao"));
}
  • this compiles and prints successfully:
immutable deg = delegate(string str) { return "deg: "~str;};
shared static this() {
  pragma(msg, deg("ciao"));
}

@GallaFrancesco GallaFrancesco deleted the fix_defaultFormatFailMsg branch May 18, 2021 09:39
@veelo
Copy link
Collaborator

veelo commented May 18, 2021

That helps. Thanks both of you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants