Recent

Author Topic: Parsing SQL scripts with SynHighlighterSQL  (Read 6639 times)

jcmontherock

  • Full Member
  • ***
  • Posts: 234
Parsing SQL scripts with SynHighlighterSQL
« on: September 09, 2021, 09:13:46 pm »
I try to parse a sql file containing several scripts. I am using SynHighlighterSQL. Every tests I did, were OK, except one thing: the hash char (#) is not taken in account. This char is used for comments in dialect MySQL. The parser give me ox23 (that's ok) fort token value and  tkUnknown for token type. This character has not been defined in procedure TSynSQLSyn.MakeMethodTables.

I am using W10/64 with Lazarus 220Rc1.
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

jcmontherock

  • Full Member
  • ***
  • Posts: 234
Re: Parsing SQL scripts with SynHighlighterSQL
« Reply #1 on: September 24, 2021, 03:33:25 pm »
Finally, I copied SynHilighterSQL in the project directory and I changed the following:
Code: Pascal  [Select][+][-]
  1. ...
  2.       '#': fProcTable[I] := @HashProc;   // added in MakeMethodTables after   '/': fProcTable[I] := @SlashProc;
  3. ...
  4. // Procedure added:
  5. procedure TSynSQLSyn.HashProc;
  6. begin                                    
  7.   fTokenID := tkComment;    
  8.   repeat                                
  9.     Inc(Run);                          
  10.   until fLine[Run] in [#0, #10, #13];  
  11. end;
  12.  
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

jcmontherock

  • Full Member
  • ***
  • Posts: 234
Re: Parsing SQL scripts with SynHighlighterSQL
« Reply #2 on: October 09, 2021, 12:16:46 pm »
Little change in proc HashProc:

Code: Pascal  [Select][+][-]
  1. procedure TSynSQLSyn.HashProc;
  2. begin
  3.   if SQLDialect = sqlMySql then begin
  4.     fTokenID := tkComment;
  5.     repeat
  6.       Inc(Run);
  7.     until fLine[Run] in [#0, #10, #13];
  8.   end
  9.   else UnknownProc;
  10. end;
  11.  
  12.  
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Parsing SQL scripts with SynHighlighterSQL
« Reply #3 on: October 09, 2021, 01:53:16 pm »
Well, just for the information, if you still have a\some troubles to parse and then check the SQL grammar of your scripts, there is also the fpsqlparser unit (fcl-db package).
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: Parsing SQL scripts with SynHighlighterSQL
« Reply #4 on: October 09, 2021, 05:44:04 pm »
Better use SynFacilSyn: https://github.com/t-edson/SynFacilSyn

It's a good lexer and well documented. Check section 5.20 of documentation: "Using the highlighter as lexical analyzer - syntactic". There you will find a basic scanner loop:

Code: Pascal  [Select][+][-]
  1.  // Explores
  2.  xLex.SetLine('Somewhere in the spot', 0);
  3.  while not xLex.GetEol do begin
  4.  //here is pointing to a token
  5.     Token : = xLex.GetToken; //reads the token
  6.     TokenAtt : = xLex.GetTokenAttribute; //read attribute
  7.  
  8.     //passes to the next
  9.    xLex.Next;
  10. end;
  11.  

Moreover, if you use SynFacilSyn and SynEdit, then you can easily implement code completion using SynFacilCompletion: https://github.com/t-edson/SynFacilCompletion

Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

 

TinyPortal © 2005-2018