This is grammar of ng. It just shows the structure how ng structure itself, but not related to actual AST and parser implementation.
ng is combined with declarations, includes module declaration:
[Module]:
[ModuleDeclaration]
[UseDeclaration]
[ExportDeclaration]
[FunctionDeclaration]
[ValueDeclaration]
Module declaration is used to specify a module name for current compilation unit.
Module name is seperated by DOT (.
), moudle name can also be an string literal if
it is a keyword.
[ModuleDeclaration]:
module [ModuleName];
[ModuleName]:
[StringLiteral]
[Identifier]
[Identifier].[ModuleName]
[StringLiteral].[ModuleName]
Use declaration is to import symbols from module
[UseDeclaration]:
use [ModuleName];
use [ModuleName].*;
use [ModuleName].[Identifier];
Export declaration is to specify which symbols are exposed to other modules.
[ExportDeclaration]:
export [Identifier] ";"
export "{"[Identifier] ("," [Identifier])* "}"
Function declaration is to declare or define a function inside a module.
[FunctionDeclaration]:
fun [FunctionSignature]
fun [FunctionSignature] [FunctionBody]
[FunctionSignature]:
[FunctionName] "(" ([FunctionParameter] ("," [FunctionParameter])* )? ")" [FunctionReturnDeclaration]
[FunctionName]:
[Identifier]
[FunctionParameter]:
[Identifier] ":" [Type]
[FunctionBody]:
[Statement]
Value declaration is to define variables inside a module or a function
[ValueDeclaration]:
val [Identifier] (":" [Type])? "=" [Expression]
Statement is basic execution unit of ng, and it is assured that statement must execute in sequential order.
Value declaration is a special case because it is also treat like a statement inside function.
[Statement]:
[SimpleStatement]
[CompoundStatement]
[IfStatement]
[ReturnStatement]
[ValueDeclaration]
Simple statement is literally an expression with a semicolon(;
) at the end.
Compound statement is a couple of statements grouped in a curly-brace pair({}
)
[SimpleStatement]:
[Expression] ";"
[CompoundStatement]:
"{" [Statement]* "}"
if
and case
are basic conditional statements using conditional expression or pattern
matching.
[IfStatement]:
if "(" [Expression] ")" [Statement]
if "(" [Expression] ")" [Statement] else [Statement]
ng has few jump statement to implement advanced control flow:
return
continue
break
yield
(in design)[ReturnStatement]:
return [Expression] ";"
"=>" [Expression] ";"
Expression is used to descirbe the calculations and function calls in ng to perform actual program logic.
[Expression]:
[PrimaryExpression]
[FunCallExpression]
[IdAccessorExpression]
[BinaryExpression]
[PrimaryExpression]:
[Identifier]
[Literals]
[FunCallExpression]:
[Expression]"(" [Expression]* ")"
[IdAccessorExpression]:
[Expression].[Identifier]
[Expression]."(" [Expression] ")"
[BinaryExpression]:
[Expression] [Operator] [Expression]