-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtypescript.grammar
130 lines (98 loc) · 3.82 KB
/
typescript.grammar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
extends "./javascript.grammar"
skip whitespace {
context Statement {
type typeDeclName TypeParams? op("=") Type semi |
namespace (~(identifier ".") variable ".")* declName Block |
interface typeDeclName TypeParams? (extends CommaSep(Type))? ObjType |
(~(kw("const") enum) kw("const") enum | enum) typeDeclName EnumBody |
(declare | kw("abstract")) Statement |
super
}
context TypeParams { "<" CommaSep(typeDeclName (extends Type)? (op("=") Type)?) ">" }
context TypeArgs { "<" CommaSep(Type) ">" }
Type { TypeAtom TypeSuffix* }
TypeAtom {
kw("this") |
atom |
kw("typeof") variable ("." propName | "[" Expression "]")* |
kw("keyof" | "readonly" | "unique") Type |
~("(" !(")" | "..." | identifier ("?" | ":"))) "(" Type ")" |
new? TypeParams? ParamListSpec op("=>") RetType |
(~(identifier ".") variable ".")* typeName TypeArgs? |
TupleType |
ObjType |
string |
number
}
TypeSuffix {
(op("&" | "|") | kw("extends")) Type |
"[" Type? "]" |
op("?") Type op(":") Type
}
TypeAnn {
":" Type
}
RetType {
(~(identifier kw("is")) variable kw("is"))? Type
}
context ParamListSpec { ParamList }
context ParamList { // FIXME typing comes before default values
"(" CommaSep(Decorator* modifier* "..."? Pattern "?"? TypeAnn? (op("=") ExpressionNoComma)?) ")"
}
context ObjType { "{" ObjTypeField? (("," | ";") ObjTypeField?)* "}" }
context TupleType { "[" CommaSep((labelName ":")? Type) "]" }
ObjTypeField {
new? TypeParams? ParamList (":" RetType)? |
modifier* ("[" (~(identifier (":" | "in")) identifier (":" | kw("in")) Type | Expression) "]" TypeAnn |
(propDeclName | string | number) "?"? (TypeParams? ParamList)? (":" RetType)?)
}
Callee { calleeName TypeArgs? }
NewExpression { new ("." kw("target") | calleeName TypeArgs? | BaseExpression) }
context ArrowRest { op("=>") TypeParams? (Block | ExpressionNoComma) }
ExpressionSuffix(ContExpression) {
kw("as") Type |
op("!") |
suffixOp |
(binOp | kwBinOp) ContExpression |
ArgList |
template |
("." | "?.") (calleeProp TypeArgs? | propName) |
"[" Expression "]" |
op("?") Expression op(":") ContExpression
}
BaseExpression {
"<" Type ">" BaseExpression |
super
}
context FunctionDef {
TypeParams? ParamList (":" RetType)? (Block | semi)
}
ClassDef {
TypeParams? (extends Type)? (kw("implements") CommaSep(Type))? ClassBody
}
context ClassBody {
"{" ((modifier | override)* ClassItem | Decorator)* "}"
}
context ClassItem {
propModifier?
(propDeclName | "[" Expression "]" | number | string)
(asyncStar? FunctionDef | ("!" | "?")? TypeAnn? (op("=") Expression)? semi)
}
VariableDeclarator { Pattern op("!")? TypeAnn? (op("=") ExpressionNoComma)? }
context EnumBody { "{" CommaSep(propDeclName (op("=") ExpressionNoComma)?) "}" }
ParamsAndArrow { ~(ParamList (":" RetType)? "=>") ParamList (":" RetType)? }
}
modifier { kw("public" | "private" | "protected" | "readonly" | "abstract" | "static") }
enum { kw("enum") }
propModifier { kw("get" | "set" | "async") !(" "* ("," | "}" | ":" | "(" | "<")) }
typeDeclName="def type" { identifier }
typeName="type" { identifier }
calleeName="variable callee" { identifier ~("<" !" " | " "* (typeArgStructure " "*)? "(") }
calleeProp="property callee" { identifier ~(" "* (typeArgStructure " "*)? "(") }
typeArgStructure { "<" (typeArgStructure | "=>" | !">" _)* ">" }
declNameAndArrow { declName ~(whitespace (":" whitespace Type whitespace)? "=>") }
type { kw("type") ~(spaceChar* identifierStart) }
namespace { kw("namespace") ~(spaceChar* identifierStart) }
interface { kw("interface") ~(spaceChar* identifierStart) }
declare { kw("declare") ~(spaceChar* identifierStart) }
override { kw("override") ~(spaceChar* identifierStart) }