DocsLanguage GuideSyntax, Grammar & Operators
Language Guide

Syntax, Grammar & Operators

Lexical conventions, grammar specification, identifiers, operators, precedence, and comments.

EXPRSTMT

NextViper Formal Grammar Specification

This document defines the formal grammar of the NextViper programming language in Extended Backus-Naur Form (EBNF).


1

Lexical Grammar

1.1 Characters & Whitespace

ebnf
Whitespace  ::= [ 	
]+
Comment     ::= SingleLineComment | MultiLineComment
SingleLineComment ::= '//' [^
]*
MultiLineComment  ::= '/*' ( MultiLineComment | [^*] | '*'+ [^*/] )* '*'+ '/'

1.2 Identifiers & Keywords

ebnf
Identifier  ::= ( [a-zA-Z_] ) [a-zA-Z0-9_]*
Keyword     ::= 'let' | 'mut' | 'fn' | 'return' | 'if' | 'else' | 'while'
              | 'for' | 'in' | 'loop' | 'break' | 'continue' | 'true'
              | 'false' | 'null' | 'nil' | 'match' | 'struct' | 'type'
              | 'import' | 'and' | 'or' | 'not'

1.3 Numeric Literals

ebnf
IntegerLiteral ::= DecimalInteger | HexInteger | BinaryInteger | OctalInteger
DecimalInteger ::= [0-9] [0-9_]*
HexInteger     ::= '0' [xX] [0-9a-fA-F] [0-9a-fA-F_]*
BinaryInteger  ::= '0' [bB] [01] [01_]*
OctalInteger   ::= '0' [oO] [0-7] [0-7_]*

FloatLiteral   ::= [0-9] [0-9_]* '.' [0-9] [0-9_]* ([eE] [+-]? [0-9]+)?
                 | [0-9] [0-9_]* [eE] [+-]? [0-9]+
                 | '.' [0-9] [0-9_]*

1.4 String Literals

ebnf
StringLiteral  ::= '"' DoubleQuotedChar* '"'
                 | "'" SingleQuotedChar* "'"
                 | 'r"' [^"]* '"'
                 | '"""' MultiLineStringChar* '"""'

EscapeSequence ::= '' ( [nrt\'"0abfv] | 'x' HexDigit{2} | 'u{' HexDigit{1,6} '}' | 'u' HexDigit{4} )

2

Syntactic Grammar (EBNF)

2.1 Program & Top-Level

ebnf
Program    ::= Statement* EOF
Statement  ::= LetStatement
             | IfStatement
             | WhileStatement
             | ForInStatement
             | ReturnStatement
             | BreakStatement
             | ContinueStatement
             | BlockStatement
             | ExpressionStatement

2.2 Statements

ebnf
LetStatement      ::= 'let' 'mut'? Identifier (':' TypeAnnotation)? ('=' Expression)? ';'?
ExpressionStatement ::= Expression ';'?
BlockStatement    ::= '{' Statement* '}'

IfStatement       ::= 'if' Expression ( BlockStatement | ':' Statement+ )
                      ( 'else' ( IfStatement | BlockStatement | ':' Statement+ ) )?

WhileStatement    ::= 'while' Expression ( BlockStatement | ':' Statement+ )
ForInStatement    ::= 'for' Identifier 'in' Expression ( BlockStatement | ':' Statement+ )

ReturnStatement   ::= 'return' Expression? ';'?
BreakStatement    ::= 'break' ';'?
ContinueStatement ::= 'continue' ';'?

TypeAnnotation    ::= Identifier ('[' TypeAnnotation ']')?

2.3 Expressions & Precedence Hierarchy

The table below outlines expression operator precedence from lowest (binding least tightly) to highest (binding most tightly):

PrecedenceOperatorDescriptionAssociativity
1 (Lowest)`=`, `+=`, `-=`, `*=`, `/=`, `%=`AssignmentRight
2`>`PipelineLeft
3`or`, ``Logical ORLeft
4`and`, `&&`Logical ANDLeft
5`==`, `!=`EqualityLeft
6`<`, `<=`, `>`, `>=`RelationalLeft
7`..`, `..=`RangeNon-assoc
8`+`, `-`AdditiveLeft
9`*`, `/`, `%`MultiplicativeLeft
10`**`, `^`PowerRight
11`!`, `not`, `-` (unary)Unary prefixRight
12 (Highest)`()`, `[]`, `.`Call, Index, MemberLeft

2.4 Expression Rules

ebnf
Expression     ::= Assignment
Assignment     ::= ( Primary '.' Identifier | Primary '[' Expression ']' | Identifier )
                   ( '=' | '+=' | '-=' | '*=' | '/=' | '%=' ) Assignment
                 | Pipeline

Pipeline       ::= LogicalOr ( '|>' CallExpr )*
LogicalOr      ::= LogicalAnd ( ( 'or' | '||' ) LogicalAnd )*
LogicalAnd     ::= Equality ( ( 'and' | '&&' ) Equality )*
Equality       ::= Relational ( ( '==' | '!=' ) Relational )*
Relational     ::= Range ( ( '<' | '<=' | '>' | '>=' ) Range )*
Range          ::= Additive ( ( '..' | '..=' ) Additive )?
Additive       ::= Multiplicative ( ( '+' | '-' ) Multiplicative )*
Multiplicative ::= Power ( ( '*' | '/' | '%' ) Power )*
Power          ::= Unary ( ( '**' | '^' ) Power )*

Unary          ::= ( '!' | 'not' | '-' ) Unary
                 | Postfix

Postfix        ::= Primary ( '(' ArgumentList? ')' | '[' Expression ']' | '.' Identifier )*
ArgumentList   ::= Expression ( ',' Expression )* ','?

Primary        ::= IntegerLiteral
                 | FloatLiteral
                 | StringLiteral
                 | 'true' | 'false' | 'null' | 'nil'
                 | Identifier
                 | ArrayLiteral
                 | ObjectLiteral
                 | '(' Expression ')'

ArrayLiteral   ::= '[' ( Expression ( ',' Expression )* ','? )? ']'
ObjectLiteral  ::= '{' ( ObjectEntry ( ',' ObjectEntry )* ','? )? '}'
ObjectEntry    ::= ( StringLiteral | Identifier ) ':' Expression