specialize ConversionFlag

This commit is contained in:
Jeong YunWon 2023-05-16 21:56:10 +09:00
parent 611dcc2e9b
commit e8391ee175
27 changed files with 53 additions and 98 deletions

View file

@ -28,6 +28,7 @@ assert BUILTIN_TYPE_NAMES.keys() == asdl.builtin_types
BUILTIN_INT_NAMES = { BUILTIN_INT_NAMES = {
"simple": "bool", "simple": "bool",
"is_async": "bool", "is_async": "bool",
"conversion": "ConversionFlag",
} }
RUST_KEYWORDS = { RUST_KEYWORDS = {

View file

@ -1,4 +1,4 @@
use crate::{builtin, fold::Fold}; use crate::{builtin, fold::Fold, ConversionFlag};
pub trait Foldable<T, U> { pub trait Foldable<T, U> {
type Mapped; type Mapped;
@ -67,5 +67,6 @@ simple_fold!(
builtin::String, builtin::String,
builtin::Identifier, builtin::Identifier,
bool, bool,
ConversionFlag,
builtin::Constant builtin::Constant
); );

View file

@ -931,7 +931,7 @@ impl<R> From<ExprCall<R>> for Expr<R> {
pub struct ExprFormattedValue<R = TextRange> { pub struct ExprFormattedValue<R = TextRange> {
pub range: R, pub range: R,
pub value: Box<Expr<R>>, pub value: Box<Expr<R>>,
pub conversion: Int, pub conversion: ConversionFlag,
pub format_spec: Option<Box<Expr<R>>>, pub format_spec: Option<Box<Expr<R>>>,
} }

View file

@ -1,5 +1,5 @@
#![allow(clippy::derive_partial_eq_without_eq)] #![allow(clippy::derive_partial_eq_without_eq)]
pub use crate::{builtin::*, text_size::TextSize, Node}; pub use crate::{builtin::*, text_size::TextSize, ConversionFlag, Node};
use std::fmt::{Debug, Display, Formatter}; use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData; use std::marker::PhantomData;

View file

@ -1,4 +1,4 @@
use crate::{source_code::SourceRange, text_size::TextRange, Node}; use crate::{source_code::SourceRange, text_size::TextRange, ConversionFlag, Node};
use num_complex::Complex64; use num_complex::Complex64;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use pyo3::prelude::*; use pyo3::prelude::*;
@ -73,6 +73,13 @@ impl ToPyo3Ast for bool {
} }
} }
impl ToPyo3Ast for ConversionFlag {
#[inline]
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
Ok((*self as i8).to_object(py))
}
}
impl ToPyo3Ast for crate::Constant { impl ToPyo3Ast for crate::Constant {
#[inline] #[inline]
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> { fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {

View file

@ -346,7 +346,7 @@ impl<'a> Unparser<'a> {
conversion, conversion,
format_spec, format_spec,
range: _range, range: _range,
}) => self.unparse_formatted(value, conversion.to_u32(), format_spec.as_deref())?, }) => self.unparse_formatted(value, *conversion, format_spec.as_deref())?,
Expr::JoinedStr(crate::ExprJoinedStr { Expr::JoinedStr(crate::ExprJoinedStr {
values, values,
range: _range, range: _range,
@ -519,7 +519,7 @@ impl<'a> Unparser<'a> {
fn unparse_formatted<U>( fn unparse_formatted<U>(
&mut self, &mut self,
val: &Expr<U>, val: &Expr<U>,
conversion: u32, conversion: ConversionFlag,
spec: Option<&Expr<U>>, spec: Option<&Expr<U>>,
) -> fmt::Result { ) -> fmt::Result {
let buffered = to_string_fmt(|f| Unparser::new(f).unparse_expr(val, precedence::TEST + 1)); let buffered = to_string_fmt(|f| Unparser::new(f).unparse_expr(val, precedence::TEST + 1));
@ -533,7 +533,7 @@ impl<'a> Unparser<'a> {
self.p(&buffered)?; self.p(&buffered)?;
drop(buffered); drop(buffered);
if conversion != ConversionFlag::None as u32 { if conversion != ConversionFlag::None {
self.p("!")?; self.p("!")?;
let buf = &[conversion as u8]; let buf = &[conversion as u8];
let c = std::str::from_utf8(buf).unwrap(); let c = std::str::from_utf8(buf).unwrap();
@ -568,7 +568,7 @@ impl<'a> Unparser<'a> {
conversion, conversion,
format_spec, format_spec,
range: _range, range: _range,
}) => self.unparse_formatted(value, conversion.to_u32(), format_spec.as_deref()), }) => self.unparse_formatted(value, *conversion, format_spec.as_deref()),
_ => unreachable!(), _ => unreachable!(),
} }
} }

View file

@ -121,9 +121,7 @@ expression: parse_ast
keywords: [], keywords: [],
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),
@ -217,9 +215,7 @@ expression: parse_ast
keywords: [], keywords: [],
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -233,9 +233,7 @@ expression: parse_ast
keywords: [], keywords: [],
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),
@ -269,9 +267,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),
@ -365,9 +361,7 @@ expression: parse_ast
keywords: [], keywords: [],
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),
@ -401,9 +395,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -31,9 +31,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -31,9 +31,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -31,9 +31,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -33,9 +33,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: Repr,
114,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -42,9 +42,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: Repr,
114,
),
format_spec: None, format_spec: None,
}, },
), ),
@ -87,9 +85,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: Repr,
114,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -33,9 +33,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: Some( format_spec: Some(
JoinedStr( JoinedStr(
ExprJoinedStr { ExprJoinedStr {

View file

@ -31,9 +31,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -31,9 +31,7 @@ expression: parse_ast
kind: None, kind: None,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -15,9 +15,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),
@ -33,9 +31,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -34,9 +34,7 @@ expression: parse_ast
], ],
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -15,9 +15,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: Some( format_spec: Some(
JoinedStr( JoinedStr(
ExprJoinedStr { ExprJoinedStr {
@ -35,9 +33,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -34,9 +34,7 @@ expression: parse_ast
], ],
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -15,9 +15,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: Some( format_spec: Some(
JoinedStr( JoinedStr(
ExprJoinedStr { ExprJoinedStr {

View file

@ -33,9 +33,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: Repr,
114,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -33,9 +33,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: Repr,
114,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -12,9 +12,7 @@ expression: parse_ast
value: None, value: None,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -22,9 +22,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -22,9 +22,7 @@ expression: parse_ast
ctx: Load, ctx: Load,
}, },
), ),
conversion: Int( conversion: None,
0,
),
format_spec: None, format_spec: None,
}, },
), ),

View file

@ -5,7 +5,7 @@
// we have to do the parsing here, manually. // we have to do the parsing here, manually.
use crate::text_size::TextRange; use crate::text_size::TextRange;
use crate::{ use crate::{
ast::{self, Constant, Expr, Int}, ast::{self, Constant, Expr},
lexer::{LexicalError, LexicalErrorType}, lexer::{LexicalError, LexicalErrorType},
parser::{parse_expression_starts_at, LalrpopError, ParseError, ParseErrorType}, parser::{parse_expression_starts_at, LalrpopError, ParseError, ParseErrorType},
token::{StringKind, Tok}, token::{StringKind, Tok},
@ -320,7 +320,7 @@ impl<'a> StringParser<'a> {
) )
})?, })?,
), ),
conversion: Int::new(conversion as _), conversion,
format_spec: spec, format_spec: spec,
range: self.range(), range: self.range(),
} }
@ -354,13 +354,13 @@ impl<'a> StringParser<'a> {
) )
})?, })?,
), ),
conversion: ast::Int::new( conversion: if conversion == ConversionFlag::None
(if conversion == ConversionFlag::None && spec.is_none() { && spec.is_none()
ConversionFlag::Repr {
} else { ConversionFlag::Repr
conversion } else {
}) as _, conversion
), },
format_spec: spec, format_spec: spec,
range: self.range(), range: self.range(),
} }