|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using ColorCode.Common; |
| 5 | + |
| 6 | +namespace ColorCode.VT; |
| 7 | + |
| 8 | +public class PowerShell : ILanguage |
| 9 | +{ |
| 10 | + public string Id |
| 11 | + { |
| 12 | + get { return LanguageId.PowerShell; } |
| 13 | + } |
| 14 | + |
| 15 | + public string Name |
| 16 | + { |
| 17 | + get { return "PowerShell"; } |
| 18 | + } |
| 19 | + |
| 20 | + public string CssClassName |
| 21 | + { |
| 22 | + get { return "powershell"; } |
| 23 | + } |
| 24 | + |
| 25 | + public string FirstLinePattern |
| 26 | + { |
| 27 | + get { return null; } |
| 28 | + } |
| 29 | + |
| 30 | + public IList<LanguageRule> Rules |
| 31 | + { |
| 32 | + get |
| 33 | + { |
| 34 | + return new List<LanguageRule> |
| 35 | + { |
| 36 | + new LanguageRule( |
| 37 | + @"(?s)(<#.*?#>)", |
| 38 | + new Dictionary<int, string> |
| 39 | + { |
| 40 | + {1, ScopeName.Comment} |
| 41 | + }), |
| 42 | + |
| 43 | + new LanguageRule( |
| 44 | + @"(#.*?)\r?$", |
| 45 | + new Dictionary<int, string> |
| 46 | + { |
| 47 | + {1, ScopeName.Comment} |
| 48 | + }), |
| 49 | + |
| 50 | + new LanguageRule( |
| 51 | + @"'[^\n]*?(?<!\\)'", |
| 52 | + new Dictionary<int, string> |
| 53 | + { |
| 54 | + {0, ScopeName.String} |
| 55 | + }), |
| 56 | + |
| 57 | + new LanguageRule( |
| 58 | + @"(?s)@"".*?""@", |
| 59 | + new Dictionary<int, string> |
| 60 | + { |
| 61 | + {0, ScopeName.StringCSharpVerbatim} |
| 62 | + }), |
| 63 | + |
| 64 | + new LanguageRule( |
| 65 | + @"(?s)(""[^\n]*?(?<!`)"")", |
| 66 | + new Dictionary<int, string> |
| 67 | + { |
| 68 | + {0, ScopeName.String} |
| 69 | + }), |
| 70 | + |
| 71 | + new LanguageRule( |
| 72 | + @"\$(?:[\d\w\-]+(?::[\d\w\-]+)?|\$|\?|\^)", |
| 73 | + new Dictionary<int, string> |
| 74 | + { |
| 75 | + {0, ScopeName.PowerShellVariable} |
| 76 | + }), |
| 77 | + |
| 78 | + new LanguageRule( |
| 79 | + @"\${[^}]+}", |
| 80 | + new Dictionary<int, string> |
| 81 | + { |
| 82 | + {0, ScopeName.PowerShellVariable} |
| 83 | + }), |
| 84 | + |
| 85 | + new LanguageRule( |
| 86 | + @"(?i)\b(begin|break|catch|continue|data|do|dynamicparam|elseif|else|end|exit|filter|finally|foreach|for|from|function|if|in|param|process|return|switch|throw|trap|try|until|while)\b", |
| 87 | + new Dictionary<int, string> |
| 88 | + { |
| 89 | + {1, ScopeName.Keyword} |
| 90 | + }), |
| 91 | + |
| 92 | + // We use positive lookbehind assertion to indicate what should immediately precedes a command name. |
| 93 | + // By using the positive lookbehind assertion, the operators like '|' and '=' can still be matched |
| 94 | + // by their respective rules. |
| 95 | + new LanguageRule( |
| 96 | + @"(?<=(?:^|[\(\|=])\s*)(?:\w+-)*\w+", |
| 97 | + new Dictionary<int, string> |
| 98 | + { |
| 99 | + {0, ScopeName.PowerShellCommand} |
| 100 | + }), |
| 101 | + |
| 102 | + new LanguageRule( |
| 103 | + @"-(?:c|i)?(?:eq|ne|gt|ge|lt|le|notlike|like|notmatch|match|notcontains|contains|replace)", |
| 104 | + new Dictionary<int, string> |
| 105 | + { |
| 106 | + {0, ScopeName.PowerShellOperator} |
| 107 | + }), |
| 108 | + |
| 109 | + new LanguageRule( |
| 110 | + @"-(?:band|and|as|join|not|bxor|xor|bor|or|isnot|is|split)", |
| 111 | + new Dictionary<int, string> |
| 112 | + { |
| 113 | + {0, ScopeName.PowerShellOperator} |
| 114 | + }), |
| 115 | + |
| 116 | + // Match parameters like '-word'. Note that we require a preceding whitespace. |
| 117 | + new LanguageRule( |
| 118 | + @"\s(-\w+)", |
| 119 | + new Dictionary<int, string> |
| 120 | + { |
| 121 | + {1, ScopeName.PowerShellParameter} |
| 122 | + }), |
| 123 | + |
| 124 | + // match options like '--word', '--word-word', and '--word-word-word', but not '--word-' or '--word-word-'. |
| 125 | + // Also match potential value for the option that is specified in the form of '--word=value', but we don't |
| 126 | + // capture the value part because it should be rendered as plain text, and our real purpose is to not let |
| 127 | + // the value part to be matched by other rules. |
| 128 | + new LanguageRule( |
| 129 | + @"\s(--(?:\w+-)*\w+)(?:=(?:\w+-)*\w+)?", |
| 130 | + new Dictionary<int, string> |
| 131 | + { |
| 132 | + {1, ScopeName.PowerShellParameter} |
| 133 | + }), |
| 134 | + |
| 135 | + new LanguageRule( |
| 136 | + @"(?:\+=|-=|\*=|/=|%=|=|\+\+|--|\+|-|\*|/|%|\||,)", |
| 137 | + new Dictionary<int, string> |
| 138 | + { |
| 139 | + {0, ScopeName.PowerShellOperator} |
| 140 | + }), |
| 141 | + |
| 142 | + new LanguageRule( |
| 143 | + @"(?:\>\>|2\>&1|\>|2\>\>|2\>)", |
| 144 | + new Dictionary<int, string> |
| 145 | + { |
| 146 | + {0, ScopeName.PowerShellOperator} |
| 147 | + }), |
| 148 | + |
| 149 | + new LanguageRule( |
| 150 | + @"(?is)\[(cmdletbinding|alias|outputtype|parameter|validatenotnull|validatenotnullorempty|validatecount|validateset|allownull|allowemptycollection|allowemptystring|validatescript|validaterange|validatepattern|validatelength|supportswildcards)[^\]]+\]", |
| 151 | + new Dictionary<int, string> |
| 152 | + { |
| 153 | + {1, ScopeName.PowerShellAttribute} |
| 154 | + }), |
| 155 | + |
| 156 | + new LanguageRule( |
| 157 | + @"(\[)([^\]]+)(\])(::)?", |
| 158 | + new Dictionary<int, string> |
| 159 | + { |
| 160 | + {1, ScopeName.PowerShellOperator}, |
| 161 | + {2, ScopeName.PowerShellType}, |
| 162 | + {3, ScopeName.PowerShellOperator}, |
| 163 | + {4, ScopeName.PowerShellOperator} |
| 164 | + }) |
| 165 | + }; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public bool HasAlias(string lang) |
| 170 | + { |
| 171 | + switch (lang.ToLower()) |
| 172 | + { |
| 173 | + case "posh": |
| 174 | + case "ps1": |
| 175 | + case "pwsh": |
| 176 | + return true; |
| 177 | + |
| 178 | + default: |
| 179 | + return false; |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments