Created
August 8, 2022 15:57
-
-
Save jennings/6f12c66610cedf9e730698c913c28269 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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