refactor(fstring): make whole decimal spec optional
This commit is contained in:
@@ -86,7 +86,7 @@ class FormatSpecParser:
|
|||||||
|
|
||||||
def number(self) -> FormatSpecNumber:
|
def number(self) -> FormatSpecNumber:
|
||||||
integral: FormatSpecIntegral = self.integral()
|
integral: FormatSpecIntegral = self.integral()
|
||||||
decimal: FormatSpecDecimal = self.decimal()
|
decimal: Optional[FormatSpecDecimal] = self.decimal()
|
||||||
return FormatSpecNumber(integral=integral, decimal=decimal)
|
return FormatSpecNumber(integral=integral, decimal=decimal)
|
||||||
|
|
||||||
def integral(self) -> FormatSpecIntegral:
|
def integral(self) -> FormatSpecIntegral:
|
||||||
@@ -101,15 +101,18 @@ class FormatSpecParser:
|
|||||||
grouping=grouping
|
grouping=grouping
|
||||||
)
|
)
|
||||||
|
|
||||||
def decimal(self) -> FormatSpecDecimal:
|
def decimal(self) -> Optional[FormatSpecDecimal]:
|
||||||
|
if not self.match(TokenType.DOT):
|
||||||
|
return None
|
||||||
|
dot: Token = self.previous()
|
||||||
precision: Optional[int] = None
|
precision: Optional[int] = None
|
||||||
grouping: Optional[Token] = None
|
grouping: Optional[Token] = None
|
||||||
if self.match(TokenType.DOT):
|
|
||||||
if self.match(TokenType.NUMBER):
|
if self.match(TokenType.NUMBER):
|
||||||
precision = self.previous().value
|
precision = self.previous().value
|
||||||
if self.match(TokenType.COMMA, TokenType.UNDERSCORE):
|
if self.match(TokenType.COMMA, TokenType.UNDERSCORE):
|
||||||
grouping = self.previous()
|
grouping = self.previous()
|
||||||
return FormatSpecDecimal(
|
return FormatSpecDecimal(
|
||||||
|
dot=dot,
|
||||||
precision=precision,
|
precision=precision,
|
||||||
grouping=grouping
|
grouping=grouping
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class FormatSpecIntegral:
|
|||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class FormatSpecDecimal:
|
class FormatSpecDecimal:
|
||||||
|
dot: Token
|
||||||
precision: Optional[int]
|
precision: Optional[int]
|
||||||
grouping: Optional[Token]
|
grouping: Optional[Token]
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ class FormatSpecDecimal:
|
|||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class FormatSpecNumber:
|
class FormatSpecNumber:
|
||||||
integral: FormatSpecIntegral
|
integral: FormatSpecIntegral
|
||||||
decimal: FormatSpecDecimal
|
decimal: Optional[FormatSpecDecimal]
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ class Formatter(Expr.Visitor[str], Stmt.Visitor[str]):
|
|||||||
res += str(spec.number.integral.width)
|
res += str(spec.number.integral.width)
|
||||||
if spec.number.integral.grouping is not None:
|
if spec.number.integral.grouping is not None:
|
||||||
res += spec.number.integral.grouping.lexeme
|
res += spec.number.integral.grouping.lexeme
|
||||||
if spec.number.decimal.precision is not None or spec.number.decimal.grouping is not None:
|
if spec.number.decimal is not None:
|
||||||
res += "."
|
res += "."
|
||||||
if spec.number.decimal.precision is not None:
|
if spec.number.decimal.precision is not None:
|
||||||
res += str(spec.number.decimal.precision)
|
res += str(spec.number.decimal.precision)
|
||||||
|
|||||||
Reference in New Issue
Block a user