The Freya Programming Language

Formal grammar

See also
NoteNote
This is the actual LALR(1) grammar used by the parser. Syntactic constructions accepted by this grammar may be rejected later by semantic analysis.

Program :
    Attributes UsingClauses Namespaces END .
    Attributes UsingClauses NamespaceSections END .

UsingClauses :
    UsingClauses USING UsingReferences ;
    ε

UsingReferences :
    UsingReferences , NonGenericTypeReference
    NonGenericTypeReference

Namespaces :
    Namespaces Namespace
    Namespace

Namespace :
    NAMESPACE NonGenericTypeReference ; UsingClauses NamespaceSections

NamespaceSections :
    NamespaceSections PUBLIC TypeDeclarations
    NamespaceSections PRIVATE TypeDeclarations
    NamespaceSections INTERNAL TypeDeclarations
    NamespaceSections IMPLEMENTATION FOR SimpleTypeReference IS MemberImplementations
    NamespaceSections IMPLEMENTATION MemberImplementations
    ε

TypeDeclarations :
    TypeDeclarations Attributes SimpleTypeReference = TypeModifiers TypeConstructor ;
    ε

TypeModifiers :
    TypeModifiers TypeModifiers
    ε

TypeModifiers :
    NEW
    ABSTRACT
    SEALED
    STATIC
    CONST
    PARTIAL

TypeConstructor :
    TypeConstr
    CLASS FormalGenerics Inheritance
    INTERFACE FormalGenerics Inheritance
    RECORD FormalGenerics Inheritance

TypeConstr :
    ( EnumValues ) EnumBase
    METHOD FormalGenerics FormalParameters OptionalType
    CLASS FormalGenerics Inheritance TypeSections Invariants TypeImplementation END
    RECORD FormalGenerics Inheritance TypeSections Invariants TypeImplementation END
    INTERFACE FormalGenerics Inheritance Members Invariants END

TypeImplementation :
    IMPLEMENTATION MemberImplementations
    ε

EnumBase :
    : NonGenericTypeReference
    ε

EnumValues :
    EnumValues , EnumValue
    EnumValue

EnumValue :
    identifier EnumInitializer

EnumInitializer :
    = Expression
    ε

FormalGenerics :
    [ FormalTypeList ]
    ε

FormalTypeList :
    FormalTypeList , FormalType
    FormalType

FormalType :
    FormalConstraint identifier
    FormalConstraint identifier ( Constraints )

FormalConstraint :
    CLASS
    RECORD
    ε

Constraints :
    Constraints , Constraint
    Constraint

Constraint :
    TypeReference
    NEW

Inheritance :
    ( TypeReferences )
    ε

TypeSections :
    TypeSections Visibility AccessList Members
    ε

Visibility :
    INTERNAL
    PRIVATE
    PROTECTED
    PUBLIC
    INTERNAL AND PROTECTED
    PROTECTED AND INTERNAL
    INTERNAL OR PROTECTED
    PROTECTED OR INTERNAL

AccessList :
    [ TypeReferences ]
    ε

Members :
    Members Attributes OptStatic Member
    ε

Member :
    CONST ConstantDefinitionList ;
    CONSTRUCTOR FormalParameters ConstructorInitializer ; Assertions OptionalBody
    EVENT identifier : TypeReference ; MemberModifier
    METHOD SymbolicOp FormalParameters : TypeReference ; Assertions OptionalBody
    METHOD SymbolicOp FormalParameters : TypeReference => Expression ; Assertions
    OPERATOR OverloadOp FormalParameters : TypeReference ; Assertions OptionalBody
    OPERATOR OverloadOp FormalParameters : TypeReference => Expression ; Assertions
    PROPERTY FieldNames FormalIndex OptionalType Initializer ; PropertyModifier Assertions OptionalBody
    PROPERTY FieldNames FormalIndex OptionalType => Expression ; MemberModifier Assertions
    FieldDeclaration
    METHOD identifier FormalGenerics FormalParameters OptionalType Extension ; MemberModifier Assertions OptionalBodyOrExtern
    METHOD identifier FormalGenerics FormalParameters OptionalType Extension => Expression ; MemberModifier Assertions
    ITERATOR identifier FormalGenerics FormalParameters OptionalType Extension ; MemberModifier Assertions OptionalBody
    identifier = TypeModifiers InnerType ;

OptionalBody :
    VarGroups BEGIN StatementList BodyPostconditions END ;
    BEGIN StatementList BodyPostconditions END ;
    ε

OptionalBodyOrExtern :
    VarGroups BEGIN StatementList BodyPostconditions END ;
    BEGIN StatementList BodyPostconditions END ;
    EXTERN ;
    ε

Extension :
    FOR identifier : TypeReference
    ε

ConstantDefinitionList :
    ConstantDefinitionList , ConstantDefinition
    ConstantDefinition

ConstantDefinition :
    identifier = Expression
    identifier : TypeReference = Expression

FieldDeclaration :
    FieldNames : TypeReference Initializer ; ReadModifier

InnerType :
    CLASS
    RECORD
    INTERFACE
    TypeConstr

MemberModifier :
    MethodModifier ;
    ε

PropertyModifier :
    MethodModifier ; READONLY ;
    READONLY ; MemberModifier
    MemberModifier

MethodModifier :
    VIRTUAL
    ABSTRACT
    OVERRIDE
    SEALED
    NEW
    NEW VIRTUAL
    NEW ABSTRACT
    VIRTUAL NEW
    ABSTRACT NEW

OverloadOp :
    SymbolicOp
    AND
    OR
    NOT
    TRUE
    FALSE
    DIV
    MOD
    SHL
    SHR
    XOR
    identifier

SymbolicOp :
    <>
    <=
    >=
    +
    -
    *
    /
    \
    %
    <<
    >>
    ^
    &
    ++
    --
    =
    <
    >

ReadModifier :
    READONLY ;
    ε

FormalParameters :
    ( ParameterList )
    ε

FormalIndex :
    [ ParameterList ]
    ( ParameterList )
    ε

ParameterList :
    ParameterList ; Parameter
    Parameter

Parameter :
    Attributes ParameterModifier IdentList : TypeReference

ParameterModifier :
    VAR
    OUT
    ε

IdentList :
    IdentList , identifier
    identifier

FieldNames :
    FieldNames , identifier
    identifier

TypeReferences :
    TypeReferences , TypeReference
    TypeReference

TypeReference :
    SimpleTypeReference ?
    SimpleTypeReference

SimpleTypeReference :
    SimpleTypeReference . identifier
    SimpleTypeReference [ TypeReferences ]
    SimpleTypeReference @ integer [ TypeReference ]
    identifier

OptionalType :
    : TypeReference
    ε

NonGenericTypeReference :
    NonGenericTypeReference . identifier
    identifier

Assertions :
    Preconditions Postconditions

Preconditions :
    REQUIRES AssertionList ;
    ε

Postconditions :
    ENSURES AssertionList ;
    ε

BodyPostconditions :
    ENSURES AssertionList OptSemicolon
    ε

Invariants :
    INVARIANT AssertionList OptSemicolon
    ε

OptSemicolon :
    ;
    ε

AssertionList :
    AssertionList , Assertion
    Assertion

Assertion :
    Expression FOR identifier AssertionName
    Expression AssertionName

AssertionName :
    RAISE stringliteral
    ε

MemberImplementations :
    MemberImplementations MemberImplementation
    ε

MemberImplementation :
    OptStatic METHOD MethodName FormalParameters OptionalType Extension ; MethodBodyExt
    OptStatic METHOD MethodName FormalParameters OptionalType Extension => Expression ;
    OptStatic ITERATOR MethodName FormalParameters : TypeReference Extension ; MethodBody
    OptStatic METHOD SymbolicOp FormalParameters : TypeReference ; MethodBody
    OptStatic METHOD SymbolicOp FormalParameters : TypeReference => Expression ;
    OptStatic OPERATOR OverloadOp FormalParameters : TypeReference ; MethodBody
    OptStatic OPERATOR OverloadOp FormalParameters : TypeReference => Expression ;
    INTERFACE Delegations ;
    PROPERTY SimpleTypeReference FormalParameters OptionalType ; MethodBody
    STATIC FieldDeclaration
    FieldDeclaration
    CONST ConstantDefinitionList ;
    STATIC CONSTRUCTOR ; MethodBody
    CLASS CONSTRUCTOR ; MethodBody
    CONSTRUCTOR FormalParameters ConstructorInitializer ; OptionalBody
    DESTRUCTOR ; MethodBody
    EVENT SimpleTypeReference FormalParameters ; MethodBody
    identifier = TypeModifiers InnerType ;

OptStatic :
    STATIC
    ε

Delegations :
    Delegations , TypeReference Delegation
    TypeReference Delegation

Delegation :
    IS identifier
    = Expression

ConstructorInitializer :
    : identifier OptArguments
    ε

MethodName :
    identifier
    MethodName . identifier
    MethodName [ ConstraintsOrTypes ]

ConstraintsOrTypes :
    ConstraintsOrTypes , ConstraintOrType
    ConstraintOrType

ConstraintOrType :
    CLASS identifier
    CLASS identifier ( Constraints )
    RECORD identifier
    RECORD identifier ( Constraints )
    identifier ( Constraints )
    TypeReference

MethodBody :
    VarGroups BEGIN StatementList BodyPostconditions END ;
    BEGIN StatementList BodyPostconditions END ;

MethodBodyExt :
    VarGroups BEGIN StatementList BodyPostconditions END ;
    BEGIN StatementList BodyPostconditions END ;
    EXTERN ;

VarGroups :
    VarGroups VarGroup
    VAR VarGroup

VarGroup :
    IdentList : TypeReference ;

VariableDeclaration :
    VAR identifier
    identifier : TypeReference

StatementList :
    StatementList ; LabeledStatement
    LabeledStatement

LabeledStatement :
    identifier : Statement
    Statement

Statement :
    SimpleStatement
    UnbalancedStatement

SimpleStatement :
    BasicExpression := Expression
    BasicExpression assign Expression
    BasicExpression ++
    BasicExpression --
    BasicExpression
    RAISE Expression
    RAISE
    GOTO identifier
    YIELD Expression
    BEGIN StatementList END
    REPEAT StatementList UNTIL Expression
    TRY StatementList FAULT StatementList END
    TRY StatementList ExceptionTraps FinallyClause END
    CASE Expression OF CaseList Otherwise END
    LOOP StatementList END
    VAR VarList
    CONST ConstantDefinitionList
    ε

VarList :
    VarList , InlineVar
    InlineVar

InlineVar :
    identifier : TypeReference := Expression
    identifier := Expression

CaseList :
    CaseList CaseLabels : Statement ;
    CaseLabels : Statement ;

CaseLabels :
    CaseLabels , CaseLabel
    CaseLabel

CaseLabel :
    Expression
    Expression .. Expression

Otherwise :
    ELSE StatementList
    ε

ExceptionTraps :
    ExceptionTraps ExceptionTrap
    ε

ExceptionTrap :
    EXCEPT StatementList
    EXCEPT ON identifier : TypeReference DO StatementList
    EXCEPT ON TypeReference DO StatementList

FinallyClause :
    FINALLY StatementList
    ε

UnbalancedStatement :
    WHILE Expression DO Statement
    USING UsingHeaders DO Statement
    FOR VariableDeclaration := Expression ToDown Expression DO Statement
    FOR VariableDeclaration IN CaseLabel DO Statement
    IF Expression THEN Statement
    IF Expression THEN BalancedStatement ELSE Statement
    LOCK Expression THEN Statement

BalancedStatement :
    SimpleStatement
    WHILE Expression DO BalancedStatement
    USING UsingHeaders DO BalancedStatement
    FOR VariableDeclaration := Expression ToDown Expression DO BalancedStatement
    FOR VariableDeclaration IN CaseLabel DO BalancedStatement
    IF Expression THEN BalancedStatement ELSE BalancedStatement
    LOCK Expression THEN BalancedStatement

ToDown :
    TO
    DOWNTO

UsingHeaders :
    UsingHeaders , UsingHeader
    UsingHeader

UsingHeader :
    VariableDeclaration := Expression
    Expression

Constant :
    integer
    real
    single
    stringliteral
    TRUE
    FALSE
    NIL

BasicExpression :
    Constant
    identifier
    ( Expression )
    BasicExpression . identifier
    BasicExpression ( Arguments )
    BasicExpression [ ItemList ]
    ArrayLiteral

PrimaryExpression :
    BasicExpression
    OLD BasicExpression
    NEW TypeReference OptInitParams OptList

UnaryExpression :
    PrimaryExpression
    + UnaryExpression
    - UnaryExpression
    NOT UnaryExpression
    ~ UnaryExpression

MultiplicativeExpression :
    MultiplicativeExpression * UnaryExpression
    MultiplicativeExpression / UnaryExpression
    MultiplicativeExpression \ UnaryExpression
    MultiplicativeExpression % UnaryExpression
    MultiplicativeExpression << UnaryExpression
    MultiplicativeExpression >> UnaryExpression
    MultiplicativeExpression ^ UnaryExpression
    MultiplicativeExpression & UnaryExpression
    MultiplicativeExpression DIV UnaryExpression
    MultiplicativeExpression MOD UnaryExpression
    MultiplicativeExpression SHL UnaryExpression
    MultiplicativeExpression SHR UnaryExpression
    MultiplicativeExpression XOR UnaryExpression
    UnaryExpression

AdditiveExpression :
    AdditiveExpression + MultiplicativeExpression
    AdditiveExpression - MultiplicativeExpression
    AdditiveExpression | MultiplicativeExpression
    MultiplicativeExpression

LogicalExpression :
    AdditiveExpression
    AdditiveExpression = AdditiveExpression
    AdditiveExpression < AdditiveExpression
    AdditiveExpression > AdditiveExpression
    AdditiveExpression <> AdditiveExpression
    AdditiveExpression <= AdditiveExpression
    AdditiveExpression >= AdditiveExpression
    AdditiveExpression IN AdditiveExpression
    AdditiveExpression NOT IN AdditiveExpression
    AdditiveExpression IS TypeReference
    AdditiveExpression IS NOT TypeReference
    AdditiveExpression AS TypeReference
    AdditiveExpression ?? LogicalExpression

Conjunction :
    Conjunction AND LogicalExpression
    LogicalExpression

Disjunction :
    Disjunction OR Conjunction
    Conjunction

ImplExpr :
    Disjunction
    Disjunction -> Disjunction
    Disjunction <-> Disjunction

Expression :
    ImplExpr
    IF ImplExpr THEN Expression ELSE Expression
    USING VariableList DO Expression

VariableList :
    VariableList , identifier := Expression
    identifier := Expression

ExpressionList :
    ExpressionList , RealParam
    RealParam

Arguments :
    ExpressionList
    ε

OptArguments :
    ( ExpressionList )
    ( )
    ε

RealParam :
    VAR BasicExpression
    OUT BasicExpression
    Expression

OptInitParams :
    ( InitParams )
    ( )
    ε

InitParams :
    InitParams , InitParam
    InitParam

InitParam :
    identifier := Expression
    Expression
    VAR BasicExpression
    OUT BasicExpression

ArrayLiteral :
    [ ItemList ]
    [ ]

ItemList :
    ItemList , Expression
    Expression

OptList :
    ! ArrayLiteral
    ε

Initializer :
    = Expression
    ε

Attributes :
    Attributes < AttributeList >
    ε

AttributeList :
    AttributeList , Attribute
    Attribute

Attribute :
    NonGenericTypeReference AttributeParameters
    identifier : NonGenericTypeReference AttributeParameters

AttributeParameters :
    ( AttributeArgumentList )
    ( )
    ε

AttributeArgumentList :
    AttributeArgumentList , AttributeArgument
    AttributeArgument

AttributeArgument :
    identifier := Expression
    Expression

See also

Home
The Freya Programming Language
Sharp Blade: the Integrated Development Environment