- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathChild.py
82 lines (71 loc) · 3.18 KB
/
Child.py
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
# flake8: noqa I201
fromClassificationimportclassification_by_name
fromTokenimportSYNTAX_TOKEN_MAP
fromkindsimportSYNTAX_BASE_KINDS, kind_to_type, lowercase_first_word
classChild(object):
"""
A child of a node, that may be declared optional or a token with a
restricted subset of acceptable kinds or texts.
"""
def__init__(self, name, kind, description=None, is_optional=False,
token_choices=None, text_choices=None, node_choices=None,
collection_element_name=None,
classification=None, force_classification=False):
"""
If a classification is passed, it specifies the color identifiers in
that subtree should inherit for syntax coloring. Must be a member of
SyntaxClassification in SyntaxClassifier.h.gyb
If force_classification is also set to true, all child nodes (not only
identifiers) inherit the syntax classification.
"""
self.name=name
self.swift_name=lowercase_first_word(name)
self.syntax_kind=kind
self.description=description
self.swift_syntax_kind=lowercase_first_word(self.syntax_kind)
self.type_name=kind_to_type(self.syntax_kind)
self.collection_element_name=collection_element_name
self.classification=classification_by_name(classification)
self.force_classification=force_classification
# If the child has "token" anywhere in the kind, it's considered
# a token node. Grab the existing reference to that token from the
# global list.
self.token_kind= \
self.syntax_kindif"Token"inself.syntax_kindelseNone
self.token=SYNTAX_TOKEN_MAP.get(self.token_kind)
self.is_optional=is_optional
# A restricted set of token kinds that will be accepted for this
# child.
self.token_choices= []
ifself.token:
self.token_choices.append(self.token)
forchoiceintoken_choicesor []:
token=SYNTAX_TOKEN_MAP[choice]
self.token_choices.append(token)
# A list of valid text for tokens, if specified.
# This will force validation logic to check the text passed into the
# token against the choices.
self.text_choices=text_choicesor []
# A list of valid choices for a child
self.node_choices=node_choicesor []
# Check the choices are either empty or multiple
assertlen(self.node_choices) !=1
# Check node choices are well-formed
forchoiceinself.node_choices:
assertnotchoice.is_optional, \
"node choice %s cannot be optional"%choice.name
assertnotchoice.node_choices, \
"node choice %s cannot have further choices"%choice.name
defis_token(self):
"""
Returns true if this child has a token kind.
"""
returnself.token_kindisnotNone
defmain_token(self):
"""
Returns the first choice from the token_choices if there are any,
otherwise returns None.
"""
ifself.token_choices:
returnself.token_choices[0]
returnNone