Skip to content

Align BBj highlighting with the BBj language server's grammar - #4142

Open
StephanWald wants to merge 1 commit into
PrismJS:v2from
StephanWald:bbj-language-improvements
Open

StephanWald wants to merge 1 commit into
PrismJS:v2from
StephanWald:bbj-language-improvements

Conversation

@StephanWald

Copy link
Copy Markdown

The BBj grammar treated BBj like a C-style language: backslash escapes in strings, '…' as strings, and a short keyword list. This aligns it with the grammar of the BBj language server (bbj.langium).

Fixes:

  • A quote inside a string is written twice ("say ""hi"""); BBj has no backslash escapes.
  • 'CS' and 'BOX'(10,12,4,4) are mnemonics, not strings. They get their own mnemonic token, aliased symbol.
  • rem starts a comment only as a whole word. Before, theorem = 1 became a comment from rem on.
  • Strings continue over lines that start with :, BBj's line continuation.
  • The keyword list now has the 183 keywords of the language server's grammar. Names that only start with a keyword (list!, tim$) are no longer keywords.

New tokens:

Token Examples
variable name$, count%, obj!; the suffix is the type
function str(x), FNADD$(x$), @(10,5)
label done:, GOTO done, ON x GOTO a, b, ERR=oops
line-number (alias label) 0010 PRINT "x"
class-name after use, class, extends, implements, new, declare, field, method, in parameter lists; BBj* types;
JPanel@
property #name$
hex-string (alias string) $0D0A$, $$
file-path (alias string) use ::lib/util.bbj::Util

? (short for PRINT), #this! and #super! are keywords. name$(1,3) stays a variable, because it is a substring, not a call.

Themes see one visible change: single-quoted text is no longer string-coloured.

test:languages: 2655 pass; the BBj tests are 20 files, including a whole program. test:patterns passes for BBj. A sample of 3,000 BBj
source files tokenizes in under a second, with no file

Fixes:
- A quote inside a string is written twice ("say ""hi"""); no backslash escapes
- Single-quoted names are mnemonics ('CS', 'BOX'(…)), not strings
- `rem` starts a comment only as a whole word (`theorem = 1` is not a comment)
- Strings continue over lines that start with `:` (line continuation)
- Keyword list from bbj.langium; names that only start with a keyword
  (list!, tim$) are not keywords

New tokens: variables with a type suffix, functions (including FN…$ and
@(x,y)), labels and label references, line numbers, class names (including
Name@ client classes and array types), #fields, hex strings and ::file::
paths. `?` is PRINT; name$(1,3) is a substring, so it stays a variable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@netlify

netlify Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for dev-prismjs-com ready!

Name Link
🔨 Latest commit 4204f30
🔍 Latest deploy log https://app.netlify.com/projects/dev-prismjs-com/deploys/6ac1510359ccf10008ba1f4e
😎 Deploy Preview https://deploy-preview-4142--dev-prismjs-com.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@DmitrySharabin DmitrySharabin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for aligning BBj with the language server's grammar! I've reviewed the PR. Please see the comments and suggestions below.

The test suggestions add only input lines. To fill in their expected tokens and update the tests that the grammar suggestions change, you can run:

npm run test:languages -- --language=bbj --update

Comment thread src/languages/bbj.js
Comment on lines +37 to +38
pattern: /(^|[^\w$])\$[\da-f]*\$/im,
lookbehind: true,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use native lookbehinds here and in the other 11 lookbehind: true patterns:

Suggested change
pattern: /(^|[^\w$])\$[\da-f]*\$/im,
lookbehind: true,
pattern: /(?<=^|[^\w$])\$[\da-f]*\$/im,

One of them needs more than that. In the class-name pattern on line 88, a native lookbehind can stop after declare. Then the auto of declare auto BBjWindow w! becomes a class name. A (?!auto\b) before the first <TYPE> keeps it a keyword:

/(?<=\b(?:declare(?:[ \t]+auto)?|extends|implements|new|use)[ \t]+)(?!auto\b)<TYPE>(?:[ \t]*,[ \t]*<TYPE>)*/

I asked Claude to run the PR with both kinds of lookbehind on 561 real .bbj files. With this guard, the output is the same.

Comment thread src/languages/bbj.js
/\?/,
RegExp(
// IND, KEY and LEN are also functions: `len(x$)`
/\b(?!(?:ind|key|len)\()(?:abstract|addr|all|argc|auto|background|begin|break|bye|call|callback|case|cast|chanopt|chdir|chn|class|classend|clear|clearp|clipclear|clipfromfile|clipfromstr|cliplock|cliptofile|clipunlock|close|continue|ctl|data|day|declare|def|default|delete|denum|dim|direct|dom|dread|drop|dsz|dump|else|end|endif|endtrace|enter|erase|err|escape|except|execute|exit|exitto|extends|extract|extractrecord|fi|field|file|fileopt|find|findrecord|floatingpoint|fnend|fnerr|for|from|fulltext|gosub|goto|if|implements|ind|indexed|initfile|input|inpute|inputn|inputrecord|interface|interfaceend|iol|iolist|key|lcheckin|len|let|limit|list|load|lock|method|methodend|methodret|mkdir|mkeyed|mode|new|next|on|open|opts|pfx|precision|prefix|print|printrecord|private|process_events|protected|psz|public|read|readrecord|read_resource|record|redim|release|remove|remove_callback|rename|repeat|resclose|reset|restore|retry|return|rev|rmdir|run|save|savep|select|serial|setday|setdrive|seterr|setesc|setopts|setterm|settime|settrace|sort|sortby|sqlchn|sqlclose|sqlcommit|sqlexec|sqlopen|sqlprep|sqlrollback|sqlset|sqlunt|ssn|start|static|step|stop|string|swend|switch|sys|table|then|throw|tim|to|unlock|unt|until|updatelic|use|vkeyed|void|wait|wend|where|while|write|writerecord|xcall|xfile|xkeyed)/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A method whose name is a keyword is highlighted as a keyword:

entry! = it!.next()  =>  ["variable", "entry!"], ["operator", "="], ["variable", "it!"], ["punctuation", "."], ["keyword", "next"], …

Before this PR, next here was a function. I asked Claude to run the PR on 561 real .bbj files. In 147 of them, 754 lines have a keyword right after ., mostly in method calls such as .next(), .remove(), .close(), .write() and .clear(). Should a word after . stay a method name? If so, a lookbehind could stop the keyword pattern from matching right after .:

Suggested change
/\b(?!(?:ind|key|len)\()(?:abstract|addr|all|argc|auto|background|begin|break|bye|call|callback|case|cast|chanopt|chdir|chn|class|classend|clear|clearp|clipclear|clipfromfile|clipfromstr|cliplock|cliptofile|clipunlock|close|continue|ctl|data|day|declare|def|default|delete|denum|dim|direct|dom|dread|drop|dsz|dump|else|end|endif|endtrace|enter|erase|err|escape|except|execute|exit|exitto|extends|extract|extractrecord|fi|field|file|fileopt|find|findrecord|floatingpoint|fnend|fnerr|for|from|fulltext|gosub|goto|if|implements|ind|indexed|initfile|input|inpute|inputn|inputrecord|interface|interfaceend|iol|iolist|key|lcheckin|len|let|limit|list|load|lock|method|methodend|methodret|mkdir|mkeyed|mode|new|next|on|open|opts|pfx|precision|prefix|print|printrecord|private|process_events|protected|psz|public|read|readrecord|read_resource|record|redim|release|remove|remove_callback|rename|repeat|resclose|reset|restore|retry|return|rev|rmdir|run|save|savep|select|serial|setday|setdrive|seterr|setesc|setopts|setterm|settime|settrace|sort|sortby|sqlchn|sqlclose|sqlcommit|sqlexec|sqlopen|sqlprep|sqlrollback|sqlset|sqlunt|ssn|start|static|step|stop|string|swend|switch|sys|table|then|throw|tim|to|unlock|unt|until|updatelic|use|vkeyed|void|wait|wend|where|while|write|writerecord|xcall|xfile|xkeyed)/
/(?<!\.)\b(?!(?:ind|key|len)\()(?:abstract|addr|all|argc|auto|background|begin|break|bye|call|callback|case|cast|chanopt|chdir|chn|class|classend|clear|clearp|clipclear|clipfromfile|clipfromstr|cliplock|cliptofile|clipunlock|close|continue|ctl|data|day|declare|def|default|delete|denum|dim|direct|dom|dread|drop|dsz|dump|else|end|endif|endtrace|enter|erase|err|escape|except|execute|exit|exitto|extends|extract|extractrecord|fi|field|file|fileopt|find|findrecord|floatingpoint|fnend|fnerr|for|from|fulltext|gosub|goto|if|implements|ind|indexed|initfile|input|inpute|inputn|inputrecord|interface|interfaceend|iol|iolist|key|lcheckin|len|let|limit|list|load|lock|method|methodend|methodret|mkdir|mkeyed|mode|new|next|on|open|opts|pfx|precision|prefix|print|printrecord|private|process_events|protected|psz|public|read|readrecord|read_resource|record|redim|release|remove|remove_callback|rename|repeat|resclose|reset|restore|retry|return|rev|rmdir|run|save|savep|select|serial|setday|setdrive|seterr|setesc|setopts|setterm|settime|settrace|sort|sortby|sqlchn|sqlclose|sqlcommit|sqlexec|sqlopen|sqlprep|sqlrollback|sqlset|sqlunt|ssn|start|static|step|stop|string|swend|switch|sys|table|then|throw|tim|to|unlock|unt|until|updatelic|use|vkeyed|void|wait|wend|where|while|write|writerecord|xcall|xfile|xkeyed)/

Comment thread src/languages/bbj.js
},
{
// ERR=, DOM=, END=, IOL= options of I/O verbs
pattern: /([(,][ \t]*(?:dom|end|err|iol)[ \t]*=[ \t]*)[a-z_]\w*/i,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*NEXT after ERR= becomes an operator and a keyword:

mkdir logdir$,err=*next  =>  …, ["keyword", "err"], ["operator", "="], ["operator", "*"], ["keyword", "next"]

If I read the language server's grammar right, *NEXT is a symbolic label, one of the 12 in labels.bbl. Claude found symbolic labels after ERR=, DOM=, END= or IOL= on 1,749 lines in 175 of the 561 files, *NEXT on 1,387 of them. Should these be labels too? If so, an optional * would cover them:

Suggested change
pattern: /([(,][ \t]*(?:dom|end|err|iol)[ \t]*=[ \t]*)[a-z_]\w*/i,
pattern: /([(,][ \t]*(?:dom|end|err|iol)[ \t]*=[ \t]*)\*?[a-z_]\w*/i,

e$ = fnAdd$(a$, b$)
f$ = name$(1,3)
g$ = line$(pos+1)
sysgui!.addWindow(10, 10, 200, 200, "Hi")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a word after . stays a method name (see the comment on line 147), this line from PluginManager.bbj would pin it:

Suggested change
sysgui!.addWindow(10, 10, 200, 200, "Hi")
sysgui!.addWindow(10, 10, 200, 200, "Hi")
entry! = it!.next()

seterr oops
read (1,key=k$,err=oops,dom=missing)a$
open (1,end=eof)"file.dat"
goto 0030

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The label tests are all lower case, so nothing pins the i flag of the first two label patterns. Lines 54 and 832 of TB.bbj would. If *NEXT becomes a label (see the comment on line 74), the last line, from DataPort_main.bbj, would pin that too:

Suggested change
goto 0030
goto 0030
UNT_END:
GOTO UNT_LOOP
mkdir logdir$,err=*next

@@ -1,4 +1,4 @@
or xor not and > >= < <= + =*
or xor not and > >= < <= <> + - * / ^ =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing pins the i flag of operator yet. This line from formValidation.bbj would:

Suggested change
or xor not and > >= < <= <> + - * / ^ =
or xor not and > >= < <= <> + - * / ^ =
if (len(firstName!) AND len(lastName!)) then

@@ -0,0 +1,27 @@
use ::lib/util.bbj::Util
use ::C:\bbj\lib.bbj::Lib
use ::a.bbj::A; use ::b.bbj::B

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing pins greedy on file-path yet. Without it, the class-name pattern \bBBj[A-Z]\w* splits any path that contains a BBj… name. This line from BusinessGrid.bbj would:

Suggested change
use ::a.bbj::A; use ::b.bbj::B
use ::a.bbj::A; use ::b.bbj::B
use ::BBjGridExWidget/BBjGridExWidget.bbj::BBjGridExWidget

This branch has not been deployed

No deployments
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.

2 participants