Skip to content

Commit

Permalink
Improve error message when a lexical/parsing error occurs when readin…
Browse files Browse the repository at this point in the history
…g from FileStream
  • Loading branch information
leppie committed Feb 15, 2022
1 parent 4781c74 commit e6c2c8f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion IronScheme/IronScheme/IronSchemeLanguageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ internal static object ReadExpressions(Stream code, CompilerContext cc)
{
return p.parsed;
}

if (code is FileStream)
{
var filename = ((FileStream)code).Name;
var errorMsg = p.GetLastError(filename);

return Builtins.LexicalError("invalid syntax", errorMsg);
}

return Builtins.LexicalError("invalid syntax", code);
}

Expand All @@ -158,7 +167,6 @@ internal static object ReadExpressions(string code, CompilerContext cc)
return p.parsed;
}
return Builtins.LexicalError("invalid syntax", code);

}

internal static CodeBlock Compile(Cons expr)
Expand Down
6 changes: 5 additions & 1 deletion IronScheme/IronScheme/gppg/ShiftReduceParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,17 @@ public bool ErrorRecovery()
return discard;
}

public string GetLastError(string filename)
{
return string.Format("unexpected {0} in '{1}' at {2}", TerminalToString(next), filename, GetLocation(lastL));
}

public void ReportError()
{
StringBuilder errorMsg = new StringBuilder();
errorMsg.AppendFormat("unexpected {0}", TerminalToString(next));

if (current_state.parser_table.Count < 20)
if (current_state.parser_table != null && current_state.parser_table.Count < 20)
{
bool first = true;
foreach (int terminal in current_state.parser_table.Keys)
Expand Down

0 comments on commit e6c2c8f

Please sign in to comment.