CompilationUnit :-
	(ImportDeclaration)* (ComponentSetup)* TypeDeclaration

ImportDeclaration :-
	"import" ImportName ["." "*"] SemicolonOrNewlineOrError

ComponentSetup :-
	"[" <ID> ["(" Expression ("," Expression)* ")"] "]"

TypeDeclaration :-
	ClassDeclaration | InterfaceDeclaration

ClassDeclaration :-
	("dynamic" | "intrinsic")* "class" Name ["extends" Type] ["implements" TypeList]
	ClassBody [<SEMICOLON>]

ClassBody :-
	"{" (ClassBodyDeclaration)* "}"

ClassBodyDeclaration :-
	TypeBodyDeclatationErrorCatcher
	| GetterDeclaration
	| SetterDeclaration
	| MethodDeclaration
	| FieldDeclaration
	| ComponentSetup
	| <SEMICOLON>

InterfaceDeclaration :-
	"interface" Name ["extends" Type] "{" (MethodDeclaration)* "}" [<SEMICOLON>]

FieldDeclaration :-
	("public" | "private" | "static")* VariableDeclarator ("," VariableDelarator)* SemicolorOrNewlineOrError

VariableDeclarator :-
	<ID> [":" Type] ["=" VariableInitializer]

VariableInitializer :-
	Expression

MethodDeclaration :-
	("public" | "private" | "static")* "function" <ID> FormalParameters [":" ReturnType]
	(Block | SemicolonOrNewlineOrError)

GetterDeclaration :-
	("public" | "private" | "static")* "function" NonKeywordGet <ID> FormalParameters [":" ReturnType]
	(Block | SemicolonOrNewlineOrError)

SetterDeclaration :-
	("public" | "private" | "static")* "function" NonKeywordSet <ID> FormalParameters [":" ReturnType]
	(Block | SemicolonOrNewlineOrError)

FormalParameters :-
	"(" ("Void" | [FormalParameter ([","] FormalParameter)*]) ")"

FormalParameter :-
	<ID> [":" Type]

Type :-
	<ID> ("." <ID>)*

TypeList :-
	Type ("," Type)*

ReturnType :-
	("Void" | Type)

Name :-
	<ID> ("." <ID>)*

ImportName :-
	<ID> ("." <ID>)*

NameList :-
	Name ("," Name)*

Expression :-
	ConditionalExpression [AssignmentOperator Expression]

AssignmentOperator :-
	"=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | ">>>=" | "&=" | "^=" | "|="

ConditionalExpression :-
	ConditionalOrExpression ["?" Expression ":" ConditionalExpression

ConditionalOrExpression :-
	ConditionalAndExpression ("||" ConditionalAndExpression)*

ConditionalAndExpression :-
	InclusiveOrExpression ("&&" InclusiveOrExpression)*

InclusiveOrExpression :-
	ExclusiveOrExpression ("|" ExclusiveOrExpression)*

ExclusiveOrExpression :-
	AndExpression ("^" AndExpression)*

AndExpression :-
	EqualityExpression ("&" EqualityExpression)*

EqualityExpression :-
	InstanceOfExpression (("==" | "!=" | "===" | "!==") InstanceOfExpression)*

InstanceOfExpression :-
	RelationalExpression ["instanceof" PrimaryExpression]

RelationalExpression :-
	ShiftExpression (("<" | ">" | "<=" | ">=") ShiftExpression)*

ShiftExpression :-
	AdditiveExpression (("<<" | ">>" | ">>>") AdditiveExpression)*

AdditiveExpression :-
	MultiplicativeExpression (("+" | "-") MultiplicativeExpression)*

MultiplicativeExpression :-
	UnaryExpression (("*" | "/" | "%") UnaryExpression)*

UnaryExpression :-
	UnaryPlusMinus UnaryExpression
	| PreIncrementExpression
	| PreDecrementExpression
	| UnaryExpressionNotPlusMinus

PreIncrementExpression :-
	"++" PrimaryExpression

PreDecrementExpression :-
	"--" PrimaryExpression

UnaryExpressionNotPlusMinus :-
	UnaryNotPlusMinus UnaryExpression
	| PrimaryExpression [PostfixExpression]

UnaryNotPlusMinus :-
	"~" | "!"

UnaryPlusMinus :-
	"+" | "-"

PostfixExpression :-
	"++" | "--"

PrimaryExpression :-
	PrimaryPrefix (PrimarySuffix)*

PrimaryPrefix :-
	Literal
	| ConstantLiteral
	| BooleanLiteral
	| NullLiteral
	| UndefinedLiteral
	| "(" Expression ")"
	| AllocationExpression
	| ArrayInitialiser
	| ObjectInitialiser
	| AnonymousFunction
	| TypeOfNode
	| DeleteNode

TypeOfNode :-
	"typeOf" PrimaryExpression

DeleteNode :-
	"delete" PrimaryExpression

ArrayInitialiser :-
	"[" [Expression ("," Expression)*] "]"

ObjectInitialiser :-
	"{" [<ID> ":" Expression ("," <ID> ":" Expression)*] "}"

AnonymousFunction :-
	"function" FormalParameters [":" ReturnType] Block

PrimarySuffix :-
	ArrayElementAccess
	| PrimarySuffixMember
	| Arguments

ArrayElementAcces :-
	"[" Expression "]"

PrimarySuffixMember :-
	"." <ID>
	| "." PrimarySuffixMemberError

ConstantLiteral :-
	<INTEGER_LITERAL> | <FLOATING_POINT_LITERAL> | <CHARACTER_LITERAL> | <STRING_LITERAL>

Literal :-
	"this" | "super" | <ID>

BooleanLiteral :-
	"true" | "false"

NullLiteral :-
	"null"

UndefinedLiteral :-
	"undefined"

Arguments :-
	"(" [ArgumentList] ")"

ArgumentList :-
	Expression ("," Expression)*

AllocationExpression :-
	"new" PrimaryExpression

Statement :-
	LocalVariableDeclaration SemicolonOrNewlineOrError
	| "{" "}"
	| StatementExpression SemicolonOrNewlineOrCommaOrError
	| Block
	| EmptyStatement
	| SwitchStatement
	| IfStatement
	| WhileStatement
	| DoStatement
	| ForInStatement
	| ForStatement
	| BreakStatement
	| ContinueStatement
	| ReturnStatement
	| ThrowStatement
	| TryStatement
	| WithStatement

Block :-
	"{" [ExplicitConstructorInvocation] BlockStatement)* "}"

ExplicitCostructorInvocation :-
	"super" Arguments SemicolonOrNewlineOrError

BlockStatement :-
	Statement

LocalVariableDeclaration :-
	"var" VariableDeclarator ("," VariableDeclarator)*

EmptyStatement :-
	<SEMICOLON>

StatementExpression :-
	PreIncrementExpression
	| PreDecrementExpression
	| PrimaryExpression [PostfixExpression | AsignmentOperator Expression]

SwitchStatement :-
	"switch" "(" Expression ")" "{" (SwitchLabel (BlockStatement)* )* "}"

SwitchLabel :-
	"case" Expression ":"
	| "default" ":"

IfStatement :-
	"if" "(" Expression ")" Statement [ "else" Statement ]

WithStatement :-
	"with" "(" Expression ")" Statement

WhileStatement :-
	"while" "(" Expression ")" Statement

DoStatement :-
	"do" Statement "while" "(" Expression ")" SemicolorOrNewlineOrError

ForStatement :-
	"for" "(" [ForInit] <SEMICOLON> [ArgumentList] <SEMICOLON> [ForUpdate] ")" Statement

ForInit :-
	LocalVariableDeclaration
	| StatementExpressionList

StatementExpressionList :-
	StatementExpression ("," StatementExpression)*

ForUpdate :-
	StatementExpressionList

ForInStatement :-
	"for" "(" ["var"] <ID> [":" Type] "in" Expression ")" Statement

BreakStatement :-
	"break" SemicolonOrNewlineOrError

ContinueStatement :-
	"continue" SemicolonOrNewlineOrError

ReturnStatement :-
	"return" SemicolonOrNewlineOrError

ThrowStatement :-
	"throw" Expression SemicolonOrNewlineOrError

TryStatement :-
	"try" Block ("catch" "(" FormalParameter ")" Block)*
	[ "finally" Block ]