forked from sebastienros/esprima-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnaryExpression.cs
More file actions
77 lines (69 loc) · 2.59 KB
/
Copy pathUnaryExpression.cs
File metadata and controls
77 lines (69 loc) · 2.59 KB
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
using Esprima.Utils;
using static Esprima.EsprimaExceptionHelper;
namespace Esprima.Ast
{
public enum UnaryOperator
{
[EnumMember(Value = "+")] Plus,
[EnumMember(Value = "-")] Minus,
[EnumMember(Value = "~")] BitwiseNot,
[EnumMember(Value = "!")] LogicalNot,
[EnumMember(Value = "delete")] Delete,
[EnumMember(Value = "void")] Void,
[EnumMember(Value = "typeof")] TypeOf,
[EnumMember(Value = "++")] Increment,
[EnumMember(Value = "--")] Decrement
}
public class UnaryExpression : Expression
{
public readonly UnaryOperator Operator;
public readonly Expression Argument;
public bool Prefix { get; protected set; }
public UnaryExpression(string? op, Expression arg) : this(Nodes.UnaryExpression, op, arg)
{
}
protected UnaryExpression(Nodes type, string? op, Expression arg) : base(type)
{
Operator = ParseUnaryOperator(op);
Argument = arg;
Prefix = true;
}
public static UnaryOperator ParseUnaryOperator(string? op)
{
return op switch
{
"+" => UnaryOperator.Plus,
"-" => UnaryOperator.Minus,
"++" => UnaryOperator.Increment,
"--" => UnaryOperator.Decrement,
"~" => UnaryOperator.BitwiseNot,
"!" => UnaryOperator.LogicalNot,
"delete" => UnaryOperator.Delete,
"void" => UnaryOperator.Void,
"typeof" => UnaryOperator.TypeOf,
_ => ThrowArgumentOutOfRangeException<UnaryOperator>(nameof(op), "Invalid unary operator: " + op)
};
}
public static string ConvertUnaryOperator(UnaryOperator op)
{
return op switch
{
UnaryOperator.Plus => "+",
UnaryOperator.Minus => "-",
UnaryOperator.Increment => "++",
UnaryOperator.Decrement => "--",
UnaryOperator.BitwiseNot => "~",
UnaryOperator.LogicalNot => "!",
UnaryOperator.Delete => "delete",
UnaryOperator.Void => "void",
UnaryOperator.TypeOf => "typeof",
_ => ThrowArgumentOutOfRangeException<string>(nameof(op), "Invalid unary operator: " + op)
};
}
public override NodeCollection ChildNodes => new(Argument);
protected internal override void Accept(AstVisitor visitor)
{
visitor.VisitUnaryExpression(this);
}
}
}