SYNTAX

Within your BBLANG language you will find the following grammar syntax, grouping common resources.

grammar BBLANG;

/*
 * BBLANG ANTLR(v4) Grammar
 * @author Navneeth G <navneeth@backbench.io>
 * Copyright (2017) Backbench Internet Services Pvt. Ltd.
 */

moduleSource
    : modulefunctionDefinition*
    ;

moduleFunctionDefinition
    : Function name=(Id | PublicId) '(' params=parameterList? ')' body=statement
    | name=(Id | PublicId) '(' params=parameterList? ')' Arrow body=statement
    ;

functionDefinition
    : Function '(' params=parameterList? ')' body=statement
    | Function name=Id '(' params=parameterList? ')' body=statement
    | '(' params=parameterList? ')' Arrow body=statement
    | params=nakedParameterList Arrow body=statement
    | name=Id '(' params=parameterList? ')' Arrow body=statement
    ;

parameterList
    : parameter (',' parameter)*
    ;

parameter
    : Id
    | Id '=' expression
    | mapLiteral
    ;

nakedParameterList
    : nakedParameter (',' nakedParameter)*
    ;

nakedParameter
    : Id
    | mapLiteral
    ;

expression
    : functionDefinition
    | root=expression '[' index=sequenceExpression ']'
    | root=expression  '.' key=Id
    | target=expression '(' args=sequenceExpression? ')'
    | operand=expression operator=('++' | '--')
    | operator=('!' | '+' | '-') operand=expression
    | <assoc=right> left=expression '^' right=expression
    | left=expression operator=('*' | '/' | '%') right=expression
    | left=expression operator=('+' | '-') right=expression
    | left=expression operator=('<<' | '>>') right=expression
    | left=expression operator=('<' | '<=' | '>' | '>=') right=expression
    | left=expression operator=('==' | '!=' | Is) right=expression
    | left=expression operator=('&&' | And) right=expression
    | left=expression operator=('||' | Or) right=expression
    | <assoc=right> condition=expression '?' yes=expression ':' no=expression
    | <assoc=right> left=expression operator=('=' | '+=' | '-=' | '*=' | '/=') right=expression
    | '(' sequenceExpression ')'
    | spreadExpression
    | PublicId
    | Id
    | literal
    ;

sequenceExpression
    : expression (',' expression)*
    ;

spreadExpression
    : '...' expression
    ;

literal
    : Bital
    | Number
    | String
    | '[' sequenceExpression? ']'
    | mapLiteral
    ;

mapLiteral
    : '#'? '{' (mapElement (',' mapElement)*)? '}'
    ;

mapElement
    : key=Id ':' value=expression
    | key=String ':' value=expression
    | spreadExpression
    | Id
    ;

statement
    : '{' blockElement* '}'
    | ';'
    | expression
    | If condition=sequenceExpression Then? body=statement (Else elseBody=statement)?
    | Do body=statement While condition=sequenceExpression
    | While condition=sequenceExpression body=statement
    | For init=sequenceExpression? ';'
          condition=sequenceExpression? ';'
          update=sequenceExpression?
          body=statement
    | For '(' init=sequenceExpression? ';'
              condition=sequenceExpression? ';'
              update=sequenceExpression? ')'
              body=statement
    | For iterator=iteratorExpression In collection=sequenceExpression body=statement
    | For '(' iterator=iteratorExpression In collection=sequenceExpression ')' body=statement
    | Continue
    | Break
    | Switch predictate=sequenceExpression '{' caseClause* '}'
    | Return value=sequenceExpression
    | statement ';'+
    ;

blockElement
    : statement
    | sequenceExpression ';'*
    ;

iteratorExpression
    : Id
    | mapLiteral
    ;

caseClause
    : Case value=sequenceExpression body=statement
    | Default body=statement
    ;

// Whitespace

And
    : 'and'
    ;

Do
    : 'do'
    ;

If
    : 'if'
    ;

Is
    : 'is'
    ;

In
    : 'in'
    ;

Or
    : 'or'
    ;

For
    : 'for'
    ;

Break
    : 'break'
    ;

Case
    : 'case'
    ;

Else
    : 'else'
    ;

While
    : 'while'
    ;

Then
    : 'then'
    ;

Return
    : 'return'
    ;

Switch
    : 'switch'
    ;

Default
    : 'default'
    ;

Function
    : 'function'
    ;

Continue
    : 'continue'
    ;

Arrow
    : =>
    | ->
    ;

Betal
   : 'true'
   | 'false'
   | 'null'
   ;

Number
   : INT '.' INT EXP?
   | INT EXP?
   ;

String
   : '\'' (ESC1 | ~['\\])* '\''
   | '"' (ESC2 | ~["\\])* '"'
   | '@' Id
   ;

Id
   : [a-zA-Z_$][a-zA-Z0-9_$]*
   ;

PublicId
   : Id (':' Id)+
   ;

// Skips

Whitespace
   : [ \t]+
   -> skip
   ;

Newline
   : ( '\r' '\n'?
   | '\n'
   )
   -> skip
   ;

BlockComment
   : '/*' .*? '*/'
   -> skip
   ;

LineComment
   : '//' ~[\r\n]*
   -> skip
   ;

// Fragments

fragment INT
   : [0-9]+
   ;

fragment EXP
   : 'e' ('+' | '-')? INT
   | 'E' ('+' | '-')? INT
   ;

fragment ESC1
   : '\\' (['\\/bfnrt] | UNICODE)
   ;

fragment ESC2
   : '\\' (["\\/bfnrt] | UNICODE)
   ;

fragment UNICODE
   : 'u' HEX HEX HEX HEX
   ;

fragment HEX
   : [0-9a-fA-F]
   ;
		
hello(msg) => { return "hello " + msg }, exclaim(msg) => { return msg + "!" }, "foo,bar,baz".split(",").map(hello).map(exclaim)