BFL grammar

The Xtext grammar of BFL as shipped in this release.

The Xtext grammar the BIM Feature Language parser is generated from, exactly as shipped in this release. Rules read top-down; => marks a syntactic predicate, += collects a list, ?= a boolean flag. Terminals not declared here (ID, STRING, INT, comments) come from the common Xtext terminals; the languages that build on BQL inherit every rule from it.

Text#
grammar nl.bimbase.bimworks.Bfl with nl.bimbase.bimworks.Bql

generate bfl "http://www.bimbase.nl/bimworks/Bfl"

import "http://www.bimbase.nl/bimworks/Bql" as bql

// BFL — the feature definition language (docs/virtual-features-design.md §5.1). One document
// is one virtual feature: a target (the BQL path the feature answers to), an optional scope
// and a body — a BQL value expression, so every condition and function of BQL is available.
//
//   DEFINE quantity."BW"."Width" : LENGTH
//     "The larger of the computed and the declared width"
//   FOR type = IFCDOOR
//   AS GREATEST(calculated.quantity.width, attribute.OverallWidth)
//
// "AS" is the separator on purpose: "=" after a FOR scope would read as a comparator of the
// scope's last condition.
Bfl:
	"DEFINE" target=VirtualTarget (":" type=VirtualValueType)?
	(description=STRING)?
	("FOR" scope=BooleanExpression)?
	("LET" lets+=LetBinding ("," lets+=LetBinding)*)?
	"AS" body=ValueArgument
;

// The six kinds of §3.1. Set, property, quantity and system names follow the BQL path rules:
// an unquoted name must not be a reserved word (quote it otherwise).
VirtualTarget:
	{PropertyTarget} "property" "." setName=(STRING | ID) "." name=(STRING | ID) |
	{QuantityTarget} "quantity" "." setName=(STRING | ID) "." name=(STRING | ID) |
	{ClassificationTarget} "classification" system=(STRING | ID) ("NAMES" "FROM" codeList=STRING)? |
	{MaterialTarget} "material" |
	{LayerTarget} "layer" |
	{AttributeTarget} "attribute" "." name=Attribute
;

// Optional declaration, checked against the body's inferred type when given. TYPE_NONE is the
// unset sentinel (first literal = EMF default), rejected by the validator.
enum VirtualValueType:
	TYPE_NONE |
	TEXT | NUMBER | BOOLEAN | LENGTH | AREA | VOLUME | MASS | COUNT
;

// LET gives a sub-expression a name for use in the body and in later bindings; it is
// referenced with a leading @ (the $ sigil is taken by query parameters, and a bare identifier
// as an expression operand would shadow every unquoted name in the language).
LetBinding:
	name=ID "=" value=ValueArgument
;

// The expression chains of Bql, extended with the LET reference as an operand. Overriding the
// three primaries is all it takes: every other rule of the chains calls them by name.
@Override
GetPrimary returns bql::Expression:
  =>LongWithUnitLiteralTyped |
  DoubleWithUnitLiteralTyped |
  AbsFunction |
  ValueFunction |
  CaseExpression |
  LetReference |
  GetFeature |
  "(" GetAddSub ")";

@Override
WherePrimary returns bql::Expression:
  =>LongWithUnitLiteralTyped |
  DoubleWithUnitLiteralTyped |
  WhereAbsFunction |
  WhereValueFunction |
  WhereCaseExpression |
  LetReference |
  RelationAggregate |
  ConditionFeature |
  "(" WhereAddSub ")";

@Override
WhereNoParenHeadPrimary returns bql::Expression:
  =>LongWithUnitLiteralTyped |
  DoubleWithUnitLiteralTyped |
  WhereAbsFunction |
  WhereValueFunction |
  WhereCaseExpression |
  LetReference |
  RelationAggregate |
  ConditionFeature;

LetReference returns bql::Expression:
	// A plain name, not a cross-reference: definitions are parsed without a resource (no
	// linking), and the compiler resolves — and orders — the bindings itself
	{LetReference} "@" name=ID
;