From f69bc6cdb10aab2d1b202668cb7ecbcc0ddf33f2 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 7 Jul 2017 14:30:26 -0700 Subject: Fully parse function bodies, even in "rewriter" mode This is in anticipation of needing to have more complete knowledge to be able to handle user code that `import`s library functionality. The big picture of this change is just to remove the `UnparsedStmt` class that was used to hold the bodies of user functions as opaque token streams, and thus to let the full parser and compiler loose on that code. That is the easy part, of course, and the hard part is all the fixes that this requires in the rest of the compielr to make this even remotely work. Subsequent commit address a lot of other issues, so this particular commit mostly represents work-in-progress. One detail is that this change puts a conditional around nearly every diagnostic message in `check.cpp` to suppress thing when in rewriter mode. I have yet to check how that works out if there are errors in anything we actually need to understand for the purposes of generating reflection data. --- source/slang/parser.cpp | 185 +++++++++++------------------------------------- 1 file changed, 43 insertions(+), 142 deletions(-) (limited to 'source/slang/parser.cpp') diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index b134f9645..2056bf809 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -1392,6 +1392,28 @@ namespace Slang return genericApp; } + // Parse option `[]` braces after a type expression, that indicate an array type + static RefPtr parsePostfixTypeSuffix( + Parser* parser, + RefPtr inTypeExpr) + { + auto typeExpr = inTypeExpr; + while (parser->LookAheadToken(TokenType::LBracket)) + { + RefPtr arrType = new IndexExpressionSyntaxNode(); + arrType->Position = typeExpr->Position; + arrType->BaseExpression = typeExpr; + parser->ReadToken(TokenType::LBracket); + if (!parser->LookAheadToken(TokenType::RBracket)) + { + arrType->IndexExpression = parser->ParseExpression(); + } + parser->ReadToken(TokenType::RBracket); + typeExpr = arrType; + } + return typeExpr; + } + static TypeSpec parseTypeSpec(Parser* parser) { @@ -1431,11 +1453,16 @@ namespace Slang typeExpr = parseGenericApp(parser, typeExpr); } + // GLSL allows `[]` directly in a type specifier + if (parser->translationUnit->sourceLanguage == SourceLanguage::GLSL) + { + typeExpr = parsePostfixTypeSuffix(parser, typeExpr); + } + typeSpec.expr = typeExpr; return typeSpec; } - static RefPtr ParseDeclaratorDecl( Parser* parser, ContainerDecl* containerDecl) @@ -2572,50 +2599,6 @@ namespace Slang RefPtr Parser::ParseBlockStatement() { - if( translationUnit->compileFlags & SLANG_COMPILE_FLAG_NO_CHECKING ) - { - // We have been asked to parse the input, but not attempt to understand it. - - // TODO: record start/end locations... - - List tokens; - - ReadToken(TokenType::LBrace); - - int depth = 1; - for( ;;) - { - switch( tokenReader.PeekTokenType() ) - { - case TokenType::EndOfFile: - goto done; - - case TokenType::RBrace: - depth--; - if(depth == 0) - goto done; - break; - - case TokenType::LBrace: - depth++; - break; - - default: - break; - } - - auto token = tokenReader.AdvanceToken(); - tokens.Add(token); - } - done: - ReadToken(TokenType::RBrace); - - RefPtr unparsedStmt = new UnparsedStmt(); - unparsedStmt->tokens = tokens; - return unparsedStmt; - } - - RefPtr scopeDecl = new ScopeDecl(); RefPtr blockStatement = new BlockStmt(); blockStatement->scopeDecl = scopeDecl; @@ -2816,19 +2799,7 @@ namespace Slang } auto typeExpr = typeSpec.expr; - while (LookAheadToken(TokenType::LBracket)) - { - RefPtr arrType = new IndexExpressionSyntaxNode(); - arrType->Position = typeExpr->Position; - arrType->BaseExpression = typeExpr; - ReadToken(TokenType::LBracket); - if (!LookAheadToken(TokenType::RBracket)) - { - arrType->IndexExpression = ParseExpression(); - } - ReadToken(TokenType::RBracket); - typeExpr = arrType; - } + typeExpr = parsePostfixTypeSuffix(this, typeExpr); return typeExpr; } @@ -2915,83 +2886,6 @@ namespace Slang } } - Operator GetOpFromToken(Token & token) - { - switch(token.Type) - { - case TokenType::Comma: - return Operator::Sequence; - case TokenType::OpAssign: - return Operator::Assign; - case TokenType::OpAddAssign: - return Operator::AddAssign; - case TokenType::OpSubAssign: - return Operator::SubAssign; - case TokenType::OpMulAssign: - return Operator::MulAssign; - case TokenType::OpDivAssign: - return Operator::DivAssign; - case TokenType::OpModAssign: - return Operator::ModAssign; - case TokenType::OpShlAssign: - return Operator::LshAssign; - case TokenType::OpShrAssign: - return Operator::RshAssign; - case TokenType::OpOrAssign: - return Operator::OrAssign; - case TokenType::OpAndAssign: - return Operator::AddAssign; - case TokenType::OpXorAssign: - return Operator::XorAssign; - case TokenType::OpOr: - return Operator::Or; - case TokenType::OpAnd: - return Operator::And; - case TokenType::OpBitOr: - return Operator::BitOr; - case TokenType::OpBitXor: - return Operator::BitXor; - case TokenType::OpBitAnd: - return Operator::BitAnd; - case TokenType::OpEql: - return Operator::Eql; - case TokenType::OpNeq: - return Operator::Neq; - case TokenType::OpGeq: - return Operator::Geq; - case TokenType::OpLeq: - return Operator::Leq; - case TokenType::OpGreater: - return Operator::Greater; - case TokenType::OpLess: - return Operator::Less; - case TokenType::OpLsh: - return Operator::Lsh; - case TokenType::OpRsh: - return Operator::Rsh; - case TokenType::OpAdd: - return Operator::Add; - case TokenType::OpSub: - return Operator::Sub; - case TokenType::OpMul: - return Operator::Mul; - case TokenType::OpDiv: - return Operator::Div; - case TokenType::OpMod: - return Operator::Mod; - case TokenType::OpInc: - return Operator::PostInc; - case TokenType::OpDec: - return Operator::PostDec; - case TokenType::OpNot: - return Operator::Not; - case TokenType::OpBitNot: - return Operator::BitNot; - default: - throw "Illegal TokenType."; - } - } - static RefPtr parseOperator(Parser* parser) { Token opToken; @@ -3194,9 +3088,8 @@ namespace Slang // but for now we will follow some hueristics. case TokenType::LParent: { - parser->ReadToken(TokenType::LParent); + Token openParen = parser->ReadToken(TokenType::LParent); - RefPtr expr; if (peekTypeName(parser) && parser->LookAheadToken(TokenType::RParent, 1)) { RefPtr tcexpr = new TypeCastExpressionSyntaxNode(); @@ -3204,15 +3097,18 @@ namespace Slang tcexpr->TargetType = parser->ParseTypeExp(); parser->ReadToken(TokenType::RParent); tcexpr->Expression = parser->ParseExpression(Precedence::Multiplicative); // Note(tfoley): need to double-check this - expr = tcexpr; + return tcexpr; } else { - expr = parser->ParseExpression(); + RefPtr base = parser->ParseExpression(); parser->ReadToken(TokenType::RParent); - } - return expr; + RefPtr parenExpr = new ParenExpr(); + parenExpr->Position = openParen.Position; + parenExpr->base = base; + return parenExpr; + } } // An initializer list `{ expr, ... }` @@ -3476,7 +3372,11 @@ namespace Slang indexExpr->BaseExpression = expr; parser->FillPosition(indexExpr.Ptr()); parser->ReadToken(TokenType::LBracket); - indexExpr->IndexExpression = parser->ParseExpression(); + // TODO: eventually we may want to support multiple arguments inside the `[]` + if (!parser->LookAheadToken(TokenType::RBracket)) + { + indexExpr->IndexExpression = parser->ParseExpression(); + } parser->ReadToken(TokenType::RBracket); expr = indexExpr; @@ -3539,6 +3439,7 @@ namespace Slang case TokenType::OpDec: case TokenType::OpNot: case TokenType::OpBitNot: + case TokenType::OpAdd: case TokenType::OpSub: { RefPtr prefixExpr = new PrefixExpr(); -- cgit v1.2.3 From 6233f9b35f1901ca33c53ce37f9b1517e91e1d79 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Sat, 8 Jul 2017 17:04:31 -0700 Subject: Revise how hidden implicit casts are recognized. The old approach used an `isRewriter` flag in the emit logic, but I kind of need that flag to go away. Instead, I now how the semantic checking pass detect whether an implicitly-generated type cast is in rewriter code, and if so it uses the new `HiddenImplicitCastExpr` AST node. The emit logic then looks for that specific node and eliminates it. --- source/slang/check.cpp | 14 +++++++++++++- source/slang/emit.cpp | 17 +++++++---------- source/slang/expr-defs.h | 11 +++++++++++ source/slang/parser.cpp | 2 +- 4 files changed, 32 insertions(+), 12 deletions(-) (limited to 'source/slang/parser.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index f21f9480c..556f141c0 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -987,7 +987,19 @@ namespace Slang RefPtr toType, RefPtr fromExpr) { - auto castExpr = new ImplicitCastExpr(); + // In "rewrite" mode, we will generate a different syntax node + // to indicate that this type-cast was implicitly generated + // by the compiler, and shouldn't appear in the output code. + RefPtr castExpr; + if (isRewriteMode()) + { + castExpr = new HiddenImplicitCastExpr(); + } + else + { + castExpr = new ImplicitCastExpr(); + } + castExpr->Position = fromExpr->Position; castExpr->TargetType.type = toType; castExpr->Type = QualType(toType); diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index a1d95ca7e..a8c0083c1 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -2031,18 +2031,15 @@ struct EmitVisitor if(needClose) Emit(")"); } - void visitTypeCastExpressionSyntaxNode(TypeCastExpressionSyntaxNode* castExpr, ExprEmitArg const& arg) + void visitHiddenImplicitCastExpr(HiddenImplicitCastExpr* castExpr, ExprEmitArg const& arg) { - if (context->isRewrite) - { - if (dynamic_cast(castExpr)) - { - // This was an implicit cast, so don't try to output it - ExprVisitorWithArg::dispatch(castExpr->Expression, arg); - return; - } - } + // This was an implicit cast inserted in code parsed in "rewriter" mode, + // so we don't want to output it and change what the user's code looked like. + ExprVisitorWithArg::dispatch(castExpr->Expression, arg); + } + void visitTypeCastExpressionSyntaxNode(TypeCastExpressionSyntaxNode* castExpr, ExprEmitArg const& arg) + { bool needClose = false; switch(context->shared->target) { diff --git a/source/slang/expr-defs.h b/source/slang/expr-defs.h index 5ca6629b9..0dac324b9 100644 --- a/source/slang/expr-defs.h +++ b/source/slang/expr-defs.h @@ -91,14 +91,25 @@ SYNTAX_CLASS(DerefExpr, ExpressionSyntaxNode) SYNTAX_FIELD(RefPtr, base) END_SYNTAX_CLASS() +// Any operation that performs type-casting SYNTAX_CLASS(TypeCastExpressionSyntaxNode, ExpressionSyntaxNode) SYNTAX_FIELD(TypeExp, TargetType) SYNTAX_FIELD(RefPtr, Expression) END_SYNTAX_CLASS() +// An explicit type-cast that appear in the user's code with `(Type) expr` syntax +SYNTAX_CLASS(ExplicitCastExpr, TypeCastExpressionSyntaxNode) +END_SYNTAX_CLASS() + +// An implicit type-cast inserted during semantic checking SYNTAX_CLASS(ImplicitCastExpr, TypeCastExpressionSyntaxNode) END_SYNTAX_CLASS() +// An implicit type-cast that should also be hidden on output, +// because we don't want to mess with the user's code +SYNTAX_CLASS(HiddenImplicitCastExpr, ImplicitCastExpr) +END_SYNTAX_CLASS() + SIMPLE_SYNTAX_CLASS(SelectExpressionSyntaxNode, OperatorExpressionSyntaxNode) SIMPLE_SYNTAX_CLASS(GenericAppExpr, AppExprBase) diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 2056bf809..0efc8cd77 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -3092,7 +3092,7 @@ namespace Slang if (peekTypeName(parser) && parser->LookAheadToken(TokenType::RParent, 1)) { - RefPtr tcexpr = new TypeCastExpressionSyntaxNode(); + RefPtr tcexpr = new ExplicitCastExpr(); parser->FillPosition(tcexpr.Ptr()); tcexpr->TargetType = parser->ParseTypeExp(); parser->ReadToken(TokenType::RParent); -- cgit v1.2.3 From a40b6679931f672a911070fcc7eeb41c52e8b8fd Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Sat, 8 Jul 2017 17:21:37 -0700 Subject: Differentiate HLSL `for` loops in AST HLSL has the bad scoping behavior for `for` loops, and we need to respect that. But, we need to have correct scoping for GLSL, and we'd like it for Slang. We also need to ensure that `for` loops written in a "correct" language get the correct behavior when emitted as HLSL. There was already code to handle this in the emit pass, but it was unfortunately using an `isRewrite` flag to try to tell if the HLSL behavior was wanted. This doesn't work when the code being emitted might come from a mix of languages. This change adds a distinct `UnscopedForStmt` syntax node type, and uses that when parsing HLSL input (bot not for other languages). We make sure to preserve this node type through lowering, and then specialize our emit logic on this case. With this, there are no more remaining uses of `isRewrite` in the emit logic, which is good because it didn't mean what I needed it to mean any more (since we now emit only a single module, that was merged during lowering). --- source/slang/emit.cpp | 17 ++--------------- source/slang/lower.cpp | 16 +++++++++++++--- source/slang/parser.cpp | 33 ++++++++++++++++++++++++++++----- source/slang/stmt-defs.h | 5 +++++ 4 files changed, 48 insertions(+), 23 deletions(-) (limited to 'source/slang/parser.cpp') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index a8c0083c1..24b0dd717 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -57,10 +57,6 @@ struct EmitContext { // The shared context that is in effect SharedEmitContext* shared; - - // Are we in "rewrite" mode, where we are trying to reproduce the input - // code as closely as posible? - bool isRewrite; }; // @@ -2196,12 +2192,10 @@ struct EmitVisitor // The one wrinkle is that HLSL implements the // bad approach to scoping a `for` loop variable, // so we need to avoid those outer `{...}` when - // we are generating HLSL via "rewrite" (that is, - // without our semantic checks). + // we are emitting code that was written in HLSL. // bool brokenScoping = false; - if (context->shared->target == CodeGenTarget::HLSL - && context->isRewrite) + if (forStmt.As()) { brokenScoping = true; } @@ -3406,10 +3400,6 @@ struct EmitVisitor } }; -bool isRewriteRequest( - SourceLanguage sourceLanguage, - CodeGenTarget target); - String emitEntryPoint( EntryPointRequest* entryPoint, ProgramLayout* programLayout, @@ -3465,9 +3455,6 @@ String emitEntryPoint( EmitContext context; context.shared = &sharedContext; - context.isRewrite = isRewriteRequest( - translationUnit->sourceLanguage, - target); EmitVisitor visitor(&context); diff --git a/source/slang/lower.cpp b/source/slang/lower.cpp index c154c96c8..8a4b7a4b1 100644 --- a/source/slang/lower.cpp +++ b/source/slang/lower.cpp @@ -723,10 +723,10 @@ struct LoweringVisitor addStmt(loweredStmt); } - - void visitForStatementSyntaxNode(ForStatementSyntaxNode* stmt) + void lowerForStmtCommon( + RefPtr loweredStmt, + ForStatementSyntaxNode* stmt) { - RefPtr loweredStmt = new ForStatementSyntaxNode(); lowerScopeStmtFields(loweredStmt, stmt); LoweringVisitor subVisitor = pushScope(loweredStmt, stmt); @@ -739,6 +739,16 @@ struct LoweringVisitor addStmt(loweredStmt); } + void visitForStatementSyntaxNode(ForStatementSyntaxNode* stmt) + { + lowerForStmtCommon(new ForStatementSyntaxNode(), stmt); + } + + void visitUnscopedForStmt(UnscopedForStmt* stmt) + { + lowerForStmtCommon(new UnscopedForStmt(), stmt); + } + void visitWhileStatementSyntaxNode(WhileStatementSyntaxNode* stmt) { RefPtr loweredStmt = new WhileStatementSyntaxNode(); diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 0efc8cd77..199275a2d 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -2673,12 +2673,32 @@ namespace Slang RefPtr Parser::ParseForStatement() { RefPtr scopeDecl = new ScopeDecl(); - RefPtr stmt = new ForStatementSyntaxNode(); + + // HLSL implements the bad approach to scoping a `for` loop + // variable, and we want to respect that, but *only* when + // parsing HLSL code. + // + + bool brokenScoping = translationUnit->sourceLanguage == SourceLanguage::HLSL; + + // We will create a distinct syntax node class for the unscoped + // case, just so that we can correctly handle it in downstream + // logic. + // + RefPtr stmt; + if (brokenScoping) + { + stmt = new UnscopedForStmt(); + } + else + { + stmt = new ForStatementSyntaxNode(); + } + stmt->scopeDecl = scopeDecl; - // Note(tfoley): HLSL implements `for` with incorrect scoping. - // We need an option to turn on this behavior in a kind of "legacy" mode -// PushScope(scopeDecl.Ptr()); + if(!brokenScoping) + PushScope(scopeDecl.Ptr()); FillPosition(stmt.Ptr()); ReadToken("for"); ReadToken(TokenType::LParent); @@ -2704,7 +2724,10 @@ namespace Slang stmt->SideEffectExpression = ParseExpression(); ReadToken(TokenType::RParent); stmt->Statement = ParseStatement(); -// PopScope(); + + if (!brokenScoping) + PopScope(); + return stmt; } diff --git a/source/slang/stmt-defs.h b/source/slang/stmt-defs.h index 165ffea83..15826abc4 100644 --- a/source/slang/stmt-defs.h +++ b/source/slang/stmt-defs.h @@ -66,6 +66,7 @@ SIMPLE_SYNTAX_CLASS(DefaultStmt, CaseStmtBase) ABSTRACT_SYNTAX_CLASS(LoopStmt, BreakableStmt) END_SYNTAX_CLASS() +// A `for` statement SYNTAX_CLASS(ForStatementSyntaxNode, LoopStmt) SYNTAX_FIELD(RefPtr, InitialStatement) SYNTAX_FIELD(RefPtr, SideEffectExpression) @@ -73,6 +74,10 @@ SYNTAX_CLASS(ForStatementSyntaxNode, LoopStmt) SYNTAX_FIELD(RefPtr, Statement) END_SYNTAX_CLASS() +// A `for` statement in a language that doesn't restrict the scope +// of the loop variable to the body. +SYNTAX_CLASS(UnscopedForStmt, ForStatementSyntaxNode); +END_SYNTAX_CLASS() SYNTAX_CLASS(WhileStatementSyntaxNode, LoopStmt) SYNTAX_FIELD(RefPtr, Predicate) -- cgit v1.2.3 From 780a0bcd3724cad77cb65f931f111273776c9ca4 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Sat, 8 Jul 2017 17:47:31 -0700 Subject: Add back `UnparsedStmt` If the user doesn't use any `import` declarations, there is no reason to parse their code at all, so having the option of falling back to `UnparsedStmt` can potentially save us some headaches down the road. The new rule now is that if you have the "no checking" flag on, *and* the parser hasn't yet seen any `import` declarations, then it still used `UnparsedStmt` to avoid touching function bodies. Otherwise, I go ahead and parse function bodies, and assume I can rewrite any code I can semantically understand. --- source/slang/check.cpp | 5 +++++ source/slang/emit.cpp | 16 ++++++++++++++ source/slang/lower.cpp | 10 +++++++++ source/slang/parser.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ source/slang/stmt-defs.h | 7 ++++++ 5 files changed, 94 insertions(+) (limited to 'source/slang/parser.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 556f141c0..2b9abef90 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -1790,6 +1790,11 @@ namespace Slang checkStmt(stmt->NegativeStatement); } + void visitUnparsedStmt(UnparsedStmt*) + { + // Nothing to do + } + void visitEmptyStatementSyntaxNode(EmptyStatementSyntaxNode*) { // Nothing to do diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 24b0dd717..20b9856c5 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -2121,6 +2121,17 @@ struct EmitVisitor } } + void EmitUnparsedStmt(RefPtr stmt) + { + // TODO: actually emit the tokens that made up the statement... + Emit("{\n"); + for( auto& token : stmt->tokens ) + { + emitTokenWithLocation(token); + } + Emit("}\n"); + } + void EmitStmt(RefPtr stmt) { // Try to ensure that debugging can find the right location @@ -2139,6 +2150,11 @@ struct EmitVisitor } return; } + else if( auto unparsedStmt = stmt.As() ) + { + EmitUnparsedStmt(unparsedStmt); + return; + } else if (auto exprStmt = stmt.As()) { EmitExpr(exprStmt->Expression); diff --git a/source/slang/lower.cpp b/source/slang/lower.cpp index 8a4b7a4b1..0d54faf0b 100644 --- a/source/slang/lower.cpp +++ b/source/slang/lower.cpp @@ -688,6 +688,16 @@ struct LoweringVisitor addStmt(loweredStmt); } + void visitUnparsedStmt(UnparsedStmt* stmt) + { + RefPtr loweredStmt = new UnparsedStmt(); + lowerStmtFields(loweredStmt, stmt); + + loweredStmt->tokens = stmt->tokens; + + addStmt(loweredStmt); + } + void visitCaseStmt(CaseStmt* stmt) { RefPtr loweredStmt = new CaseStmt(); diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 199275a2d..759fc0c20 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -44,6 +44,11 @@ namespace Slang String fileName; int genericDepth = 0; + // Have we seen any `import` declarations? If so, we need + // to parse function bodies completely, even if we are in + // "rewrite" mode. + bool haveSeenAnyImportDecls = false; + // Is the parser in a "recovering" state? // During recovery we don't emit additional errors, until we find // a token that we expected, when we exit recovery. @@ -802,6 +807,8 @@ namespace Slang static RefPtr parseImportDecl( Parser* parser) { + parser->haveSeenAnyImportDecls = true; + parser->ReadToken("__import"); auto decl = new ImportDecl(); @@ -842,6 +849,8 @@ namespace Slang static RefPtr parsePoundImportDecl( Parser* parser) { + parser->haveSeenAnyImportDecls = true; + Token importToken = parser->ReadToken(TokenType::PoundImport); auto decl = new ImportDecl(); @@ -2599,6 +2608,53 @@ namespace Slang RefPtr Parser::ParseBlockStatement() { + // If we are being asked not to check things *and* we haven't + // seen any `import` declarations yet, then we can safely assume + // that function bodies should be left as-is. + if( (translationUnit->compileFlags & SLANG_COMPILE_FLAG_NO_CHECKING) + && !haveSeenAnyImportDecls ) + { + // We have been asked to parse the input, but not attempt to understand it. + + // TODO: record start/end locations... + + List tokens; + + ReadToken(TokenType::LBrace); + + int depth = 1; + for( ;;) + { + switch( tokenReader.PeekTokenType() ) + { + case TokenType::EndOfFile: + goto done; + + case TokenType::RBrace: + depth--; + if(depth == 0) + goto done; + break; + + case TokenType::LBrace: + depth++; + break; + + default: + break; + } + + auto token = tokenReader.AdvanceToken(); + tokens.Add(token); + } + done: + ReadToken(TokenType::RBrace); + + RefPtr unparsedStmt = new UnparsedStmt(); + unparsedStmt->tokens = tokens; + return unparsedStmt; + } + RefPtr scopeDecl = new ScopeDecl(); RefPtr blockStatement = new BlockStmt(); blockStatement->scopeDecl = scopeDecl; diff --git a/source/slang/stmt-defs.h b/source/slang/stmt-defs.h index 15826abc4..9dea40fbf 100644 --- a/source/slang/stmt-defs.h +++ b/source/slang/stmt-defs.h @@ -16,6 +16,13 @@ SYNTAX_CLASS(BlockStmt, ScopeStmt) SYNTAX_FIELD(RefPtr, body); END_SYNTAX_CLASS() +// A statement that we aren't going to parse or check, because +// we want to let a downstream compiler handle any issues +SYNTAX_CLASS(UnparsedStmt, StatementSyntaxNode) + // The tokens that were contained between `{` and `}` + FIELD(List, tokens) +END_SYNTAX_CLASS() + SIMPLE_SYNTAX_CLASS(EmptyStatementSyntaxNode, StatementSyntaxNode) SIMPLE_SYNTAX_CLASS(DiscardStatementSyntaxNode, StatementSyntaxNode) -- cgit v1.2.3