shulkerscript-lang/grammar.md

2.0 KiB

Grammar of the ShulkerScript language

Table of contents

Program

Program: Namespace Declaration*;

Namespace

Namespace: 'namespace' StringLiteral;

Declaration

Declaration: FunctionDeclaration | Import;

Import

Import: 'from' StringLiteral 'import' Identifier;

FunctionDeclaration

Function:
    Annotation* 'pub'? 'fn' Identifier '(' ParameterList? ')' Block
    ;
ParameterList:
    Identifier (',' Identifier)* ','?  
    ;

Annotation

Annotation: '#[' Identifier ('=' StringLiteral)? ']';

Statement

Statement:
    Block
    | LiteralCommand
    | Conditional
    | Grouping
    | DocComment
    | Semicolon
    | Run
    ;

Block

Block: '{' Statement* '}';

Run

Run:
    'run' Expression ';'
    ;

Conditional

Conditional:
    'if' ParenthizedCondition Block ('else' Block)?
    ;

Condition

Condition:
    PrimaryCondition
    BinaryCondition
    ;

PrimaryCondition

PrimaryCondition:
    ConditionalPrefix
    | ParenthesizedCondition
    | StringLiteral
    ;

ConditionalPrefix

ConditionalPrefix:
    ConditionalPrefixOperator PrimaryCondition
    ;

ConditionalPrefixOperator

ConditionalPrefixOperator: '!';

BinaryCondition

BinaryCondition:
    Condition ConditionalBinaryOperator Condition
    ;

ConditionalBinaryOperator

ConditionalBinaryOperator:
    '&&'
    | '||'
    ;

ParenthizedCondition

ParenthizedCondition:
    '(' Condition ')'
    ;

Grouping

Grouping:
    'group' Block
    ;

Expression

Expression:
    Primary
    ;

Primary

Primary:
    FunctionCall
    ;

FunctionCall

FunctionCall:
    Identifier '(' (Expression (',' Expression)*)? ')'
    ;

LuaCode

LuaCode:
    'lua' '(' (Expression (',' Expression)*)? ')' '{' (.*?)* '}'
    ;