CodeSOD: A Dumbain Specific Language |
Ive had to write a few domain-specific-languages in the past. As per Remys Law of Requirements Gathering, its been mostly because the users needed an Excel-like formula language. The danger of DSLs, of course, is that theyre often YAGNI in the extreme, or at least a sign that you dont really understand your problem.
XML, coupled with schemas, is a tool for building data-focused DSLs. If you have some complex structure, you can convert each of its features into an XML attribute. For example, if you had a grammar that looked something like this:
The Source specification obeys the following syntax
source = ( Feature1+Feature2+... ":" ) ? steps
Feature1 = "local" | "global"
Feature2 ="real" | "virtual" | "ComponentType.all"
Feature3 ="self" | "ancestors" | "descendants" | "Hierarchy.all"
Feature4 = "first" | "last" | "DayAllocation.all"
If features are specified, the order of features as given above has strictly to be followed.
steps = oneOrMoreNameSteps | zeroOrMoreNameSteps | componentSteps
oneOrMoreNameSteps = nameStep ( "." nameStep ) *
zeroOrMoreNameSteps = ( nameStep "." ) *
nameStep = "#" name
name is a string of characters from "A"-"Z", "a"-"z", "0"-"9", "-" and "_". No umlauts allowed, one character is minimum.
componentSteps is a list of valid values, see below.
Valid 'componentSteps' are:
- GlobalValue
- Product
- Product.Brand
- Product.Accommodation
- Product.Accommodation.SellingAccom
- Product.Accommodation.SellingAccom.Board
- Product.Accommodation.SellingAccom.Unit
- Product.Accommodation.SellingAccom.Unit.SellingUnit
- Product.OnewayFlight
- Product.OnewayFlight.BookingClass
- Product.ReturnFlight
- Product.ReturnFlight.BookingClass
- Product.ReturnFlight.Inbound
- Product.ReturnFlight.Outbound
- Product.Addon
- Product.Addon.Service
- Product.Addon.ServiceFeature
In addition to that all subsequent steps from the paths above are permitted, that is 'Board',
'Accommodation.SellingAccom' or 'SellingAccom.Unit.SellingUnit'.
'Accommodation.Unit' in the contrary is not permitted, as here some intermediate steps are missing.
You could turn that grammar into an XML document by converting syntax elements to attributes and elements. You could do that, but Stellas predecessor did not do that. That of course, would have been work, and they may have had to put some thought on how to relate their homebrew grammar to XSD rules, so instead they created an XML schema rule for SourceAttributeType that verifies that the data in the field is valid according to the grammar… using regular expressions. 1,310 characters of regular expressions.
Theres a bug in that regex that Stella needed to fix. As she put it: Every time you evaluate it a few little kitties die because you shouldnt use kitties to polish your car. Im so, so sorry, little kitties…
The full, unexcerpted code is below, so… at least it has documentation. In two languages!
Die Source Angabe folgt folgender Syntax
source = ( Eigenschaft1+Eigenschaft2+... ":" ) ? steps
Eigenschaft1 = "local" | "global"
Eigenschaft2 ="real" | "virtual" | "ComponentType.all"
Eigenschaft3 ="self" | "ancestors" | "descendants" | "Hierarchy.all"
Eigenschaft4 = "first" | "last" | "DayAllocation.all"
Falls Eigenschaften angegeben werden muss zwingend die oben angegebene Reihenfolge der Eigenschaften eingehalten werden.
steps = oneOrMoreNameSteps | zeroOrMoreNameSteps | componentSteps
oneOrMoreNameSteps = nameStep ( "." nameStep ) *
zeroOrMoreNameSteps = ( nameStep "." ) *
nameStep = "#" name
name ist eine Folge von Zeichen aus der Menge "A"-"Z", "a"-"z", "0"-"9", "-" und "_". Keine Umlaute. Mindestens ein Zeichen
componentSteps ist eine Liste g"ultiger Werte, siehe im folgenden
G"ultige 'componentSteps' sind zun"achst:
- GlobalValue
- Product
- Product.Brand
- Product.Accommodation
- Product.Accommodation.SellingAccom
- Product.Accommodation.SellingAccom.Board
- Product.Accommodation.SellingAccom.Unit
- Product.Accommodation.SellingAccom.Unit.SellingUnit
- Product.OnewayFlight
- Product.OnewayFlight.BookingClass
- Product.ReturnFlight
- Product.ReturnFlight.BookingClass
- Product.ReturnFlight.Inbound
- Product.ReturnFlight.Outbound
- Product.Addon
- Product.Addon.Service
- Product.Addon.ServiceFeature
Desweiteren sind alle Unterschrittfolgen aus obigen Pfaden erlaubt, also 'Board', 'Accommodation.SellingAccom' oder 'SellingAccom.Unit.SellingUnit'.
'Accommodation.Unit' hingegen ist nicht erlaubt, da in diesem Fall einige Zwischenschritte fehlen.
The Source specification obeys the following syntax
source = ( Feature1+Feature2+... ":" ) ? steps
Feature1 = "local" | "global"
Feature2 ="real" | "virtual" | "ComponentType.all"
Feature3 ="self" | "ancestors" | "descendants" | "Hierarchy.all"
Feature4 = "first" | "last" | "DayAllocation.all"
If features are specified, the order of features as given above has strictly to be followed.
steps = oneOrMoreNameSteps | zeroOrMoreNameSteps | componentSteps
oneOrMoreNameSteps = nameStep ( "." nameStep ) *
zeroOrMoreNameSteps = ( nameStep "." ) *
nameStep = "#" name
name is a string of characters from "A"-"Z", "a"-"z", "0"-"9", "-" and "_". No umlauts allowed, one character is minimum.
componentSteps is a list of valid values, see below.
Valid 'componentSteps' are:
- GlobalValue
- Product
- Product.Brand
- Product.Accommodation
- Product.Accommodation.SellingAccom
- Product.Accommodation.SellingAccom.Board
- Product.Accommodation.SellingAccom.Unit
- Product.Accommodation.SellingAccom.Unit.SellingUnit
- Product.OnewayFlight
- Product.OnewayFlight.BookingClass
- Product.ReturnFlight
- Product.ReturnFlight.BookingClass
- Product.ReturnFlight.Inbound
- Product.ReturnFlight.Outbound
- Product.Addon
- Product.Addon.Service
- Product.Addon.ServiceFeature
In addition to that all subsequent steps from the paths above are permitted, that is 'Board', 'Accommodation.SellingAccom' or 'SellingAccom.Unit.SellingUnit'.
'Accommodation.Unit' in the contrary is not permitted, as here some intermediate steps are missing.
[Advertisement] Release!
is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies - only $9.95 shipped!
https://thedailywtf.com/articles/a-dumbain-specific-language
| Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |