代码拉取完成,页面将自动刷新
// QuickScript 语法文件
// ANTLR 4.13.2
grammar QuickScriptFixed;
// 自定义令牌类型
CONST: 'const';
// ===== 解析器规则 =====
// 程序入口
program
: statement* EOF
;
// 语句
statement
: expressionStmt
| variableDeclaration
| constDeclaration
| ifStatement
| 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
;
// If 语句
ifStatement
: IF LeftParen expression RightParen statement (ELSE 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
;
// 变量声明简化版
varDecl
: type Identifier Assign expression
;
// ===== 表达式 =====
expression
: LogicalOrExpression
;
LogicalOrExpression
: LogicalAndExpression (LogicalOr LogicalAndExpression)*
;
LogicalAndExpression
: BitwiseOrExpression (LogicalAnd BitwiseOrExpression)*
;
BitwiseOrExpression
: BitwiseXorExpression (BitwiseOr BitwiseXorExpression)*
;
BitwiseXorExpression
: BitwiseAndExpression (BitwiseXor BitwiseAndExpression)*
;
BitwiseAndExpression
: EqualityExpression (BitwiseAnd EqualityExpression)*
;
EqualityExpression
: RelationalExpression (Equal RelationalExpression)*
;
RelationalExpression
: ShiftExpression (Relational ShiftExpression)*
;
ShiftExpression
: AdditiveExpression (Shift AdditiveExpression)*
;
AdditiveExpression
: MultiplicativeExpression (Add MultiplicativeExpression)*
;
MultiplicativeExpression
: PowerExpression (Mul PowerExpression)*
;
PowerExpression
: UnaryExpression (Power UnaryExpression)*
;
UnaryExpression
: Unary primary
| primary
;
// 一元运算符
Unary
: Plus | Minus | LogicalNot | BitwiseNot
;
// 二元运算符
LogicalOr: '||';
LogicalAnd: '&&';
BitwiseOr: '|';
BitwiseXor: '^';
BitwiseAnd: '&';
Equal: '==' | '!=';
Relational: '<' | '>' | '<=' | '>=';
Shift: '<<' | '>>';
Add: '+' | '-';
Mul: '*' | '/' | '%';
Power: '**';
// 字面量
literal
: IntegerLiteral
| FloatLiteral
| StringLiteral
| BooleanLiteral
| NullLiteral
;
// 类型
type
: INT | INT64 | UINT | UINT64 | FLOAT | DOUBLE | BOOL | STRING | CHAR | VOID
;
// ===== Lexer 规则 =====
// 字面量
IntegerLiteral: [0-9]+;
FloatLiteral: [0-9]+ '.' [0-9]+;
StringLiteral: '"' (~["\\\r\n] | '\\' .)* '"';
BooleanLiteral: TRUE | FALSE;
NullLiteral: NULL;
// 关键字(优先级最高)
IF: 'if';
ELSE: 'else';
WHILE: 'while';
DO: 'do';
FOR: 'for';
BREAK: 'break';
CONTINUE: 'continue';
RETURN: 'return';
VAR: 'var';
// 类型关键字
INT: 'int';
INT64: 'int64';
UINT: 'uint';
UINT64: 'uint64';
FLOAT: 'float';
DOUBLE: 'double';
BOOL: 'bool';
STRING: 'string';
CHAR: 'char';
VOID: 'void';
// 其他关键字
TRUE: 'true';
FALSE: 'false';
NULL: 'null';
// 标识符(必须在所有关键字之后)
Identifier
: [a-zA-Z_][a-zA-Z0-9_]*
;
// 运算符
Plus: '+';
Minus: '-';
Multiply: '*';
Divide: '/';
Modulo: '%';
Assign: '=';
LogicalNot: '!';
BitwiseNot: '~';
// 标点符号
Semicolon: ';';
LeftParen: '(';
RightParen: ')';
LeftBrace: '{';
RightBrace: '}';
// 空白符(跳过)
WS
: [ \t\r\n\u000C]+ -> skip
;
// 注释(跳过)
LINE_COMMENT
: '//' ~[\r\n]* -> skip
;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。