afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)
afuna ([personal profile] afuna) wrote in [site community profile] dw_dev_training2009-12-16 02:55 pm

BML, line numbers, and you!

So one of the quirks of BML is that if there's an error, the line number in the error message displayed is numbered as if "1" were the very first line of the code block you are currently in, and not the line number of the BML file.


So to find out exactly which line is causing the error, you need to:

1.) open up the BML file
2.) find which-th line the starting <?_code tag is located
3.) add that number to the line displayed in the error message, then subtract 1.

So, for example, if you have this BML file:

 1 <?_c
 2 # header
 3 # header
 4 # header
 5 # more header
 6 _c?>
 7 <?page
 8 body<=
 9 <?_code
10 {
11     die "BLAH " x 3;
12     return "blah";
13 }
14 _code?>
15 <=body
16 page?>


You will get this error message:

[Error: BLAH BLAH BLAH at '/dreamhack/8080-afuna/dw/htdocs/blah.bml' line 3. @ li-166]

Line 3 in the BML file is just part of the header comments. The actual line causing the page to fail is at line 9 (where code block begins) + 3 (line number in the error message) - 1 (because that's just the way things turn out to be!) = 11.

Right where the code is told to die.


If you're working on huge BML files, with multiple errors, it can be somewhat of a pain to calculate the error location manually each time. So a tip to make it easier: use the #line directive, which says that we should start the line counting at this location, from this number.

On the very first line of your code block, add this:

# line next_line_number

So now your BML file looks like this:

 1 <?_c
 2 # header
 3 # header
 4 # header
 5 # more header
 6 _c?>
 7 <?page
 8 body<=
 9 <?_code
10 {
11 # line 12
12     die "BLAH " x 3;
13     return "blah";
14 }
15 _code?>
16 <=body
17 page?>


And now your error message tells you the exact line that's erroring:

[Error: BLAH BLAH BLAH at '/dreamhack/8080-afuna/dw/htdocs/blah.bml' line 12. @ li-166]

No need to remember which line the <?_code block is on, or do addition in your head.

Note that the "#" of the #line directive must be the very first character in the line (don't indent it). Also note that you should give it the line number immediately after your #line directive -- think of it as telling the Perl compiler, "the next line is line 12. Start counting from there".

You'll probably find the line number in your text editor -- either on the side, or on some sort of status bar at the bottom. Depending on your text editor, you may first need to figure out how to display the line numbers in the first place.

As for which of the two methods to use: The first method involves math, but is more convenient if you need to troubleshoot something very very quickly. The second method involves just a bit more effort up front, but means that you don't need to do any calculations in your head. I prefer it for if I'm going to be spending a lot of time with this file (just need to remember to take it out before generating the patch...)

Phew, that's it. I am so looking forward to the day Dreamwidth pages are fully on .tt instead of .bml.
foxfirefey: A wee rat holds a paw to its mouth. Oh, the shock! (omg)

[personal profile] foxfirefey 2009-12-16 09:08 am (UTC)(link)
Oh wow, such esoterica. You amaze me!
janinedog: (Default)

[personal profile] janinedog 2009-12-16 04:26 pm (UTC)(link)
I had no idea about that line number definition thing. That's pretty cool!

You didn't say it explicitly here, but it's probably best to remove the line number definition before making a patch, if only because adding lines to the header at the top of the file later will make it have the wrong value, which will be confusing if someone forgets to update it.
sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)

[personal profile] sophie 2009-12-16 05:06 pm (UTC)(link)
I don't know if it was edited in or not, but I see a parenthetical "(just need to remember to take it out before generating the patch...)" at the end of the second-to-last paragraph. Might have been added though!
janinedog: (Default)

[personal profile] janinedog 2009-12-16 05:23 pm (UTC)(link)
Hm, maybe it was there and I just skipped over it accidentally. Oh well! :)
sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)

[personal profile] sophie 2009-12-16 05:05 pm (UTC)(link)
I also had absolutely no clue about the #line directive! The things you learn. :D
baggyeyes: Bugs Bunny and the Bull (Default)

[personal profile] baggyeyes 2009-12-17 12:44 am (UTC)(link)
And this is why you are the Mighty [personal profile] afuna and I am merely okwari.

Eek.
mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)

[staff profile] mark 2009-12-17 08:03 am (UTC)(link)
I have never heard of this. Wow. :o