Lazarus

Programming => Packages and Libraries => SynEdit => Topic started by: jcmontherock on September 09, 2021, 09:13:46 pm

Title: Parsing SQL scripts with SynHighlighterSQL
Post by: jcmontherock 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.
Title: Re: Parsing SQL scripts with SynHighlighterSQL
Post by: jcmontherock 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.  
Title: Re: Parsing SQL scripts with SynHighlighterSQL
Post by: jcmontherock 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.  
Title: Re: Parsing SQL scripts with SynHighlighterSQL
Post by: devEric69 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).
Title: Re: Parsing SQL scripts with SynHighlighterSQL
Post by: Edson 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

TinyPortal © 2005-2018