TextHighlighter: Fix compilation when there are several alternative commands for a same state.

[GB.HIGHLIGHT]
* BUG: TextHighlighter: Fix compilation when there are several alternative commands for a same state.
This commit is contained in:
Benoît Minisini 2023-11-09 11:22:26 +01:00
parent c81c73a3be
commit dbd5395941
8 changed files with 75 additions and 41 deletions

View file

@ -34,27 +34,26 @@ Public Sub Compile(hState As CState)
If Not $sFrom Then
Else If CState.IsRegExp(ByRef $sFrom) Then
Else If CState.IsRegExp($sFrom) Then
CState.Print("sWord = MatchBefore(" & Quote($sFrom) & ")")
CState.Print("sWord = MatchBefore(" & Quote(CState.Pattern) & ")")
CState.Print("If Not sWord Then Goto " & hState.GetNextLabel())
Else
CState.IfEndWith($sFrom, hState.GetNextLabel())
CState.IfEndWith(CState.Pattern, hState.GetNextLabel())
Endif
hState.PrintLabel(hState.GetLabel() & "_LOOP")
If CState.IsRegExp(ByRef $sTo) Then
If CState.IsRegExp($sTo) Then
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", True, " & Quote($sTo) & ", aState) Then Return")
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", True, " & Quote(CState.Pattern) & ", aState) Then Return")
Else
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", False, " & Quote($sTo) & ", aState) Then Return")
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", False, " & Quote(CState.Pattern) & ", aState) Then Return")
Endif
@ -64,24 +63,24 @@ Public Sub Compile(hState As CState)
If Not $sFrom Then
Else If CState.IsRegExp(ByRef $sFrom) Then
Else If CState.IsRegExp($sFrom) Then
CState.Print("sWord = MatchBefore(" & Quote($sFrom) & ")")
CState.Print("sWord = MatchBefore(" & Quote(CState.Pattern) & ")")
CState.Print("If Not sWord Then Goto " & hState.GetNextLabel())
Else
CState.IfEndWith($sFrom, hState.GetNextLabel())
CState.IfEndWith(CState.Pattern, hState.GetNextLabel())
Endif
hState.PrintLabel(hState.GetLabel() & "_LOOP")
If CState.IsRegExp(ByRef $sTo) Then
CState.Print("sWord = Match(" & Quote($sTo) & ")")
If CState.IsRegExp($sTo) Then
CState.Print("sWord = Match(" & Quote(CState.Pattern) & ")")
CState.Print("If sWord Then")
Else
CState.IfStartWith($sTo)
CState.IfStartWith(CState.Pattern)
Endif
hState.Continue()

View file

@ -34,35 +34,35 @@ Public Sub Compile(hState As CState)
If Not $sFrom Then
Else If CState.IsRegExp(ByRef $sFrom) Then
Else If CState.IsRegExp($sFrom) Then
CState.Print("sWord = Match(" & Quote($sFrom) & ")")
CState.Print("sWord = Match(" & Quote(CState.Pattern) & ")")
CState.Print("If Not sWord Then Goto " & hState.GetNextLabel())
CState.Print("SetIncludeHighlight(" & CStr($iInclude) & ")")
hState.Forward("String.Len(sWord)")
Else
CState.IfStartWith($sFrom, hState.GetNextLabel())
CState.IfStartWith(CState.Pattern, hState.GetNextLabel())
CState.Print("SetIncludeHighlight(" & CStr($iInclude) & ")")
hState.Forward(String.Len($sFrom))
hState.Forward(String.Len(CState.Pattern))
Endif
hState.PrintLabel(hState.GetLabel() & "_LOOP")
If CState.IsRegExp(ByRef $sTo) Then
If CState.IsRegExp($sTo) Then
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", True, " & Quote($sTo) & ", aState) Then Return")
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", True, " & Quote(CState.Pattern) & ", aState) Then Return")
CState.Print("SetIncludeHighlight(" & CStr($iInclude) & ")")
hState.Forward("String.Len(Match(" & Quote($sTo) & "))")
hState.Forward("String.Len(Match(" & Quote(CState.Pattern) & "))")
CState.Print("SetIncludeHighlight(Me._IncludeHighlight)")
Else
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", False, " & Quote($sTo) & ", aState) Then Return")
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", False, " & Quote(CState.Pattern) & ", aState) Then Return")
CState.Print("SetIncludeHighlight(" & CStr($iInclude) & ")")
hState.Forward(String.Len($sTo))
hState.Forward(String.Len(CState.Pattern))
CState.Print("SetIncludeHighlight(Me._IncludeHighlight)")
Endif
@ -73,28 +73,28 @@ Public Sub Compile(hState As CState)
If Not $sFrom Then
Else If CState.IsRegExp(ByRef $sFrom) Then
Else If CState.IsRegExp($sFrom) Then
CState.Print("sWord = Match(" & Quote($sFrom) & ")")
CState.Print("sWord = Match(" & Quote(CState.Pattern) & ")")
CState.Print("If Not sWord Then Goto " & hState.GetNextLabel())
hState.Forward("String.Len(sWord)")
Else
CState.IfStartWith($sFrom, hState.GetNextLabel())
hState.Forward(String.Len($sFrom))
CState.IfStartWith(CState.Pattern, hState.GetNextLabel())
hState.Forward(String.Len(CState.Pattern))
Endif
hState.PrintLabel(hState.GetLabel() & "_LOOP")
If CState.IsRegExp(ByRef $sTo) Then
CState.Print("sWord = Match(" & Quote($sTo) & ")")
If CState.IsRegExp($sTo) Then
CState.Print("sWord = Match(" & Quote(CState.Pattern) & ")")
CState.Print("If sWord Then")
hState.Forward("String.Len(sWord)")
Else
CState.IfStartWith($sTo)
hState.Forward(String.Len($sTo))
CState.IfStartWith(CState.Pattern)
hState.Forward(String.Len(CState.Pattern))
Endif
hState.Continue()

View file

@ -16,16 +16,16 @@ End
Public Sub Compile(hState As CState)
If CState.IsRegExp(ByRef $sFrom) Then
If CState.IsRegExp($sFrom) Then
CState.Print("sWord = Match(" & Quote($sFrom) & ")")
CState.Print("sWord = Match(" & Quote(CState.Pattern) & ")")
CState.Print("If Not sWord Then Goto " & hState.GetNextLabel())
hState.Forward("String.Len(sWord)")
Else
CState.IfStartWith($sFrom, hState.GetNextLabel())
hState.Forward(String.Len($sFrom))
CState.IfStartWith(CState.Pattern, hState.GetNextLabel())
hState.Forward(String.Len(CState.Pattern))
Endif

View file

@ -18,6 +18,8 @@ Static Private $cInclude As New Collection
Static Private $cSubst As New Collection
Static Public Pattern As String
Public Name As String
Public ColorIndex As Integer
Public Indent As Integer
@ -449,14 +451,16 @@ Static Public Sub AddInclude(sInclude As String) As Integer
End
Static Public Sub IsRegExp(ByRef sPattern As String) As Boolean
Static Public Sub IsRegExp(sPattern As String) As Boolean
If sPattern Begins "/" And If sPattern Ends "/" And If Len(sPattern) > 2 Then
sPattern = Mid$(sPattern, 2, -1)
Pattern = Mid$(sPattern, 2, -1)
Return True
Else If sPattern Begins "\"" And If sPattern Ends "\"" Then
sPattern = UnQuote(sPattern)
Pattern = UnQuote(sPattern)
Return False
Else
Pattern = sPattern
Endif
End

View file

@ -22,13 +22,14 @@ 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["webpage"]
With TextHighlighter["javascript"]
Print .Colors.Join()
.CanRewrite = True
'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"))
File.Save("~/test.html", .ToHTML(File.Load("~/asap/omogen/master/src/kernel/guygle.cgi/.src/Site/CloseMe.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("~/asap/omogen/master/src/kernel/guygle.cgi/.src/Site/CloseMe.webpage")))
End With
End

View file

@ -0,0 +1,17 @@
arg.subst{Preprocessor}:
match /\$\($(IDENT)\)
ident{Normal}:
match .*?
string:
from " to "
string.subst{Preprocessor}:
match /\$\($(IDENT)\)
escape:
match /\\[fnrtv0'"\\]/
match /\\x[0-9a-fA-F]{2}/
regexp{RegExp=Datatype}:
match //\S//
regexp.subst{Preprocessor}:
match /\$\($(IDENT)\)
regexp.escape{Escape}:
match /\\./

View file

@ -0,0 +1,13 @@
$(IDENT)=[A-Za-z][A-Za-z0-9.]*
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

View file

@ -23,7 +23,7 @@ string.subst{String}:
subst{Escape}:
from ${ to } with javascript
regexp{RegExp=Datatype}:
match //.*?/[a-z]*/
match //.*?(/[a-z]*)?/
regexp.escape{Escape}:
match /\\./
number: