Last active
December 22, 2015 05:28
-
-
Save guillaume86/6423775 to your computer and use it in GitHub Desktop.
SolLewitt.Grammar for Irony Parser
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Irony.Parsing; | |
namespace SolLewitt.Parser | |
{ | |
[Language("SolLewittDrawing", "0.1", "Sol Lewitt style drawing description language")] | |
public class SolLewittGrammar : Grammar | |
{ | |
public SolLewittGrammar() : base (false) | |
{ | |
// 1. Terminals | |
var upper = ToTerm("upper"); | |
var top = ToTerm("top"); | |
var lower = ToTerm("lower"); | |
var bottom = ToTerm("bottom"); | |
var left = ToTerm("left"); | |
var right = ToTerm("right"); | |
var center = ToTerm("center of the wall"); | |
var from = ToTerm("from"); | |
var to = ToTerm("to"); | |
var and = ToTerm("and"); | |
var start = ToTerm("start"); | |
var of = ToTerm("of"); | |
var end = ToTerm("end"); | |
var the = ToTerm("the"); | |
var halfwayBetween = ToTerm("halfway between"); | |
var midpointOf = ToTerm("midpoint of"); | |
var aPoint = ToTerm("a point"); | |
var comma = ToTerm(","); | |
var dot = ToTerm("."); | |
// 2. Non-terminals | |
var LineDefinition = new NonTerminal("LineDefinition"); | |
var LineDefinitionList = new NonTerminal("LineDefinitionList"); | |
var PolygonDescription = new NonTerminal("PolygonDescription"); | |
var Polygon = new NonTerminal("Polygon"); | |
var Nth = new NonTerminal("Nth"); | |
var TopDirection = new NonTerminal("TopDirection"); | |
var BottomDirection = new NonTerminal("BottomDirection"); | |
var HorizontalDirection = new NonTerminal("HorizontalDirection"); | |
var VerticalDirection = new NonTerminal("VerticalDirection"); | |
var Direction = new NonTerminal("Direction"); | |
var PointList = new NonTerminal("PointList"); | |
var EquidistantPoint = new NonTerminal("EquidistantPoint"); | |
var CrossingLinesPoint = new NonTerminal("CrossingLinesPoint"); | |
var TwoPointsLine = new NonTerminal("TwoPointsLine"); | |
var Line = new NonTerminal("Line"); | |
var ReferencedLine = new NonTerminal("ReferencedLine"); | |
var Side = new NonTerminal("Side"); | |
var HalfwayPoint = new NonTerminal("HalfwayPoint"); | |
var Point = new NonTerminal("Point"); | |
var Corner = new NonTerminal("Corner"); | |
var Drawing = new NonTerminal("Drawing"); | |
var LineExtremityPoint = new NonTerminal("LineExtremityPoint"); | |
// 3. BNF rules | |
TopDirection.Rule = (top | upper); | |
BottomDirection.Rule = (bottom | lower); | |
HorizontalDirection.Rule = (left | right); | |
VerticalDirection.Rule = (TopDirection | BottomDirection); | |
Direction.Rule = HorizontalDirection | VerticalDirection; | |
Side.Rule = Direction + "side"; | |
TwoPointsLine.Rule = (from + Point + to + Point) | |
| (Point + and + Point) | |
| (Point + to + Point) | |
| ("a line drawn" + TwoPointsLine) | |
| ("an axis between" + TwoPointsLine); | |
Nth.Rule = ToTerm("first") | "second" | "third" | "fourth"; | |
ReferencedLine.Rule = Nth + "line"; | |
LineExtremityPoint.Rule = (start | end) + of + Line; | |
Line.Rule = Side | |
| ReferencedLine | |
| TwoPointsLine | |
| the + Line | |
| Line; | |
HalfwayPoint.Rule = halfwayBetween + Line | |
| midpointOf + Line | |
| aPoint + HalfwayPoint; | |
CrossingLinesPoint.Rule = | |
aPoint + ToTerm("which is on") + Line + "where" + Line + "would cross that axis" | |
| aPoint + "where a line would cross" + Line + "if it were drawn" + Line; | |
PointList.Rule = MakeListRule(PointList, comma | and, Point); | |
EquidistantPoint.Rule = aPoint + "equidistant from" + PointList; | |
Point.Rule = HalfwayPoint | |
| Corner | |
| LineExtremityPoint | |
| CrossingLinesPoint | |
| EquidistantPoint | |
| center | |
| the + Point; | |
Corner.Rule = VerticalDirection + HorizontalDirection + "corner"; | |
PolygonDescription.Rule = ToTerm("a") + "quadrangle" + "which is formed and enclosed by" + "four" + "lines"; | |
LineDefinition.Rule = the + Nth + (ToTerm("line") | "of which is drawn") + Line; | |
LineDefinitionList.Rule = MakeListRule(LineDefinitionList, comma, LineDefinition); | |
Polygon.Rule = PolygonDescription + comma + LineDefinitionList + dot; | |
Drawing.Rule = MakeStarRule(Drawing, Polygon); | |
this.Root = Drawing; | |
// 4. Operators precedence | |
// 5. Punctuation and transient terms | |
MarkPunctuation(from, to, halfwayBetween, midpointOf, the, aPoint, and); | |
MarkTransient(HorizontalDirection, VerticalDirection, Point, Line); | |
this.LanguageFlags = LanguageFlags.NewLineBeforeEOF; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment