CodeBlock ParseCodeBlock(TokenRange toks) { List <Statement> statements = new List <Statement>(); while (true) { Token delim = toks .FirstOrDefault( t => t.ParenDepth == toks.First().ParenDepth&& t.BraceDepth == toks.First().BraceDepth&& (t.Value == ";" || t.Value == "}") ); if (delim == null) { break; } ParseStatement(range(toks.Begin, delim.Position + 1), statements); toks = range(delim.Position + 1, toks.End); } if (!toks.All(Whitespace)) { throw new Error(toks.First(NonWhitespace).SourceLine, "Trailing unterminated statement in code block"); } return(new CodeBlock { statements = statements.ToArray() }); }
List <Declaration> ParseDescrCodeBlock(TokenRange toks) { List <Declaration> declarations = new List <Declaration>(); while (true) { Token delim = toks.FirstOrDefault(t => t.Value == ";"); if (delim == null) { break; } int pos = delim.Position + 1; var potentialComment = range(pos, toks.End).SkipWhile(t => t.Value == "\t" || t.Value == " "); if (!potentialComment.IsEmpty && potentialComment.First().Value.StartsWith("//")) { pos = potentialComment.First().Position + 1; } ParseDeclaration(range(toks.Begin, pos), declarations); toks = range(pos, toks.End); } if (!toks.All(Whitespace)) { throw new Error(toks.First(NonWhitespace).SourceLine, "Trailing unterminated statement in code block"); } return(declarations); }
void ParseDescrHeading(Descr descr, TokenRange toks) { toks.First(NonWhitespace).Assert("non-struct DESCR!", t => t.Value == "struct"); toks = toks.SkipWhile(Whitespace).Skip(1).SkipWhile(Whitespace); var colon = toks.FirstOrDefault(t => t.Value == ":"); if (colon != null) { descr.superClassList = str(range(colon.Position + 1, toks.End)).Trim(); toks = range(toks.Begin, colon.Position); } descr.name = str(toks).Trim(); }