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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
from collections import OrderedDict
class ASTNode:
def __init__(self, path, lineno, lexpos):
"""
Args:
lineno (int): The line number where the start of this element
occurs.
lexpos (int): The character offset into the file where this element
occurs.
"""
self.path = path
self.lineno = lineno
self.lexpos = lexpos
class AstNamespace(ASTNode):
def __init__(self, path, lineno, lexpos, name, doc):
"""
Args:
name (str): The namespace of the spec.
doc (Optional[str]): The docstring for this namespace.
"""
super().__init__(path, lineno, lexpos)
self.name = name
self.doc = doc
def __str__(self):
return self.__repr__()
def __repr__(self):
return 'AstNamespace({!r})'.format(self.name)
class AstImport(ASTNode):
def __init__(self, path, lineno, lexpos, target):
"""
Args:
target (str): The name of the namespace to import.
"""
super().__init__(path, lineno, lexpos)
self.target = target
def __str__(self):
return self.__repr__()
def __repr__(self):
return 'AstImport({!r})'.format(self.target)
class AstAlias(ASTNode):
def __init__(self, path, lineno, lexpos, name, type_ref, doc):
"""
Args:
name (str): The name of the alias.
type_ref (AstTypeRef): The data type of the field.
doc (Optional[str]): Documentation string for the alias.
"""
super().__init__(path, lineno, lexpos)
self.name = name
self.type_ref = type_ref
self.doc = doc
self.annotations = []
def set_annotations(self, annotations):
self.annotations = annotations
def __repr__(self):
return 'AstAlias({!r}, {!r})'.format(self.name, self.type_ref)
class AstTypeDef(ASTNode):
def __init__(self, path, lineno, lexpos, name, extends, doc, fields,
examples):
"""
Args:
name (str): Name assigned to the type.
extends (Optional[str]); Name of the type this inherits from.
doc (Optional[str]): Docstring for the type.
fields (List[AstField]): Fields of a type, not including
inherited ones.
examples (Optional[OrderedDict[str, AstExample]]): Map from label
to example.
"""
super().__init__(path, lineno, lexpos)
self.name = name
assert isinstance(extends, (AstTypeRef, type(None))), type(extends)
self.extends = extends
assert isinstance(doc, (str, type(None)))
self.doc = doc
assert isinstance(fields, list)
self.fields = fields
assert isinstance(examples, (OrderedDict, type(None))), type(examples)
self.examples = examples
def __str__(self):
return self.__repr__()
def __repr__(self):
return 'AstTypeDef({!r}, {!r}, {!r})'.format(
self.name,
self.extends,
self.fields,
)
class AstStructDef(AstTypeDef):
def __init__(self, path, lineno, lexpos, name, extends, doc, fields,
examples, subtypes=None):
"""
Args:
subtypes (Tuple[List[AstSubtypeField], bool]): Inner list
enumerates subtypes. The bool indicates whether this struct
is a catch-all.
See AstTypeDef for other constructor args.
"""
super().__init__(
path, lineno, lexpos, name, extends, doc, fields, examples)
assert isinstance(subtypes, (tuple, type(None))), type(subtypes)
self.subtypes = subtypes
def __repr__(self):
return 'AstStructDef({!r}, {!r}, {!r})'.format(
self.name,
self.extends,
self.fields,
)
class AstStructPatch(ASTNode):
def __init__(self, path, lineno, lexpos, name, fields, examples):
super().__init__(path, lineno, lexpos)
self.name = name
assert isinstance(fields, list)
self.fields = fields
assert isinstance(examples, (OrderedDict, type(None))), type(examples)
self.examples = examples
def __repr__(self):
return 'AstStructPatch({!r}, {!r})'.format(
self.name,
self.fields,
)
class AstUnionDef(AstTypeDef):
def __init__(self, path, lineno, lexpos, name, extends, doc, fields,
examples, closed=False):
"""
Args:
closed (bool): Set if this is a closed union.
See AstTypeDef for other constructor args.
"""
super().__init__(
path, lineno, lexpos, name, extends, doc, fields, examples)
self.closed = closed
def __repr__(self):
return 'AstUnionDef({!r}, {!r}, {!r}, {!r})'.format(
self.name,
self.extends,
self.fields,
self.closed,
)
class AstUnionPatch(ASTNode):
def __init__(self, path, lineno, lexpos, name, fields, examples, closed):
super().__init__(path, lineno, lexpos)
self.name = name
assert isinstance(fields, list)
self.fields = fields
assert isinstance(examples, (OrderedDict, type(None))), type(examples)
self.examples = examples
self.closed = closed
def __repr__(self):
return 'AstUnionPatch({!r}, {!r}, {!r})'.format(
self.name,
self.fields,
self.closed,
)
class AstTypeRef(ASTNode):
def __init__(self, path, lineno, lexpos, name, args, nullable, ns):
"""
Args:
name (str): Name of the referenced type.
args (tuple[list, dict]): Arguments to type.
nullable (bool): Whether the type is nullable (can be null)
ns (Optional[str]): Namespace that referred type is a member of.
If none, then refers to the current namespace.
"""
super().__init__(path, lineno, lexpos)
self.name = name
self.args = args
self.nullable = nullable
self.ns = ns
def __repr__(self):
return 'AstTypeRef({!r}, {!r}, {!r}, {!r})'.format(
self.name,
self.args,
self.nullable,
self.ns,
)
class AstTagRef(ASTNode):
def __init__(self, path, lineno, lexpos, tag):
"""
Args:
tag (str): Name of the referenced type.
"""
super().__init__(path, lineno, lexpos)
self.tag = tag
def __repr__(self):
return 'AstTagRef({!r})'.format(
self.tag,
)
class AstAnnotationRef(ASTNode):
def __init__(self, path, lineno, lexpos, annotation, ns):
"""
Args:
annotation (str): Name of the referenced annotation.
"""
super().__init__(path, lineno, lexpos)
self.annotation = annotation
self.ns = ns
def __repr__(self):
return 'AstAnnotationRef({!r}, {!r})'.format(
self.annotation, self.ns
)
class AstAnnotationDef(ASTNode):
def __init__(self, path, lineno, lexpos, name, annotation_type,
annotation_type_ns, args, kwargs):
"""
Args:
name (str): Name of the defined annotation.
annotation_type (str): Type of annotation to define.
annotation_type_ns (Optional[str]): Namespace where the annotation
type was defined. If None, current namespace or builtin.
args (str): Arguments to define annotation.
kwargs (str): Keyword Arguments to define annotation.
"""
super().__init__(path, lineno, lexpos)
self.name = name
self.annotation_type = annotation_type
self.annotation_type_ns = annotation_type_ns
self.args = args
self.kwargs = kwargs
def __repr__(self):
return 'AstAnnotationDef({!r}, {!r}, {!r}, {!r}, {!r})'.format(
self.name,
self.annotation_type,
self.annotation_type_ns,
self.args,
self.kwargs,
)
class AstAnnotationTypeDef(ASTNode):
def __init__(self, path, lineno, lexpos, name, doc, params):
"""
Args:
name (str): Name of the defined annotation type.
doc (str): Docstring for the defined annotation type.
params (List[AstField]): Parameters that can be passed to the
annotation type.
"""
super().__init__(path, lineno, lexpos)
self.name = name
self.doc = doc
self.params = params
def __repr__(self):
return 'AstAnnotationTypeDef({!r}, {!r}, {!r})'.format(
self.name,
self.doc,
self.params,
)
class AstField(ASTNode):
"""
Represents both a field of a struct and a field of a union.
TODO(kelkabany): Split this into two different classes.
"""
def __init__(self, path, lineno, lexpos, name, type_ref):
"""
Args:
name (str): The name of the field.
type_ref (AstTypeRef): The data type of the field.
"""
super().__init__(path, lineno, lexpos)
self.name = name
self.type_ref = type_ref
self.doc = None
self.has_default = False
self.default = None
self.annotations = []
def set_doc(self, docstring):
self.doc = docstring
def set_default(self, default):
self.has_default = True
self.default = default
def set_annotations(self, annotations):
self.annotations = annotations
def __repr__(self):
return 'AstField({!r}, {!r}, {!r})'.format(
self.name,
self.type_ref,
self.annotations,
)
class AstVoidField(ASTNode):
def __init__(self, path, lineno, lexpos, name):
super().__init__(path, lineno, lexpos)
self.name = name
self.doc = None
self.annotations = []
def set_doc(self, docstring):
self.doc = docstring
def set_annotations(self, annotations):
self.annotations = annotations
def __str__(self):
return self.__repr__()
def __repr__(self):
return 'AstVoidField({!r}, {!r})'.format(
self.name,
self.annotations,
)
class AstSubtypeField(ASTNode):
def __init__(self, path, lineno, lexpos, name, type_ref):
super().__init__(path, lineno, lexpos)
self.name = name
self.type_ref = type_ref
def __repr__(self):
return 'AstSubtypeField({!r}, {!r})'.format(
self.name,
self.type_ref,
)
class AstRouteDef(ASTNode):
def __init__(self, path, lineno, lexpos, name, version, deprecated,
arg_type_ref, result_type_ref, error_type_ref=None):
super().__init__(path, lineno, lexpos)
self.name = name
self.version = version
self.deprecated = deprecated
self.arg_type_ref = arg_type_ref
self.result_type_ref = result_type_ref
self.error_type_ref = error_type_ref
self.doc = None
self.attrs = {}
def set_doc(self, docstring):
self.doc = docstring
def set_attrs(self, attrs):
self.attrs = attrs
class AstAttrField(ASTNode):
def __init__(self, path, lineno, lexpos, name, value):
super().__init__(path, lineno, lexpos)
self.name = name
self.value = value
def __repr__(self):
return 'AstAttrField({!r}, {!r})'.format(
self.name,
self.value,
)
class AstExample(ASTNode):
def __init__(self, path, lineno, lexpos, label, text, fields):
super().__init__(path, lineno, lexpos)
self.label = label
self.text = text
self.fields = fields
def __repr__(self):
return 'AstExample({!r}, {!r}, {!r})'.format(
self.label,
self.text,
self.fields,
)
class AstExampleField(ASTNode):
def __init__(self, path, lineno, lexpos, name, value):
super().__init__(path, lineno, lexpos)
self.name = name
self.value = value
def __repr__(self):
return 'AstExampleField({!r}, {!r})'.format(
self.name,
self.value,
)
class AstExampleRef(ASTNode):
def __init__(self, path, lineno, lexpos, label):
super().__init__(path, lineno, lexpos)
self.label = label
def __repr__(self):
return 'AstExampleRef({!r})'.format(self.label)
|