Author Topic: Bas file reader with keyword highlighting  (Read 6133 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Bas file reader with keyword highlighting
« on: March 12, 2019, 09:08:24 pm »
Coding practice for today:

Code: QB64: [Select]
  1. _TITLE "Highlight QB64 words" 'B+ 2019-03-12
  2.  
  3. CONST xmax = 1200
  4. CONST ymax = 720
  5. SCREEN _NEWIMAGE(xmax, ymax, 32)
  6. _SCREENMOVE 100, 10
  7.  
  8. 'get list of qb words
  9. OPEN "Just QB64 words.txt" FOR BINARY AS #1
  10. fs$ = SPACE$(LOF(1))
  11. GET #1, , fs$
  12. Split1000 fs$, CHR$(13) + CHR$(10), qb()
  13. 'FOR i = 0 TO 20
  14. '    PRINT i, qb(i)
  15. 'NEXT
  16. 'SLEEP
  17. ubqb = UBOUND(QB)
  18.  
  19. 'now get a bas file to highlite
  20. REDIM fList(0) AS STRING
  21. loadFA "*.bas", fList()
  22. WHILE _KEYDOWN(27) = 0
  23.     fnum = -1
  24.     COLOR &HFF00FFFF, &HFF000000
  25.     FOR i = 0 TO UBOUND(fList)
  26.         PRINT i; " "; fList(i)
  27.         IF i MOD 30 = 29 THEN
  28.             COLOR &HFFFFFF00, &HFF000000
  29.             PRINT: INPUT "Just press enter for more, u for up, q to quit, or file number to select "; choice$
  30.             IF LEN(choice$) THEN
  31.                 IF choice$ = "u" AND i > 59 THEN i = i - 59 ELSE i = -1
  32.                 IF choice$ = "q" THEN CLS: END
  33.                 IF VAL(choice$) > LBOUND(flist) AND VAL(choice$) <= UBOUND(flist) THEN fnum = VAL(choice$): EXIT FOR
  34.             END IF
  35.             COLOR &HFF00FFFF, &HFF000000: CLS
  36.         END IF
  37.     NEXT
  38.     COLOR &HFF00FFFF, &HFF000000: CLS
  39.     IF fnum <> -1 THEN
  40.         OPEN fList(fnum) FOR BINARY AS #1
  41.         fs$ = SPACE$(LOF(1))
  42.         GET #1, , fs$
  43.         CLOSE #1
  44.         REDIM basFile(0) AS STRING
  45.         Split1000 fs$, CHR$(13) + CHR$(10), basFile()
  46.         FOR i = 0 TO UBOUND(basFile)
  47.             apost = INSTR(basFile(i), "'")
  48.             IF apost THEN
  49.                 fLine$ = MID$(basFile(i), 1, apost - 1): tagOn$ = MID$(basFile(i), apost)
  50.             ELSE
  51.                 fLine$ = basFile(i): tagOn$ = ""
  52.             END IF
  53.             REDIM wline(0) AS STRING
  54.             Split1000 fLine$, " ", wline()
  55.             FOR j = 0 TO UBOUND(wLine)
  56.                 REDIM parts(0) AS STRING
  57.                 Split1000 wline(j), "(", parts()
  58.                 FOR K = 0 TO UBOUND(parts)
  59.                     found = 0
  60.                     IF RIGHT$(parts(K), 1) = "," THEN
  61.                         addp$ = ",": test$ = LEFT$(parts(K), LEN(parts(K)) - 1)
  62.                     ELSEIF RIGHT$(parts(K), 1) = ":" THEN
  63.                         addp$ = ":": test$ = LEFT$(parts(K), LEN(parts(K)) - 1)
  64.                     ELSEIF RIGHT$(parts(K), 1) = ")" THEN
  65.                         addp$ = ")": test$ = LEFT$(parts(K), LEN(parts(K)) - 1)
  66.                     ELSE
  67.                         addp$ = "": test$ = parts(K)
  68.                     END IF
  69.                     FOR l = 0 TO ubqb
  70.                         IF qb(l) = test$ THEN found = -1: EXIT FOR
  71.                     NEXT
  72.                     IF found THEN
  73.                         'highlight word
  74.                         COLOR &HFF0000FF, &HFF000000
  75.                     ELSE
  76.                         COLOR &HFF00FFFF, &HFF000000
  77.                     END IF
  78.                     PRINT test$;
  79.                     COLOR &HFF00FFFF, &HFF000000
  80.                     PRINT addp$;
  81.                     IF K <> UBOUND(parts) THEN PRINT "(";
  82.                 NEXT
  83.                 PRINT " ";
  84.             NEXT
  85.             PRINT tagOn$
  86.             IF i MOD 30 = 29 THEN
  87.                 COLOR &HFFFFFF00, &HFF000000
  88.                 PRINT: INPUT "Just press enter for more, u for up, q to quit "; choice$
  89.                 IF LEN(choice$) THEN
  90.                     IF choice$ = "u" AND i > 59 THEN i = i - 59 ELSE i = -1
  91.                     IF choice$ = "q" THEN CLS: END
  92.                 END IF
  93.                 COLOR &HFF00FFFF, &HFF000000: CLS
  94.             END IF
  95.         NEXT
  96.     END IF
  97.  
  98. SUB loadFA (spec$, fa() AS STRING)
  99.     cd$ = _CWD$
  100.     CHDIR ".." '>>>>>>>>>>>>>>>>>>>>>>>  you might not want to do this depending where you run this from
  101.     CONST TmpFile$ = "DIR$INF0.INF"
  102.     'SHELL _HIDE "DIR " + spec$ + " /b > " + TmpFile$
  103.     SHELL _HIDE "DIR " + spec$ + " /b /s /o:gen> " + TmpFile$
  104.     OPEN TmpFile$ FOR INPUT AS #1
  105.     Index% = -1
  106.     DO WHILE NOT EOF(1)
  107.         Index% = Index% + 1
  108.         REDIM _PRESERVE fa(Index%) AS STRING
  109.         LINE INPUT #1, fa(Index%)
  110.     LOOP
  111.     CLOSE #1
  112.     KILL TmpFile$
  113.     CHDIR cd$
  114.  
  115. 'notes: REDIM the array(0) to be loaded before calling Split '<<<<<<<<<<<<<<<<<<<<<<< IMPORTANT!!!!
  116. SUB Split1000 (mystr AS STRING, delim AS STRING, arr() AS STRING)
  117.     ' bplus modifications of Galleon fix of Bulrush Split reply #13
  118.     ' http://xmaxw.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=1612.0
  119.     ' this sub further developed and tested here: \test\Strings\Split test.bas
  120.     DIM copy AS STRING, p AS LONG, curpos AS LONG, arrpos AS LONG, dpos AS LONG
  121.  
  122.     copy = mystr 'make copy since we are messing with mystr when the delimiter is a space
  123.  
  124.  
  125.     ' >>>>>>>>>>>>>>>>> This was interferring with indentations
  126.     'special case if delim is space, probably want to remove all excess space
  127.     'IF delim = " " THEN
  128.     '    copy = RTRIM$(LTRIM$(copy))
  129.     '    p = INSTR(copy, "  ")
  130.     '    WHILE p > 0
  131.     '        copy = MID$(copy, 1, p - 1) + MID$(copy, p + 1)
  132.     '        p = INSTR(copy, "  ")
  133.     '    WEND
  134.     'END IF
  135.  
  136.  
  137.     curpos = 1
  138.     arrpos = 0
  139.     LD = LEN(delim) 'mod
  140.     dpos = INSTR(curpos, copy, delim)
  141.     DO UNTIL dpos = 0
  142.         arr(arrpos) = MID$(copy, curpos, dpos - curpos)
  143.         arrpos = arrpos + 1
  144.         IF arrpos > UBOUND(arr) THEN REDIM _PRESERVE arr(UBOUND(arr) + 1000) AS STRING
  145.         curpos = dpos + LD
  146.         dpos = INSTR(curpos, copy, delim)
  147.     LOOP
  148.     arr(arrpos) = MID$(copy, curpos)
  149.     REDIM _PRESERVE arr(arrpos) AS STRING 'need this line? YES to get the ubound correct
  150.  
  151.  

 
Highlight QB64 words.PNG

Notice key words in comments not highlighted.

BTW _EXPLICIT is missing from Wiki list.
* Just QB64 words.txt (Filesize: 3.42 KB, Downloads: 193)
« Last Edit: March 12, 2019, 09:18:08 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #1 on: March 14, 2019, 05:25:48 pm »
Hi Bplus
it lets me remember an old project made in quickbasic CORSOBAS.BAS
that  uses two my libs, FINEDOS.BAS and MOUSEINT.BAS both these posted on www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]'s forum after porting to QB64 for incomplete compatibility about INT calling with mouse services.
CORSOBAS.BAS is a program that shows codefile.BAS in plaint text and listed in a filelist.TXT and after user's request it runs the relative .EXE. This program uses a TXT file with all token for QuickBasic to parser the code and to colorize the screen output.

If I find the ported files I'll post it here the entire program.

Thanks to let me remember!

Here attached LEZ10.BAS
* LEZ10.BAS (Filesize: 7.73 KB, Downloads: 175)
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #2 on: March 14, 2019, 09:47:29 pm »
Hi TempodiBasic,

Did you get the right file? It looks like a quick graphics sampler.

BTW, I have been thinking about color coding everything like the IDE does. Which would require identifying each element in a program line.

Also I know of one situation that this highlighter code would get wrong.
I leave that as a quiz to you guys. ;-))

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #3 on: March 15, 2019, 11:52:05 am »
Hi Bplus
i need just some time to search in the various PC/notebooks where are the different files...(old Qbasic code, Modded to QB64 libraries, file of token)

Quote
FINEDOS.BAS and MOUSEINT.BAS both these posted on www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]'s forum
Quote
CORSOBAS.BAS
Quote
This program uses a TXT file with all token for QuickBasic to parser the code and to colorize the screen output.

Thanks for attention

Yes LEZ10.BAS is the original file that is loaded and showed in program as demo.

See later
Programming isn't difficult, only it's  consuming time and coffee

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #4 on: March 19, 2019, 07:44:40 am »
Hi Mark
idea is good and how much is universal i don't know
but why is soo slow on my win7 machine?
is that because SDL layer..hmmm
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #5 on: March 19, 2019, 10:41:01 am »
Hi Mark
idea is good and how much is universal i don't know
but why is soo slow on my win7 machine?
is that because SDL layer..hmmm

First we get the code to work, then we work on speed if it is worth the trouble.
I doubt QB64 is using SDL.

Aurel do you want to take the challenge?
Quote
Also I know of one situation that this highlighter code would get wrong.
I leave that as a quiz to you guys. ;-))

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #6 on: March 19, 2019, 11:29:55 am »
Mark
I am not for chalange..
but i don't get it how you highlite the code
as output i get some uncler (very bad shaped letters) in window
without any syntax coloring like you show it ?
so might be problem with my graphic card or openGL version or
SDL version ..i am not sure what is in game ?
sorry i am new to qb64 ..yes do you can believe? heh
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #7 on: March 19, 2019, 11:41:52 am »
Mark
I am not for chalange..
but i don't get it how you highlite the code
as output i get some uncler (very bad shaped letters) in window
without any syntax coloring like you show it ?
so might be problem with my graphic card or openGL version or
SDL version ..i am not sure what is in game ?
sorry i am new to qb64 ..yes do you can believe? heh

I will bet you are not working with a current version of QB64. Checkout the QB64.org Home Page on first Announcements Board.
« Last Edit: March 19, 2019, 11:57:02 am by bplus »

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #8 on: March 19, 2019, 11:57:31 am »
Mark
I download last stable version from qb64.org site
so i don't know what might be the latest release?
ok i can check again !
qbDownload.png
* qbDownload.png (Filesize: 69.14 KB, Dimensions: 882x520, Views: 250)
« Last Edit: March 19, 2019, 12:06:14 pm by Aurel »
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #9 on: March 19, 2019, 12:09:37 pm »
Hi Aurel,
 
Also you will need the attached file in the OP of the QB64 words to highlight, put in same Folder as compiled code.

Update: Our posts crossed, I just checked the code with the stable download, it worked fine.
« Last Edit: March 19, 2019, 12:16:22 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #10 on: March 19, 2019, 12:14:11 pm »
Quote
There are some Basic hints for QB64 coming from older QB's:

The _KEYWORDS are new to QB64 and are listed first in Alphabetics Keywords in Help, ones without _prefix are compatible with older QBs. You can find on-line Wiki help here: http://qb64.org/wiki/Main_Page which is for me easier to use than Help in IDE.

You might want to Select and bullet "Output EXE to source folder" so that the compiled programs stay with the source .bas in same folder.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Bas file reader with keyword highlighting
« Reply #11 on: March 20, 2019, 06:55:08 pm »
Hi
for now I have found only the previous version of CORSOBAS.BAS that doesn't colorize the line of code !
Sigh!  :-(( this can happen when you use different pc in different days and have no a single place in which to put files.

however the results that I have got are the same that you can see if you load this code into Qb64 IDE
Code: QB64: [Select]
  1. REM if game isn't running (main menu)
so you can observe REM if in keyword color, game isn in text color, 't running (main menu) in comment color

this has been gained using a single character processor of a single line of code.
Programming isn't difficult, only it's  consuming time and coffee