picjs Grammar

The picjs grammar, as annotated EBNF

This is the grammar that picjs's parser is built from, written as EBNF. It is generated from the parser's grammar, src/peg_parser/jp.pegjs, so it is always current.

Rules are listed from the top down, starting with Start. Each rule appears once, under the first rule that uses it, and the rules under one rule are in alphabetical order. The notes come from the comments in the grammar.

Notation

The grammar is a PEG (parsing expression grammar), so it differs from ordinary EBNF in two ways:

  • Alternatives are tried in order, and the first that matches wins: in A | B, B is tried only if A fails.
  • Lookaheads check what comes next without consuming it: !X matches only where X does not, and &X only where it does.
Notation Means
'word' the characters, exactly ('word'i ignores case)
[a-z], [^"] one character in, or not in, the set
. any one character
X?, X*, X+ X optionally, zero or more times, one or more times
? … ? described in words

Spacing is explicit: _ is optional space, __ is required space, and SameLineSpace is space that doesn't include a newline. Space includes comments. A keyword such as 'move' only matches the whole word, so mover is a name, not move followed by r. The keywords, with their abbreviations, are listed at the end.

Programs

Start           = _ Program _ EOF ;

A program is a list of expressions (statements). There are no separators; whitespace (including newlines) delimits statements.

Program         = ExpressionList? ;
ExpressionList  = Expression ( _ Expression )+
                | Expression ;
Expression      = Inspect
                | ShapeDefaultSetter
                | GroupExpression
                | Shape
                | AnimationSequence
                | Assignment
                | IfExpression
                | LogicalORExpression ;

ActualFontSize

ActualFontSize  = ( 'xx-small' | 'x-small' | 'small' | 'medium' | 'large' |
                    'x-large' | 'xx-large' | 'xxx-large' | 'smaller' |
                    'larger' ) !IdentifierPart
                | Percent
                | SimpleDecimalNumber SizeUnit ;

AdditiveExpression

AdditiveExpression
                = MultiplicativeExpression ( _ AdditiveOperator _
                    MultiplicativeExpression )* ;
AdditiveOperator
                = '+'
                | '-' !'>' ;
MultiplicativeExpression
                = PowerExpression ( _ MultiplicativeOperator _
                    PowerExpression )* ;
MultiplicativeOperator
                = '*'
                | '/'
                | '%' ;
PowerExpression
                = UnaryExpression ( _ '^' _ UnaryExpression )* ;
UnaryExpression
                = UnaryOperator _ QualifiedExpression
                | QualifiedExpression ;
QualifiedExpression
                = FunctionDefOrPrimary Qualifier* ;

The leaves of the expression tree: literals, variables, parenthesized exprs, and function definitions.

FunctionDefOrPrimary
                = FunctionDefinitionExpression
                | Primary ;

Arrow function syntax: (params) => body, name => body, or => body

FunctionDefinitionExpression
                = '(' _ FormalParameterList _ ')' _ '=>' _ FunctionBody
                | Identifier _ '=>' _ FunctionBody
                | ( '(' _ ')' _ )? '=>' _ FunctionBody ;
FormalParameterList
                = Identifier ( _ ( ',' _ )? Identifier )* ;
FunctionBody    = '{' _ ExpressionList _ '}'
                | Expression ;
Primary         = ShapeReference
                | Number
                | Boolean
                | Color
                | Position
                | String
                | SetTime
                | TimelineValue
                | ShapeDefaultGetter
                | VariableValue
                | ArrayOrRange
                | '(' _ Expression _ ')' ;

[a, b, c] for arrays, [start..end] for ranges. Commas are optional.

ArrayOrRange    = '[' _ Expression _ '..' _ Expression _ ']'
                | '[' _ ElementList _ ']'
                | '[' _ ']' ;
ElementList     = Expression ( _ ( ',' _ )? Expression )* ;
Boolean         = 'true'
                | 'false' ;

Named (~red), hex (#rgb, #rrggbb), model (rgb(...), oklch(...)), or dynamic (~#{expr}) colors.

Color           = ColorModel '(' _ ColorComponents _ ')'
                | '#' HexByte HexByte HexByte HexByte? !HexNibble
                | '#' HexNibble HexNibble HexNibble HexNibble? !HexNibble
                | '~#{' _ Expression _ '}'
                | '~' [a-zA-Z0-9]+ ;
ColorComponents
                = Expression _ ',' _ Expression _ ',' _ Expression _ ',' _
                    Expression
                | Expression _ ',' _ Expression _ ',' _ Expression ;
ColorModel      = ( 'oklch'i | 'rgb'i | 'hsl'i | 'hsv'i ) 'a'i? ;

move, rotate, set, draw, pause — each with optional take/ease params. Animations can be chained with "then" for sequential execution.

SetTime         = '@@' ;
ShapeDefaultGetter
                = ShapeName SEClass '.' AttrName
                | ShapeName '.' AttrName ;
TimelineValue   = '@' ![.([] ;
UnaryOperator   = '+'
                | '-' !'>'
                | '!' ;

AnimationSequence

AnimationSequence
                = Animation ( _ 'then' __ Animation )+
                | Animation ;
Animation       = 'move' __ Expression __ CardinalVector __ AdditiveExpression _
                    AnimationParams
                | 'move' __ Expression __ CardinalVector _ AnimationParams
                | 'move' __ Expression _ ( 'to' __ )? Expression _
                    AnimationParams
                | 'rotate' __ Expression __ 'by' __ Expression _ 'about' __
                    PositionValue _ AnimationParams
                | 'rotate' __ Expression __ 'by' __ Expression _ AnimationParams
                | 'set' __ LValue ( _ ',' _ | __ ( 'to' __ )? ) Expression _
                    AnimationParams
                | 'draw' __ Expression _ AnimationParams
                | 'pause' __ LogicalORExpression
                | 'pause' ;
AnimationParams
                = AnimationParam* ;
AnimationParam  = 'take' __ Expression _
                | 'ease' __ String _
                | 'ease' __ [a-zA-Z] [a-zA-Z0-9]* _ ;

Assignment

Supports simple (=) and compound (+=, -=, *=, /=, %=) assignment. LValues can be identifiers, qualified paths (obj.attr, list[i]), or @ (timeline).

Assignment      = LValue _ '=' ![=>] _ Expression
                | LValue _ AssignmentOperator '=' _ Expression ;
AssignmentOperator
                = '*'
                | '/'
                | '%'
                | '+'
                | '-' ;

AttrName

AttrName        = 'align'
                | 'at'
                | 'ccw'
                | 'cw'
                | 'dashed'
                | 'dotted'
                | 'ease'
                | 'fill'
                | 'font_family'
                | 'font_size'
                | 'font_stretch'
                | 'font_style'
                | 'font_variant'
                | 'font_weight'
                | 'font'
                | 'from'
                | ( 'height' | 'ht' )
                | ( 'length' | 'len' )
                | 'line_height'
                | 'maxwidth'
                | 'opacity'
                | ( 'padding' | 'pad' )
                | ( 'radius' | 'rad' | 'r' )
                | ( 'rotation' | 'rot' )
                | 'rx'
                | 'ry'
                | ( 'smooth' | 'curve' | 'curved' )
                | 'solid'
                | ( 'stepped' | 'step' )
                | 'straight'
                | 'stroke_width'
                | 'stroke'
                | 'take'
                | ( 'thickness' | 'thick' )
                | 'to'
                | 'turn'
                | ( 'width' | 'wid' )
                | 'with'
                | 'x'
                | 'y' ;

Cardinal

Cardinal vectors (north, south, etc.) for layout directions

Cardinal        = '.' ( 'nw' | 'ne' | 'n' | 'sw' | 'se' | 's' | 'w' | 'e' |
                    'c' ) !IdentifierPart ;

CardinalVector

CardinalVector  = 'northwest' !IdentifierPart
                | 'northeast' !IdentifierPart
                | 'north' !IdentifierPart
                | 'southwest' !IdentifierPart
                | 'southeast' !IdentifierPart
                | 'south' !IdentifierPart
                | 'west' !IdentifierPart
                | 'east' !IdentifierPart
                | 'nw' !IdentifierPart
                | 'ne' !IdentifierPart
                | 'sw' !IdentifierPart
                | 'se' !IdentifierPart
                | 'up' !IdentifierPart
                | 'down' !IdentifierPart
                | 'left' !IdentifierPart
                | 'right' !IdentifierPart
                | 'n' !IdentifierPart
                | 's' !IdentifierPart
                | 'w' !IdentifierPart
                | 'e' !IdentifierPart ;

DecimalDigit

DecimalDigit    = [0-9] ;

DecimalIntegerLiteral

DecimalIntegerLiteral
                = '0'
                | [1-9] DecimalDigit* ;

DecimalPoint

DecimalPoint    = '.' !'.' ;

GroupExpression

Groups ({ ... }) collect shapes into a shared coordinate space. Aside is a non-layout-affecting variant. Groups accept common options and with-constraints for positioning.

GroupExpression
                = 'Aside' _ '{' _ ExpressionList _ '}'
                | 'Group' ( __ SEGroupOption )* _ '{' _ ExpressionList _ '}'
                    ( __ SEGroupOption )* ( __ WithConstraint )? ( __
                    SEGroupOption )*
                | '{' _ ExpressionList _ '}' ( __ SEGroupOption )* ( __
                    WithConstraint )? ( __ SEGroupOption )* ;

Styling a group reads better before its body, positioning it after, so both are allowed. SERadii rounds the background's corners; SEPadding holds the children away from its edges.

SEGroupOption   = SEPadding
                | SERadii
                | SECommon ;

A single inset, or one per axis reusing the position literal.

SEPadding       = ( 'padding' | 'pad' ) __ Position
                | ( 'padding' | 'pad' ) __ Expression ;

HexByte

HexByte         = HexNibble HexNibble ;

HexNibble

HexNibble       = [0-9a-f]i ;

Identifier

Start with letter, _, or $; continue with those plus digits. @ is a special identifier for the timeline object.

Identifier      = !Reserved IdentifierStart IdentifierPart*
                | '@' ;

Words that cannot be used as identifiers. AlwaysReserved covers shape names, animation verbs, keywords, and constants. AttrName covers attribute keywords that are only reserved in attribute position.

Reserved        = AlwaysReserved ;
AlwaysReserved  = ( 'Arc' | 'arc' )
                | 'Aside'
                | ( 'Box' | 'box' )
                | ( 'Circle' | 'circle' )
                | ( 'Ellipse' | 'ellipse' )
                | 'Face'
                | 'Gap'
                | 'Goto'
                | 'Group'
                | ( 'Line' | 'line' )
                | 'Label'
                | ( 'Oval' | 'oval' )
                | 'Skip'
                | 'move'
                | 'pause'
                | 'rotate'
                | 'set'
                | 'then'
                | 'wait'
                | 'draw'
                | 'else'
                | 'if'
                | 'true'
                | 'false' ;

IdentifierPart

IdentifierPart  = IdentifierStart
                | UnicodeCombiningMark
                | UnicodeDigit
                | UnicodeConnectorPunctuation
                | '\u200C'
                | '\u200D' ;
UnicodeCombiningMark
                = Mn
                | Mc ;
Mc              = ? Unicode Mc: Mark, Spacing Combining ? ;
Mn              = ? Unicode Mn: Mark, Nonspacing ? ;
UnicodeConnectorPunctuation
                = Pc ;

Punctuation, Connector

Pc              = [_\u203F-\u2040\u2054\uFE33-\uFE34\uFE4D-\uFE4F\uFF3F] ;
UnicodeDigit    = Nd ;
Nd              = ? Unicode Nd: Number, Decimal Digit ? ;

IdentifierStart

IdentifierStart
                = UnicodeLetter
                | '_'
                | '$' ;
UnicodeLetter   = Lu
                | Ll
                | Lt
                | Lm
                | Lo
                | Nl ;
Ll              = ? Unicode Ll: Letter, Lowercase ? ;
Lm              = ? Unicode Lm: Letter, Modifier ? ;
Lo              = ? Unicode Lo: Letter, Other ? ;
Lt              = ? Unicode Lt: Letter, Titlecase ? ;
Lu              = ? Unicode Lu: Letter, Uppercase ? ;
Nl              = ? Unicode Nl: Number, Letter ? ;

IfExpression

if (test) body [else body]. Body is a single expression or { block }.

IfExpression    = 'if' _ '(' _ LogicalORExpression _ ')' _ ExpressionOrBlock _
                    'else' _ ExpressionOrBlock
                | 'if' _ '(' _ LogicalORExpression _ ')' _ ExpressionOrBlock
                | LogicalORExpression ;
ExpressionOrBlock
                = '{' _ ExpressionList _ '}'
                | Expression ;

Inspect

Inspect         = '??' _ Expression ;

LValue

LValue          = '@' !'.'
                | BaseLValue Qualifier+
                | Identifier ;
BaseLValue      = '(' _ Expression _ ')'
                | ShapeReference
                | VariableValue ;

LogicalORExpression

Standard precedence: OR < AND < equality < relational < additive < multiplicative < power < unary

LogicalORExpression
                = LogicalANDExpression ( _ '||' _ LogicalANDExpression )* ;
LogicalANDExpression
                = EqualityExpression ( _ '&&' _ EqualityExpression )* ;
EqualityExpression
                = RelationalExpression ( _ EqualityOperator _
                    RelationalExpression )* ;
EqualityOperator
                = '=='
                | '!=' ;
RelationalExpression
                = AdditiveExpression ( _ RelationalOperator _
                    AdditiveExpression )* ;
RelationalOperator
                = '<='
                | '>='
                | '<' !( '-' | Identifier '>' )
                | '>' ;

NonShapeExpression

NonShapeExpression
                = Inspect
                | Assignment
                | IfExpression
                | LogicalORExpression ;

Number

Integers, decimals, scientific notation. Append % to divide by 100.

Number          = ActualNumber '%'
                | ActualNumber ;
ActualNumber    = DecimalIntegerLiteral DecimalPoint DecimalDigit+ ExponentPart?
                | DecimalPoint DecimalDigit+ ExponentPart?
                | DecimalIntegerLiteral ExponentPart? ;
ExponentPart    = 'e'i [+\-]? DecimalDigit+ ;

Percent

Percent         = SimpleDecimalNumber '%' ;

Position

(x, y) coordinate pair — comma is optional

Position        = '(' _ Expression _ ( ',' _ )? Expression _ ')' ;

PositionValue

PositionValue   = Expression ;

Qualifier

Qualifier       = Arguments
                | '[' _ Expression _ ']'
                | '.' ( AttrName | Identifier ) ;
Arguments       = '(' _ ArgumentList _ ')'
                | '(' _ ')' ;
ArgumentList    = Expression ( _ ( ',' _ )? Expression )* ;

RichLabel

RichLabel       = '(' _ NonShapeExpression LabelTextAttr* _ ')' ;
LabelTextAttr   = __ SEText
                | __ SEFill
                | __ SEStroke
                | __ SEClass
                | __ ActualFontSize ;

SEClass

SEClass         = '.' Identifier ;

SECommon

SECommon        = SELabel
                | SERotation
                | SEPos
                | SEFill
                | SEStroke
                | SEStrokeAttr
                | SEOpacity
                | SEFit
                | SEBehind
                | SESame
                | SEClass
                | SEElementAttr ;
SEBehind        = 'behind' __ NonShapeExpression ;

Attributes copied onto the shape's outermost SVG element, for scripts and CSS. A bare value is taken as a single-word string, not an expression.

SEElementAttr   = 'attr' __ ElementAttrName __ ElementAttrValue
                | 'id' __ ElementAttrValue ;
ElementAttrName
                = [a-zA-Z] [a-zA-Z0-9\-]*
                | String ;
ElementAttrValue
                = [a-zA-Z0-9_.\-]+
                | String ;
SEFit           = 'fit' ;
SELabel         = RichLabel
                | String ;
SEOpacity       = 'opacity' __ Number ;
SEPos           = 'at' __ Expression
                | 'at'? _ Position
                | 'x' __ Expression
                | 'y' __ Expression ;
SERotation      = ( 'rotation' | 'rot' ) __ Expression __ 'about' _
                    PositionValue
                | ( 'rotation' | 'rot' ) __ Expression ;
SESame          = 'same' ;

SEFill

SEFill          = 'fill' __ NonShapeExpression ;

SERadii

SERadii         = ( 'rx' | 'ry' ) __ Expression
                | ( 'radius' | 'rad' | 'r' ) __ Expression ;

SEStroke

SEStroke        = SEStrokeAttr ;

SEStrokeAttr

SEStrokeAttr    = 'stroke' __ NonShapeExpression
                | ( ( 'thickness' | 'thick' ) | 'stroke_width' ) __
                    NonShapeExpression
                | 'solid' | 'dotted' | 'dashed' ;

SEText

SEText          = 'align' __ Cardinal
                | 'maxwidth' __ Number
                | 'line_height' __ Number
                | 'font' __ FontSpec
                | 'font_size' __ ActualFontSize
                | 'font_weight' __ ( 'bold' | 'lighter' | 'darker' | [0-9] '00'
                    | 'normal' ) !IdentifierPart
                | 'font_style' __ ( 'italic' | 'oblique' | 'normal' )
                    !IdentifierPart
                | 'font_variant' __ ( 'small-caps' | 'normal' ) !IdentifierPart
                | 'font_stretch' __ ( 'ultra-condensed' | 'extra-condensed' |
                    'condensed' | 'semi-condensed' | 'semi-expanded' |
                    'expanded' | 'extra-expanded' | 'ultra-expanded' |
                    'normal' ) !IdentifierPart
                | ActualFontSize ;

A subset of CSS font syntax: [style] [variant] [weight] [stretch] size[/line-height] family

FontSpec        = FontProperties ;
FontProperties  = ( BeforeSizeProperty __ )* FontSize __ FontFamilyNames ;
BeforeSizeProperty
                = FontStyle
                | FontVariant
                | FontWeight
                | FontStretch ;
FontStretch     = IgnoreNormal
                | 'ultra-condensed' | 'extra-condensed' | 'condensed' |
                    'semi-condensed' | 'semi-expanded' | 'expanded' |
                    'extra-expanded' | 'ultra-expanded' | Percent ;
FontStyle       = IgnoreNormal
                | 'italic' !IdentifierPart | 'oblique' _ ObliqueAngle |
                    'oblique' !IdentifierPart ;
ObliqueAngle    = [+\-]? [0-9] [0-9] AngleUnit ;
AngleUnit       = 'deg'
                | 'grad'
                | 'rad'
                | 'turn' ;
FontVariant     = IgnoreNormal
                | 'small-caps' !IdentifierPart ;
FontWeight      = IgnoreNormal
                | 'bold' | 'lighter' | 'darker' | [0-9] '00' ;
FontFamilyNames
                = FontFamilyName ( _ ',' _ FontFamilyName )* ;

An unquoted name can be several words, so it ends at the end of the line or at an attribute keyword: in font 12pt Georgia fill ~red the family is Georgia

FontFamilyName  = '"' ( !'"' . )+ '"'
                | FontFamilyWord ( SameLineSpace FontFamilyWord )* ;
FontFamilyWord  = !AttrName Identifier ;
FontSize        = ActualFontSize '/' LineHeight
                | ActualFontSize ;
LineHeight      = IgnoreNormal
                | Percent
                | SimpleDecimalNumber SizeUnit? ;
IgnoreNormal    = 'normal' ;

SameLineSpace

SameLineSpace   = ( '\t' | '\v' | '\f' | ' ' | '\u00A0' | '\uFEFF' | Zs )+ ;

Shape

Shape           = ( 'Arc' | 'arc' ) __ FromPosition __ ToPosition ( _
                    ( SELineEndings | SELineDraw | SELineLabel | SECommon |
                    SETurn ) )*
                | ( 'Arc' | 'arc' ) __ SELineEndings __ FromPosition __
                    ToPosition ( _ ( SELineDraw | SELineLabel | SECommon |
                    SETurn ) )*
                | ( 'Arc' | 'arc' ) !'.' ( __ ( SELineEndings | SELineDraw |
                    SELineLabel | SECommon | SETurn ) )*
                | ( 'Box' | 'box' ) !'.' !'(' ( __ ( SECommon | SESize |
                    SERadii ) )* ( __ WithConstraint )? ( __ ( SECommon | SESize
                    | SERadii ) )*
                | ( 'Circle' | 'circle' ) !'.' !'(' ( __ ( SECommon |
                    SERadius ) )* ( __ WithConstraint )? ( __ ( SECommon |
                    SERadius ) )*
                | ( 'Ellipse' | 'ellipse' ) !'.' !'(' ( __ ( SECommon |
                    SERadii ) )* ( __ WithConstraint )? ( __ ( SECommon |
                    SERadii ) )*
                | ( 'Oval' | 'oval' ) !'.' !'(' ( __ ( SECommon | SESize |
                    SERadii ) )* ( __ WithConstraint )? ( __ ( SECommon | SESize
                    | SERadii ) )*
                | LineOrAbbrev __ FromPosition ( __ ThenToPosition )+ ( __
                    'close' )? ( _ ( SELineEndings | SELineDraw | SELineShape |
                    SELineLabel | SECommon | SERadii ) )*
                | ( 'Line' | 'line' ) __ SELineEndings __ FromPosition ( __
                    ThenToPosition )+ ( __ 'close' )? ( _ ( SELineDraw |
                    SELineShape | SELineLabel | SECommon | SERadii ) )*
                | LineOrAbbrev __ FromPosition __ ToPosition ( __
                    ThenToPosition )+ ( __ 'close' )? ( _ ( SELineEndings |
                    SELineDraw | SELineShape | SELineLabel | SECommon |
                    SERadii ) )*
                | ( 'Line' | 'line' ) __ SELineEndings __ FromPosition __
                    ToPosition ( __ ThenToPosition )+ ( __ 'close' )? ( _
                    ( SELineDraw | SELineShape | SELineLabel | SECommon |
                    SERadii ) )*
                | LineOrAbbrev __ FromPosition __ DirectionalWaypoint ( __
                    ThenToPosition )+ ( __ 'close' )? ( _ ( SELineEndings |
                    SELineDraw | SELineShape | SELineLabel | SECommon |
                    SERadii ) )*
                | ( 'Line' | 'line' ) __ SELineEndings __ FromPosition __
                    DirectionalWaypoint ( __ ThenToPosition )+ ( __ 'close' )?
                    ( _ ( SELineDraw | SELineShape | SELineLabel | SECommon |
                    SERadii ) )*
                | LineOrAbbrev __ DirectionalWaypoint ( __ ThenToPosition )+
                    ( __ 'close' )? ( _ ( SELineEndings | SELineDraw |
                    SELineShape | SELineLabel | SECommon | SERadii ) )*
                | ( 'Line' | 'line' ) __ SELineEndings __ DirectionalWaypoint
                    ( __ ThenToPosition )+ ( __ 'close' )? ( _ ( SELineDraw |
                    SELineShape | SELineLabel | SECommon | SERadii ) )*
                | LineOrAbbrev __ ToPosition ( __ ThenToPosition )+ ( __
                    'close' )? ( _ ( SELineEndings | SELineDraw | SELineShape |
                    SELineLabel | SECommon | SERadii ) )*
                | ( 'Line' | 'line' ) __ SELineEndings __ ToPosition ( __
                    ThenToPosition )+ ( __ 'close' )? ( _ ( SELineDraw |
                    SELineShape | SELineLabel | SECommon | SERadii ) )*
                | LineOrAbbrev __ FromPosition __ ToPosition ( _ ( SELineEndings
                    | SELineDraw | SELineShape | SELineLabel | SECommon ) )*
                | ( 'Line' | 'line' ) __ SELineEndings __ FromPosition __
                    ToPosition ( _ ( SELineDraw | SELineShape | SELineLabel |
                    SECommon ) )*
                | ( 'Line' | 'line' ) __ SELineEndings __ FromPosition ( _
                    ( SELineDraw | SELineShape | SELineLabel | SECommon |
                    SELineLength ) )*
                | ( 'Line' | 'line' ) __ SELineEndings __ ToPosition ( _
                    ( SELineDraw | SELineShape | SELineLabel | SECommon |
                    SELineLength ) )*
                | LineOrAbbrev __ FromPosition ( _ ( SELineEndings | SELineDraw
                    | SELineShape | SELineLabel | SECommon | SELineLength ) )*
                | LineOrAbbrev __ ToPosition ( _ ( SELineEndings | SELineDraw |
                    SELineShape | SELineLabel | SECommon | SELineLength ) )*
                | LineOrAbbrev __ DirectionalWaypoint ( _ ( SELineEndings |
                    SELineDraw | SELineLabel | SECommon | SELineShape ) )*
                | ( 'Line' | 'line' ) __ SELineEndings __ DirectionalWaypoint
                    ( _ ( SELineDraw | SELineLabel | SECommon | SELineShape ) )*
                | LineOrAbbrev !'.' ( __ ( SELineEndings | SELineDraw |
                    SELineLabel | SECommon | SELineShape | SELineLength ) )*
                | 'Label' __ ( ( SEText | SECommon ) __ )+ ( String |
                    RichLabel ) ( __ ( SEText | SECommon ) )* ( __
                    WithConstraint )?
                | 'Label' _ ( String | RichLabel ) ( __ ( SEText | SECommon ) )*
                    ( __ WithConstraint )?
                | 'Face' __ CardinalVector
                | 'Face' __ Expression
                | 'Gap' __ 'same'
                | 'Gap' __ CardinalVector SameLineSpace MoveDistance
                | 'Gap' __ CardinalVector
                | 'Gap' SameLineSpace MoveDistance
                | 'Gap'
                | 'Goto' SameLineSpace CardinalVector SameLineSpace MoveDistance
                | 'Goto' SameLineSpace CardinalVector
                | 'Goto' SameLineSpace Position
                | 'Goto' SameLineSpace PositionValue
                | 'Goto' SameLineSpace MoveDistance
                | 'Goto'
                | 'Skip' __ SkipArgs ;
DirectionalWaypoint
                = DirectionalComponent __ DirectionalComponent
                | DirectionalComponent ;
DirectionalComponent
                = CardinalVector __ LogicalORExpression ;

From/to positions, waypoints (directional and absolute), and line abbreviations.

FromPosition    = 'from' __ PositionValue
                | Position ;
LineOrAbbrev    = ( 'Line' | 'line' )
                | SELineEndings ;
MoveDistance    = AdditiveExpression ;
SELineDraw      = 'nodraw' !IdentifierPart ;
SELineEndings   = '--'
                | '~~'
                | SELineEndStart LinePath SELineEndEnd
                | SELineEndStart LinePath
                | LinePath SELineEndEnd ;
LinePath        = '-'
                | '~' ;
SELineEndEnd    = '>'
                | '|'
                | 'o' ;
SELineEndStart  = '<'
                | '|'
                | 'o' ;

Line labels with optional path position (at <percent>) and side (above/below/inside/outside) Both orderings are accepted: "label" above at 60% OR "label" at 60% above Returns { _lineLabel: { text, pathPercent, side } } which gets merged into shape args

SELineLabel     = RichLabel __ ( 'above' | 'below' | 'inside' | 'outside' ) ( __
                    'at' __ Number )?
                | RichLabel __ 'at' __ Number ( __ ( 'above' | 'below' |
                    'inside' | 'outside' ) )?
                | RichLabel
                | String __ ( 'above' | 'below' | 'inside' | 'outside' ) ( __
                    'at' __ Number )?
                | String __ 'at' __ Number ( __ ( 'above' | 'below' | 'inside' |
                    'outside' ) )?
                | String ;
SELineLength    = ( 'length' | 'len' ) __ Expression ;
SELineShape     = 'straight'
                | ( 'stepped' | 'step' )
                | ( 'smooth' | 'curve' | 'curved' ) ;
SERadius        = ( 'radius' | 'rad' | 'r' ) __ Expression ;

Width and height can't be assignments, and the "x" must be on the width's line and not start a name, so Box followed by lines like b = Box and xs = 100 aren't taken as a size. "2x3" still is.

SESize          = LogicalORExpression SameLineSpace? [x\u00D7] !IdentifierStart
                    _ LogicalORExpression
                | ( 'width' | 'wid' ) __ Expression
                | ( 'height' | 'ht' ) __ Expression ;
SETurn          = ( 'turn' __ )? ( 'ccw' | 'cw' )
                | 'turn' __ Expression ;
SkipArgs        = 'to' __ Expression
                | Position
                | 'x' __ Expression __ 'y' __ Expression
                | 'y' __ Expression __ 'x' __ Expression ;
ThenToPosition  = 'then' __ CardinalVector __ 'until' __ ( ( 'even' | 'level' )
                    __ 'with' __ )? PositionValue
                | 'then' __ DirectionalWaypoint
                | 'then' __ ( 'to' __ )? PositionValue ;
ToPosition      = 'to' __ PositionValue
                | Position ;

ShapeDefaultSetter

ShapeDefaultSetter
                = ShapeName SEClass '.' AttrName _ '=' _ Expression
                | ShapeName '.' AttrName _ '=' _ Expression ;

ShapeName

The shapes that can carry defaults. Aside is absent on purpose: it creates no shape, so Aside.fill = … would be stored under a name nothing reads.

ShapeName       = ( 'Arc' | 'arc' ) | ( 'Box' | 'box' ) | ( 'Circle' |
                    'circle' ) | ( 'Ellipse' | 'ellipse' ) | ( 'Oval' | 'oval' )
                    | ( 'Line' | 'line' ) | 'Label' | 'Skip' | 'Group'
                | 'Shape' !IdentifierPart ;

ShapeReference

"last box", "2nd circle", "second last line": a shape drawn earlier, counted in the order shapes were drawn. The ordinal words are not reserved: they only mean this when a shape name follows on the same line.

ShapeReference  = Ordinal SameLineSpace ( 'last' !IdentifierPart
                    SameLineSpace )? ShapeReferenceName
                | 'last' !IdentifierPart SameLineSpace ShapeReferenceName ;
Ordinal         = [0-9]+ ( 'st' | 'nd' | 'rd' | 'th' ) !IdentifierPart
                | 'first' !IdentifierPart
                | 'second' !IdentifierPart
                | 'third' !IdentifierPart
                | 'fourth' !IdentifierPart
                | 'fifth' !IdentifierPart
                | 'sixth' !IdentifierPart
                | 'seventh' !IdentifierPart
                | 'eighth' !IdentifierPart
                | 'ninth' !IdentifierPart
                | 'tenth' !IdentifierPart ;

The shapes that can be referred to: ShapeName without Skip, which draws nothing

ShapeReferenceName
                = ( 'Arc' | 'arc' ) | ( 'Box' | 'box' ) | ( 'Circle' |
                    'circle' ) | ( 'Ellipse' | 'ellipse' ) | ( 'Oval' | 'oval' )
                    | ( 'Line' | 'line' ) | 'Label' | 'Group'
                | 'Shape' !IdentifierPart ;

SimpleDecimalNumber

SimpleDecimalNumber
                = DecimalIntegerLiteral ( DecimalPoint DecimalDigit* )? ;

SizeUnit

SizeUnit        = ( 'Q' | 'ch' | 'cm' | 'em' | 'ex' | 'in' | 'lh' | 'mm' | 'pc'
                    | 'pt' | 'px' | 'rem' | 'vh' | 'vmax' | 'vmin' | 'vw' )
                    !IdentifierPart ;

String

Single/double quoted with escape sequences. Double-quoted support #{expr} interpolation. Triple-quoted (''' and """) for multi-line strings.

String          = '"""' TripleDoubleStringPart* '"""'
                | "'''" TripleSingleStringCharacter* "'''"
                | '"' DoubleStringPart* '"'
                | "'" SingleStringCharacter* "'" ;

Double-quoted strings support #{expr} interpolation and ## for literal #

DoubleStringPart
                = '#{' _ Expression _ '}'
                | '##'
                | DoubleStringCharacter+ ;
DoubleStringCharacter
                = !( '"' | '\\' | '#{' | '##' | LineTerminator ) .
                | '\\' EscapeSequence
                | LineContinuation ;
EscapeSequence  = CharacterEscapeSequence
                | '0' !DecimalDigit
                | HexEscapeSequence
                | UnicodeEscapeSequence ;
CharacterEscapeSequence
                = SingleEscapeCharacter
                | NonEscapeCharacter ;
NonEscapeCharacter
                = !( EscapeCharacter | LineTerminator ) . ;
EscapeCharacter
                = SingleEscapeCharacter
                | DecimalDigit
                | 'x'
                | 'u' ;
SingleEscapeCharacter
                = "'"
                | '"'
                | '\\'
                | 'b'
                | 'f'
                | 'n'
                | 'r'
                | 't'
                | 'v' ;
HexEscapeSequence
                = 'x' HexByte ;
UnicodeEscapeSequence
                = 'u' HexByte HexByte? ;
LineContinuation
                = '\\' LineTerminatorSequence ;
LineTerminatorSequence
                = '\n'
                | '\r\n'
                | '\r'
                | '\u2028'
                | '\u2029' ;
SingleStringCharacter
                = !( "'" | '\\' | LineTerminator ) .
                | '\\' EscapeSequence
                | LineContinuation ;

Triple-double-quoted strings also support interpolation

TripleDoubleStringPart
                = '#{' _ Expression _ '}'
                | '##'
                | TripleDoubleStringCharacter+ ;
TripleDoubleStringCharacter
                = !( '"""' | '#{' | '##' ) . ;
TripleSingleStringCharacter
                = !"'''" . ;

VariableValue

VariableValue   = Identifier ;

WithConstraint

WithConstraint  = 'with' __ SelfAnchor ( _ 'at' )? _ Expression
                | 'with' __ ( _ Cardinal )? ( _ 'at' )? _ Expression ;
SelfAnchor      = 'self.' Identifier Cardinal? ;

__

__              = WhiteSpace+ ;

Spacing

EOF             = !. ;
LineTerminator  = [\n\r\u2028\u2029] ;
WhiteSpace      = '\t'
                | '\v'
                | '\f'
                | ' '
                | '\u00A0'
                | '\uFEFF'
                | Zs
                | Comment
                | LineTerminator ;
Comment         = '//' ( !LineTerminator . )* ;
Zs              = ? Unicode Zs: Separator, Space ? ;

_ = optional whitespace, __ = required whitespace, SameLineSpace = no newlines. Comments are // to end-of-line.

_               = WhiteSpace* ;

Keywords

These are written as words in the rules above. Where a keyword has more than one spelling, any of them may be used.

Keyword Spellings
about about
above above
align align
Arc Arc, arc
Aside Aside
at at
attr attr
behind behind
below below
Box Box, box
by by
ccw ccw
Circle Circle, circle
close close
cw cw
dashed dashed
dotted dotted
draw draw
ease ease
Ellipse Ellipse, ellipse
else else
Face Face
false false
fill fill
fit fit
font font
font_family font_family
font_size font_size
font_stretch font_stretch
font_style font_style
font_variant font_variant
font_weight font_weight
from from
Gap Gap
Goto Goto
Group Group
height height, ht
id id
if if
inside inside
Label Label
length length, len
Line Line, line
line_height line_height
maxwidth maxwidth
move move
opacity opacity
outside outside
Oval Oval, oval
padding padding, pad
pause pause
radius radius, rad, r
rotate rotate
rotation rotation, rot
rx rx
ry ry
same same
set set
Skip Skip
smooth smooth, curve, curved
solid solid
stepped stepped, step
straight straight
stroke stroke
stroke_width stroke_width
take take
then then
thickness thickness, thick
to to
true true
turn turn
wait wait
width width, wid
with with
x x
y y