Tea-文法
1 词法文法(Lexical Grammar)
Input:
InputSectionopt
InputSection:
InputSectionPart
InputSection InputSectionPart
InputSectionPart:
InputElementsopt NewLine
PpDirective
InputElements:
InputElement
InputElements InputElement
InputElement:
Whitespace
Comment
Token
1.1 行结束符(line Terminators)
NewLine:
Carriage return character (U+000D)
Line feed character (U+000A)
Carriage return character (U+000D) followed by line feed character (U+000A)
Next line character (U+2085)
Line separator character (U+2028)
Paragraph separator character (U+2029)
1.2 注释(Comments)
Comment:
SingleLineComment
DelimitedComment
SingleLineComment:
// InputCharactersopt
InputCharacters:
InputCharacter
InputCharacters InputCharacter
InputCharacter:
Any unicode character except a NewLineCharacter
NewLineCharacter:
Carriage return character (U+000D)
Line feed character (U+000A)
Next line character (U+0085)
Line separator character (U+2028)
Paragraph separator character (U+2029)
DelimitedComment:
/ DelimitedCommentTextopt Asterisks* /
DelimitedCommentText:
DelimitedCommentSection
DelimitedCommentText DelimitedCommentSection
DelimitedCommentSection:
/
Asterisksopt NotSlashOrAsterisk
Asterisks:
Asterisks
NotSlashOrAsterisk:
Any unicode character except / or *
1.3 空白(Whitespaces)
Whitespace:
WhitespaceCharacters
WhitespaceCharacters:
WhitespaceCharacter
WhitespaceCharacters WhitespaceCharacter
WhitespaceCharacter:
Any character with unicode class Zs
Horizontal tab character (U+0009)
Vertical tab character (U+000B)
Form feed character (U+000C)
1.4 标记(Tokens)
Token:
Identifier
Keyword
IntegerLiteral
RealLiteral
CharacterLiteral
StringLiteral
OperatorOrPunctuator
1.5 Unicode 字符转义序列(Unicode escape Sequences)
UnicodeEscapeSequence:
\u HexDigit HexDigit HexDigit HexDigit
\U HexDigit HexDigit HexDigit HexDigit
1.6 Identifiers
Identifier:
AvailableIdentifier
$ IdentifierPartCharacters
$ StringLiteral
AvailableIdentifier:
An IdentifierOrKeyword that is not a Keyword
IdentifierOrKeyword:
IdentifierStartCharacter IdentifierPartCharactersopt
IdentifierStartCharacter:
LetterCharacter
_ (The underscore character U+005F)
IdentifierPartCharacters:
IdentifierPartCharacter
IdentifierPartCharacters IdentifierPartCharacter
IdentifierPartCharacter:
LetterCharacter
DecimalDigitCharacter
ConnectingCharacter
CombiningCharacter
FormattingCharacter
LetterCharacter:
A unicode character of classes lu, Ll, Lt, Lm, Lo, Or Nl
A UnicodeEscapeSequence representing a character of classes lu, Ll, Lt, Lm, Lo, Or Nl
CombiningCharacter:
A unicode character of classes Mn or Mc
A UnicodeEscapeSequence representing a character of classes Mn or Mc
DecimalDigitCharacter:
A unicode character of the class Nd
A UnicodeEscapeSequence representing a character of the class Nd
ConnectingCharacter:
A unicode character of the class Pc
A UnicodeEscapeSequence representing a character of the class Pc
FormattingCharacter:
A unicode character of the class Cf
A UnicodeEscapeSequence representing a character of the class Cf
1.7 关键字(Keywords)
Keyword: one of
abstract as assert await base
bool break byte callee case
catch char checked class const
continue do double dynamic else
enum extend extern false final
finally float for goto if
int interface internal is lock
long loop namespace new null
object once or out override
params partial private protected public
readonly ref return short static
string struct switch template this
throw true try typeof uint
ulong unchecked until ushort using
var virtual void volatile while
with yield
1.8 文本(Literals)
Literal:
BooleanLiteral
IntegerLiteral
RealLiteral
StringLiteral
NullLiteral
BooleanLiteral:
true
false
IntegerLiteral:
DecimalIntegerLiteral
HexadecimalIntegerLiteral
DecimalIntegerLiteral:
DecimalDigits
DecimalDigits:
DecimalDigit
DecimalDigits DecimalDigit
DecimalDigit: one of
0 1 2 3 4 5 6 7 8 9
HexadecimalIntegerLiteral:
0 x HexDigits
0 X HexDigits
HexDigits:
HexDigit
HexDigits HexDigit
HexDigit: one of
0 1 2 3 4 5 6 7 8 9 A B C D e F a b c d e f
RealLiteral:
DecimalDigits
. DecimalDigits
SingleCharacter:
Any character except " (U+0027), \ (U+005C), and NewLineCharacter
SimpleEscapeSequence: one of
\' \" \ \0 \a \b \f \n \r \t \v $ - \s
StringLiteral:
VerbatimStringLiteral
RegularStringLiteral
VerbatimStringLiteral:
' VerbatimStringLiteralCharactersopt '
''' VerbatimStringLiteralCharactersopt '''
VerbatimStringLiteralCharacters:
VerbatimStringLiteralCharacter
VerbatimStringLiteralCharacters VerbatimStringLiteralCharacter
VerbatimStringLiteralCharacter:
SingleVerbatimStringLiteralCharacter
''
SingleVerbatimStringLiteralCharacter:
Any character except ' and NewLineCharacter
RegularStringLiteral:
" RegularStringLiteralCharactersopt "
RegularStringLiteralCharacters:
RegularStringLiteralCharacter
RegularStringLiteralCharacters RegularStringLiteralCharacter
RegularStringLiteralCharacter:
SingleRegularStringLiteralCharacter
SimpleEscapeSequence
UnicodeEscapeSequence
SingleRegularStringLiteralCharacter:
Any character except " (U+0022), \ (U+005C), and NewLineCharacter
NullLiteral:
null
1.9 运算符和标点符号(Operators and Punctuators)
OperatorOrPunctuator: one of
{ } [ ] ( ) . , : ;
+ - / ^ % & | ~ +~ -~ ~+ ~-
= < > ? ++ -- && || !
== != <= >= += -= = /= %= &= |= &&= ||=
=> := ->
1.10 预处理指令(PreProcessing Directives)
PpDirective:
PpDefinition
PpConditional
PpLine
PpDiagnostic
PpRegion
PpPragma
PpToDo
PpToFix
PpComment
ConditionalSymbol:
IdentifierOrKeyword
PpDefinition:
Whitespaceopt # Whitespaceopt define Whitespace ConditionalSymbol PpNewLine
Whitespaceopt # Whitespaceopt undef Whitespace ConditionalSymbol PpNewLine
PpNewLine:
Whitespaceopt SingleLineCommentopt NewLine
PpConditional:
PpIfSection PpElseSectionopt PpEndif
PpIfSection:
Whitespaceopt # Whitespaceopt ifdef Whitespace ConditionalSymbol PpNewLine ConditionalSectionopt
Whitespaceopt # Whitespaceopt ifndef Whitespace ConditionalSymbol PpNewLine ConditionalSectionopt
PpElseSection:
Whitespaceopt # Whitespaceopt else PpNewLine ConditionalSectionopt
PpEndif:
Whitespaceopt # Whitespaceopt endif PpNewLine
ConditionalSection:
InputSection
SkippedSection
SkippedSection:
SkippedSectionPart
SkippedSection SkippedSectionPart
SkippedSectionPart:
SkippedCharactersopt NewLine
PpDirective
SkippedCharacters:
Whitespaceopt NotNumberSign InputCharactersopt
NotNumberSign:
Any input-character except #
PpDiagnostic:
Whitespaceopt # Whitespaceopt error PpMessage
Whitespaceopt # Whitespaceopt warning PpMessage
PpMessage:
NewLine
Whitespace InputCharactersopt NewLine
PpLine:
Whitespaceopt # Whitespaceopt line Whitespace LineIndicator Whitespaceopt NewLine
LineIndicator:
DecimalDigits Whitespace , Whitespace FileName
DecimalDigits
hidden
default
FileName:
FileNameCharacters
FileNameCharacters:
FileNameCharacter
FileNameCharacters FileNameCharacter
FileNameCharacter:
Any character except "(U+0022) and NewLineCharacter*
PpRegion:
PpEndRegion
PpStartRegion InputSectionopt
PpStartRegion:
Whitespaceopt # Whitespaceopt region PpMessage
PpEndRegion:
Whitespaceopt # Whitespaceopt endregion PpMessage
PpPragma:
Whitespaceopt # Whitespaceopt pragma PpPragmaText
PpPragmaText:
NewLine
Whitespace InputCharactersopt NewLine
PpToDo:
Whitespaceopt # Whitespaceopt todo PpMessage
PpToFix:**:
Whitespaceopt # Whitespaceopt tofix PpMessage*
PpComment:**:
Whitespaceopt # Whitespaceopt # PpMessage
Whitespaceopt # Whitespaceopt ! PpMessage*
2 句法文法(Syntactic grammar)
2.1 基本概念(Basic concepts)
SourceUnit:
UsingDirectiveOrMemberDefinitionListopt
UsingDirectiveOrMemberDefinitionList:
UsingDirectiveOrMemberDefinition
UsingDirectiveOrMemberDefinitionList UsingDirectiveOrMemberDefinition
UsingDirectiveOrMemberDefinition:
MemberDefinition
UsingDirective
2.2 类型(Types)
Type:
PredefinedType
TypeName
PredefinedType:
bool
byte
char
double
dynamic
float
int
long
object
short
string
uint
ulong
ushort
var
void
TypeName:
Identifier GenericTypeArgumentListopt
TypeName . Identifier GenericTypeArgumentListopt
GenericTypeArgumentList:
< TypeList >
TypeList:
Type
TypeList , Type
2.3 变量(Variables)
VariableReference:
Expression
2.4 表达式(Expressions)
Expression:
ChainExpression
ChainExpression:
AssignmentExpression
ChainExpression .. AssignmentExpression
AssignmentExpression:
AssignToExpression
AssignToExpression AssignmentOperator Expression
AssignmentOperator: one of
= : += -= *= /= %= &= |= &&= ||=
AssignToExpression:
RangeExpression
AssignToExpression => RangeExpression
RangeExpression:
ConditionalExpression
RangeExpressionopt RangeOperator ConditionalExpressionopt
RangeOperator: one of
~ +~ -~ ~+ ~-
ConditionalExpression:
ConditionalOrExpression
ConditionalOrExpression ? Expression : Expression
ConditionalOrExpression:
ConditionalAndExpression
ConditionalOrExpression || ConditionalAndExpression
ConditionalAndExpression:
VariableOrExpression
ConditionalAndExpression && VariableOrExpression
VariableOrExpression:
VariableAndExpression
VariableOrExpression | VariableAndExpression
VariableAndExpression:
EqualityExpression
VariableAndExpression & EqualityExpression
EqualityExpression:
RelationalExpression
EqualityExpression == RelationalExpression
EqualityExpression != RelationalExpression
RelationalExpression:
AdditiveExpression
RelationalExpression < AdditiveExpression
RelationalExpression > AdditiveExpression
RelationalExpression <= AdditiveExpression
RelationalExpression >= AdditiveExpression
RelationalExpression is Type
RelationalExpression as Type
AdditiveExpression:
MultiplicativeExpression
AdditiveExpression + MultiplicativeExpression
AdditiveExpression – MultiplicativeExpression
MultiplicativeExpression:
UnaryExpression
MultiplicativeExpression UnaryExpression
MultiplicativeExpression / UnaryExpression
MultiplicativeExpression % UnaryExpression
MultiplicativeExpression ^ UnaryExpression*
UnaryExpression:
PrimaryExpression
+ UnaryExpression
- UnaryExpression
! UnaryExpression
++ UnaryExpression
-- UnaryExpression
await UnaryExpression
typeof UnaryExpression
PrimaryExpression:
Type
ParenthesizedExpression
MemberCallExpression
FuncCallExpression
IndexCallExpression
NewExpression
ThisAccess
BaseAccess
CalleeAccess
PostfixExpression
ConstantLiteral
ParenthesizedExpression:
( Expression )
MemberCallExpression:
PrimaryExpression . Identifier GenericTypeArgumentListopt
FuncCallExpression:
PrimaryExpression ( ArgumentListopt )
ArgumentList:
Argument
ArgumentList , Argument
Argument:
ArgumentValue
Identifier : ArgumentValue
ArgumentValue:
Expression
ref Expression
out Expression
IndexCallExpression:
PrimaryExpression [ ArgumentList ]
ThisAccess:
this
BaseAccess:
base
CalleeAccess:
callee
PostfixExpression:
PrimaryExpression ++
PrimaryExpression --
ConstantLiteral:
Literal
ListLiteral
DictLiteral
FuncLiteral
ListLiteral:
[ ExpressionListopt ]
DictLiteral:
{ DictPropertyList }
DictPropertyList:
DictProperty
DictPropertyList , DictProperty
DictProperty:
Identifier : Expression
ExpressionList:
Expression
ExpressionList , Expression
FuncLiteral:
FuncLiteralParameters -> FuncLiteralBody
FuncLiteralParameters:
Identifier
( IdentifierListopt )
IdentifierList:
Identifier
IdentifierList , Identifier
FuncLiteralBody:
Expression
Block
2.5 语句(Statements)
Statement:
LabeledStatement
VariableStatement
EmbeddedStatement
EmbeddedStatement:
Block
EmptyStatement
ExpressionStatement
SelectionStatement
IterationStatement
JumpStatement
TryStatement
LockStatement
WithStatement
YieldStatement
CheckedStatement
UncheckedStatement
OnceStatement
TraceStatement
Block:
{ StatementListopt }
StatementList:
Statement
StatementList Statement
EmptyStatement:
;
LabeledStatement:
Identifier : Statement
VariableStatement:
VariableDefinition
const VariableDefinition
readonly VariableDefinition
static readonly VariableDefinition
readonly static VariableDefinition
VariableDefinition:
Type VariableDeclarators ;
VariableDeclarators:
VariableDeclarator
VariableDeclarators , VariableDeclarator
VariableDeclarator:
Identifier VariableInitializeropt
VariableInitializer:
= Expression
: AssignToExpression
= Expression : AssignToExpression
: AssignToExpression = Expression
ExpressionStatement:
Expression ;
SelectionStatement:
IfStatement
SwitchStatement
IfStatement:
if Condition EmbeddedStatement
if Condition EmbeddedStatement else EmbeddedStatement
Condition:
( Expression )
SwitchStatement:
switch Conditionopt SwitchBlock
SwitchBlock:
{ SwitchSectionsopt }
SwitchSections:
SwitchSection
SwitchSections SwitchSection
SwitchSection:
SwitchLabel StatementList
SwitchLabel:
case SwitchLabelValueList :
SwitchLabelValueList:
SwitchLabelValue
SwitchLabelValueList , SwitchLabelValue
SwitchLabelValue:
Expression
else
IterationStatement:
WhileStatement
UntilStatement
DoWhileStatement
DoUntilStatement
ForStatement
ForInStatement
ForToStatement
WhileStatement:
while Condition EmbeddedStatement
UntilStatement:
until Condition EmbeddedStatement
DoWhileStatement:
do EmbeddedStatement while Condition ;
DoUntilStatement:
do EmbeddedStatement until Condition ;
ForStatement:
for ( ForInitializeropt ; ForConditionopt ; ForIteratoropt ) EmbeddedStatement
ForInitializer:
VariableStatement
ExpressionStatement
ForCondition:
Expression
ForIterator:
ExpressionList
ForInStatement:
for ( ForInVariableDefinitions in Expression ) EmbeddedStatement
ForInVariableDefinitions:
Type IdentifierList
ForToStatement:
for ( Type Identifier = Expression to Expression ) EmbeddedStatement
JumpStatement:
BreakStatement
ContinueStatement
GotoStatement
ReturnStatement
ThrowStatement
BreakStatement:
break ;
ContinueStatement:
continue ;
GotoStatement:
goto Identifier ;
goto case SwitchLabelValue ;
ReturnStatement:
return Expressionopt ;
ThrowStatement:
throw Expressionopt ;
TryStatement:
try EmbeddedStatement CatchClauseopt FinallyClauseopt
CatchClause:
catch EmbeddedStatement
catch ( Type ) EmbeddedStatement
catch ( Type Identifier ) EmbeddedStatement
FinallyClause:
finally EmbeddedStatement
LockStatement:
lock Condition EmbeddedStatement
WithStatement:
with ( ResourceAcquisition ) EmbeddedStatement
ResourceAcquisition:
LocalVariableDefinition
Expression
YieldStatement:
yield Expression ;
yield break ;
yield continue ;
CheckedStatement:
checked EmbeddedStatement ;
UncheckedStatement:
unchecked EmbeddedStatement ;
OnceStatement:
once Condition EmbeddedStatement ;
TraceStatement:
TraceOperartor Expression ;
TraceOperartor:
>
TraceOperartor >
2.6 成员(Members)
MemberDefinition:
FieldDefinition
MethodDefinition
PropertyDefinition
OperatorDefinition
TypeDefinition
NamespaceDefinition
TypeDefinition:
ClassDefinition
InterfaceDefinition
EnumDefinition
MemberHeader:
Annotationsopt Modifiersopt
Annotations:
Annotation
Annotations Annotation
Annotation:
Type
@ Type ( ArgumentList )
Modifiers: one of
abstract
await
const
extern
final
internal
new
once
override
partial
private
protected
public
readonly
static
virtual
volatile
ExlipctOwner:
Type .
FieldDefinition:
MemberHeader ExlipctOwneropt VariableDeclarators ;
MethodDefinition:
MemberHeader Type ExlipctOwneropt Identifier TypeParameterListopt ( FormalParameterListopt ) MethodBody
MethodBody:
Blockopt ;
FormalParameterList:
FormalParameter
FormalParameterList , FormalParameter
FormalParameter:
Parameter
ref Parameter
out Parameter
params Parameter
.. .opt
Parameter:
Type Identifier ?opt VariableInitializeropt
PropertyDefinition:
MemberHeader Type ExlipctOwneropt Identifier TypeParameterListopt { AccessorDefinitions }
AccessorDefinitions:
GetAccessorDefinition
SetAccessorDefinition
AddAccessorDefinition
RemoveAccessorDefinition
GetAccessorDefinition SetAccessorDefinitionopt
SetAccessorDefinition GetAccessorDefinitionopt
AddAccessorDefinition RemoveAccessorDefinition
RemoveAccessorDefinition AddAccessorDefinition
GetAccessorDefinition:
get AccessorBody
SetAccessorDefinition:
set AccessorBody
AddAccessorDefinition:
add AccessorBody
RemoveAccessorDefinition:
remove AccessorBody
AccessorBody:
;
Block
OperatorDefinition:
UnaryOperatorDeclarator
BinaryOperatorDeclarator
IndexOperatorDefinition
AsOperatorDeclarator
TraceOperatorDeclarator
UnaryOperatorDeclarator:
MemberHeader Type OverloadableUnaryOperator ( ) MethodBody
OverloadableUnaryOperator: one of
+ - ! ++ --
BinaryOperatorDeclarator:
MemberHeader Type OverloadableBinaryOperator ( Type Identifier ) MethodBody
OverloadableBinaryOperator: one of
+ - * / % ^
== != > < <= >= .
2.6 类(Classes)
ClassDefinition:
Annotationsopt class Identifier TypeParameterListopt ClassBaseopt ClassBody ;
ClassBase:
: TypeList
ClassBody:
{ ClassMemberDefinitionsopt }
ClassMemberDefinitions:
ClassMemberDefinition
ClassMemberDefinitions ClassMemberDefinition
ClassMemberDefinition:
ConstantDefinition
FieldDefinition
MethodDefinition
PropertyDefinition
EventDefinition
IndexerDefinition
OperatorDefinition
TypeDefinition
ConstantDefinition:
Annotationsopt const Typeopt ConstantDeclarators ;
ConstantDeclarators:
ConstantDeclarator
ConstantDeclarators , ConstantDeclarator
ConstantDeclarator:
Identifier = Expression
FieldModifiers:
FieldModifier
FieldModifiers FieldModifier
FieldModifier:
static
volatile
IndexerDefinition:
Annotationsopt IndexerModifiers Typeopt IndexerDeclarator PropertyBody
IndexerModifiers:
PropertyModifiers
IndexerDeclarator:
this [ FormalParameterList ]
InterfaceType . this [ FormalParameterLists ]
OperatorDefinition:
Annotationsopt Typeopt OperatorModifiers OperatorDeclarator OperatorBody
OperatorModifiers:
OperatorModifier
OperatorModifiers OperatorModifier
OperatorModifier:
static
extern
AsOperatorDeclarator:
as Type
OperatorBody:
Block ;
2.7 Namespace
NamespaceDefinition:
Annotationsopt namespace Identifier TypeParameterListopt ClassBaseopt ClassBody ;
2.8 Interfaces
InterfaceDefinition:
Annotationsopt interface Identifier TypeParameterListopt ClassBaseopt ClassBody ;
2.9 Enums
EnumDefinition:
Annotationsopt enum Identifier EnumBaseopt EnumBody ;
EnumBase:
: IntegralType
EnumBody:
{ EnumMemberDefinitionsopt }
EnumMemberDefinitions:
EnumMemberDefinition
EnumMemberDefinitions , EnumMemberDefinition
EnumMemberDefinition:
Annotationsopt Identifier
Annotationsopt Identifier = Expression
2.11 Generics
TypeParameterList:
< TypeParameters >
TypeParameters:
TypeParameter
TypeParameters , TypeParameter
TypeParameter:
Identifier
Identifier : Type
TypeArgumentList:
< TypeArguments >
TypeArguments:
TypeArgument
TypeArguments , TypeArgument
TypeArgument:
Type