-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathts2python.ebnf
More file actions
225 lines (180 loc) · 8.26 KB
/
ts2python.ebnf
File metadata and controls
225 lines (180 loc) · 8.26 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# a grammar for a subset of the Typescript programming language
# for Typescript see: https://www.typescriptlang.org/docs/
# for examples of typescript-interfaces
# see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/
#######################################################################
#
# EBNF-Directives
#
#######################################################################
@ optimizations = all
@ whitespace = /\s*/ # insignificant whitespace, includes linefeeds
@ literalws = right # literals have implicit whitespace on the right hand side
@ comment = /(?:\/\/.*)\n?|(?:\/\*(?:.|\n)*?\*\/) *\n?/ # /* ... */ or // to EOL
@ ignorecase = False # literals and regular expressions are case-sensitive
@ reduction = merge_treetops # anonymous nodes are being reduced where possible
@ disposable = INT, NEG, FRAC, DOT, EXP, EOF,
_array_ellipsis, _top_level_assignment, _top_level_literal,
_quoted_identifier, _namespace, _part, _reserved, _keyword
@ drop = whitespace, no_comments, strings, EOF,
_array_ellipsis, _top_level_assignment, _top_level_literal,
_reserved, _keyword
#######################################################################
#
#: Typescript Document
#
#######################################################################
root = document EOF
document = ~ { interface | type_alias | _namespace | enum | const | module
| _top_level_assignment | _array_ellipsis | _top_level_literal
| Import [";"] | function [";"] | declaration [";"] }
@interface_resume = /(?=export|$)/
@type_alias_resume = /(?=export|$)/
@enum_resume = /(?=export|$)/
@const_resume = /(?=export|$)/
@declaration_resume = /(?=export|$)/
@_top_level_assignment_resume = /(?=export|$)/
@_top_level_literal_resume = /(?=export|$)/
@module_resume = /(?=export|$)/
module = "declare" "module" _quoted_identifier "{" document "}"
#######################################################################
#
#: Imports
#
#######################################################################
Import = "import" [(wildcard | importList ) "from"] string
importList = (symList | identifier) { "," (symList | identifier) }
symList = "{" symbol { "," symbol } "}"
symbol = ["type"] identifier [ "as" alias ]
wildcard = "*" "as" alias
alias = identifier
#######################################################################
#
#: Interfaces
#
#######################################################################
interface = ["export"] ["declare"] ("interface"|"class") §identifier [type_parameters]
[extends] declarations_block [";"]
extends = ("extends" | "implements")
(generic_type|type_name) { "," (generic_type|type_name)}
declarations_block = "{" [ (function|declaration) { [";"|","] (function|declaration) } [";"|","] ]
[map_signature [";"|","] ] "}"
declaration = ["export"] qualifiers ["let"|"var"] !_keyword identifier [optional] !`(` [":" §types]
# @function_resume = /(?=;)/
function = [["export"] qualifiers
["function"] !_keyword identifier [optional] [type_parameters]]
"(" §[arg_list] ")" [":" §types]
| special
arg_list = (argument { "," argument } ["," arg_tail] | arg_tail) [","]
arg_tail = "..." identifier [":" §(array_of | generic_type)]
argument = identifier [optional] [":" §types] # same as declaration ?
special = "[" name "]" "(" §[arg_list] ")" [":" §types]
_keyword = (`readonly` | `function` | `const` | `public` | `private` | `protected`) !/\w/
qualifiers = [readonly] ° [static] ° ['public'] ° ['protected'] ° ['private']
readonly = "readonly"
static = "static"
optional = "?"
#######################################################################
#
#: Types
#
#######################################################################
type_alias = ["export"] "type" §identifier [type_parameters] "=" types [";"]
types = ["|"] (intersection | type) { "|" (intersection | type) }
intersection = type { "&" §type }+
type = [readonly]
( array_of | basic_type | generic_type | indexed_type
| type_name !`(` | "(" types ")" | mapped_type
| declarations_block | type_tuple | declarations_tuple
| literal | func_type )
generic_type = type_name type_parameters
type_parameters = "<" §parameter_types { "," parameter_types } ">"
parameter_types = ["|"] parameter_type { "|" parameter_type }
parameter_type = [readonly]
( array_of | basic_type | generic_type # actually, only a subset of array_of
| indexed_type | type_name [extends_type] [equals_type]
| declarations_block | type_tuple | declarations_tuple )
extends_type = "extends" [keyof] (basic_type | type_name | mapped_type )
equals_type = "=" (basic_type|type_name)
array_of = array_types "[]"
array_types = array_type
array_type = basic_type | generic_type | type_name | "(" types ")"
| type_tuple | declarations_block
type_name = name
type_tuple = "[" types {"," types} "]"
declarations_tuple = "[" declaration { "," declaration } "]"
mapped_type = "{" map_signature [";"] "}"
map_signature = index_signature ":" types
index_signature = [readonly]
"[" identifier (":" | ["in"] keyof | "in") type "]"
[optional]
keyof = "keyof"
indexed_type = type_name "[" (type_name | literal) "]"
func_type = ["new"] "(" [arg_list] ")" "=>" types
#######################################################################
#
#: Namespaces
#
#######################################################################
_namespace = virtual_enum | namespace
virtual_enum = ["export"] "namespace" identifier "{"
{ interface | type_alias | enum | const
| declaration [";"] } "}"
namespace = ["export"] "namespace" §identifier "{"
{ interface | type_alias | enum | const
| declaration [";"] | function [";"] } "}"
#######################################################################
#
#: Enums
#
#######################################################################
enum = ["export"] "enum" identifier §"{" item { "," item } [","] "}"
item = _quoted_identifier ["=" literal]
#######################################################################
#
#: Consts
#
#######################################################################
const = ["export"] "const" §declaration ["=" (literal | identifier)] [";"]
_top_level_assignment = assignment
assignment = variable "=" (literal | variable) [";"] # no expressions, yet
#######################################################################
#
#: literals
#
#######################################################################
_array_ellipsis = literal { "," literal }
_top_level_literal = literal
literal = integer | number | boolean | string | array | object
integer = INT !/[.Ee]/ ~
number = INT FRAC EXP ~
boolean = (`true` | `false`) ~
string = /"[^"\n]*"/~ | /'[^'\n]*'/~
array = "[" [ literal { "," literal } ] "]"
object = "{" [ association { "," association } ] [","] "}"
association = key §":" literal
key = identifier | '"' identifier '"'
#######################################################################
#
#: Keywords
#
#######################################################################
basic_type = (`object` | `array` | `string` | `number` | `boolean` | `null`
| `integer` | `uinteger` | `decimal` | `unknown` | `any` | `void`) ~
#######################################################################
#
#: Entities
#
#######################################################################
variable = name
_quoted_identifier = identifier | '"' identifier §'"' | "'" identifier §"'"
name = !_reserved _part {`.` _part} ~
identifier = !_reserved _part ~
_part = /(?!\d)\w+/
_reserved = `true` | `false`
INT = [NEG] ( /[1-9][0-9]+/ | /[0-9]/ )
NEG = `-`
FRAC = [ DOT /[0-9]+/ ]
DOT = `.`
EXP = [ (`E`|`e`) [`+`|`-`] /[0-9]+/ ]
EOF = !/./ # no more characters ahead, end of file reached