Repository navigation
Align BBj highlighting with the BBj language server's grammar - #4142
StephanWald wants to merge 1 commit into
Conversation
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>
✅ Deploy Preview for dev-prismjs-com ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
DmitrySharabin
left a comment
There was a problem hiding this comment.
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| pattern: /(^|[^\w$])\$[\da-f]*\$/im, | ||
| lookbehind: true, |
There was a problem hiding this comment.
Please use native lookbehinds here and in the other 11 lookbehind: true patterns:
| 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.
| /\?/, | ||
| 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)/ |
There was a problem hiding this comment.
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 .:
| /\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)/ |
| }, | ||
| { | ||
| // ERR=, DOM=, END=, IOL= options of I/O verbs | ||
| pattern: /([(,][ \t]*(?:dom|end|err|iol)[ \t]*=[ \t]*)[a-z_]\w*/i, |
There was a problem hiding this comment.
*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:
| 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") |
There was a problem hiding this comment.
If a word after . stays a method name (see the comment on line 147), this line from PluginManager.bbj would pin it:
| 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 |
There was a problem hiding this comment.
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:
| 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 > >= < <= <> + - * / ^ = | |||
There was a problem hiding this comment.
Nothing pins the i flag of operator yet. This line from formValidation.bbj would:
| 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 | |||
There was a problem hiding this comment.
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:
| use ::a.bbj::A; use ::b.bbj::B | |
| use ::a.bbj::A; use ::b.bbj::B | |
| use ::BBjGridExWidget/BBjGridExWidget.bbj::BBjGridExWidget |
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:
"say ""hi"""); BBj has no backslash escapes.'CS'and'BOX'(10,12,4,4)are mnemonics, not strings. They get their ownmnemonictoken, aliasedsymbol.remstarts a comment only as a whole word. Before,theorem = 1became a comment fromremon.:, BBj's line continuation.list!,tim$) are no longer keywords.New tokens:
variablename$,count%,obj!; the suffix is the typefunctionstr(x),FNADD$(x$),@(10,5)labeldone:,GOTO done,ON x GOTO a, b,ERR=oopsline-number(aliaslabel)0010 PRINT "x"class-nameuse,class,extends,implements,new,declare,field,method, in parameter lists;BBj*types;JPanel@property#name$hex-string(aliasstring)$0D0A$,$$file-path(aliasstring)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:patternspasses for BBj. A sample of 3,000 BBjsource files tokenizes in under a second, with no file