News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Syntax Highlight PlayBASIC source code as HTML

Started by kevin, February 01, 2022, 07:31:44 PM

Previous topic - Next topic

kevin

   Syntax Highlight PlayBASIC source code as HTML


  KeyWords:  (Printing Code, Print Source Code, HTML)

 
PlayBASIC Code: [Select]
; PROJECT : Syntax-Highlighter-To-Html
; AUTHOR : Kevin Picone - PlayBASIC Tutor - http://playbasic.com
; CREATED : 4/05/2014
; EDITED : 2/02/2022
; ---------------------------------------------------------------------

;----------------------------------------------------------------------------------
;----------------------------------------------------------------------------------
;----------------------------------------------------------------------------------
;----------------------------------------------------------------------------------
;----------------------------------------------------------------------------------
;----------------------------------------------------------------------------------
;----------------------------------------------------------------------------------

// NOTE: This DEMO with highlight and display in the browser
// it's OWN CODE. You'll need to mod it to display other
// projects !!

;----------------------------------------------------------------------------------
;----------------------------------------------------------------------------------



STartTime=Timer()
Html_Template$=PB2HTML_LoadFileToString("templates/HTML-TEMPLATE.txt")
code$=PB2HTML_ConvertFile$("temp.source")
print "Parse time:"+str$(timer()-startTime )
sync

; stick template together
Html_Template$=replace$(Html_Template$,"[BODY]",code$)


TestFileURL$=currentdir$()+"Test.html"

PB2HTML_SaveStringAsFile(TestFileURL$,Html_Template$)


OpenWebSite(TestFileURL$)




sync






Constant SW_SHOWNORMAL = 1

LinkDll "Shell32.dll"
ShellExecute(hwnd, lpOperation$, lpFile$, lpParameters$, lpDirectory$, nShowCmd) alias "ShellExecuteA" as Integer
EndLinkDLL



FUnction OpenWebSite(URL$)

Url$="file://"+URL$
Url$=replace$(Url$,"\","/")

hwnd=GetScreenHandle()
if hwnd
ShellExecute hwnd, "open", URL$, "", "", SW_SHOWNORMAL
endif

EndFunction






  Download Attached Code

BigDan256

Does that tokenizer match how the interpreter tokenizes?

I have a half-finished VSCode extension I've been using and gradually working on. This tokenizer would greatly help finish it off.

Wasn't going to say anything until I got a bit further.

kevin

#2
  
QuoteDoes that tokenizer match how the interpreter tokenizes?

   
     yeah..  I think so..  but don't quote me on that.


 
QuoteI have a half-finished VSCode extension I've been using and gradually working on. This tokenizer would greatly help finish it off.

      Cool, that'd be awesome!    The idea crossed my mine a while ago but had no idea how to implement it.. no surprises there! :(

BigDan256

I don't have much idea either ;) But it's easy to get an extension project started, and easy to do the basic tokenizing for highlighting.
I've hard-coded the keywords.txt content into it, and no idea how to make that dynamically load.
And then the trickier stuff like distinguishing commands/constants/functions/identifiers I haven't figured out.
It'd be great to do some auto-formatting or static analysis extension similar to other languages.
But yeah, I'm planning too far ahead and then crashing and burning.

Having fun pulling your code apart though. Very interesting to see hash table implemented in basic.
Always felt string manipulation was a little behind in basic, but you do some cool things, like the array splitting.

A few things need tweaking like negative numbers, the c-style hex literals and mismatching remstart/remend with /* */ but that's more at the parsing level than the tokenizing, I think this'll be great reference. Thinking of the single context just as a remstate that only changes per-line is really helpful.

kevin

  Hey Dan,

QuoteI don't have much idea either

   Good to know ! :)

Quote
I've hard-coded the keywords.txt content into it, and no idea how to make that dynamically load.
And then the trickier stuff like distinguishing commands/constants/functions/identifiers I haven't figured out.

   I don't the rules off hand, but PB just checks stuff in order.  If it's not a known keyword it's a variable or array name.   There's a few exceptions that come to mind like fields within types can use command name keywords / constants (from memory) .    Since they period character precedes them in the lex it just assumes field and never searches for the keyword anywhere else.

   The actual command list is dumped by PB upon start up from the IDE.   Which I know doesn't help you, but there's a command line control for it. 


Quote
It'd be great to do some auto-formatting or static analysis extension similar to other languages.


   Well, if you can it working that'd awesome too.


Quote
Having fun pulling your code apart though. Very interesting to see hash table implemented in basic.
Always felt string manipulation was a little behind in basic, but you do some cool things, like the array splitting.

   Dunno..  It's a pretty old snippet so had to go back and have a look.    I think normally i've just sorted the keywords and binary chopped them.    Could prolly just have  dynamically created array of strings indexes that share the same hashes, then just search that group which I suspect would suit PB more.   



QuoteBut yeah, I'm planning too far ahead and then crashing and burning.

     It's hard not to put the cart before the horse ..   exciting tho !