Skip to content

Commit 109832a

Browse files
committed
#16 - implement 'with' expression parsing and AST node
1 parent fae165b commit 109832a

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.ComplexLiterals;
2+
3+
namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions;
4+
5+
[AutoVisitable<IAbstractSyntaxTreeNode>]
6+
public partial class WithExpression : Expression
7+
{
8+
protected override IReadOnlyList<IAbstractSyntaxTreeNode> Children { get; }
9+
10+
public Expression Expression { get; }
11+
public ObjectLiteral ObjectLiteral { get; }
12+
13+
public WithExpression(Expression expression, ObjectLiteral objectLiteral)
14+
{
15+
Expression = expression;
16+
Expression.Parent = this;
17+
18+
ObjectLiteral = objectLiteral;
19+
ObjectLiteral.Parent = this;
20+
21+
Children = [Expression, ObjectLiteral];
22+
}
23+
24+
protected override string NodeRepresentation() => "with";
25+
}

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/TopDownParser.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,16 +541,32 @@ private Expression MemberExpression()
541541
}
542542

543543
/// <summary>
544-
/// CastExpression -> ConditionalExpression 'as' 'string'
544+
/// CastExpression -> WithExpression 'as' 'string'
545545
/// </summary>
546546
private Expression CastExpression()
547547
{
548-
var cond = ConditionalExpression();
548+
var withExpr = WithExpression();
549549
if (CurrentIsKeyword("as"))
550550
{
551551
var asKeyword = Expect("Keyword", "as");
552552
var type = TypeValue();
553-
return new CastAsExpression(cond, type) {Segment = asKeyword.Segment};
553+
return new CastAsExpression(withExpr, type) {Segment = asKeyword.Segment};
554+
}
555+
556+
return withExpr;
557+
}
558+
559+
/// <summary>
560+
/// WithExpression -> ConditionalExpression 'with' ObjectLiteral
561+
/// </summary>
562+
private Expression WithExpression()
563+
{
564+
var cond = ConditionalExpression();
565+
if (CurrentIsKeyword("with"))
566+
{
567+
var withKeyword = Expect("Keyword", "with");
568+
var objectLiteral = ObjectLiteral();
569+
return new WithExpression(cond, objectLiteral) {Segment = withKeyword.Segment};
554570
}
555571

556572
return cond;

0 commit comments

Comments
 (0)