代码拉取完成,页面将自动刷新
// QuickScript 语法文件
// ANTLR 4.13.2
grammar QuickScript;
// ===== 解析器规则 =====
// 程序入口
program
: statement* EOF
;
// 语句
statement
: expressionStmt
| variableDeclaration
| constDeclaration
| funcDeclaration
| ifStatement
| switchStatement
| whileStatement
| forStatement
| doStatement
| returnStatement
| breakStatement
| continueStatement
| blockStatement
| emptyStatement
;
// 表达式语句
expressionStmt
: expression Semicolon
;
// 变量声明
variableDeclaration
: type Identifier (Assign expression)? Semicolon
| VAR Identifier Assign expression Semicolon
;
// 常量声明
constDeclaration
: CONST type Identifier Assign expression Semicolon
;
// 函数声明
funcDeclaration
: PUBLIC? type Identifier LeftParen paramList? RightParen blockStatement
;
// 参数列表
paramList
: param (Comma param)*
;
// 单个参数
param
: type Identifier
;
// If 语句
ifStatement
: IF LeftParen expression RightParen statement (ELSE statement)?
;
// Switch 语句
switchStatement
: SWITCH LeftParen expression RightParen LeftBrace switchCase* RightBrace
;
switchCase
: (CASE literal | DEFAULT) ':' statement*
;
// While 语句
whileStatement
: WHILE LeftParen expression RightParen statement
;
// For 语句
forStatement
: FOR LeftParen (expression Semicolon | varDecl Semicolon) expression? Semicolon expression? RightParen statement
;
// Do 语句
doStatement
: DO statement WHILE LeftParen expression RightParen Semicolon
;
// Return 语句
returnStatement
: RETURN expression? Semicolon
;
// Break 语句
breakStatement
: BREAK Semicolon
;
// Continue 语句
continueStatement
: CONTINUE Semicolon
;
// 代码块
blockStatement
: LeftBrace statement* RightBrace
;
// 空语句
emptyStatement
: Semicolon
;
// 变量声明简化版(for 循环中使用)
varDecl
: type Identifier Assign expression
;
// ===== 表达式(从低到高优先级)=====
expression
: assignmentExpression
;
assignmentExpression
: Identifier Assign expression
| Identifier ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=') expression
| Identifier LeftBracket expression RightBracket Assign expression
| Identifier LeftBracket expression RightBracket ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=') expression
| conditionalExpression
;
conditionalExpression
: logicalOrExpression ('?' expression ':' conditionalExpression)?
;
logicalOrExpression
: logicalAndExpression ('||' logicalAndExpression)*
;
logicalAndExpression
: bitwiseOrExpression ('&&' bitwiseOrExpression)*
;
bitwiseOrExpression
: bitwiseXorExpression ('|' bitwiseXorExpression)*
;
bitwiseXorExpression
: bitwiseAndExpression ('^' bitwiseAndExpression)*
;
bitwiseAndExpression
: equalityExpression ('&' equalityExpression)*
;
equalityExpression
: relationalExpression (('==' | '!=') relationalExpression)*
;
relationalExpression
: shiftExpression (('<' | '>' | '<=' | '>=') shiftExpression)*
;
shiftExpression
: additiveExpression (('<<' | '>>') additiveExpression)*
;
additiveExpression
: multiplicativeExpression (('+' | '-') multiplicativeExpression)*
;
multiplicativeExpression
: unaryExpression (('*' | '/' | '%') unaryExpression)*
;
unaryExpression
: ('++' | '--') unaryExpression
| ('+' | '-' | '!' | '~') unaryExpression
| postfixExpression
;
postfixExpression
: primary (LeftBracket expression RightBracket)* ('++' | '--')?
;
primary
: literal
| Identifier LeftParen argList? RightParen
| Identifier
| NEW baseType LeftBracket expression RightBracket
| LeftParen type RightParen unaryExpression
| LeftParen expression RightParen
;
// 参数列表(调用时)
argList
: expression (Comma expression)*
;
// 字面量
literal
: IntegerLiteral
| FloatLiteral
| StringLiteral
| TRUE | FALSE
| NULL
;
// 类型
type
: baseType (LeftBracket RightBracket)?
;
baseType
: INT | INT64 | UINT | UINT64 | FLOAT | DOUBLE | BOOL | STRING | CHAR | VOID
;
// ===== Lexer 规则 =====
// 常量关键字
CONST: 'const';
// 关键字
IF: 'if';
ELSE: 'else';
WHILE: 'while';
DO: 'do';
FOR: 'for';
BREAK: 'break';
CONTINUE: 'continue';
RETURN: 'return';
VAR: 'var';
FUNC: 'func';
// 类型关键字
INT: 'int';
INT64: 'int64';
UINT: 'uint';
UINT64: 'uint64';
FLOAT: 'float';
DOUBLE: 'double';
BOOL: 'bool';
STRING: 'string';
CHAR: 'char';
VOID: 'void';
// 其他关键字
PUBLIC: 'public';
TRUE: 'true';
FALSE: 'false';
NULL: 'null';
NEW: 'new';
SWITCH: 'switch';
CASE: 'case';
DEFAULT: 'default';
// 字面量
IntegerLiteral
: '0' [xX] [0-9a-fA-F]+ // 十六进制: 0xFF, 0XFF
| '0' [bB] [01]+ // 二进制: 0b1010, 0B1010
| [0-9]+ // 十进制: 42
;
FloatLiteral
: [0-9]+ '.' [0-9]+ ([eE] [+-]? [0-9]+)? // 3.14, 1.5e10, 1.5e-3
| [0-9]+ [eE] [+-]? [0-9]+ // 1e10, 2e-5
;
StringLiteral: '"' (~["\\\r\n] | '\\' .)* '"';
// 标识符(必须在所有关键字之后)
Identifier
: [a-zA-Z_][a-zA-Z0-9_]*
;
// 赋值运算符
Assign: '=';
// 标点符号
Semicolon: ';';
LeftParen: '(';
RightParen: ')';
LeftBrace: '{';
RightBrace: '}';
LeftBracket: '[';
RightBracket: ']';
Comma: ',';
// 空白符(跳过)
WS
: [ \t\r\n\u000C]+ -> skip
;
// 注释(跳过)
LINE_COMMENT
: '//' ~[\r\n]* -> skip
;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。