Issue#1531: Formatting

This commit is contained in:
Jukka Hertzog 2025-12-23 17:54:46 +00:00 committed by JHertz5
parent 3e259fe369
commit fe33a92014
1 changed files with 24 additions and 26 deletions

View File

@ -213,10 +213,7 @@ def find_character_literal_candidates(lQuotes, lChars):
def is_character_literal_candidate(iIndex, lQuotes, lChars):
iQuote = lQuotes[iIndex]
if (
there_is_a_single_token_between_quotes(iIndex, lQuotes)
and token_between_quotes_is_a_single_character(iQuote, lChars)
):
if there_is_a_single_token_between_quotes(iIndex, lQuotes) and token_between_quotes_is_a_single_character(iQuote, lChars):
return True
return False
@ -233,38 +230,39 @@ def token_between_quotes_is_a_single_character(iQuote, lChars):
return False
def filter_character_literal_candidates(lLiterals):
def filter_character_literal_candidates(lCandidates):
lReturn = []
lSequentialCandidates = []
for iIndex, lLiteral in enumerate(lLiterals):
for iIndex, lCandidate in enumerate(lCandidates):
# The algorithm is a bit more complex than one might expect because it needs to be able to handle sequences of
# character literals separated by a single character, e.g. `'1','0','a'`, as well as character literals inside
# qualified expressions, e.g. std_logic'('1'), both of which include "red herring" candidates.
# First, build up a sequence of sequential candidates, i.e. candidates that are separated by one character. Most
# of the time, this sequence will be one long.
lSequentialCandidates.append(lLiteral)
lSequentialCandidates.append(lCandidate)
bCandidateIsLast = iIndex != len(lLiterals) - 1
if not bCandidateIsLast:
lNextLiteral = lLiterals[iIndex + 1]
bNextCandidateOverlapsWithCurrent = lLiteral[1] == lNextLiteral[0]
if not bNextCandidateOverlapsWithCurrent:
bCandidateIsLast = iIndex == len(lCandidates) - 1
if bCandidateIsLast:
bCandidateIsLastInSequence = True
else:
lNextLiteral = lCandidates[iIndex + 1]
bCandidateIsLastInSequence = lCandidate[1] != lNextLiteral[0]
# At the end of a sequence, filter the candidates to find the character literals. Sequential candidates
# will alternate between valid and invalid candidates. For example, in `'1','0'`, the first candidate
# ('1') is valid, the second (',') is invalid, and the third ('0') is valid. The first in the sequence
# will always be valid unless a qualified expression is present. For example, in `std_logic'('1')`, the
# first candidate ('(') is invalid and the second candidate ('1') is valid. If there is a qualified
# expression, the number of candidates will be even; otherwise the number will be odd.
# Therefore, filter by selecting every second candidate, starting with 0 if the number of candidates is
# odd and starting with 1 if the number of candidates is even.
iSequenceStart = (len(lSequentialCandidates) + 1) % 2
lFilteredLiterals = [lSequentialCandidates[x] for x in range(iSequenceStart, len(lSequentialCandidates), 2)]
lReturn.extend(lFilteredLiterals)
if bCandidateIsLastInSequence:
# At the end of a sequence, filter the candidates to find the character literals. Sequential candidates will
# alternate between valid and invalid candidates. For example, in `'1','0'`, the first candidate ('1') is
# valid, the second (',') is invalid, and the third ('0') is valid. The first in the sequence will always be
# valid unless a qualified expression is present. For example, in `std_logic'('1')`, the first candidate
# ('(') is invalid and the second candidate ('1') is valid. If there is a qualified expression, the number
# of candidates will be even; otherwise the number will be odd.
# Therefore, filter by selecting every second candidate, starting with 0 if the number of candidates is odd
# and starting with 1 if the number of candidates is even.
iSequenceStart = (len(lSequentialCandidates) + 1) % 2
lFilteredLiterals = [lSequentialCandidates[x] for x in range(iSequenceStart, len(lSequentialCandidates), 2)]
lReturn.extend(lFilteredLiterals)
# Clear the sequential candidates for the next sequence.
lSequentialCandidates = []
# Clear the sequential candidates for the next sequence.
lSequentialCandidates = []
return lReturn