public bool ImportFile(SourceStream source, out SymbolTable result) { result = null; if (!source.IsValid()) { return(false); } var slice = source.GetSlice(); if (!slice.MatchString(KeywordImport)) { return(false); } SkipCommentsAndWhitespace(slice); if (!slice.MatchString(TokenImportFirst)) { throw new ParseError($"Expected '{TokenImportFirst}'.", slice); } int start = slice.Index(); int count = slice.FindNext(TokenImportFinal); if (count < 0) { throw new ParseError($"Expected '{TokenImportFinal}'.", slice); } if (!slice.ReadAbsolute(start, count, out string path)) { throw new ParseError("Unable to read inlcude path.", slice); } slice.MoveBy(path.Length); if (!slice.MatchString(TokenImportFinal)) { throw new ParseError($"Expected '{TokenImportFinal}'.", slice); } result = GetSymbolsFromFile(path); source.Join(slice); return(true); }
static bool SkipCommentsAndWhitespace(SourceStream source) { if (!source.IsValid()) { return(false); } var slice = source.GetSlice(); while (true) { slice.SkipWhitespace(); if (slice.MatchString(TokenCommentBlockFirst)) { int index = slice.FindNext(TokenCommentBlockFinal); if (index < 0) { slice.MoveBy(-TokenCommentBlockFirst.Length); throw new ParseError($"unterminated comment, expected to find '{TokenCommentBlockFinal}'.", slice); } slice.MoveBy(index + TokenCommentBlockFinal.Length); } else if (slice.MatchString(TokenCommentLineFirst)) { int index = slice.FindNext(TokenCommandLineFinal); if (index < 0) { if (index < 0) { slice.MoveBy(-TokenCommentLineFirst.Length); throw new ParseError($"unterminated comment, expected to find '\\n'.", slice); } } slice.MoveBy(index + TokenCommandLineFinal.Length); } else { source.Join(slice); return(true); } } }
public bool Match(SourceStream source, out StructType result) { result = null; if (!source.IsValid()) { return(false); } var slice = source.GetSlice(); if (!slice.MatchString(KeywordStruct)) { return(false); } SkipCommentsAndWhitespace(slice); if (!slice.ReadWord(out string name)) { throw new ParseError("expected struct name.", slice); } if (!name.IsNameLegal()) { slice.MoveBy(-name.Length); throw new ParseError($"'{name}' is not a legal struct name.", slice); } SkipCommentsAndWhitespace(slice); if (!slice.MatchString(TokenStructFirst)) { throw new ParseError($"expeceted '{TokenStructFirst}'.", slice); } var members = new TypeSet(); while (slice.IsValid()) { SkipCommentsAndWhitespace(slice); if (slice.MatchString(TokenStructFinal)) { break; } if (!Match(slice, out MemberType member)) { throw new ParseError("failed to parse member.", slice); } members.Add(member.Ordinal(), member); } var span = source.Join(slice); result = new StructType(name, members, span); return(true); }