Forum > SynEdit

Parsing SQL scripts with SynHighlighterSQL

(1/1)

jcmontherock:
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.

jcmontherock:
Finally, I copied SynHilighterSQL in the project directory and I changed the following:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...      '#': fProcTable[I] := @HashProc;   // added in MakeMethodTables after   '/': fProcTable[I] := @SlashProc;...// Procedure added:procedure TSynSQLSyn.HashProc;begin                                       fTokenID := tkComment;       repeat                                     Inc(Run);                             until fLine[Run] in [#0, #10, #13];  end; 

jcmontherock:
Little change in proc HashProc:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TSynSQLSyn.HashProc;begin  if SQLDialect = sqlMySql then begin    fTokenID := tkComment;    repeat      Inc(Run);    until fLine[Run] in [#0, #10, #13];  end  else UnknownProc;end;  

devEric69:
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).

Edson:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- // Explores xLex.SetLine('Somewhere in the spot', 0); while not xLex.GetEol do begin //here is pointing to a token    Token : = xLex.GetToken; //reads the token    TokenAtt : = xLex.GetTokenAttribute; //read attribute     //passes to the next   xLex.Next; end; 
Moreover, if you use SynFacilSyn and SynEdit, then you can easily implement code completion using SynFacilCompletion: https://github.com/t-edson/SynFacilCompletion

Navigation

[0] Message Index

Go to full version