Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_list(peaker):
# type: (Peaker[Token]) -> Node
prev = parse_word(peaker)
children = [prev]
while ((prev.value or '').endswith(',')
and peaker.has_next()
and _is(TokenType.WORD, peaker.peak())):
prev = parse_word(peaker)
children.append(prev)
return Node(
node_type=NodeType.LIST,
children=children
)
next_child.line_number,
next_child.line_number,
)
)
# It is possible that there are no children at this point, in
# which case there is likely just the newline. In this case,
# we try to set the token so that the line can have a line
# number.
token = None
if peaker.has_next():
if not children:
token = peaker.next()
else:
peaker.next() # Throw away newline.
return Node(
NodeType.LINE,
children=children,
token=token,
)
def parse_list(peaker):
# type: (Peaker[Token]) -> Node
prev = parse_word(peaker)
children = [prev]
while (prev.value.endswith(',')
and peaker.has_next()
and _is(TokenType.WORD, peaker.peak())):
prev = parse_word(peaker)
children.append(prev)
return Node(
node_type=NodeType.LIST,
children=children
)
children.append(parse_lparen(peaker))
elif _is(TokenType.RPAREN, peaker.peak()):
i -= 1
children.append(parse_rparen(peaker))
else:
encountered_word = True
children.append(parse_word(peaker))
Assert(
i == 0,
'Mismatched parentheses in parenthetical type.'
)
Assert(
encountered_word,
'Parenthetical type must contain at least one word.'
)
return Node(
node_type=NodeType.TYPE,
children=children,
)
def parse_simple_section(peaker):
# type: (Peaker[Token]) -> Node
AssertNotEmpty(peaker, 'parse section')
children = [
parse_section_head(peaker, expecting={'Returns', 'Yields'}),
parse_section_simple_body(peaker),
]
Assert(
peaker.has_next() and _is(TokenType.NEWLINE, peaker.peak()),
'Expected newline after section.'
)
peaker.next() # Discard newline.
return Node(
NodeType.SECTION,
children=children,
)
def parse_rparen(peaker):
# type: (Peaker[Token]) -> Node
AssertNotEmpty(peaker, 'parse right parenthesis')
Assert(
_is(TokenType.RPAREN, peaker.peak()),
'Unable to parse right parenthesis: expected {} '
'but received {}'.format(
TokenType.RPAREN, peaker.peak().token_type
),
token=peaker.peak(),
)
token = peaker.next()
return Node(
node_type=NodeType.RPAREN,
value=token.value,
token=token,
)
parse_description(peaker)
]
while peaker.has_next():
two_ahead = peaker.peak(lookahead=2)
if two_ahead is None:
parse_line(peaker) # Throw away final newline.
break
if two_ahead.value in keyword_parse_lookup:
children.append(
keyword_parse_lookup[two_ahead.value](peaker)
)
else:
children.append(
parse_long_description(peaker)
)
return Node(
node_type=NodeType.DOCSTRING,
children=children,
)
def parse_noqa_head(peaker):
# type: (Peaker[Token]) -> Node
children = [
parse_hash(peaker),
]
word = parse_word(peaker)
Assert(
word.value == 'noqa',
'Failed to parse noqa statement. '
'Expected "# noqa" but received "# {}"'.format(
word.value,
),
token=peaker.peak(),
)
children.append(word)
return Node(
node_type=NodeType.NOQA_HEAD,
children=children,
)
def parse_noqa_head(peaker):
# type: (Peaker[Token]) -> Node
children = [
parse_hash(peaker),
]
word = parse_word(peaker)
Assert(
word.value == 'noqa',
'Failed to parse noqa statement. '
'Expected "# noqa" but received "# {}"'.format(
word.value,
)
)
children.append(word)
return Node(
node_type=NodeType.NOQA_HEAD,
children=children,
)
Assert(
_is(TokenType.WORD, peaker.peak()),
'Unable to parse keyword: expected {} but received {}'.format(
TokenType.WORD, peaker.peak().token_type
),
token=peaker.peak(),
)
Assert(
peaker.peak().value in KEYWORDS,
'Unable to parse keyword: "{}" is not a keyword'.format(
peaker.peak().token_type
),
token=peaker.peak(),
)
token = peaker.next()
return Node(KEYWORDS[token.value], value=token.value, token=token)