Repository: alecthomas/chroma
Pull request #812 (2023-08-15 14:46:25)

Prevent panic from hax lexer
Prevents `runtime error: slice bounds out of range` when list is empty.


Patch:
diff --git a/lexers/embedded/csharp.xml b/lexers/embedded/csharp.xml
index 8c2ff45..801a954 100644
--- a/lexers/embedded/csharp.xml
+++ b/lexers/embedded/csharp.xml
@@ -52,7 +52,7 @@
       <rule pattern="0[xX][0-9a-fA-F]+[Ll]?|\d[_\d]*(\.\d*)?([eE][+-]?\d+)?[flFLdD]?">
         <token type="LiteralNumber"/>
       </rule>
-      <rule pattern="#[ \t]*(if|endif|else|elif|define|undef|line|error|warning|region|endregion|pragma|nullable)\b[^\n\r]+">
+      <rule pattern="#[ \t]*(if|endif|else|elif|define|undef|line|error|warning|region|endregion|pragma|nullable)\b">
         <token type="CommentPreproc"/>
       </rule>
       <rule pattern="\b(extern)(\s+)(alias)\b">
@@ -62,7 +62,7 @@
           <token type="Keyword"/>
         </bygroups>
       </rule>
-      <rule pattern="(abstract|as|async|await|base|break|by|case|catch|checked|const|continue|default|delegate|do|else|enum|event|explicit|extern|false|finally|fixed|for|foreach|goto|if|implicit|in|init|internal|is|let|lock|new|null|on|operator|out|override|params|private|protected|public|readonly|ref|return|sealed|sizeof|stackalloc|static|switch|this|throw|true|try|typeof|unchecked|unsafe|virtual|void|while|get|set|new|partial|yield|add|remove|value|alias|ascending|descending|from|group|into|orderby|select|thenby|where|join|equals)\b">
+      <rule pattern="(as|await|base|break|by|case|catch|checked|continue|default|delegate|do|else|event|finally|fixed|for|foreach|goto|if|in|init|is|let|lock|new|on|out|params|readonly|ref|return|sizeof|stackalloc|switch|this|throw|try|typeof|unchecked|virtual|void|while|get|set|new|yield|add|remove|value|alias|ascending|descending|from|group|into|orderby|select|thenby|where|join|equals)\b">
         <token type="Keyword"/>
       </rule>
       <rule pattern="(global)(::)">
@@ -71,9 +71,15 @@
           <token type="Punctuation"/>
         </bygroups>
       </rule>
+      <rule pattern="(abstract|async|const|enum|explicit|extern|implicit|internal|operator|override|partial|private|protected|public|static|sealed|unsafe|volatile)\b">
+        <token type="KeywordDeclaration"/>
+      </rule>
       <rule pattern="(bool|byte|char|decimal|double|dynamic|float|int|long|object|sbyte|short|string|uint|ulong|ushort|var)\b\??">
         <token type="KeywordType"/>
       </rule>
+      <rule pattern="(true|false|null)\b">
+        <token type="KeywordConstant"/>
+      </rule>
       <rule pattern="(class|struct|record|interface)(\s+)">
         <bygroups>
           <token type="Keyword"/>
diff --git a/lexers/haxe.go b/lexers/haxe.go
index 6e23938..9a72de8 100644
--- a/lexers/haxe.go
+++ b/lexers/haxe.go
@@ -630,7 +630,9 @@ func haxePreProcMutator(state *LexerState) error {
 			state.Stack = stack[len(stack)-1]
 		}
 	case "end":
-		stack = stack[:len(stack)-1]
+		if len(stack) > 0 {
+			stack = stack[:len(stack)-1]
+		}
 	}
 
 	if proc == "if" || proc == "elseif" {


Test patch:
diff --git a/lexers/testdata/csharp/csharp_8_nullable.expected b/lexers/testdata/csharp/csharp_8_nullable.expected
index 61cfde3..448fb04 100644
--- a/lexers/testdata/csharp/csharp_8_nullable.expected
+++ b/lexers/testdata/csharp/csharp_8_nullable.expected
@@ -4,9 +4,11 @@
   {"type":"NameNamespace","value":"System"},
   {"type":"Punctuation","value":";"},
   {"type":"Text","value":"\n\n"},
-  {"type":"CommentPreproc","value":"#nullable enable"},
+  {"type":"CommentPreproc","value":"#nullable"},
+  {"type":"Text","value":" "},
+  {"type":"Name","value":"enable"},
   {"type":"Text","value":"\n\n"},
-  {"type":"Keyword","value":"public"},
+  {"type":"KeywordDeclaration","value":"public"},
   {"type":"Text","value":" "},
   {"type":"Keyword","value":"struct"},
   {"type":"Text","value":" "},
@@ -14,21 +16,21 @@
   {"type":"Text","value":"\n"},
   {"type":"Punctuation","value":"{"},
   {"type":"Text","value":"\n    "},
-  {"type":"Keyword","value":"public"},
+  {"type":"KeywordDeclaration","value":"public"},
   {"type":"Text","value":" "},
   {"type":"KeywordType","value":"string"},
   {"type":"Text","value":" "},
   {"type":"Name","value":"FirstName"},
   {"type":"Punctuation","value":";"},
   {"type":"Text","value":"\n    "},
-  {"type":"Keyword","value":"public"},
+  {"type":"KeywordDeclaration","value":"public"},
   {"type":"Text","value":" "},
   {"type":"KeywordType","value":"string?"},
   {"type":"Text","value":" "},
   {"type":"Name","value":"MiddleName"},
   {"type":"Punctuation","value":";"},
   {"type":"Text","value":"\n    "},
-  {"type":"Keyword","value":"public"},
+  {"type":"KeywordDeclaration","value":"public"},
   {"type":"Text","value":" "},
   {"type":"KeywordType","value":"string"},
   {"type":"Text","value":" "},
diff --git a/lexers/testdata/csharp/csharp_9_records_1.expected b/lexers/testdata/csharp/csharp_9_records_1.expected
index 4bf2446..8117222 100644
--- a/lexers/testdata/csharp/csharp_9_records_1.expected
+++ b/lexers/testdata/csharp/csharp_9_records_1.expected
@@ -1,5 +1,5 @@
 [
-  {"type":"Keyword","value":"public"},
+  {"type":"KeywordDeclaration","value":"public"},
   {"type":"Text","value":" "},
   {"type":"Keyword","value":"record"},
   {"type":"Text","value":" "},
diff --git a/lexers/testdata/csharp/csharp_9_records_2.expected b/lexers/testdata/csharp/csharp_9_records_2.expected
index 56ac690..37b4f38 100644
--- a/lexers/testdata/csharp/csharp_9_records_2.expected
+++ b/lexers/testdata/csharp/csharp_9_records_2.expected
@@ -1,5 +1,5 @@
 [
-  {"type":"Keyword","value":"public"},
+  {"type":"KeywordDeclaration","value":"public"},
   {"type":"Text","value":" "},
   {"type":"Keyword","value":"record"},
   {"type":"Text","value":" "},
@@ -7,7 +7,7 @@
   {"type":"Text","value":"\n"},
   {"type":"Punctuation","value":"{"},
   {"type":"Text","value":"\n    "},
-  {"type":"Keyword","value":"public"},
+  {"type":"KeywordDeclaration","value":"public"},
   {"type":"Text","value":" "},
   {"type":"KeywordType","value":"string"},
   {"type":"Text","value":" "},
@@ -28,7 +28,7 @@
   {"type":"Keyword","value":"default"},
   {"type":"Punctuation","value":"!;"},
   {"type":"Text","value":"\n    "},
-  {"type":"Keyword","value":"public"},
+  {"type":"KeywordDeclaration","value":"public"},
   {"type":"Text","value":" "},
   {"type":"KeywordType","value":"string"},
   {"type":"Text","value":" "},
diff --git a/lexers/testdata/csharp/csharp_interface.expected b/lexers/testdata/csharp/csharp_interface.expected
index dfd0f61..8ac9e10 100644
--- a/lexers/testdata/csharp/csharp_interface.expected
+++ b/lexers/testdata/csharp/csharp_interface.expected
@@ -1,5 +1,5 @@
 [
-  {"type":"Keyword","value":"public"},
+  {"type":"KeywordDeclaration","value":"public"},
   {"type":"Text","value":" "},
   {"type":"Keyword","value":"interface"},
   {"type":"Text","value":" "},