Skip to content

Instantly share code, notes, and snippets.

@jennings
Created August 8, 2022 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennings/6f12c66610cedf9e730698c913c28269 to your computer and use it in GitHub Desktop.
Save jennings/6f12c66610cedf9e730698c913c28269 to your computer and use it in GitHub Desktop.
var ParseError = from _ in Parse.String("error") select Level.Error;
var ParseWarning = from _ in Parse.String("warning") select Level.Warning;
var ParseLevel = ParseError.Or(ParseWarning);
var ParsePosition =
from lp in Parse.String("(")
from line in Parse.Number
from comma in Parse.String(", ")
from column in Parse.Number
from rp in Parse.String(")")
select new Position
{
Line = int.Parse(line),
Column = int.Parse(column),
};
var ParseMessage =
from level in ParseLevel
from position in ParsePosition
from _ in Parse.Char(' ')
from code in Parse.AnyChar.Until(Parse.Char(':')).Text()
from text in Parse.AnyChar.Until(Parse.LineEnd).Text()
select new Message{
Level = level,
Position = position,
Code = code,
Text = text,
};
ParseMessage.Many().Parse(@"error(12, 34) CS0234: You are a bad programmer
error(1, 33) CS08934: I am very tired
warning(2, 3) CS33333: Bad things are happening here
").Dump();
class Message{
public Level Level;
public Position Position;
public string Code;
public string Text;
}
class Position
{
public int Line;
public int Column;
}
enum Level
{
Error,
Warning,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment