Skip to content
Frank Wienberg edited this page Mar 14, 2014 · 2 revisions

Jangaroo Language: Syntax

The Jangaroo syntax is defined by a context-free grammar.

anonFunctionExpr ::=  
    "function" "(" parameters ")" [typeRelation] block;

annotationFields ::=  
    [annotationField {"," annotationField}];

annotationField ::=  
    (IDENTIFIER "=") expr;

arguments ::=  
    [exprOrObjectLiteral {"," exprOrObjectLiteral}];

arrayLiteral ::=  
    "[" arguments "]";

block ::=  
    "{" statements "}";

catches ::=  
    {"catch" "(" parameter ")" block};

classBody ::=  
    "{" {directive | memberDeclaration | staticInitializer} "}";

classDeclaration ::=  
    modifiers "class" IDENTIFIER  
    ["extends" type] ["implements" type {"," type}]
    classBody;

commaExpr ::=  
    expr {"," expr};

compilationUnit ::=  
    packageDeclaration  
    "{" directives compilationUnitDeclaration "}"

compilationUnitDeclaration ::=  
    classDeclaration  
  | memberDeclaration;

constOrVar ::=  
    "const"  
  | "var";

directives ::=  
    {directive};

directive ::=  
    "import" type ["." "*"]  
  | "[" IDENTIFIER ["(" annotationFields ")"] "]"  
  | "use" IDENTIFIER type  
  | ";";

expr ::=  
    INT_LITERAL  
  | FLOAT_LITERAL  
  | STRING_LITERAL  
  | REGEXP_LITERAL  
  | "true"  
  | "false"  
  | "null"  
  | arrayLiteral  
  | lvalue  
  | anonFunctionExpr  
  | "this"  
  | parenthesizedExpr  
  | "new" type ["(" arguments ")"]  
  | "delete" expr  
  | expr "as" type  
  | expr "is" expr  
  | PREFIX_OPERATOR expr  
  | expr POSTFIX_OPERATOR  
  | expr INFIX_OPERATOR expr  
  | expr "(" arguments ")"  
  | expr "?" exprOrObjectLiteral ":" exprOrObjectLiteral;

exprOrObjectLiteral ::=  
    expr  
  | objectLiteral  
  | namedFunctionExpr;

fieldDeclaration ::=  
    modifiers constOrVar identifierDeclaration  
    {"," identifierDeclaration };

identifierDeclaration ::=  
    IDENTIFIER [typeRelation] ["=" exprOrObjectLiteral]  


labelableStatement ::=  
    "if" parenthesizedExpr statement "else" statement  
  | "if" parenthesizedExpr statement  
  | "switch" parenthesizedExpr "{" {statementInSwitch} "}"  
  | "while" parenthesizedExpr statement  
  | "do" statement "while" parenthesizedExpr ";"  
  | "for" "(" [commaExpr] ";"  
    [commaExpr] ";" [commaExpr] ")" statement  
  | "for" "(" "var" identifierDeclaration {"," identifierDeclaration} ";"   
    [commaExpr] ";" [commaExpr] ")" statement  
  | "for" ["each"] "(" IDENTIFIER "in" expr ")" statement  
  | "for" ["each"] "(" "var" IDENTIFIER [typeRelation]  
    "in" expr ")" statement  
  | "try" block catches  
  | "try" block [catches] "finally" block  
  | namedFunctionExpr  
  | block;

lvalue ::=  
    namespacedIdentifier  
  | expr "." namespacedIdentifier  
  | expr "[" commaExpr"]"  
  | "super" "." namespacedIdentifier;

memberDeclaration ::=  
    fieldDeclaration ";"  
  | methodDeclaration;

methodDeclaration ::=  
    modifiers "function" ["get" | "set"] IDENTIFIER  
    "(" parameters ")" [typeRelation] optBody;

modifier ::=  
    "public"  
  | "protected"  
  | "private"  
  | "static"  
  | "abstract"  
  | "final"  
  | "override"  
  | "internal";

modifiers ::=  
   {modifier};

namedFunctionExpr ::=  
    "function" IDENTIFIER "(" parameters ")" [typeRelation]  
    block;

namespacedIdentifier ::=  
    [modifier "::"] IDENTIFIER;

objectField ::=  
    IDENTIFIER ":" exprOrObjectLiteral  
  | STRING_LITERAL ":" exprOrObjectLiteral  
  | INT_LITERAL ":" exprOrObjectLiteral;

objectFields ::=  
    [objectField {"," objectField}];

objectLiteral ::=  
    "{" objectFields "}";

optBody ::=  
    block  
  | ";";

packageDeclaration ::=  
    "package" [qualifiedIde];

parameter ::=  
    ["const"] IDENTIFIER [typeRelation] ["=" exprOrObjectLiteral];

parameters ::=  
    [parameter {"," parameter}]  
  | [parameter {"," parameter} ","] IDENTIFIER [typeRelation];

parenthesizedExpr ::=  
    "(" exprOrObjectLiteral ")";

qualifiedIde ::=  
    IDENTIFIER {"." IDENTIFIER};

statement ::=  
    ";"  
  | commaExpr ";"  
  | IDENTIFIER ":" labelableStatement  
  | variableDeclaration ";"  
  | "break" [IDENTIFIER] ";"  
  | "continue" [IDENTIFIER] ";"  
  | "return" [exprOrObjectLiteral] ";"  
  | "throw" commaExpr ";"  
  | "super" "(" arguments ")"  
  | labelableStatement;

statements ::= {statement};

statementInSwitch ::=  
    statement  
  | "case" expr ":"  
  | "default" ":";  
staticInitializer ::=  
    block;

type ::=  
    qualifiedIde  
  | "*"  
  | "void";

typeList ::=  
    type {"," typeList};

typeRelation ::=  
    ":" type;

variableDeclaration ::=  
    constOrVar identifierDeclaration  
    {"," identifierDeclaration};  

Syntax constructs that go beyond ordinary JavaScript are marked bold in the previous EBNF specification.

For the available operators as referenced by PREFIX_OPERATOR, POSTFIX_OPERATOR, and INFIX_OPERATOR, see the operators page.

Identifiers as referenced by IDENTIFIER can contain alphanumeric characters and the special characters _ and $. They must not start with a digit.

Integer literals must conform to the regular expression 0|[1-9][0-9]*|0x[0-9a-fA-F]*. Examples of integer literals are 0, 99999, or 0xFFFFFFFF.

Float literals must conform to the regular expression ([0-9]+\.[0-9]*|[0-9]+|\.[0-9]+)([eE][+-]?[0-9]+)?. Examples of float literals are 0, .9, or 1.4322e+7.

String literals are enclosed in either single quotes or double quotes. If the delimiting character occurs inside the string literal, it must be escaped with a backslash. Inside string literals, the character sequences b, f, n, r, t, ", ', and \ represent a backspace, a form feed, a newline, a return, a tab, a double quote, a single quote, and a backslash, respectively. \uXXXX where XXXX is a hexadecimal number represents a 16-bit Unicode character. \xXX where XX is a hexadecimal number represents an 8-bit character. String literals must not span multiple lines. Examples of string literals are "", 'nn', "I'm a string.", and '\u00FCxfc'.

Regexp literals are delimited by slashes. They must not span multiple lines.

At any point in the source code,

include "FILENAME"

instructs the compiler to include the indicated file into the parsed file at the point of the include.

Clone this wiki locally