From 816fbdc2107fcae22f2564ecb58e6b92d65ddaf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Fri, 10 Nov 2023 19:53:18 +0100 Subject: [PATCH] TextHighlighter: Fix 'highlight' definition file highlighting. [GB.HIGHLIGHT] * BUG: TextHighlighter: Understand "from here" and "between here" commands. * BUG: TextHighlighter: Fix 'highlight' definition file highlighting. * NEW: TextHighlighter: "@word" special command allows to define the regular expression that defines what a word is. --- .../gb.highlight/.src/CCommandBetween.class | 1 + comp/src/gb.highlight/.src/CCommandFrom.class | 10 ++++ comp/src/gb.highlight/.src/Main.module | 6 +-- .../gb.highlight/.src/TextHighlighter.class | 12 +++++ .../custom/CustomHighlighter.class | 2 - .../src/gb.highlight/highlight/diff.highlight | 6 +-- .../highlight/highlight.highlight | 50 ++++++++++++++++--- 7 files changed, 71 insertions(+), 16 deletions(-) diff --git a/comp/src/gb.highlight/.src/CCommandBetween.class b/comp/src/gb.highlight/.src/CCommandBetween.class index 1a6cbcaaa..57a19a6e4 100644 --- a/comp/src/gb.highlight/.src/CCommandBetween.class +++ b/comp/src/gb.highlight/.src/CCommandBetween.class @@ -21,6 +21,7 @@ Public Sub SetArgs(aArgs As String[]) Endif Else If aArgs.Count = 1 Then $sFrom = aArgs[0] + If $sFrom = "here" Then $sFrom = "" $sTo = "\n" Else Error.Raise("Syntax error") diff --git a/comp/src/gb.highlight/.src/CCommandFrom.class b/comp/src/gb.highlight/.src/CCommandFrom.class index a27e10759..a85a99ec8 100644 --- a/comp/src/gb.highlight/.src/CCommandFrom.class +++ b/comp/src/gb.highlight/.src/CCommandFrom.class @@ -21,6 +21,7 @@ Public Sub SetArgs(aArgs As String[]) Endif Else If aArgs.Count = 1 Then $sFrom = aArgs[0] + If $sFrom = "here" Then $sFrom = "" $sTo = "\n" Else Error.Raise("Syntax error") @@ -99,6 +100,15 @@ Public Sub Compile(hState As CState) hState.Continue() CState.Print("Endif") + + ' If $sMatch Then + ' If CState.IsRegExp($sMatch) Then + ' CState.Print("sWord = Match(" & Quote(CState.Pattern) & ")") + ' CState.Print("If Not sWord Then Goto " & hState.GetContinueLabel()) + ' Else + ' CState.IfStartWith(CState.Pattern, hState.GetContinueLabel()) + ' Endif + ' Endif hState.CompileChildren(hState.GetLabel() & "_LOOP", True) diff --git a/comp/src/gb.highlight/.src/Main.module b/comp/src/gb.highlight/.src/Main.module index be2bfc488..ce54325ce 100644 --- a/comp/src/gb.highlight/.src/Main.module +++ b/comp/src/gb.highlight/.src/Main.module @@ -22,13 +22,13 @@ Public Sub Main() 'File.Save("~/test.html", TextHighlighter["sh"].ToHTML(File.Load("~/gambas/git/master/acinclude.m4"))) 'Print TextHighlighter["javascript"].ToANSI(File.Load("~/asap/omogen/master/src/kernel/guygle.cgi/javascript/selectr.js")) - With TextHighlighter["javascript"] + With TextHighlighter["highlight"] Print .Colors.Join() 'Print .ToANSI(File.Load("~/asap/omogen/master/src/kernel/guygle.cgi/style/style-modern.css")) 'Print .ToANSI(File.Load("~/gambas/git/master/comp/src/gb.desktop/.src/Desktop.class")) 'Print .ToANSI(File.Load("~/asap/omogen/master/src/kernel/guygle.cgi/.src/Site/Dialog.webpage")) - 'Print .ToANSI(File.Load("highlight/c.highlight")) - Print .ToANSI("'ceci est une chaîne avec \\\'function(toto){}'") + File.Save("~/test.html", .ToHTML(File.Load("highlight/webpage.highlight"))) + 'Print .ToANSI("'ceci est une chaîne avec \\\'function(toto){}'") 'File.Save("~/test.html", .ToHTML(File.Load("~/asap/omogen/master/src/kernel/guygle.cgi/.src/Site/CloseMe.webpage"))) End With diff --git a/comp/src/gb.highlight/.src/TextHighlighter.class b/comp/src/gb.highlight/.src/TextHighlighter.class index b7cfd1695..08e9f7f5a 100644 --- a/comp/src/gb.highlight/.src/TextHighlighter.class +++ b/comp/src/gb.highlight/.src/TextHighlighter.class @@ -288,6 +288,7 @@ Static Private Sub CreateCustomHighlighter(sHighlight As String, sPath As String Dim aLines As String[] Dim aInclude As String[] Dim I As Integer + Dim sWordRegExp As String = "[A-Za-z_][A-Za-z0-9_]*" sDir = File.Dir(Temp$()) &/ "gb.highlight." & sHighlight @@ -335,6 +336,16 @@ Static Private Sub CreateCustomHighlighter(sHighlight As String, sPath As String Continue Endif + If sLine Begins "@word " Then + sWordRegExp = Trim(Mid$(sLine, 7)) + If sWordRegExp Begins "/" And If sWordRegExp Ends "/" Then + sWordRegExp = Mid$(sWordRegExp, 2, -1) + Else + sWordRegExp = "" + Endif + If Not sWordRegExp Then Error.Raise("Syntax error. Bad regular expression") + Endif + If sLine Begins "$(" Then sLine = Mid$(sLine, 3) iPos = InStr(sLine, ")=") @@ -408,6 +419,7 @@ Static Private Sub CreateCustomHighlighter(sHighlight As String, sPath As String CState.Print("aKeywords.ReadOnly = True") CState.Print("Return aKeywords") CState.Print("End") + CState.Print("Static $sWordRegExp As String = " & Quote(sWordRegExp)) ' CState.Print("Public Sub GetStateFromName(sName As String) As Integer") ' CState.Print() diff --git a/comp/src/gb.highlight/custom/CustomHighlighter.class b/comp/src/gb.highlight/custom/CustomHighlighter.class index a8478c4a4..37236c29c 100644 --- a/comp/src/gb.highlight/custom/CustomHighlighter.class +++ b/comp/src/gb.highlight/custom/CustomHighlighter.class @@ -7,7 +7,6 @@ Inherits TextHighlighter Property Read Keywords As String[] Static Private $hWordRegExp As RegExp -Static Private $sWordRegExp As String Static Private $cRegExp As New Collection Private $sText As String @@ -34,7 +33,6 @@ Private Sub Init(sText As String) $bTextAfter = False If Not $hWordRegExp Then - $sWordRegExp = "[A-Za-z_][A-Za-z0-9_]*" $hWordRegExp = New RegExp $hWordRegExp.Compile("^" & $sWordRegExp & "(.*)") Endif diff --git a/comp/src/gb.highlight/highlight/diff.highlight b/comp/src/gb.highlight/highlight/diff.highlight index f5231a5fd..c17d04e4e 100644 --- a/comp/src/gb.highlight/highlight/diff.highlight +++ b/comp/src/gb.highlight/highlight/diff.highlight @@ -10,8 +10,8 @@ file{File=Keyword}: position{Position=Datatype}: from /^@@/ to @@ added{Added}: - from /^\+/ to "\n" + from /^\+/ removed{Removed}: - from /^-/ to "\n" + from /^-/ normal: - from /./ to "\n" + from /./ diff --git a/comp/src/gb.highlight/highlight/highlight.highlight b/comp/src/gb.highlight/highlight/highlight.highlight index dc2cba526..f029b5b3d 100644 --- a/comp/src/gb.highlight/highlight/highlight.highlight +++ b/comp/src/gb.highlight/highlight/highlight.highlight @@ -1,13 +1,47 @@ +@word /\S+/ $(IDENT)=[A-Za-z][A-Za-z0-9.]* +comment: + from # class{Function}: match /$(IDENT)({$(IDENT)(=$(IDENT))?})?:/ subst{Preprocessor}: from /^\$\($(IDENT)\)=/ -command.from{Keyword}: - from "from" to "\n" - command.to{Keyword}: - match "to" - @include highlight.arg -command.match{Keyword}: - from "match" to "\n" - @include highlight.arg +include{Preprocessor}: + from "@include" +command{Keyword}: + match /(from|between|match)/ + args: + from here + keyword: + match /(to|and|here)/ + with{Preprocessor}: + from "with" + arg{Normal}: + word " "" / // /* /** + space{Normal}: + match /(/s|[^"/])/ + string: + match "\"" + string.contents{String}: + between here and /\s/ + string.subst{Preprocessor}: + match /\$\($(IDENT)\)/ + escape: + match /\\[fnrtv0'"\\]/ + match /\\x[0-9a-fA-F]{2}/ + regexp{Datatype}: + match "/" + regexp.contents{Datatype}: + between here and /\s/ + regexp.subst{Preprocessor}: + match /\$\($(IDENT)\)/ + regexp.escape{Escape}: + match /\\./ +word{Keyword}: + match /(word|keyword|symbol)/ + args{Normal}: + from here + include{Preprocessor}: + from @ +limit{Keyword}: + match "limit"