program = _{SOI~statement* ~ EOI} statement = _{ emptyStatement |importStatement ~eos? |assignStatement~eos? |setStatement~eos? | controlFlow ~ eos? | definitions~ eos? | expr~eos? |data } controlFlow = _{ifStatement|forStatement} definitions = _{function_define|type_statement} /*====================================================================================================================*/ emptyStatement = {eos | Separate} eos = {Semicolon} comma_or_semi = _{Comma|Semicolon} /*====================================================================================================================*/ //FIXME: 修复 nested using importStatement = _{ Use ~use_alias|Use ~use_module_select|Use ~use_module_string } use_module_select = !{ moduleName ~ (( Dot | Proportion|With) ~ moduleSelect)? } use_module_except = !{ moduleName ~ (Slash|Except) ~ moduleSelect } use_module_string = !{ String ~ (With ~moduleSelect)?} moduleName = {namespace| maybeSymbol|SYMBOL} // MaybeSymbol as Namespace here moduleSelect = { Star | "{"~idTuples~ ( comma_or_semi? ~ idTuples)* ~comma_or_semi?~"}"} idTuples={use_alias|use_module_select} use_alias = !{ moduleName~As~SYMBOL} ///#C678DD Cover = ${Star} //!#C678DD: Use|Expose|With|Except|As Use =@{"use"} Expose =@{"expose"} With = @{"with"} Except = @{"except"} As =@{"as"} /*====================================================================================================================*/ block = !{ "{" ~ statement* ~ "}" | Begin ~ (!End~statement)*~ End |Begin ~ expr } suite= !{ "{" ~ statement* ~ "}" | Begin ~ statement* } //!#56B6C2: Begin|End Begin = @{"begin"|Colon} End =@{"end"|"\u{AD}"} /*====================================================================================================================*/ expr = !{ term ~ (!NEWLINE~ slice) | trinoculars | slot_function | lambda_function | term ~ (Infix ~ term)* } term = { Prefix* ~ node ~ Postfix* } node = _{ data| "(" ~ expr ~ ")" } condition = { node|expr} expressions = {"("~expr~ ( comma_or_semi? ~ expr)* ~comma_or_semi?~")"} trinoculars = _{if_trinocular} expr_or_block = {expr|"{"~statement+~"}"} /*====================================================================================================================*/ functionApply = { function_name ~ "(" ~ expr? ~")"| function_module? ~ function_name ~ "(" ~ expr? ~")" } methodApply = {expr ~Dot ~ function_name ~ "(" ~ expr? ~")"} ///#61AFEF function_name = {SYMBOL} function_module = {(namespace ~ Dot)? ~ (SYMBOL ~ Dot)*} /*====================================================================================================================*/ //!#C678DD: Pass|Return|Break Pass = @{"pass"} Return = @{"return"} Break = @{"break"} /*====================================================================================================================*/ ifStatement =_{if_nested_else|if_nested|if_single_else|if_single} if_single = { If~condition~ if_then_block} if_nested = { If~ condition~ if_then_suite ~ else_if_suite+}//FIXME: 无效语法 if_single_else = { If~condition~ if_then_suite ~ Else ~ if_then_block} if_nested_else = { If~ condition~ if_then_suite~ else_if_suite+ ~Else ~ if_then_block} if_trinocular = { term ~ Question ~ term ~ Colon ~ term | term ~ If ~ term ~ Else ~ term } else_if_suite = !{ elif ~condition~ if_nest_suite} else_if_block = !{ elif ~condition~ if_then_block} if_nest_suite = _{ "{" ~ statement+ ~ "}" | Begin? ~ (!if_forbidden~statement) | Begin? ~ (!if_forbidden~statement)+ ~ End? } if_then_suite = _{ "{" ~ statement+ ~ "}" | Begin? ~ (!if_forbidden~statement)+ ~ End? } if_then_block = _{ "{" ~ statement+ ~ "}" | Begin? ~ (!End~statement)+~ End | Begin? ~ (!End~expr) } elif = _{Ef|Else ~ If} if_forbidden = _{If|Else|elif} //!#C678DD: Ef|If|Else Ef = @{"ef"} If = @{"if"} Else = @{"else"} /*====================================================================================================================*/ switchStatement = {Switch} //!#C678DD: If|Else Switch =@{"switch"} Case =@{"case"} /*====================================================================================================================*/ forStatement = _{For ~forLoop|For ~forInLoop} forLoop = {expressions ~block} forInLoop = {SYMBOL~In~expr~block} //!#C678DD: For|In For = @{"for"} In = @{"in"} /*====================================================================================================================*/ /// #E06C75 type_expr = { type_term ~ (Infix ~ type_term)* | data | functionApply} /// #E06C75 type_term = { Prefix* ~ type_node ~ Postfix* } type_node = _{ "(" ~ type_expr ~ ")" } /// #E06C75 type_atom = ${type_symbol|Integer} /// #E06C75 type_symbol = ${Symbol} type_prefix = _{type_expr~ Symbol} type_suffix = _{"<:"~type_expr} type_statement = {Type~type_symbol~type_block} type_block = !{ "{" ~ type_expr ~ "}" |Begin ~ type_expr ~ End? } //!#C678DD: Type|To Type = @{"type"} /*====================================================================================================================*/ assignStatement = {Var ~ (!assign_pair ~ modifier)* ~assign_pair} assign_name = {SYMBOL~type_suffix?} assign_pair = {assign_name ~ block} setStatement = _{ SYMBOL ~Set ~expr } ///Orange modifier = ${Symbol} //!#C678DD: Val|Var Var = @{"var"} Val = @{"val"} /*====================================================================================================================*/ function_define = {Def ~ (!function_body ~modifier)*~ function_body~ type_suffix? ~ block } function_body = _{function_name ~ lambda_body |function_name ~ "(" ~ ")"} function_parameter = {type_expr~ function_symbol|function_symbol} function_symbol = {Symbol~ Star?} function_define_block = !{ "{" ~ statement* ~ "}" | Begin ~ (!End~statement)*~ End |Begin ~ expr } ///#C678DD Def = @{"def"} /*====================================================================================================================*/ slot_function = {term ~ Ampersand} lambda_function = {lambda_body~To~expr_or_block} lambda_body = _{"(" ~function_parameter ~ ( comma_or_semi? ~ function_parameter )* ~comma_or_semi?~ ")"} //!#C678DD: Ampersand ///#A6E22E SlotSymbol = @{Dollar~(SYMBOL|Integer)} SlotMark = ${Dollar} /*====================================================================================================================*/ traitStatement = { trait_head ~"{" ~ traitExpression* ~ "}" } trait_head = _{Trait ~ SYMBOL ~classExtend? ~classTrait?} traitExpression = {interfaceExpression | structureExpression} classExtend =${"classExtend"} classTrait = ${"classTrait"} /*====================================================================================================================*/ interfaceStatement = { interface_head~"{"~ interfaceExpression* ~"}" } interface_head = _{Interface ~SYMBOL~ classExtend? ~classTrait?} interfaceExpression= { interfaceFunction ~Colon ~type_expr~comma_or_semi?} interfaceFunction= { SYMBOL ~"(" ~interfaceParameters? ~")"~ nullable?} interfaceParameters= { type_expr ~SYMBOL ~ (Comma ~type_expr~ SYMBOL )*} nullable = ${"?"} /*====================================================================================================================*/ structure_statement = { structure_head~"{" ~structureExpression* ~"}" } structure_head = _{Structure ~SYMBOL ~classExtend? ~classTrait?} structureExpression= { SYMBOL~ nullable~ Colon~ type_expr ~comma_or_semi?} /*====================================================================================================================*/ enumerateStatement = { enumerate_head~"{" ~enumerateExpression* ~"}" } enumerate_head = _{Enumerate ~(Plus | Star)? ~SYMBOL ~classExtend? ~classTrait?} enumerateExpression= { SYMBOL ~comma_or_semi? | SYMBOL ~ Colon~ enumerateNumber~ comma_or_semi?} enumerateNumber= { Number | SYMBOL ~ Or ~SYMBOL} /*====================================================================================================================*/ ///#C678DD name_head = ${(Local| Class)?~Proportion} //!#C678DD: Trait|Interface|Structure|Enumerate Trait =@{"trait"} Interface =@{"interface"} Structure =@{"structure"} Enumerate = @{"enumerate"} Local =@{"local"} Class =@{"class"} /*====================================================================================================================*/ data = {dict|list|Null|Boolean|Byte|Number| String |Symbol|SlotSymbol} dict = { "{" ~key_value? ~(Comma ~key_value)* ~Comma? ~"}"} list ={ "[" ~element? ~(Comma ~element)* ~Comma? ~"]"} slice ={"[" ~index ~(Comma ~index)* ~Comma? ~"]"} index = { index_step|index_range|index_element} key_value =!{key_valid ~ Colon ~element} key_valid =!{Integer | SYMBOL | String} element =!{data | expr | statement} index_element = !{element} index_range = !{element? ~ Colon ~ element?} index_step = !{element? ~ Colon ~ element?~ Colon ~ element?} /*====================================================================================================================*/ /*====================================================================================================================*/ ///#D19A66 Null = @{"null"} /*====================================================================================================================*/ ///#D19A66 Boolean = !{ True | False } True = @{"true"} False =@{"false"} /*====================================================================================================================*/ ///#D19A66 Byte = ${Byte_BIN|Byte_OCT| Byte_HEX}//TODO: 广义 0 前导 Byte_BIN = ${ Zero ~ B ~ (Underline? ~ ASCII_BIN_DIGIT)+ } Byte_OCT = ${ Zero ~ O ~ (Underline? ~ ASCII_OCT_DIGIT)+ } Byte_HEX = ${ Zero ~ X ~ (Underline? ~ ASCII_HEX_DIGIT)+ } /*====================================================================================================================*/ //!#D19A66: Integer|Decimal|DecimalBad|Exponent|ComplexHandler Number = ${Exponent|Complex|Decimal|DecimalBad|Integer} Decimal = ${Integer ~Dot~ ASCII_DIGIT+} DecimalBad = ${Integer~Dot | Dot~ASCII_DIGIT+} Exponent = ${(Decimal|Integer) ~E~ (Decimal|Integer)} Complex = ${(Decimal|Integer)~ ComplexHandler} Integer = @{ Zero|ASCII_DIGIT ~ (Underline? ~ ASCII_NONZERO_DIGIT)* } ComplexHandler = @{SYMBOL} /*====================================================================================================================*/ ///#98C379 String = ${StringSingle|StringBlock|LiteralString| LiteralBlock}//TODO: 字符串表达式 StringSingle = ${ S2 ~ (!S2~ ANY)* ~ S2 } StringBlock = ${ S6 ~ (!S6~ ANY)* ~ S6 } LiteralString= ${ S1 ~ (!S1~ ANY)* ~ S1 } LiteralBlock = ${ S3 ~ (!S3~ ANY)* ~ S3 } /*====================================================================================================================*/ Symbol = ${maybeSymbol|mustSymbol| SYMBOL} mustSymbol = ${namespace~(Dot~SYMBOL)+} maybeSymbol = ${SYMBOL~ (Dot~SYMBOL)+} namespace = ${name_head? ~ SYMBOL ~ (Proportion ~ SYMBOL)+} ///#E5C07B // SYMBOL = @{NameStartCharacter~ NameCharacter*} NameCharacter = _{ASCII_DIGIT| NameStartCharacter} NameStartCharacter = _{Underline | ASCII_ALPHA} /*====================================================================================================================*/ // NEWLINE = @{"\r"~"\n"|"\r"|"\n"} WHITESPACE = _{NEWLINE|SPACE_SEPARATOR | "\t"} ///#7F848E COMMENT = ${Shebang|SingleLineComment| MultiLineComment} Shebang = ${"#!" ~ (!NEWLINE ~ ANY)*} SingleLineComment = _{ "///" ~ (!NEWLINE ~ ANY)*} MultiLineComment = _{ "%%%" ~ (MultiLineComment | !"%%%" ~ ANY)* ~ "%%%"} /*====================================================================================================================*/ Keywords = _{Val|Var|Def|Use} //TODO: 删除 Zero = _{"0"} E = _{"e"|"E"} X = _{"x"|"X"} O = _{"o"|"O"} B = _{"b"|"B"} //!#56B6C2: Infix Prefix = ${Plus|Minus|Increase|Decrease} Postfix = ${"!"|Ampersand} Infix = ${o_add|o_times} Set = @{"="} Or = @{"|"} LazyOr = @{"||"} Star =@{"*"} Slash =@{"/"} Proportion = @{"::"|"∷"}//U+2237 Comma =@{","|","} //U+FF0C Dot = @{"."} Separate = @{";;"} Semicolon = @{";" | ";"} //U+FF1B //!#56B6C2: Question|Colon Colon = @{":"|":"} //U+FF1A Question=@{"?"} Underline = @{"_"} Import =@{"<<<"|"⋘"} //U+22D8 Export =@{">>>"|"⋙"} //U+22D9 LeftShift =@{"<<"|"≪"} //U+226A RightShift =@{">>"|"≫"} //U+226B LessEqual =@{"<="} GraterEqual =@{">="} Less =@{"<"} Grater =@{">"} Equivalent =@{"==="} NotEquivalent =@{"=!="} Equal =@{"=="} NotEqual =@{"!="|"≠"} //U+2260 o_add = _{Plus|Minus} Plus =@{"+"} Minus =@{"-"} o_times = _{Multiply|CenterDot|Kronecker|TensorProduct|Divide|Quotient |Modulo } Multiply =@{Star | "×"} //U+00D7 CenterDot = @{"⋅"} //U+22C5 Kronecker =@{"⊗"} //U+2297 TensorProduct=@{"⊙"} //U+2299 Divide = @{Slash | "÷"} //U+00F7 Quotient = @{"//"} Modulo = @{"%"} Remainder =@{"⁒"} //U+2052 //⸓ o_power= _{ Power } Power = @{"^"} Surd = @{"√"} //U+221A Increase =@{"++"} Decrease =@{"--"} To =@{"->"} Elvis =@{":?"} Map =@ {"/@"} Quote =@{"`"} Acute =@{"´"} // U+00B4 S1 = _{"'"} S2 = _{"\""} S3 = _{"'''"} S6 = _{"\"\"\""} LogicOr =@{"||"|"∧"} //U+2227 LogicAnd =@{"&&"|"∨"} //U+2228 LogicNot =@{"¬"} //U+00AC Ellipsis =@{"..."|"…"} // U+2026 Dollar = @{"$"} Ampersand = @{"&"} /* LogicXor : '\u2295'; //U+2295 ⊕ MapAll : '//@'; Output : '%%'; /* =~ */ Concat : '~~'; Destruct : '~='; /* |&! */ DoubleBang : '!!'; BitNot : '!' | '\uFF01'; //U+FF01 ! /* $ @ */ Curry : '@@@'; Apply : '@@'; LetAssign : '@='; /* upper lower*/ */