File size: 21,613 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/********************************************************************++
Project: PowerShell
Contents: PowerShell token interface for syntax editors
Classes: System.Management.Automation.PSToken
--********************************************************************/
using System.Management.Automation.Language;
using Dbg = System.Management.Automation.Diagnostics;
namespace System.Management.Automation
{
/// <summary>
/// This is public class for representing a powershell token.
/// </summary>
/// <remarks>
/// There is already an internal class Token for representing the token.
///
/// This class wraps the internal Token class for providing limited information
/// to syntax editor.
/// </remarks>
public sealed class PSToken
{
internal PSToken(Token token)
{
Type = GetPSTokenType(token);
_extent = token.Extent;
if (token is StringToken)
{
_content = ((StringToken)token).Value;
}
else if (token is VariableToken)
{
_content = ((VariableToken)token).VariablePath.ToString();
}
}
internal PSToken(IScriptExtent extent)
{
Type = PSTokenType.Position;
_extent = extent;
}
/// <summary>
/// Resulting text for the token.
/// </summary>
/// <remarks>
/// The text here represents the content of token. It can be the same as
/// the text chunk within script resulting into this token, but usually is not
/// the case.
///
/// For example, -name in following command result into a parameter token.
///
/// get-process -name foo
///
/// Text property in this case is 'name' instead of '-name'.
/// </remarks>
public string Content
{
get
{
return _content ?? _extent.Text;
}
}
private readonly string _content;
#region Token Type
/// <summary>
/// Map a V3 token to a V2 PSTokenType.
/// </summary>
/// <param name="token">The V3 token.</param>
/// <returns>The V2 PSTokenType.</returns>
public static PSTokenType GetPSTokenType(Token token)
{
if ((token.TokenFlags & TokenFlags.CommandName) != 0)
{
return PSTokenType.Command;
}
if ((token.TokenFlags & TokenFlags.MemberName) != 0)
{
return PSTokenType.Member;
}
if ((token.TokenFlags & TokenFlags.AttributeName) != 0)
{
return PSTokenType.Attribute;
}
if ((token.TokenFlags & TokenFlags.TypeName) != 0)
{
return PSTokenType.Type;
}
return s_tokenKindMapping[(int)token.Kind];
}
/// <summary>
/// Token type.
/// </summary>
public PSTokenType Type { get; }
private static readonly PSTokenType[] s_tokenKindMapping = new PSTokenType[]
{
#region Flags for unclassified tokens
/* Unknown */ PSTokenType.Unknown,
/* Variable */ PSTokenType.Variable,
/* SplattedVariable */ PSTokenType.Variable,
/* Parameter */ PSTokenType.CommandParameter,
/* Number */ PSTokenType.Number,
/* Label */ PSTokenType.LoopLabel,
/* Identifier */ PSTokenType.CommandArgument,
/* Generic */ PSTokenType.CommandArgument,
/* Newline */ PSTokenType.NewLine,
/* LineContinuation */ PSTokenType.LineContinuation,
/* Comment */ PSTokenType.Comment,
/* EndOfInput */ PSTokenType.Unknown,
#endregion Flags for unclassified tokens
#region Flags for strings
/* StringLiteral */ PSTokenType.String,
/* StringExpandable */ PSTokenType.String,
/* HereStringLiteral */ PSTokenType.String,
/* HereStringExpandable */ PSTokenType.String,
#endregion Flags for strings
#region Flags for punctuators
/* LParen */ PSTokenType.GroupStart,
/* RParen */ PSTokenType.GroupEnd,
/* LCurly */ PSTokenType.GroupStart,
/* RCurly */ PSTokenType.GroupEnd,
/* LBracket */ PSTokenType.Operator,
/* RBracket */ PSTokenType.Operator,
/* AtParen */ PSTokenType.GroupStart,
/* AtCurly */ PSTokenType.GroupStart,
/* DollarParen */ PSTokenType.GroupStart,
/* Semi */ PSTokenType.StatementSeparator,
#endregion Flags for punctuators
#region Flags for operators
/* AndAnd */ PSTokenType.Operator,
/* OrOr */ PSTokenType.Operator,
/* Ampersand */ PSTokenType.Operator,
/* Pipe */ PSTokenType.Operator,
/* Comma */ PSTokenType.Operator,
/* MinusMinus */ PSTokenType.Operator,
/* PlusPlus */ PSTokenType.Operator,
/* DotDot */ PSTokenType.Operator,
/* ColonColon */ PSTokenType.Operator,
/* Dot */ PSTokenType.Operator,
/* Exclaim */ PSTokenType.Operator,
/* Multiply */ PSTokenType.Operator,
/* Divide */ PSTokenType.Operator,
/* Rem */ PSTokenType.Operator,
/* Plus */ PSTokenType.Operator,
/* Minus */ PSTokenType.Operator,
/* Equals */ PSTokenType.Operator,
/* PlusEquals */ PSTokenType.Operator,
/* MinusEquals */ PSTokenType.Operator,
/* MultiplyEquals */ PSTokenType.Operator,
/* DivideEquals */ PSTokenType.Operator,
/* RemainderEquals */ PSTokenType.Operator,
/* Redirection */ PSTokenType.Operator,
/* RedirectInStd */ PSTokenType.Operator,
/* Format */ PSTokenType.Operator,
/* Not */ PSTokenType.Operator,
/* Bnot */ PSTokenType.Operator,
/* And */ PSTokenType.Operator,
/* Or */ PSTokenType.Operator,
/* Xor */ PSTokenType.Operator,
/* Band */ PSTokenType.Operator,
/* Bor */ PSTokenType.Operator,
/* Bxor */ PSTokenType.Operator,
/* Join */ PSTokenType.Operator,
/* Ieq */ PSTokenType.Operator,
/* Ine */ PSTokenType.Operator,
/* Ige */ PSTokenType.Operator,
/* Igt */ PSTokenType.Operator,
/* Ilt */ PSTokenType.Operator,
/* Ile */ PSTokenType.Operator,
/* Ilike */ PSTokenType.Operator,
/* Inotlike */ PSTokenType.Operator,
/* Imatch */ PSTokenType.Operator,
/* Inotmatch */ PSTokenType.Operator,
/* Ireplace */ PSTokenType.Operator,
/* Icontains */ PSTokenType.Operator,
/* Inotcontains */ PSTokenType.Operator,
/* Iin */ PSTokenType.Operator,
/* Inotin */ PSTokenType.Operator,
/* Isplit */ PSTokenType.Operator,
/* Ceq */ PSTokenType.Operator,
/* Cne */ PSTokenType.Operator,
/* Cge */ PSTokenType.Operator,
/* Cgt */ PSTokenType.Operator,
/* Clt */ PSTokenType.Operator,
/* Cle */ PSTokenType.Operator,
/* Clike */ PSTokenType.Operator,
/* Cnotlike */ PSTokenType.Operator,
/* Cmatch */ PSTokenType.Operator,
/* Cnotmatch */ PSTokenType.Operator,
/* Creplace */ PSTokenType.Operator,
/* Ccontains */ PSTokenType.Operator,
/* Cnotcontains */ PSTokenType.Operator,
/* Cin */ PSTokenType.Operator,
/* Cnotin */ PSTokenType.Operator,
/* Csplit */ PSTokenType.Operator,
/* Is */ PSTokenType.Operator,
/* IsNot */ PSTokenType.Operator,
/* As */ PSTokenType.Operator,
/* PostFixPlusPlus */ PSTokenType.Operator,
/* PostFixMinusMinus */ PSTokenType.Operator,
/* Shl */ PSTokenType.Operator,
/* Shr */ PSTokenType.Operator,
/* Reserved slot 1 */ PSTokenType.Unknown,
/* Reserved slot 2 */ PSTokenType.Unknown,
/* Reserved slot 3 */ PSTokenType.Unknown,
/* Reserved slot 4 */ PSTokenType.Unknown,
/* Reserved slot 5 */ PSTokenType.Unknown,
/* Reserved slot 6 */ PSTokenType.Unknown,
/* Reserved slot 7 */ PSTokenType.Unknown,
/* Reserved slot 8 */ PSTokenType.Unknown,
/* Reserved slot 9 */ PSTokenType.Unknown,
/* Reserved slot 10 */ PSTokenType.Unknown,
/* Reserved slot 11 */ PSTokenType.Unknown,
/* Reserved slot 12 */ PSTokenType.Unknown,
/* Reserved slot 13 */ PSTokenType.Unknown,
/* Reserved slot 14 */ PSTokenType.Unknown,
/* Reserved slot 15 */ PSTokenType.Unknown,
/* Reserved slot 16 */ PSTokenType.Unknown,
/* Reserved slot 17 */ PSTokenType.Unknown,
/* Reserved slot 18 */ PSTokenType.Unknown,
/* Reserved slot 19 */ PSTokenType.Unknown,
/* Reserved slot 20 */ PSTokenType.Unknown,
#endregion Flags for operators
#region Flags for keywords
/* Begin */ PSTokenType.Keyword,
/* Break */ PSTokenType.Keyword,
/* Catch */ PSTokenType.Keyword,
/* Class */ PSTokenType.Keyword,
/* Continue */ PSTokenType.Keyword,
/* Data */ PSTokenType.Keyword,
/* Define */ PSTokenType.Keyword,
/* Do */ PSTokenType.Keyword,
/* Dynamicparam */ PSTokenType.Keyword,
/* Else */ PSTokenType.Keyword,
/* ElseIf */ PSTokenType.Keyword,
/* End */ PSTokenType.Keyword,
/* Exit */ PSTokenType.Keyword,
/* Filter */ PSTokenType.Keyword,
/* Finally */ PSTokenType.Keyword,
/* For */ PSTokenType.Keyword,
/* Foreach */ PSTokenType.Keyword,
/* From */ PSTokenType.Keyword,
/* Function */ PSTokenType.Keyword,
/* If */ PSTokenType.Keyword,
/* In */ PSTokenType.Keyword,
/* Param */ PSTokenType.Keyword,
/* Process */ PSTokenType.Keyword,
/* Return */ PSTokenType.Keyword,
/* Switch */ PSTokenType.Keyword,
/* Throw */ PSTokenType.Keyword,
/* Trap */ PSTokenType.Keyword,
/* Try */ PSTokenType.Keyword,
/* Until */ PSTokenType.Keyword,
/* Using */ PSTokenType.Keyword,
/* Var */ PSTokenType.Keyword,
/* While */ PSTokenType.Keyword,
/* Workflow */ PSTokenType.Keyword,
/* Parallel */ PSTokenType.Keyword,
/* Sequence */ PSTokenType.Keyword,
/* InlineScript */ PSTokenType.Keyword,
/* Configuration */ PSTokenType.Keyword,
/* DynamicKeyword */ PSTokenType.Keyword,
/* Public */ PSTokenType.Keyword,
/* Private */ PSTokenType.Keyword,
/* Static */ PSTokenType.Keyword,
/* Interface */ PSTokenType.Keyword,
/* Enum */ PSTokenType.Keyword,
/* Namespace */ PSTokenType.Keyword,
/* Module */ PSTokenType.Keyword,
/* Type */ PSTokenType.Keyword,
/* Assembly */ PSTokenType.Keyword,
/* Command */ PSTokenType.Keyword,
/* Hidden */ PSTokenType.Keyword,
/* Base */ PSTokenType.Keyword,
/* Default */ PSTokenType.Keyword,
#endregion Flags for keywords
/* LastToken */ PSTokenType.Unknown,
};
#endregion
#region Position Information
private readonly IScriptExtent _extent;
/// <summary>
/// Offset of token start in script buffer.
/// </summary>
public int Start
{
get { return _extent.StartOffset; }
}
/// <summary>
/// Offset of token end in script buffer.
/// </summary>
public int Length
{
get
{
return _extent.EndOffset - _extent.StartOffset;
}
}
/// <summary>
/// Line number of token start.
/// </summary>
/// <remarks>
/// StartLine, StartColumn, EndLine, and EndColumn are 1-based,
/// i.e., first line has a line number 1 and first character in
/// a line has column number 1.
/// </remarks>
public int StartLine { get { return _extent.StartLineNumber; } }
/// <summary>
/// Position of token start in start line.
/// </summary>
public int StartColumn { get { return _extent.StartColumnNumber; } }
/// <summary>
/// Line number of token end.
/// </summary>
public int EndLine { get { return _extent.EndLineNumber; } }
/// <summary>
/// Position of token end in end line.
/// </summary>
public int EndColumn { get { return _extent.EndColumnNumber; } }
#endregion
}
/// <summary>
/// PowerShell token types.
/// </summary>
public enum PSTokenType
{
/// <summary>
/// Unknown token.
/// </summary>
Unknown,
/// <summary>
/// <para>
/// Command.
/// </para>
/// </para>
/// For example, 'get-process' in
///
/// <c><code>get-process -name foo</code></c>
/// </para>
/// </summary>
Command,
/// <summary>
/// <para>
/// Command Parameter.
/// </para>
/// <para>
/// For example, '-name' in
///
/// <c><code>get-process -name foo</code></c>
/// </para>
/// </summary>
CommandParameter,
/// <summary>
/// <para>
/// Command Argument.
/// </para>
/// <para>
/// For example, 'foo' in
///
/// <c><code>get-process -name foo</code></c>
/// </para>
/// </summary>
CommandArgument,
/// <summary>
/// <para>
/// Number.
/// </para>
/// <para>
/// For example, 12 in
///
/// <c><code>$a=12</code></c>
/// </para>
/// </summary>
Number,
/// <summary>
/// <para>
/// String.
/// </para>
/// <para>
/// For example, "12" in
///
/// <c><code>$a="12"</code></c>
/// </para>
/// </summary>
String,
/// <summary>
/// <para>
/// Variable.
/// </para>
/// <para>
/// <remarks>
/// For example, $a in
///
/// <c><code>$a="12"</code></c>
/// <para>
/// </summary>
Variable,
/// <summary>
/// <para>
/// Property name or method name.
/// </para>
/// <para>
/// For example, Name in
///
/// <c><code>$a.Name</code></c>
/// </para>
/// </summary>
Member,
/// <summary>
/// <para>
/// Loop label.
/// </para>
/// <para>
/// For example, :loop in
///
/// <c><code>
/// :loop
/// foreach($a in $b)
/// {
/// $a
/// }
/// </code></c>
/// </summary>
LoopLabel,
/// <summary>
/// <para>
/// Attributes.
/// </para>
/// <para>
/// For example, Mandatory in
///
/// <c><code>param([Mandatory] $a)</code></c>
/// </para>
/// </summary>
Attribute,
/// <summary>
/// <para>
/// Types.
/// </para>
/// <para>
/// For example, [string] in
///
/// <c><code>$a = [string] 12</code></c>
/// </para>
/// </summary>
Type,
/// <summary>
/// <para>
/// Operators.
/// </para>
/// <para>
/// For example, + in
///
/// <c><code>$a = 1 + 2</code></c>
/// </para>
/// </summary>
Operator,
/// <summary>
/// <para>
/// Group Starter.
/// </para>
/// <para>
/// For example, { in
///
/// <c><code>
/// if ($a -gt 4)
/// {
/// $a++;
/// }
/// </code></c>
/// </para>
/// </summary>
GroupStart,
/// <summary>
/// <para>
/// Group Ender.
/// </para>
/// <para>
/// For example, } in
///
/// <c><code>
/// if ($a -gt 4)
/// {
/// $a++;
/// }
/// </code></c>
/// </para>
/// </summary>
GroupEnd,
/// <summary>
/// <para>
/// Keyword.
/// </para>
/// <para>
/// For example, if in
///
/// <c><code>
/// if ($a -gt 4)
/// {
/// $a++;
/// }
/// </code></c>
/// </para>
/// </summary>
Keyword,
/// <summary>
/// <para>
/// Comment.
/// </para>
/// <para>
/// For example, #here in
///
/// <c><code>
/// #here
/// if ($a -gt 4)
/// {
/// $a++;
/// }
/// </code></c>
/// </para>
/// </summary>
Comment,
/// <summary>
/// <para>
/// Statement separator. This is ';'
/// </para>
/// <para>
/// For example, ; in
///
/// <c><code>
/// #here
/// if ($a -gt 4)
/// {
/// $a++;
/// }
/// </code></c>
/// </para>
/// </summary>
StatementSeparator,
/// <summary>
/// <para>
/// New line. This is '\n'
/// </para>
/// <para>
/// For example, \n in
///
/// <c><code>
/// #here
/// if ($a -gt 4)
/// {
/// $a++;
/// }
/// </code></c>
/// </para>
/// </summary>
NewLine,
/// <summary>
/// <para>
/// Line continuation.
/// </para>
/// <para>
/// For example, ` in
///
/// <c><code>
/// get-command -name `
/// foo
/// </code></c>
/// </para>
/// </summary>
LineContinuation,
/// <summary>
/// <para>
/// Position token.
/// </para>
/// <para>
/// Position tokens are bogus tokens generated for identifying a location
/// in the script.
/// </para>
/// </summary>
Position
}
}
|