289 lines
6.2 KiB
Plaintext
289 lines
6.2 KiB
Plaintext
// Charter Language Grammar (Pest Parser)
|
|
// NAC原生智能合约语言语法定义
|
|
|
|
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
|
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* ~ "\n" | "/*" ~ (!"*/" ~ ANY)* ~ "*/" }
|
|
|
|
// ============================================================================
|
|
// 基础类型
|
|
// ============================================================================
|
|
|
|
// 标识符
|
|
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
|
|
|
// 字面量
|
|
integer = @{ ASCII_DIGIT+ }
|
|
hex_number = @{ "0x" ~ ASCII_HEX_DIGIT+ }
|
|
string_literal = @{ "\"" ~ (!"\"" ~ ANY)* ~ "\"" }
|
|
boolean = @{ "true" | "false" }
|
|
|
|
// GNACS编码 (48位十六进制)
|
|
gnacs_code = @{ "0x" ~ ASCII_HEX_DIGIT{12} }
|
|
|
|
// DID标识符
|
|
did = @{ "did:nac:" ~ identifier ~ ":" ~ identifier ~ ":" ~ (ASCII_ALPHANUMERIC | "_")+ }
|
|
|
|
// ============================================================================
|
|
// 类型系统
|
|
// ============================================================================
|
|
|
|
primitive_type = {
|
|
"uint8" | "uint16" | "uint32" | "uint64" | "uint128" | "uint256" |
|
|
"int8" | "int16" | "int32" | "int64" | "int128" | "int256" |
|
|
"bool" | "string" | "bytes" | "address" | "hash" | "timestamp"
|
|
}
|
|
|
|
nac_type = {
|
|
"DID" | "GNACSCode" | "ConstitutionalReceipt" | "AssetInstance" |
|
|
"ACC20" | "ACC721" | "ACC1155" | "ACCRWA"
|
|
}
|
|
|
|
array_type = { (primitive_type | nac_type) ~ "[" ~ integer? ~ "]" }
|
|
|
|
type_annotation = { primitive_type | nac_type | array_type }
|
|
|
|
// ============================================================================
|
|
// 资产定义
|
|
// ============================================================================
|
|
|
|
asset_definition = {
|
|
"asset" ~ identifier ~ "{" ~
|
|
gnacs_declaration ~
|
|
sovereignty_declaration? ~
|
|
asset_fields ~
|
|
asset_methods? ~
|
|
"}"
|
|
}
|
|
|
|
gnacs_declaration = {
|
|
"gnacs" ~ ":" ~ gnacs_code ~ ";"
|
|
}
|
|
|
|
sovereignty_declaration = {
|
|
"sovereignty" ~ ":" ~ sovereignty_type ~ ";"
|
|
}
|
|
|
|
sovereignty_type = {
|
|
"A0" | "C0" | "C1" | "C2" | "D0" | "D1" | "D2"
|
|
}
|
|
|
|
asset_fields = {
|
|
(field_declaration ~ ";")*
|
|
}
|
|
|
|
field_declaration = {
|
|
identifier ~ ":" ~ type_annotation
|
|
}
|
|
|
|
asset_methods = {
|
|
(method_declaration)*
|
|
}
|
|
|
|
// ============================================================================
|
|
// 合约定义
|
|
// ============================================================================
|
|
|
|
contract_definition = {
|
|
"contract" ~ identifier ~ "{" ~
|
|
contract_fields ~
|
|
contract_methods ~
|
|
"}"
|
|
}
|
|
|
|
contract_fields = {
|
|
(field_declaration ~ ";")*
|
|
}
|
|
|
|
contract_methods = {
|
|
(method_declaration)*
|
|
}
|
|
|
|
method_declaration = {
|
|
method_modifiers? ~
|
|
"fn" ~ identifier ~ "(" ~ parameter_list? ~ ")" ~
|
|
("->" ~ type_annotation)? ~
|
|
requires_clause? ~
|
|
ensures_clause? ~
|
|
method_body
|
|
}
|
|
|
|
method_modifiers = {
|
|
("public" | "private" | "internal" | "payable" | "view" | "pure")+
|
|
}
|
|
|
|
parameter_list = {
|
|
parameter ~ ("," ~ parameter)*
|
|
}
|
|
|
|
parameter = {
|
|
identifier ~ ":" ~ type_annotation
|
|
}
|
|
|
|
// ============================================================================
|
|
// 宪法约束(前置/后置条件)
|
|
// ============================================================================
|
|
|
|
requires_clause = {
|
|
"requires" ~ "{" ~ expression_list ~ "}"
|
|
}
|
|
|
|
ensures_clause = {
|
|
"ensures" ~ "{" ~ expression_list ~ "}"
|
|
}
|
|
|
|
expression_list = {
|
|
(expression ~ ";")*
|
|
}
|
|
|
|
// ============================================================================
|
|
// 方法体
|
|
// ============================================================================
|
|
|
|
method_body = {
|
|
"{" ~ statement* ~ "}"
|
|
}
|
|
|
|
statement = {
|
|
let_statement |
|
|
assign_statement |
|
|
if_statement |
|
|
for_statement |
|
|
while_statement |
|
|
return_statement |
|
|
emit_statement |
|
|
cr_statement |
|
|
expression_statement
|
|
}
|
|
|
|
let_statement = {
|
|
"let" ~ identifier ~ (":" ~ type_annotation)? ~ "=" ~ expression ~ ";"
|
|
}
|
|
|
|
assign_statement = {
|
|
identifier ~ "=" ~ expression ~ ";"
|
|
}
|
|
|
|
if_statement = {
|
|
"if" ~ expression ~ method_body ~ ("else" ~ method_body)?
|
|
}
|
|
|
|
for_statement = {
|
|
"for" ~ identifier ~ "in" ~ expression ~ method_body
|
|
}
|
|
|
|
while_statement = {
|
|
"while" ~ expression ~ method_body
|
|
}
|
|
|
|
return_statement = {
|
|
"return" ~ expression? ~ ";"
|
|
}
|
|
|
|
emit_statement = {
|
|
"emit" ~ identifier ~ "(" ~ argument_list? ~ ")" ~ ";"
|
|
}
|
|
|
|
// 宪法收据相关语句
|
|
cr_statement = {
|
|
"require_cr" ~ "(" ~ expression ~ ")" ~ ";" |
|
|
"verify_cr" ~ "(" ~ expression ~ ")" ~ ";"
|
|
}
|
|
|
|
expression_statement = {
|
|
expression ~ ";"
|
|
}
|
|
|
|
// ============================================================================
|
|
// 表达式
|
|
// ============================================================================
|
|
|
|
expression = {
|
|
logical_or_expr
|
|
}
|
|
|
|
logical_or_expr = {
|
|
logical_and_expr ~ ("||" ~ logical_and_expr)*
|
|
}
|
|
|
|
logical_and_expr = {
|
|
equality_expr ~ ("&&" ~ equality_expr)*
|
|
}
|
|
|
|
equality_expr = {
|
|
relational_expr ~ (("==" | "!=") ~ relational_expr)*
|
|
}
|
|
|
|
relational_expr = {
|
|
additive_expr ~ (("<" | ">" | "<=" | ">=") ~ additive_expr)*
|
|
}
|
|
|
|
additive_expr = {
|
|
multiplicative_expr ~ (("+" | "-") ~ multiplicative_expr)*
|
|
}
|
|
|
|
multiplicative_expr = {
|
|
unary_expr ~ (("*" | "/" | "%") ~ unary_expr)*
|
|
}
|
|
|
|
unary_expr = {
|
|
("!" | "-") ~ unary_expr |
|
|
primary_expr
|
|
}
|
|
|
|
primary_expr = {
|
|
integer |
|
|
hex_number |
|
|
string_literal |
|
|
boolean |
|
|
gnacs_code |
|
|
did |
|
|
identifier |
|
|
function_call |
|
|
member_access |
|
|
array_access |
|
|
"(" ~ expression ~ ")"
|
|
}
|
|
|
|
function_call = {
|
|
identifier ~ "(" ~ argument_list? ~ ")"
|
|
}
|
|
|
|
argument_list = {
|
|
expression ~ ("," ~ expression)*
|
|
}
|
|
|
|
member_access = {
|
|
identifier ~ ("." ~ identifier)+
|
|
}
|
|
|
|
array_access = {
|
|
identifier ~ "[" ~ expression ~ "]"
|
|
}
|
|
|
|
// ============================================================================
|
|
// 顶层定义
|
|
// ============================================================================
|
|
|
|
module_definition = {
|
|
"module" ~ identifier ~ ";"
|
|
}
|
|
|
|
import_statement = {
|
|
"import" ~ string_literal ~ ";"
|
|
}
|
|
|
|
top_level_item = {
|
|
module_definition |
|
|
import_statement |
|
|
asset_definition |
|
|
contract_definition
|
|
}
|
|
|
|
// ============================================================================
|
|
// 程序入口
|
|
// ============================================================================
|
|
|
|
program = {
|
|
SOI ~ top_level_item* ~ EOI
|
|
}
|