Nyx Formal Grammar Reference
Complete Extended Backus–Naur Form (EBNF) grammar specification for the Nyx Programming Language.
1. Lexical Grammar
IDENTIFIER ::= [a-zA-Z_][a-zA-Z0-9_]*
INTEGER_LITERAL ::= [0-9]+ | "0x" [0-9a-fA-F]+ | "0b" [01]+
FLOAT_LITERAL ::= [0-9]+ "." [0-9]+ ([eE][+-]?[0-9]+)?
STRING_LITERAL ::= '"' ( [^"\\] | '\\' . )* '"'
CHAR_LITERAL ::= "'" ( [^'\\] | '\\' . ) "'"
BOOLEAN_LITERAL ::= "true" | "false"
COMMENT ::= "//" [^\n]* | "/*" .*? "*/"
2. Program Structure
SourceFile ::= ModuleDecl? ImportDecl* TopLevelDecl*
ModuleDecl ::= "module" IDENTIFIER ( "." IDENTIFIER )* ";" ?
ImportDecl ::= "import" IDENTIFIER ( "." IDENTIFIER )* ( "as" IDENTIFIER )? ";" ?
TopLevelDecl ::= StructDecl
| EnumDecl
| FunctionDecl
| ImplBlock
| TraitDecl
| TypeAlias
3. Declarations
StructDecl ::= "pub"? "struct" IDENTIFIER GenericParams? "{" StructField* "}"
StructField ::= "pub"? IDENTIFIER ":" Type ( "," )?
EnumDecl ::= "pub"? "enum" IDENTIFIER GenericParams? "{" EnumVariant* "}"
EnumVariant ::= IDENTIFIER ( "(" TypeList ")" )? ( "," )?
FunctionDecl ::= "pub"? "async"? "fn" IDENTIFIER GenericParams?
"(" ParamList? ")" ( "->" Type )? Block
ParamList ::= Param ( "," Param )*
Param ::= "mut"? IDENTIFIER ":" Type
ImplBlock ::= "impl" GenericParams? IDENTIFIER ( "for" Type )? "{" FunctionDecl* "}"
4. Statements and Expressions
Statement ::= LetStmt
| AssignmentStmt
| ReturnStmt
| ExprStmt
LetStmt ::= "let" "mut"? IDENTIFIER ( ":" Type )? ( "=" Expression )? ";" ?
AssignmentStmt ::= LValue "=" Expression ";" ?
ReturnStmt ::= "return" Expression? ";" ?
Expression ::= IfExpr
| MatchExpr
| WhileExpr
| ForExpr
| Block
| BinaryExpr
| UnaryExpr
| CallExpr
| FieldAccessExpr
| MethodCallExpr
| StructInitExpr
| Literal
IfExpr ::= "if" Expression Block ( "else" "if" Expression Block )* ( "else" Block )?
WhileExpr ::= "while" Expression Block
ForExpr ::= "for" Pattern "in" Expression Block
MatchExpr ::= "match" Expression "{" MatchArm* "}"
MatchArm ::= Pattern ( "if" Expression )? "=>" ( Expression ","? | Block )
5. Type System
Type ::= PrimitiveType
| NominalType
| TupleType
| ReferenceType
| ArrayType
| FunctionType
PrimitiveType ::= "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64"
| "f32" | "f64" | "bool" | "char" | "String"
NominalType ::= IDENTIFIER ( "<" TypeList ">" )?
ReferenceType ::= "&" "mut"? ( "'" IDENTIFIER )? Type
TupleType ::= "(" Type ( "," Type )* ")"
ArrayType ::= "[" Type ( ";" INTEGER_LITERAL )? "]"
FunctionType ::= "fn" "(" TypeList? ")" "->" Type