Better error message when the ; is missing at the end of class definition #4

Open
opened 2025-12-22 23:25:04 +00:00 by michaliskambi · 2 comments
michaliskambi commented 2025-12-22 23:25:04 +00:00 (Migrated from github.com)

The following snippet

let hudObject = {
  // imagine a lot of content here :)
  xx1: 1,
  xx2: 2,
  xx3: 3,
  xx4: 4,
  xx5: 5,
  xx6: 6,
  xx7: 7,
  xx8: 8,
  xx9: 9,
  xx10: 10,
  xx11: 11,
  xx12: 12,
  xx13: 13,
  xx14: 14,
  xx15: 15,
  xx16: 16
}

return hudObject;

fails to parse with error:

[Exception(EPOCASyntaxError["poca/hud.poca"):2,15]: Precedence level overflow

A better error message would be helpful, pointing that return right after } is not correct.

Right now the

  • line number (2) points to the beginning of statement (not to the line where } or return are)
  • and the message is not obvious :) -- what is wrong with "precedence level" here.

I tested what JS reports for this, but it seems that missing ; is actually allowed. That is,

function foo()
{
let hudObject = {
  xx1: 1,
  xx2: 2,
  xx3: 3,
  xx4: 4,
  xx5: 5,
  xx6: 6,
  xx7: 7,
  xx8: 8,
  xx9: 9,
  xx10: 10,
  xx11: 11,
  xx12: 12,
  xx13: 13,
  xx14: 14,
  xx15: 15,
  xx16: 16
}

return hudObject;
}

works OK in a browser (Firefox) console and https://runjs.app/play .

The following snippet ``` let hudObject = { // imagine a lot of content here :) xx1: 1, xx2: 2, xx3: 3, xx4: 4, xx5: 5, xx6: 6, xx7: 7, xx8: 8, xx9: 9, xx10: 10, xx11: 11, xx12: 12, xx13: 13, xx14: 14, xx15: 15, xx16: 16 } return hudObject; ``` fails to parse with error: ``` [Exception(EPOCASyntaxError["poca/hud.poca"):2,15]: Precedence level overflow ``` A better error message would be helpful, pointing that `return` right after `}` is not correct. Right now the - line number (2) points to the beginning of statement (not to the line where `}` or `return` are) - and the message is not obvious :) -- what is wrong with "precedence level" here. I tested what JS reports for this, but it seems that missing `;` is actually allowed. That is, ``` function foo() { let hudObject = { xx1: 1, xx2: 2, xx3: 3, xx4: 4, xx5: 5, xx6: 6, xx7: 7, xx8: 8, xx9: 9, xx10: 10, xx11: 11, xx12: 12, xx13: 13, xx14: 14, xx15: 15, xx16: 16 } return hudObject; } ``` works OK in a browser (Firefox) console and https://runjs.app/play .
BeRo1985 commented 2025-12-23 00:34:51 +00:00 (Migrated from github.com)

You're right that JavaScript accepts this code without a semicolon after the closing } of the object literal. That is because JavaScript has a feature called "Automatic Semicolon Insertion" (ASI), the parser automatically inserts semicolons where it thinks they should go, which is even in the JavaScript community considered a controversial feature.

POCA deliberately doesn't have this feature, okay, it has, but it's inactive by default and must be enabled explicitly per preprocessor pragma directive (yes, POCA has also a full C preprocessor implementation). The reason is that ASI, while convenient, has a history of causing really subtle and frustrating bugs, the kind where your code looks perfectly fine but behaves in unexpected ways because the parser inserted a semicolon somewhere you didn't expect. So I made the design decision early on to require explicit semicolons. It's in fact a bit more typing, but it avoids a whole category of hard-to-debug issues.

In your example, just add a semicolon after the closing } of the object literal, and it will work fine.

You're right that JavaScript accepts this code without a semicolon after the closing } of the object literal. That is because JavaScript has a feature called "Automatic Semicolon Insertion" (ASI), the parser automatically inserts semicolons where it thinks they should go, which is even in the JavaScript community considered a controversial feature. POCA deliberately doesn't have this feature, okay, it has, but it's inactive by default and must be enabled explicitly per preprocessor pragma directive (yes, POCA has also a full C preprocessor implementation). The reason is that ASI, while convenient, has a history of causing really subtle and frustrating bugs, the kind where your code looks perfectly fine but behaves in unexpected ways because the parser inserted a semicolon somewhere you didn't expect. So I made the design decision early on to require explicit semicolons. It's in fact a bit more typing, but it avoids a whole category of hard-to-debug issues. In your example, just add a semicolon after the closing } of the object literal, and it will work fine.
michaliskambi commented 2025-12-23 02:02:01 +00:00 (Migrated from github.com)

I wasn't really asking to insert the ; like in JS.

My point of this bugreport is that Precedence level overflow (and with a line number quite far from the error) is a confusing error message in this case. An error message that suggests it's a bad syntax, and points around the place where ; should be, would be more helpful.

I wasn't really asking to insert the `;` like in JS. My point of this bugreport is that `Precedence level overflow` (and with a line number quite far from the error) is a confusing error message in this case. An error message that suggests it's a bad syntax, and points around the place where `;` should be, would be more helpful.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
BeRo1985/poca#4
No description provided.