private TotemAst ParseFileWorker(bool makeModule, bool returnValue) { _globalParent = new TotemAst(makeModule, _languageFeatures, false, _context); StartParsing(); List<Stmt> l = new List<Stmt>(); List<string> imports = new List<string>(); // import-statements are only allowed in the start of the module while (PeekToken(TokenType.KeywordImport)) { imports.Add(ParseImport()); } _importAllowed = false; while (true) { if (MaybeEat(TokenType.EndOfFile)) break; if (MaybeEatNewLine()) continue; Stmt s = ParseStmt(); l.Add(s); } Stmt[] stmts = l.ToArray(); string[] imps = imports.ToArray(); if (returnValue && stmts.Length > 0) { ExprStmt exprStmt = stmts[stmts.Length - 1] as ExprStmt; if (exprStmt != null) { var retStmt = new ReturnStmt(exprStmt.Expr); stmts[stmts.Length - 1] = retStmt; retStmt.SetLoc(_globalParent, exprStmt.Expr.IndexSpan); } } BlockStmt ret = new BlockStmt(stmts); ret.SetLoc(_globalParent, 0, GetEnd()); return FinishParsing(ret); }
private Stmt ParseBlock() { Eat(TokenType.LeftBrace); int start = GetStart(); List<Stmt> statements = new List<Stmt>(); try { _openParen++; while (!MaybeEat(TokenType.RightBrace)) { var stmt = ParseStmt(); if (stmt == null) return null; statements.Add(stmt); } } finally { _openParen--; } Stmt ret = new BlockStmt(statements.ToArray()); ret.SetLoc(_globalParent, start, GetEnd()); return ret; }