From a14e43e03a2a6f38a6ec5f86321d562e9bf766bf Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sat, 6 May 2023 21:35:43 +0900 Subject: [PATCH] Separate byteoffset ast and located ast --- ast/Cargo.toml | 3 +- ast/asdl_rs.py | 423 ++++++++++++++++------------- ast/src/attributed.rs | 76 ++++++ ast/src/constant.rs | 17 +- ast/src/fold_helpers.rs | 2 +- ast/src/{ast_gen.rs => generic.rs} | 102 ++----- ast/src/lib.rs | 14 +- ast/src/located.rs | 95 +++++++ ast/src/locator.rs | 57 ++++ core/src/error.rs | 75 ++++- core/src/lib.rs | 9 +- core/src/location.rs | 7 +- parser/src/function.rs | 3 +- parser/src/lexer.rs | 4 +- parser/src/parser.rs | 20 +- parser/src/python.lalrpop | 209 +++++++------- parser/src/python.rs | 279 ++++++++++--------- parser/src/string.rs | 18 +- ruff_text_size/src/range.rs | 7 + ruff_text_size/src/size.rs | 28 +- scripts/update_asdl.sh | 7 + 21 files changed, 893 insertions(+), 562 deletions(-) create mode 100644 ast/src/attributed.rs rename ast/src/{ast_gen.rs => generic.rs} (96%) create mode 100644 ast/src/located.rs create mode 100644 ast/src/locator.rs create mode 100755 scripts/update_asdl.sh diff --git a/ast/Cargo.toml b/ast/Cargo.toml index 5de211d..85f3b13 100644 --- a/ast/Cargo.toml +++ b/ast/Cargo.toml @@ -8,8 +8,9 @@ repository = "https://github.com/RustPython/RustPython" license = "MIT" [features] -default = ["constant-optimization", "fold"] +default = ["constant-optimization", "fold", "location"] constant-optimization = ["fold"] +location = [] fold = [] unparse = ["rustpython-literal"] diff --git a/ast/asdl_rs.py b/ast/asdl_rs.py index 9db17c9..2b9d192 100755 --- a/ast/asdl_rs.py +++ b/ast/asdl_rs.py @@ -7,6 +7,8 @@ import textwrap from argparse import ArgumentParser from pathlib import Path +from typing import Optional, Dict +from attr import dataclass import asdl @@ -62,38 +64,62 @@ def asdl_of(name, obj): return "{} = {}".format(name, types) -class EmitVisitor(asdl.VisitorBase): - """Visit that emits lines""" - - def __init__(self, file): - self.file = file - self.identifiers = set() - super(EmitVisitor, self).__init__() - - def emit_identifier(self, name): - name = str(name) - if name in self.identifiers: - return - self.emit("_Py_IDENTIFIER(%s);" % name, 0) - self.identifiers.add(name) - - def emit(self, line, depth): - if line: - line = (" " * TABSIZE * depth) + line - self.file.write(line + "\n") - - class TypeInfo: + name: str + enum_name: Optional[str] + has_userdata: Optional[bool] + has_attributes: bool + children: set + boxed: bool + product: bool + has_expr: bool = False + def __init__(self, name): self.name = name + self.enum_name = None self.has_userdata = None + self.has_attributes = False self.children = set() self.boxed = False self.product = False + self.product_has_expr = False def __repr__(self): return f"" + @property + def rust_name(self): + return get_rust_type(self.name) + + @property + def sum_name(self): + if self.enum_name is None: + return self.name + else: + return f"{self.enum_name}_{self.name}" + + @property + def rust_sum_name(self): + rust_name = get_rust_type(self.name) + if self.enum_name is None: + return rust_name + else: + name = get_rust_type(self.enum_name) + rust_name + return name + + @property + def rust_suffix(self): + if self.product: + if self.has_attributes: + return "Data" + else: + return "" + else: + if self.has_attributes: + return "Kind" + else: + return "" + def determine_userdata(self, typeinfo, stack): if self.name in stack: return None @@ -110,6 +136,41 @@ class TypeInfo: return self.has_userdata +class TypeInfoMixin: + typeinfo: Dict[str, TypeInfo] + + def has_userdata(self, typ): + return self.typeinfo[typ].has_userdata + + def get_generics(self, typ, *generics): + if self.has_userdata(typ): + return [f"<{g}>" for g in generics] + else: + return ["" for g in generics] + + +class EmitVisitor(asdl.VisitorBase, TypeInfoMixin): + """Visit that emits lines""" + + def __init__(self, file, typeinfo): + self.file = file + self.typeinfo = typeinfo + self.identifiers = set() + super(EmitVisitor, self).__init__() + + def emit_identifier(self, name): + name = str(name) + if name in self.identifiers: + return + self.emit("_Py_IDENTIFIER(%s);" % name, 0) + self.identifiers.add(name) + + def emit(self, line, depth): + if line: + line = (" " * TABSIZE * depth) + line + self.file.write(line + "\n") + + class FindUserdataTypesVisitor(asdl.VisitorBase): def __init__(self, typeinfo): self.typeinfo = typeinfo @@ -132,21 +193,29 @@ class FindUserdataTypesVisitor(asdl.VisitorBase): info.has_userdata = False else: for t in sum.types: - self.typeinfo[t.name] = TypeInfo(t.name) + if not t.fields: + continue + t_info = TypeInfo(t.name) + t_info.enum_name = name + self.typeinfo[t.name] = t_info self.add_children(t.name, t.fields) if len(sum.types) > 1: info.boxed = True if sum.attributes: - # attributes means Located, which has the `custom: U` field + # attributes means located, which has the `custom: U` field info.has_userdata = True + info.has_attributes = True + for variant in sum.types: self.add_children(name, variant.fields) def visitProduct(self, product, name): info = self.typeinfo[name] if product.attributes: - # attributes means Located, which has the `custom: U` field + # attributes means located, which has the `custom: U` field info.has_userdata = True + info.has_attributes = True + info.has_expr = product_has_expr(product) if len(product.fields) > 2: info.boxed = True info.product = True @@ -163,24 +232,17 @@ def rust_field(field_name): return field_name -class TypeInfoEmitVisitor(EmitVisitor): - def __init__(self, file, typeinfo): - self.typeinfo = typeinfo - super().__init__(file) - - def has_userdata(self, typ): - return self.typeinfo[typ].has_userdata - - def get_generics(self, typ, *generics): - if self.has_userdata(typ): - return [f"<{g}>" for g in generics] - else: - return ["" for g in generics] +def product_has_expr(product): + return any(f.type != "identifier" for f in product.fields) -class StructVisitor(TypeInfoEmitVisitor): +class StructVisitor(EmitVisitor): """Visitor to generate typedefs for AST.""" + def __init__(self, *args, **kw): + super().__init__(*args, **kw) + self.rust_type_defs = [] + def visitModule(self, mod): for dfn in mod.dfns: self.visit(dfn) @@ -208,57 +270,56 @@ class StructVisitor(TypeInfoEmitVisitor): def sum_with_constructors(self, sum, name, depth): typeinfo = self.typeinfo[name] - enumname = rustname = get_rust_type(name) + suffix = typeinfo.rust_suffix + rustname = get_rust_type(name) # all the attributes right now are for location, so if it has attrs we - # can just wrap it in Located<> - if sum.attributes: - enumname = rustname + "Kind" + # can just wrap it in Attributed<> for t in sum.types: if not t.fields: continue - self.emit_attrs(depth) - self.typeinfo[t] = TypeInfo(t) - t_generics, t_generics_applied = self.get_generics(t.name, "U = ()", "U") - payload_name = f"{rustname}{t.name}" - self.emit(f"pub struct {payload_name}{t_generics} {{", depth) - for f in t.fields: - self.visit(f, typeinfo, "pub ", depth + 1, t.name) - self.emit("}", depth) - self.emit( - textwrap.dedent( - f""" - impl{t_generics_applied} From<{payload_name}{t_generics_applied}> for {enumname}{t_generics_applied} {{ - fn from(payload: {payload_name}{t_generics_applied}) -> Self {{ - {enumname}::{t.name}(payload) - }} - }} - """ - ), - depth, - ) + self.sum_subtype_struct(typeinfo, t, rustname, depth) generics, generics_applied = self.get_generics(name, "U = ()", "U") self.emit_attrs(depth) - self.emit(f"pub enum {enumname}{generics} {{", depth) + self.emit(f"pub enum {rustname}{suffix}{generics} {{", depth) for t in sum.types: if t.fields: - t_generics, t_generics_applied = self.get_generics( - t.name, "U = ()", "U" - ) + (t_generics_applied,) = self.get_generics(t.name, "U") self.emit( f"{t.name}({rustname}{t.name}{t_generics_applied}),", depth + 1 ) else: self.emit(f"{t.name},", depth + 1) self.emit("}", depth) - if sum.attributes: + if typeinfo.has_attributes: self.emit( - f"pub type {rustname} = Located<{enumname}{generics_applied}, U>;", + f"pub type {rustname} = Attributed<{rustname}{suffix}{generics_applied}, U>;", depth, ) self.emit("", depth) + def sum_subtype_struct(self, sum_typeinfo, t, rustname, depth): + self.emit_attrs(depth) + generics, generics_applied = self.get_generics(t.name, "U = ()", "U") + payload_name = f"{rustname}{t.name}" + self.emit(f"pub struct {payload_name}{generics} {{", depth) + for f in t.fields: + self.visit(f, sum_typeinfo, "pub ", depth + 1, t.name) + self.emit("}", depth) + self.emit( + textwrap.dedent( + f""" + impl{generics_applied} From<{payload_name}{generics_applied}> for {rustname}{sum_typeinfo.rust_suffix}{generics_applied} {{ + fn from(payload: {payload_name}{generics_applied}) -> Self {{ + {rustname}{sum_typeinfo.rust_suffix}::{t.name}(payload) + }} + }} + """ + ), + depth, + ) + def visitConstructor(self, cons, parent, depth): if cons.fields: self.emit(f"{cons.name} {{", depth) @@ -300,7 +361,7 @@ class StructVisitor(TypeInfoEmitVisitor): if product.attributes: dataname = rustname + "Data" self.emit_attrs(depth) - has_expr = any(f.type != "identifier" for f in product.fields) + has_expr = product_has_expr(product) if has_expr: datadef = f"{dataname}{generics}" else: @@ -314,20 +375,24 @@ class StructVisitor(TypeInfoEmitVisitor): if not has_expr: generics_applied = "" self.emit( - f"pub type {rustname} = Located<{dataname}{generics_applied}, U>;", + f"pub type {rustname} = Attributed<{dataname}{generics_applied}, U>;", depth, ) self.emit("", depth) -class FoldTraitDefVisitor(TypeInfoEmitVisitor): +class FoldTraitDefVisitor(EmitVisitor): def visitModule(self, mod, depth): self.emit("pub trait Fold {", depth) self.emit("type TargetU;", depth + 1) self.emit("type Error;", depth + 1) self.emit( "fn map_user(&mut self, user: U) -> Result;", - depth + 2, + depth + 1, + ) + self.emit( + "fn map_located(&mut self, located: Attributed) -> Result, Self::Error> { let custom = self.map_user(located.custom)?; Ok(Attributed { range: located.range, custom, node: located.node }) }", + depth + 1, ) for dfn in mod.dfns: self.visit(dfn, depth + 2) @@ -345,14 +410,14 @@ class FoldTraitDefVisitor(TypeInfoEmitVisitor): self.emit("}", depth) -class FoldImplVisitor(TypeInfoEmitVisitor): +class FoldImplVisitor(EmitVisitor): def visitModule(self, mod, depth): self.emit( - "fn fold_located + ?Sized, T, MT>(folder: &mut F, node: Located, f: impl FnOnce(&mut F, T) -> Result) -> Result, F::Error> {", + "fn fold_located + ?Sized, T, MT>(folder: &mut F, node: Attributed, f: impl FnOnce(&mut F, T) -> Result) -> Result, F::Error> {", depth, ) self.emit( - "Ok(Located { custom: folder.map_user(node.custom)?, range: node.range, node: f(folder, node.node)? })", + "let node = folder.map_located(node)?; Ok(Attributed { custom: node.custom, range: node.range, node: f(folder, node.node)? })", depth + 1, ) self.emit("}", depth) @@ -363,11 +428,11 @@ class FoldImplVisitor(TypeInfoEmitVisitor): self.visit(type.value, type.name, depth) def visitSum(self, sum, name, depth): + typeinfo = self.typeinfo[name] apply_t, apply_u, apply_target_u = self.get_generics( name, "T", "U", "F::TargetU" ) enumname = get_rust_type(name) - is_located = bool(sum.attributes) self.emit(f"impl Foldable for {enumname}{apply_t} {{", depth) self.emit(f"type Mapped = {enumname}{apply_u};", depth + 1) @@ -383,15 +448,13 @@ class FoldImplVisitor(TypeInfoEmitVisitor): f"pub fn fold_{name} + ?Sized>(#[allow(unused)] folder: &mut F, node: {enumname}{apply_u}) -> Result<{enumname}{apply_target_u}, F::Error> {{", depth, ) - if is_located: + if typeinfo.has_attributes: self.emit("fold_located(folder, node, |folder, node| {", depth) - rustname = enumname + "Kind" - else: - rustname = enumname + self.emit("match node {", depth + 1) for cons in sum.types: fields_pattern = self.make_pattern( - enumname, rustname, cons.name, cons.fields + enumname, typeinfo.rust_suffix, cons.name, cons.fields ) self.emit( f"{fields_pattern[0]} {{ {fields_pattern[1]} }} {fields_pattern[2]} => {{", @@ -402,7 +465,7 @@ class FoldImplVisitor(TypeInfoEmitVisitor): ) self.emit("}", depth + 2) self.emit("}", depth + 1) - if is_located: + if typeinfo.has_attributes: self.emit("})", depth) self.emit("}", depth) @@ -411,7 +474,7 @@ class FoldImplVisitor(TypeInfoEmitVisitor): name, "T", "U", "F::TargetU" ) structname = get_rust_type(name) - is_located = bool(product.attributes) + has_attributes = bool(product.attributes) self.emit(f"impl Foldable for {structname}{apply_t} {{", depth) self.emit(f"type Mapped = {structname}{apply_u};", depth + 1) @@ -427,7 +490,7 @@ class FoldImplVisitor(TypeInfoEmitVisitor): f"pub fn fold_{name} + ?Sized>(#[allow(unused)] folder: &mut F, node: {structname}{apply_u}) -> Result<{structname}{apply_target_u}, F::Error> {{", depth, ) - if is_located: + if has_attributes: self.emit("fold_located(folder, node, |folder, node| {", depth) rustname = structname + "Data" else: @@ -435,16 +498,16 @@ class FoldImplVisitor(TypeInfoEmitVisitor): fields_pattern = self.make_pattern(rustname, structname, None, product.fields) self.emit(f"let {rustname} {{ {fields_pattern[1]} }} = node;", depth + 1) self.gen_construction(rustname, product.fields, "", depth + 1) - if is_located: + if has_attributes: self.emit("})", depth) self.emit("}", depth) - def make_pattern(self, rustname, pyname, fieldname, fields): + def make_pattern(self, rustname, suffix, fieldname, fields): if fields: - header = f"{pyname}::{fieldname}({rustname}{fieldname}" + header = f"{rustname}{suffix}::{fieldname}({rustname}{fieldname}" footer = ")" else: - header = f"{pyname}::{fieldname}" + header = f"{rustname}{suffix}::{fieldname}" footer = "" body = ",".join(rust_field(f.name) for f in fields) @@ -458,7 +521,7 @@ class FoldImplVisitor(TypeInfoEmitVisitor): self.emit(f"}}{footer})", depth) -class FoldModuleVisitor(TypeInfoEmitVisitor): +class FoldModuleVisitor(EmitVisitor): def visitModule(self, mod): depth = 0 self.emit('#[cfg(feature = "fold")]', depth) @@ -576,10 +639,10 @@ class TraitImplVisitor(EmitVisitor): if sum.attributes: rustname = enumname + "Kind" - self.emit(f"impl NamedNode for ast::{rustname} {{", depth) + self.emit(f"impl NamedNode for ast::located::{rustname} {{", depth) self.emit(f"const NAME: &'static str = {json.dumps(name)};", depth + 1) self.emit("}", depth) - self.emit(f"impl Node for ast::{rustname} {{", depth) + self.emit(f"impl Node for ast::located::{rustname} {{", depth) self.emit( "fn ast_to_object(self, _vm: &VirtualMachine) -> PyObjectRef {", depth + 1 ) @@ -597,10 +660,12 @@ class TraitImplVisitor(EmitVisitor): self.emit("}", depth) def constructor_to_object(self, cons, enumname, rustname, depth): - self.emit(f"ast::{rustname}::{cons.name}", depth) + self.emit(f"ast::located::{rustname}::{cons.name}", depth) if cons.fields: fields_pattern = self.make_pattern(cons.fields) - self.emit(f"( ast::{enumname}{cons.name} {{ {fields_pattern} }} )", depth) + self.emit( + f"( ast::located::{enumname}{cons.name} {{ {fields_pattern} }} )", depth + ) self.emit(" => {", depth) self.make_node(cons.name, cons.fields, depth + 1) self.emit("}", depth) @@ -610,15 +675,17 @@ class TraitImplVisitor(EmitVisitor): if product.attributes: structname += "Data" - self.emit(f"impl NamedNode for ast::{structname} {{", depth) + self.emit(f"impl NamedNode for ast::located::{structname} {{", depth) self.emit(f"const NAME: &'static str = {json.dumps(name)};", depth + 1) self.emit("}", depth) - self.emit(f"impl Node for ast::{structname} {{", depth) + self.emit(f"impl Node for ast::located::{structname} {{", depth) self.emit( "fn ast_to_object(self, _vm: &VirtualMachine) -> PyObjectRef {", depth + 1 ) fields_pattern = self.make_pattern(product.fields) - self.emit(f"let ast::{structname} {{ {fields_pattern} }} = self;", depth + 2) + self.emit( + f"let ast::located::{structname} {{ {fields_pattern} }} = self;", depth + 2 + ) self.make_node(name, product.fields, depth + 2) self.emit("}", depth + 1) self.emit( @@ -656,11 +723,14 @@ class TraitImplVisitor(EmitVisitor): for cons in sum.types: self.emit(f"if _cls.is(Node{cons.name}::static_type()) {{", depth) if cons.fields: - self.emit(f"ast::{rustname}::{cons.name} (ast::{enumname}{cons.name} {{", depth + 1) + self.emit( + f"ast::located::{rustname}::{cons.name} (ast::located::{enumname}{cons.name} {{", + depth + 1, + ) self.gen_construction_fields(cons, sumname, depth + 1) self.emit("})", depth + 1) else: - self.emit(f"ast::{rustname}::{cons.name}", depth + 1) + self.emit(f"ast::located::{rustname}::{cons.name}", depth + 1) self.emit("} else", depth) self.emit("{", depth) @@ -684,14 +754,14 @@ class TraitImplVisitor(EmitVisitor): ) def gen_construction(self, cons_path, cons, name, depth): - self.emit(f"ast::{cons_path} {{", depth) + self.emit(f"ast::located::{cons_path} {{", depth) self.gen_construction_fields(cons, name, depth + 1) self.emit("}", depth) def extract_location(self, typename, depth): row = self.decode_field(asdl.Field("int", "lineno"), typename) column = self.decode_field(asdl.Field("int", "col_offset"), typename) - self.emit(f"let _location = ast::Location::new({row}, {column});", depth) + self.emit(f"let _location = Location::new({row}, {column});", depth) def decode_field(self, field, typename): name = json.dumps(field.name) @@ -711,84 +781,18 @@ class ChainOfVisitors: v.emit("", 0) -def write_ast_def(mod, typeinfo, f): +def write_generic_def(mod, typeinfo, f): f.write( textwrap.dedent( """ #![allow(clippy::derive_partial_eq_without_eq)] - pub use crate::constant::*; - pub use rustpython_compiler_core::text_size::{TextSize, TextRange}; + pub use crate::{Attributed, constant::*}; + use rustpython_compiler_core::{text_size::{TextSize, TextRange}}; type Ident = String; \n - """ - ) - ) - StructVisitor(f, typeinfo).emit_attrs(0) - f.write( - textwrap.dedent( - """ - pub struct Located { - pub range: TextRange, - pub custom: U, - pub node: T, - } - - impl Located { - pub fn new(start: TextSize, end: TextSize, node: T) -> Self { - Self { range: TextRange::new(start, end), custom: (), node } - } - - /// Creates a new node that spans the position specified by `range`. - pub fn with_range(node: T, range: TextRange) -> Self { - Self { - range, - custom: (), - node, - } - } - - /// Returns the absolute start position of the node from the beginning of the document. - #[inline] - pub const fn start(&self) -> TextSize { - self.range.start() - } - - /// Returns the node - #[inline] - pub fn node(&self) -> &T { - &self.node - } - - /// Consumes self and returns the node. - #[inline] - pub fn into_node(self) -> T { - self.node - } - - /// Returns the `range` of the node. The range offsets are absolute to the start of the document. - #[inline] - pub const fn range(&self) -> TextRange { - self.range - } - - /// Returns the absolute position at which the node ends in the source document. - #[inline] - pub const fn end(&self) -> TextSize { - self.range.end() - } - } - - impl std::ops::Deref for Located { - type Target = T; - - fn deref(&self) -> &Self::Target { - &self.node - } - } - \n - """.lstrip() + """ ) ) @@ -796,24 +800,59 @@ def write_ast_def(mod, typeinfo, f): c.visit(mod) -def write_ast_mod(mod, f): +def write_located_def(typeinfo, f): f.write( textwrap.dedent( """ - #![allow(clippy::all)] + use rustpython_compiler_core::LocationRange; - use super::*; - use crate::common::ascii; + pub type Located = super::generic::Attributed; + """ + ) + ) + for info in typeinfo.values(): + if info.has_userdata: + generics = "::" + else: + generics = "" + f.write( + f"pub type {info.rust_sum_name} = super::generic::{info.rust_sum_name}{generics};\n" + ) + if info.rust_suffix: + if info.rust_suffix == "Data" and not info.has_expr: + generics = "" + f.write( + f"pub type {info.rust_sum_name}{info.rust_suffix} = super::generic::{info.rust_sum_name}{info.rust_suffix}{generics};\n" + ) - """ + +def write_ast_mod(mod, typeinfo, f): + f.write( + textwrap.dedent( + """ + #![allow(clippy::all)] + + use super::*; + use crate::common::ascii; + """ ) ) - c = ChainOfVisitors(ClassDefVisitor(f), TraitImplVisitor(f), ExtendModuleVisitor(f)) + c = ChainOfVisitors( + ClassDefVisitor(f, typeinfo), + TraitImplVisitor(f, typeinfo), + ExtendModuleVisitor(f, typeinfo), + ) c.visit(mod) -def main(input_filename, ast_mod_filename, ast_def_filename, dump_module=False): +def main( + input_filename, + generic_filename, + located_filename, + module_filename, + dump_module=False, +): auto_gen_msg = AUTOGEN_MESSAGE.format("/".join(Path(__file__).parts[-2:])) mod = asdl.parse(input_filename) if dump_module: @@ -825,22 +864,34 @@ def main(input_filename, ast_mod_filename, ast_def_filename, dump_module=False): typeinfo = {} FindUserdataTypesVisitor(typeinfo).visit(mod) - with ast_def_filename.open("w") as def_file, ast_mod_filename.open("w") as mod_file: - def_file.write(auto_gen_msg) - write_ast_def(mod, typeinfo, def_file) + with generic_filename.open("w") as generic_file, located_filename.open( + "w" + ) as located_file: + generic_file.write(auto_gen_msg) + write_generic_def(mod, typeinfo, generic_file) + located_file.write(auto_gen_msg) + write_located_def(typeinfo, located_file) - mod_file.write(auto_gen_msg) - write_ast_mod(mod, mod_file) + with module_filename.open("w") as module_file: + module_file.write(auto_gen_msg) + write_ast_mod(mod, typeinfo, module_file) - print(f"{ast_def_filename}, {ast_mod_filename} regenerated.") + print(f"{generic_filename}, {located_filename}, {module_filename} regenerated.") if __name__ == "__main__": parser = ArgumentParser() parser.add_argument("input_file", type=Path) - parser.add_argument("-M", "--mod-file", type=Path, required=True) - parser.add_argument("-D", "--def-file", type=Path, required=True) + parser.add_argument("-G", "--generic-file", type=Path, required=True) + parser.add_argument("-L", "--located-file", type=Path, required=True) + parser.add_argument("-M", "--module-file", type=Path, required=True) parser.add_argument("-d", "--dump-module", action="store_true") args = parser.parse_args() - main(args.input_file, args.mod_file, args.def_file, args.dump_module) + main( + args.input_file, + args.generic_file, + args.located_file, + args.module_file, + args.dump_module, + ) diff --git a/ast/src/attributed.rs b/ast/src/attributed.rs new file mode 100644 index 0000000..381d46f --- /dev/null +++ b/ast/src/attributed.rs @@ -0,0 +1,76 @@ +use rustpython_compiler_core::{ + text_size::{TextRange, TextSize}, + Location, LocationRange, +}; + +#[derive(Clone, Debug, PartialEq)] +pub struct Attributed { + pub range: TextRange, + pub custom: U, + pub node: T, +} + +impl Attributed { + /// Returns the node + #[inline] + pub fn node(&self) -> &T { + &self.node + } + + /// Returns the `range` of the node. The range offsets are absolute to the start of the document. + #[inline] + pub const fn range(&self) -> TextRange { + self.range + } + + /// Returns the absolute start position of the node from the beginning of the document. + #[inline] + pub const fn start(&self) -> TextSize { + self.range.start() + } + + /// Returns the absolute position at which the node ends in the source document. + #[inline] + pub const fn end(&self) -> TextSize { + self.range.end() + } +} + +impl Attributed { + /// Creates a new node that spans the position specified by `range`. + pub fn new(range: impl Into, node: T) -> Self { + Self { + range: range.into(), + custom: (), + node, + } + } + + /// Consumes self and returns the node. + #[inline] + pub fn into_node(self) -> T { + self.node + } +} + +impl Attributed { + /// Returns the absolute start position of the node from the beginning of the document. + #[inline] + pub const fn location(&self) -> Location { + self.custom.start + } + + /// Returns the absolute position at which the node ends in the source document. + #[inline] + pub const fn end_location(&self) -> Location { + self.custom.end + } +} + +impl std::ops::Deref for Attributed { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.node + } +} diff --git a/ast/src/constant.rs b/ast/src/constant.rs index a059b8d..aad3652 100644 --- a/ast/src/constant.rs +++ b/ast/src/constant.rs @@ -137,6 +137,7 @@ impl crate::fold::Fold for ConstantOptimizer { #[cfg(test)] mod tests { use super::*; + use rustpython_compiler_core::text_size::TextRange; #[cfg(feature = "constant-optimization")] #[test] @@ -146,13 +147,13 @@ mod tests { let range = TextRange::default(); #[allow(clippy::let_unit_value)] let custom = (); - let ast = Located { + let ast = Attributed { range, custom, node: ExprTuple { ctx: ExprContext::Load, elts: vec![ - Located { + Attributed { range, custom, node: ExprConstant { @@ -161,7 +162,7 @@ mod tests { } .into(), }, - Located { + Attributed { range, custom, node: ExprConstant { @@ -170,13 +171,13 @@ mod tests { } .into(), }, - Located { + Attributed { range, custom, node: ExprTuple { ctx: ExprContext::Load, elts: vec![ - Located { + Attributed { range, custom, node: ExprConstant { @@ -185,7 +186,7 @@ mod tests { } .into(), }, - Located { + Attributed { range, custom, node: ExprConstant { @@ -194,7 +195,7 @@ mod tests { } .into(), }, - Located { + Attributed { range, custom, node: ExprConstant { @@ -216,7 +217,7 @@ mod tests { .unwrap_or_else(|e| match e {}); assert_eq!( new_ast, - Located { + Attributed { range, custom, node: ExprConstant { diff --git a/ast/src/fold_helpers.rs b/ast/src/fold_helpers.rs index 969ea4e..83c7ae4 100644 --- a/ast/src/fold_helpers.rs +++ b/ast/src/fold_helpers.rs @@ -1,6 +1,6 @@ use crate::{constant, fold::Fold}; -pub(crate) trait Foldable { +pub trait Foldable { type Mapped; fn fold + ?Sized>( self, diff --git a/ast/src/ast_gen.rs b/ast/src/generic.rs similarity index 96% rename from ast/src/ast_gen.rs rename to ast/src/generic.rs index dc45167..6c108f9 100644 --- a/ast/src/ast_gen.rs +++ b/ast/src/generic.rs @@ -2,75 +2,11 @@ #![allow(clippy::derive_partial_eq_without_eq)] -pub use crate::constant::*; -pub use rustpython_compiler_core::text_size::{TextRange, TextSize}; +pub use crate::{constant::*, Attributed}; +use rustpython_compiler_core::text_size::{TextRange, TextSize}; type Ident = String; -#[derive(Clone, Debug, PartialEq)] -pub struct Located { - pub range: TextRange, - pub custom: U, - pub node: T, -} - -impl Located { - pub fn new(start: TextSize, end: TextSize, node: T) -> Self { - Self { - range: TextRange::new(start, end), - custom: (), - node, - } - } - - /// Creates a new node that spans the position specified by `range`. - pub fn with_range(node: T, range: TextRange) -> Self { - Self { - range, - custom: (), - node, - } - } - - /// Returns the absolute start position of the node from the beginning of the document. - #[inline] - pub const fn start(&self) -> TextSize { - self.range.start() - } - - /// Returns the node - #[inline] - pub fn node(&self) -> &T { - &self.node - } - - /// Consumes self and returns the node. - #[inline] - pub fn into_node(self) -> T { - self.node - } - - /// Returns the `range` of the node. The range offsets are absolute to the start of the document. - #[inline] - pub const fn range(&self) -> TextRange { - self.range - } - - /// Returns the absolute position at which the node ends in the source document. - #[inline] - pub const fn end(&self) -> TextSize { - self.range.end() - } -} - -impl std::ops::Deref for Located { - type Target = T; - - fn deref(&self) -> &Self::Target { - &self.node - } -} - #[derive(Clone, Debug, PartialEq)] pub struct ModModule { pub body: Vec>, @@ -467,7 +403,7 @@ pub enum StmtKind { Break, Continue, } -pub type Stmt = Located, U>; +pub type Stmt = Attributed, U>; #[derive(Clone, Debug, PartialEq)] pub struct ExprBoolOp { @@ -827,7 +763,7 @@ pub enum ExprKind { Tuple(ExprTuple), Slice(ExprSlice), } -pub type Expr = Located, U>; +pub type Expr = Attributed, U>; #[derive(Clone, Debug, PartialEq)] pub enum ExprContext { @@ -906,7 +842,7 @@ impl From> for ExcepthandlerKind { pub enum ExcepthandlerKind { ExceptHandler(ExcepthandlerExceptHandler), } -pub type Excepthandler = Located, U>; +pub type Excepthandler = Attributed, U>; #[derive(Clone, Debug, PartialEq)] pub struct Arguments { @@ -925,21 +861,21 @@ pub struct ArgData { pub annotation: Option>>, pub type_comment: Option, } -pub type Arg = Located, U>; +pub type Arg = Attributed, U>; #[derive(Clone, Debug, PartialEq)] pub struct KeywordData { pub arg: Option, pub value: Expr, } -pub type Keyword = Located, U>; +pub type Keyword = Attributed, U>; #[derive(Clone, Debug, PartialEq)] pub struct AliasData { pub name: Ident, pub asname: Option, } -pub type Alias = Located; +pub type Alias = Attributed; #[derive(Clone, Debug, PartialEq)] pub struct Withitem { @@ -1059,7 +995,7 @@ pub enum PatternKind { MatchAs(PatternMatchAs), MatchOr(PatternMatchOr), } -pub type Pattern = Located, U>; +pub type Pattern = Attributed, U>; #[derive(Clone, Debug, PartialEq)] pub struct TypeIgnoreTypeIgnore { @@ -1086,6 +1022,17 @@ pub mod fold { type TargetU; type Error; fn map_user(&mut self, user: U) -> Result; + fn map_located( + &mut self, + located: Attributed, + ) -> Result, Self::Error> { + let custom = self.map_user(located.custom)?; + Ok(Attributed { + range: located.range, + custom, + node: located.node, + }) + } fn fold_mod(&mut self, node: Mod) -> Result, Self::Error> { fold_mod(self, node) } @@ -1164,11 +1111,12 @@ pub mod fold { } fn fold_located + ?Sized, T, MT>( folder: &mut F, - node: Located, + node: Attributed, f: impl FnOnce(&mut F, T) -> Result, - ) -> Result, F::Error> { - Ok(Located { - custom: folder.map_user(node.custom)?, + ) -> Result, F::Error> { + let node = folder.map_located(node)?; + Ok(Attributed { + custom: node.custom, range: node.range, node: f(folder, node.node)?, }) diff --git a/ast/src/lib.rs b/ast/src/lib.rs index cf7d6ad..37014c7 100644 --- a/ast/src/lib.rs +++ b/ast/src/lib.rs @@ -1,11 +1,21 @@ -mod ast_gen; +mod attributed; mod constant; #[cfg(feature = "fold")] mod fold_helpers; +mod generic; mod impls; +#[cfg(feature = "location")] +pub mod located; +#[cfg(feature = "location")] +mod locator; + #[cfg(feature = "unparse")] mod unparse; -pub use ast_gen::*; +pub use attributed::Attributed; +pub use constant::{Constant, ConversionFlag}; +pub use generic::*; +#[cfg(feature = "location")] +pub use locator::Locator; pub type Suite = Vec>; diff --git a/ast/src/located.rs b/ast/src/located.rs new file mode 100644 index 0000000..ad5c27d --- /dev/null +++ b/ast/src/located.rs @@ -0,0 +1,95 @@ +// File automatically generated by ast/asdl_rs.py. + +use rustpython_compiler_core::LocationRange; + +pub type Located = super::generic::Attributed; +pub type Mod = super::generic::Mod; +pub type ModModule = super::generic::ModModule; +pub type ModInteractive = super::generic::ModInteractive; +pub type ModExpression = super::generic::ModExpression; +pub type ModFunctionType = super::generic::ModFunctionType; +pub type Stmt = super::generic::Stmt; +pub type StmtKind = super::generic::StmtKind; +pub type StmtFunctionDef = super::generic::StmtFunctionDef; +pub type StmtAsyncFunctionDef = super::generic::StmtAsyncFunctionDef; +pub type StmtClassDef = super::generic::StmtClassDef; +pub type StmtReturn = super::generic::StmtReturn; +pub type StmtDelete = super::generic::StmtDelete; +pub type StmtAssign = super::generic::StmtAssign; +pub type StmtAugAssign = super::generic::StmtAugAssign; +pub type StmtAnnAssign = super::generic::StmtAnnAssign; +pub type StmtFor = super::generic::StmtFor; +pub type StmtAsyncFor = super::generic::StmtAsyncFor; +pub type StmtWhile = super::generic::StmtWhile; +pub type StmtIf = super::generic::StmtIf; +pub type StmtWith = super::generic::StmtWith; +pub type StmtAsyncWith = super::generic::StmtAsyncWith; +pub type StmtMatch = super::generic::StmtMatch; +pub type StmtRaise = super::generic::StmtRaise; +pub type StmtTry = super::generic::StmtTry; +pub type StmtTryStar = super::generic::StmtTryStar; +pub type StmtAssert = super::generic::StmtAssert; +pub type StmtImport = super::generic::StmtImport; +pub type StmtImportFrom = super::generic::StmtImportFrom; +pub type StmtGlobal = super::generic::StmtGlobal; +pub type StmtNonlocal = super::generic::StmtNonlocal; +pub type StmtExpr = super::generic::StmtExpr; +pub type Expr = super::generic::Expr; +pub type ExprKind = super::generic::ExprKind; +pub type ExprBoolOp = super::generic::ExprBoolOp; +pub type ExprNamedExpr = super::generic::ExprNamedExpr; +pub type ExprBinOp = super::generic::ExprBinOp; +pub type ExprUnaryOp = super::generic::ExprUnaryOp; +pub type ExprLambda = super::generic::ExprLambda; +pub type ExprIfExp = super::generic::ExprIfExp; +pub type ExprDict = super::generic::ExprDict; +pub type ExprSet = super::generic::ExprSet; +pub type ExprListComp = super::generic::ExprListComp; +pub type ExprSetComp = super::generic::ExprSetComp; +pub type ExprDictComp = super::generic::ExprDictComp; +pub type ExprGeneratorExp = super::generic::ExprGeneratorExp; +pub type ExprAwait = super::generic::ExprAwait; +pub type ExprYield = super::generic::ExprYield; +pub type ExprYieldFrom = super::generic::ExprYieldFrom; +pub type ExprCompare = super::generic::ExprCompare; +pub type ExprCall = super::generic::ExprCall; +pub type ExprFormattedValue = super::generic::ExprFormattedValue; +pub type ExprJoinedStr = super::generic::ExprJoinedStr; +pub type ExprConstant = super::generic::ExprConstant; +pub type ExprAttribute = super::generic::ExprAttribute; +pub type ExprSubscript = super::generic::ExprSubscript; +pub type ExprStarred = super::generic::ExprStarred; +pub type ExprName = super::generic::ExprName; +pub type ExprList = super::generic::ExprList; +pub type ExprTuple = super::generic::ExprTuple; +pub type ExprSlice = super::generic::ExprSlice; +pub type ExprContext = super::generic::ExprContext; +pub type Boolop = super::generic::Boolop; +pub type Operator = super::generic::Operator; +pub type Unaryop = super::generic::Unaryop; +pub type Cmpop = super::generic::Cmpop; +pub type Comprehension = super::generic::Comprehension; +pub type Excepthandler = super::generic::Excepthandler; +pub type ExcepthandlerKind = super::generic::ExcepthandlerKind; +pub type ExcepthandlerExceptHandler = super::generic::ExcepthandlerExceptHandler; +pub type Arguments = super::generic::Arguments; +pub type Arg = super::generic::Arg; +pub type ArgData = super::generic::ArgData; +pub type Keyword = super::generic::Keyword; +pub type KeywordData = super::generic::KeywordData; +pub type Alias = super::generic::Alias; +pub type AliasData = super::generic::AliasData; +pub type Withitem = super::generic::Withitem; +pub type MatchCase = super::generic::MatchCase; +pub type Pattern = super::generic::Pattern; +pub type PatternKind = super::generic::PatternKind; +pub type PatternMatchValue = super::generic::PatternMatchValue; +pub type PatternMatchSingleton = super::generic::PatternMatchSingleton; +pub type PatternMatchSequence = super::generic::PatternMatchSequence; +pub type PatternMatchMapping = super::generic::PatternMatchMapping; +pub type PatternMatchClass = super::generic::PatternMatchClass; +pub type PatternMatchStar = super::generic::PatternMatchStar; +pub type PatternMatchAs = super::generic::PatternMatchAs; +pub type PatternMatchOr = super::generic::PatternMatchOr; +pub type TypeIgnore = super::generic::TypeIgnore; +pub type TypeIgnoreTypeIgnore = super::generic::TypeIgnoreTypeIgnore; diff --git a/ast/src/locator.rs b/ast/src/locator.rs new file mode 100644 index 0000000..83a86b1 --- /dev/null +++ b/ast/src/locator.rs @@ -0,0 +1,57 @@ +use crate::attributed::Attributed; +use crate::fold_helpers::Foldable; +use rustpython_compiler_core::{ + text_size::{TextRange, TextSize}, + Location, LocationRange, +}; + +/// Converts source code byte-offset to Python convention line and column numbers. +#[derive(Default)] +pub struct Locator<'a> { + source: &'a str, +} + +impl<'a> Locator<'a> { + #[inline] + pub fn new(source: &'a str) -> Self { + Self { source } + } + + pub fn source(&'a self) -> &'a str { + self.source + } + + pub fn locate(&mut self, offset: TextSize) -> Location { + todo!() + } + + pub fn locate_range(&mut self, range: TextRange) -> LocationRange { + self.locate(range.start())..self.locate(range.end()) + } + + pub fn locate_ast>(&mut self, ast: X) -> X::Mapped { + ast.fold(self).unwrap() + } +} + +impl crate::fold::Fold<()> for Locator<'_> { + type TargetU = LocationRange; + type Error = std::convert::Infallible; + + #[cold] + fn map_user(&mut self, _user: ()) -> Result { + unreachable!("implemented map_located"); + } + + fn map_located( + &mut self, + node: Attributed, + ) -> Result, Self::Error> { + let location = self.locate_range(node.range); + Ok(Attributed { + range: node.range, + custom: location, + node: node.node, + }) + } +} diff --git a/core/src/error.rs b/core/src/error.rs index d62f704..2380926 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -1,11 +1,10 @@ -use ruff_text_size::TextSize; -use std::error::Error as StdError; +use crate::{text_size::TextSize, Location}; use std::fmt::Display; #[derive(Debug, PartialEq, Eq)] pub struct BaseError { pub error: T, - pub location: TextSize, + pub offset: TextSize, pub source_path: String, } @@ -17,11 +16,11 @@ impl std::ops::Deref for BaseError { } } -impl StdError for BaseError +impl std::error::Error for BaseError where - T: StdError + 'static, + T: std::error::Error + 'static, { - fn source(&self) -> Option<&(dyn StdError + 'static)> { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.error) } } @@ -35,7 +34,7 @@ where f, "{} at byte offset {}", &self.error, - u32::from(self.location) + u32::from(self.offset) ) } } @@ -51,7 +50,7 @@ impl BaseError { { Self { error: obj.error.into(), - location: obj.location, + offset: obj.offset, source_path: obj.source_path, } } @@ -62,4 +61,64 @@ impl BaseError { { BaseError::from(self) } + + pub fn into_located(self, locator: &str) -> LocatedError + where + T: Into, + { + todo!() + } +} + +#[derive(Debug, PartialEq, Eq)] +pub struct LocatedError { + pub error: T, + pub location: Location, + pub source_path: String, +} + +impl LocatedError { + pub fn error(self) -> T { + self.error + } + + pub fn from(obj: LocatedError) -> Self + where + U: Into, + { + Self { + error: obj.error.into(), + location: obj.location, + source_path: obj.source_path, + } + } + + pub fn into(self) -> LocatedError + where + T: Into, + { + LocatedError::from(self) + } +} + +impl Display for LocatedError +where + T: std::fmt::Display, +{ + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!( + f, + "{} at row {} col {}", + &self.error, self.location.row, self.location.column, + ) + } +} + +impl std::error::Error for LocatedError +where + T: std::error::Error + 'static, +{ + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(&self.error) + } } diff --git a/core/src/lib.rs b/core/src/lib.rs index 76b0ae2..596da01 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -8,8 +8,13 @@ pub mod marshal; mod mode; pub use bytecode::*; -pub use error::BaseError; -pub use location::Location; +pub use error::{BaseError, LocatedError}; +pub use location::{Location, LocationRange}; pub use mode::Mode; pub use ruff_text_size as text_size; // re-export mandatory and frequently accessed dependency + +// FIXME: temp code +pub fn to_location(offset: &text_size::TextSize, source: &str) -> Location { + todo!() +} diff --git a/core/src/location.rs b/core/src/location.rs index 2a3c81c..65f9c9c 100644 --- a/core/src/location.rs +++ b/core/src/location.rs @@ -1,9 +1,6 @@ -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; - /// Source code location. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Location { pub(super) row: u32, pub(super) column: u32, @@ -96,6 +93,8 @@ impl Location { } } +pub type LocationRange = std::ops::Range; + #[cfg(test)] mod tests { use super::*; diff --git a/parser/src/function.rs b/parser/src/function.rs index 810e622..803ede1 100644 --- a/parser/src/function.rs +++ b/parser/src/function.rs @@ -114,8 +114,7 @@ pub(crate) fn parse_args(func_args: Vec) -> Result impl Iterator + '_ { pub fn lex_located( source: &str, mode: Mode, - start_location: TextSize, + start_offset: TextSize, ) -> impl Iterator + '_ { - SoftKeywordTransformer::new(Lexer::new(source.chars(), start_location), mode) + SoftKeywordTransformer::new(Lexer::new(source.chars(), start_offset), mode) } impl Lexer diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 96edb6a..85337f8 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -92,9 +92,9 @@ pub fn parse_expression(source: &str, path: &str) -> Result Result { - parse_located(source, Mode::Expression, path, location).map(|top| match top { + parse_located(source, Mode::Expression, path, offset).map(|top| match top { ast::Mod::Expression(ast::ModExpression { body }) => *body, _ => unreachable!(), }) @@ -161,9 +161,9 @@ pub fn parse_located( source: &str, mode: Mode, source_path: &str, - location: TextSize, + offset: TextSize, ) -> Result { - let lxr = lexer::lex_located(source, mode, location); + let lxr = lexer::lex_located(source, mode, offset); parse_tokens(lxr, mode, source_path) } @@ -233,17 +233,17 @@ fn parse_error_from_lalrpop( // TODO: Are there cases where this isn't an EOF? LalrpopError::InvalidToken { location } => ParseError { error: ParseErrorType::Eof, - location, + offset: location, source_path, }, LalrpopError::ExtraToken { token } => ParseError { error: ParseErrorType::ExtraToken(token.1), - location: token.0, + offset: token.0, source_path, }, LalrpopError::User { error } => ParseError { error: ParseErrorType::Lexical(error.error), - location: error.location, + offset: error.location, source_path, }, LalrpopError::UnrecognizedToken { token, expected } => { @@ -252,7 +252,7 @@ fn parse_error_from_lalrpop( let expected = (expected.len() == 1).then(|| expected[0].clone()); ParseError { error: ParseErrorType::UnrecognizedToken(token.1, expected), - location: token.0, + offset: token.0, source_path, } } @@ -262,13 +262,13 @@ fn parse_error_from_lalrpop( if indent_error { ParseError { error: ParseErrorType::Lexical(LexicalErrorType::IndentationError), - location, + offset: location, source_path, } } else { ParseError { error: ParseErrorType::Eof, - location, + offset: location, source_path, } } diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index ebcfdad..6da0315 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -68,7 +68,7 @@ SmallStatement: ast::Stmt = { PassStatement: ast::Stmt = { "pass" => { ast::Stmt::new( - location, + location.. end_location, ast::StmtKind::Pass, ) @@ -78,7 +78,7 @@ PassStatement: ast::Stmt = { DelStatement: ast::Stmt = { "del" => { ast::Stmt::new( - location, + location.. end_location, ast::StmtDelete { targets: targets.into_iter().map(|expr| set_context(expr, ast::ExprContext::Del)).collect() }.into() ) @@ -90,7 +90,7 @@ ExpressionStatement: ast::Stmt = { // Just an expression, no assignment: if suffix.is_empty() { ast::Stmt::new( - location, + location.. end_location, ast::StmtExpr { value: Box::new(expression) }.into() ) @@ -105,7 +105,7 @@ ExpressionStatement: ast::Stmt = { let value = Box::new(values.into_iter().next().unwrap()); ast::Stmt::new( - location, + location.. end_location, ast::StmtAssign { targets, value, type_comment: None }.into() ) @@ -113,7 +113,7 @@ ExpressionStatement: ast::Stmt = { }, => { ast::Stmt::new( - location, + location.. end_location, ast::StmtAugAssign { target: Box::new(set_context(target, ast::ExprContext::Store)), @@ -125,7 +125,7 @@ ExpressionStatement: ast::Stmt = { > ":" > => { let simple = matches!(target.node, ast::ExprKind::Name { .. }); ast::Stmt::new( - location, + location.. end_location, ast::StmtAnnAssign { target: Box::new(set_context(target, ast::ExprContext::Store)), @@ -186,28 +186,28 @@ AugAssign: ast::Operator = { FlowStatement: ast::Stmt = { "break" => { ast::Stmt::new( - location, + location.. end_location, ast::StmtKind::Break, ) }, "continue" => { ast::Stmt::new( - location, + location.. end_location, ast::StmtKind::Continue, ) }, "return" => { ast::Stmt::new( - location, + location.. end_location, ast::StmtReturn { value: value.map(Box::new) }.into() ) }, => { ast::Stmt::new( - location, + location.. end_location, ast::StmtExpr { value: Box::new(expression) }.into() ) @@ -218,14 +218,14 @@ FlowStatement: ast::Stmt = { RaiseStatement: ast::Stmt = { "raise" => { ast::Stmt::new( - location, + location.. end_location, ast::StmtRaise { exc: None, cause: None }.into() ) }, "raise" > )?> => { ast::Stmt::new( - location, + location.. end_location, ast::StmtRaise { exc: Some(Box::new(t)), cause: c.map(|x| Box::new(x.1)) }.into() ) @@ -235,7 +235,7 @@ RaiseStatement: ast::Stmt = { ImportStatement: ast::Stmt = { "import" >> => { ast::Stmt::new( - location, + location.. end_location, ast::StmtImport { names }.into() ) @@ -243,7 +243,7 @@ ImportStatement: ast::Stmt = { "from" "import" => { let (level, module) = source; ast::Stmt::new( - location, + location.. end_location, ast::StmtImportFrom { level, @@ -273,14 +273,14 @@ ImportAsNames: Vec = { "(" >> ","? ")" => i, "*" => { // Star import all - vec![ast::Alias::new(location, end_location, ast::AliasData { name: "*".to_string(), asname: None })] + vec![ast::Alias::new(location..end_location, ast::AliasData { name: "*".to_string(), asname: None })] }, }; #[inline] ImportAsAlias: ast::Alias = { - => ast::Alias::new(location, end_location, ast::AliasData { name, asname: a.map(|a| a.1) }), + => ast::Alias::new(location..end_location, ast::AliasData { name, asname: a.map(|a| a.1) }), } // A name like abc or abc.def.ghi @@ -299,7 +299,7 @@ DottedName: String = { GlobalStatement: ast::Stmt = { "global" > => { ast::Stmt::new( - location, + location.. end_location, ast::StmtGlobal { names }.into() ) @@ -309,7 +309,7 @@ GlobalStatement: ast::Stmt = { NonlocalStatement: ast::Stmt = { "nonlocal" > => { ast::Stmt::new( - location, + location.. end_location, ast::StmtNonlocal { names }.into() ) @@ -319,7 +319,7 @@ NonlocalStatement: ast::Stmt = { AssertStatement: ast::Stmt = { "assert" > )?> => { ast::Stmt::new( - location, + location.. end_location, ast::StmtAssert { test: Box::new(test), @@ -350,7 +350,7 @@ MatchStatement: ast::Stmt = { .unwrap() .end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtMatch { subject: Box::new(subject), @@ -367,7 +367,7 @@ MatchStatement: ast::Stmt = { .unwrap() .end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtMatch { subject: Box::new(subject), @@ -386,11 +386,11 @@ MatchStatement: ast::Stmt = { let mut subjects = subjects; subjects.insert(0, subject); ast::Stmt::new( - location, + location.. end_location, ast::StmtMatch { subject: Box::new(ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts: subjects, @@ -421,7 +421,7 @@ Guard: ast::Expr = { Patterns: ast::Pattern = { "," => ast::Pattern::new( - location, + location.. end_location, ast::PatternMatchSequence { patterns: vec![pattern] @@ -431,7 +431,7 @@ Patterns: ast::Pattern = { let mut patterns = patterns; patterns.insert(0, pattern); ast::Pattern::new( - location, + location.. end_location, ast::PatternMatchSequence { patterns @@ -455,7 +455,7 @@ AsPattern: ast::Pattern = { })? } else { Ok(ast::Pattern::new( - location, + location.. end_location, ast::PatternMatchAs { pattern: Some(Box::new(pattern)), @@ -472,7 +472,7 @@ OrPattern: ast::Pattern = { let mut patterns = patterns; patterns.insert(0, pattern); ast::Pattern::new( - location, + location.. end_location, ast::PatternMatchOr { patterns }.into() ) @@ -481,37 +481,37 @@ OrPattern: ast::Pattern = { ClosedPattern: ast::Pattern = { => ast::Pattern::new( - location, + location.. end_location, node, ), => ast::Pattern::new( - location, + location.. end_location, node, ), => ast::Pattern::new( - location, + location.. end_location, node, ), => ast::Pattern::new( - location, + location.. end_location, node, ), => ast::Pattern::new( - location, + location.. end_location, node, ), => ast::Pattern::new( - location, + location.. end_location, node, ), => ast::Pattern::new( - location, + location.. end_location, node, ), @@ -543,7 +543,7 @@ StarPattern: ast::PatternKind = { ConstantAtom: ast::Expr = { => ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value, kind: None }.into() ), @@ -552,7 +552,7 @@ ConstantAtom: ast::Expr = { ConstantExpr: ast::Expr = { ConstantAtom, "-" => ast::Expr::new( - location, + location.. end_location, ast::ExprUnaryOp { op: ast::Unaryop::USub, @@ -563,7 +563,7 @@ ConstantExpr: ast::Expr = { AddOpExpr: ast::Expr = { => ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(left), @@ -603,7 +603,7 @@ CapturePattern: ast::PatternKind = { MatchName: ast::Expr = { => ast::Expr::new( - location, + location.. end_location, ast::ExprName { id: name, ctx: ast::ExprContext::Load }.into(), ), @@ -611,7 +611,7 @@ MatchName: ast::Expr = { MatchNameOrAttr: ast::Expr = { "." => ast::Expr::new( - location, + location.. end_location, ast::ExprAttribute { value: Box::new(name), @@ -620,7 +620,7 @@ MatchNameOrAttr: ast::Expr = { }.into(), ), "." => ast::Expr::new( - location, + location.. end_location, ast::ExprAttribute { value: Box::new(e), @@ -641,7 +641,7 @@ MappingKey: ast::Expr = { AddOpExpr, MatchNameOrAttr, "None" => ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value: ast::Constant::None, @@ -649,7 +649,7 @@ MappingKey: ast::Expr = { }.into(), ), "True" => ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value: true.into(), @@ -657,7 +657,7 @@ MappingKey: ast::Expr = { }.into(), ), "False" => ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value: false.into(), @@ -804,7 +804,7 @@ IfStatement: ast::Stmt = { // handle elif: for i in s2.into_iter().rev() { let x = ast::Stmt::new( - i.0, + i.0.. end_location, ast::StmtIf { test: Box::new(i.2), body: i.4, orelse: last }.into() ); @@ -812,7 +812,7 @@ IfStatement: ast::Stmt = { } ast::Stmt::new( - location, + location.. end_location, ast::StmtIf { test: Box::new(test), body, orelse: last }.into() ) @@ -828,7 +828,7 @@ WhileStatement: ast::Stmt = { .unwrap() .end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtWhile { test: Box::new(test), @@ -855,7 +855,7 @@ ForStatement: ast::Stmt = { } else { ast::StmtFor { target, iter, body, orelse, type_comment }.into() }; - ast::Stmt::new(location, end_location, node) + ast::Stmt::new(location..end_location, node) }, }; @@ -870,7 +870,7 @@ TryStatement: ast::Stmt = { .or_else(|| handlers.last().map(|last| last.end())) .unwrap(); ast::Stmt::new( - location, + location.. end_location, ast::StmtTry { body, @@ -890,7 +890,7 @@ TryStatement: ast::Stmt = { .or_else(|| handlers.last().map(|last| last.end())) .unwrap(); ast::Stmt::new( - location, + location.. end_location, ast::StmtTryStar { body, @@ -906,7 +906,7 @@ TryStatement: ast::Stmt = { let finalbody = finally.2; let end_location = finalbody.last().unwrap().end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtTry { body, @@ -922,7 +922,7 @@ ExceptStarClause: ast::Excepthandler = { "except" "*" > ":" => { let end_location = body.last().unwrap().end(); ast::Excepthandler::new( - location, + location.. end_location, ast::ExcepthandlerExceptHandler { type_: Some(Box::new(typ)), @@ -934,7 +934,7 @@ ExceptStarClause: ast::Excepthandler = { "except" "*" "as" Identifier)> ":" => { let end_location = body.last().unwrap().end(); ast::Excepthandler::new( - location, + location.. end_location, ast::ExcepthandlerExceptHandler { type_: Some(Box::new(x.0)), @@ -950,7 +950,7 @@ ExceptClause: ast::Excepthandler = { "except" ?> ":" => { let end_location = body.last().unwrap().end(); ast::Excepthandler::new( - location, + location.. end_location, ast::ExcepthandlerExceptHandler { type_: typ.map(Box::new), @@ -962,7 +962,7 @@ ExceptClause: ast::Excepthandler = { "except" "as" Identifier)> ":" => { let end_location = body.last().unwrap().end(); ast::Excepthandler::new( - location, + location.. end_location, ast::ExcepthandlerExceptHandler { type_: Some(Box::new(x.0)), @@ -982,7 +982,7 @@ WithStatement: ast::Stmt = { } else { ast::StmtWith { items, body, type_comment }.into() }; - ast::Stmt::new(location, end_location, node) + ast::Stmt::new(location..end_location, node) }, }; @@ -1023,7 +1023,7 @@ FuncDef: ast::Stmt = { } else { ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment }.into() }; - ast::Stmt::new(location, end_location, node) + ast::Stmt::new(location..end_location, node) }, }; @@ -1126,7 +1126,7 @@ ParameterDef: (ast::Arg, Option) = { UntypedParameter: ast::Arg = { => ast::Arg::new( - location, + location.. end_location, ast::ArgData { arg, annotation: None, type_comment: None }, ), @@ -1135,14 +1135,14 @@ UntypedParameter: ast::Arg = { TypedParameter: ast::Arg = { )?> => { let annotation = a.map(|x| Box::new(x.1)); - ast::Arg::new(location, end_location, ast::ArgData { arg, annotation, type_comment: None }) + ast::Arg::new(location..end_location, ast::ArgData { arg, annotation, type_comment: None }) }, }; StarTypedParameter: ast::Arg = { => { let annotation = a.map(|x| Box::new(x.1)); - ast::Arg::new(location, end_location, ast::ArgData { arg, annotation, type_comment: None }) + ast::Arg::new(location..end_location, ast::ArgData { arg, annotation, type_comment: None }) }, }; @@ -1193,7 +1193,7 @@ ClassDef: ast::Stmt = { }; let end_location = body.last().unwrap().end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtClassDef { name, @@ -1215,12 +1215,12 @@ Decorator: ast::Expr = { YieldExpr: ast::Expr = { "yield" => ast::Expr::new( - location, + location.. end_location, ast::ExprYield { value: value.map(Box::new) }.into() ), "yield" "from" > => ast::Expr::new( - location, + location.. end_location, ast::ExprYieldFrom { value: Box::new(e) }.into() ), @@ -1228,7 +1228,7 @@ YieldExpr: ast::Expr = { Test: ast::Expr = { > "if" > "else" > => ast::Expr::new( - location, + location.. end_location, ast::ExprIfExp { test: Box::new(test), @@ -1248,11 +1248,11 @@ NamedExpressionTest: ast::Expr = { NamedExpression: ast::Expr = { ":=" > => { ast::Expr::new( - location, + location.. value.end(), ast::ExprNamedExpr { target: Box::new(ast::Expr::new( - location, + location.. end_location, ast::ExprName { id, ctx: ast::ExprContext::Store }.into(), )), @@ -1279,7 +1279,7 @@ LambdaDef: ast::Expr = { ))?; Ok(ast::Expr::new( - location, + location.. end_location, ast::ExprLambda { args: Box::new(p), @@ -1294,7 +1294,7 @@ OrTest: ast::Expr = { let mut values = vec![e1]; values.extend(e2.into_iter().map(|e| e.1)); ast::Expr::new( - location, + location.. end_location, ast::ExprBoolOp { op: ast::Boolop::Or, values }.into() ) @@ -1307,7 +1307,7 @@ AndTest: ast::Expr = { let mut values = vec![e1]; values.extend(e2.into_iter().map(|e| e.1)); ast::Expr::new( - location, + location.. end_location, ast::ExprBoolOp { op: ast::Boolop::And, values }.into() ) @@ -1317,7 +1317,7 @@ AndTest: ast::Expr = { NotTest: ast::Expr = { "not" > => ast::Expr::new( - location, + location.. end_location, ast::ExprUnaryOp { operand: Box::new(e), op: ast::Unaryop::Not }.into() ), @@ -1328,7 +1328,7 @@ Comparison: ast::Expr = { > )+> => { let (ops, comparators) = comparisons.into_iter().unzip(); ast::Expr::new( - location, + location.. end_location, ast::ExprCompare { left: Box::new(left), ops, comparators }.into() ) @@ -1351,7 +1351,7 @@ CompOp: ast::Cmpop = { Expression: ast::Expr = { > "|" > => ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op: ast::Operator::BitOr, right: Box::new(e2) }.into() ), @@ -1360,7 +1360,7 @@ Expression: ast::Expr = { XorExpression: ast::Expr = { > "^" > => ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op: ast::Operator::BitXor, right: Box::new(e2) }.into() ), @@ -1369,7 +1369,7 @@ XorExpression: ast::Expr = { AndExpression: ast::Expr = { > "&" > => ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op: ast::Operator::BitAnd, right: Box::new(e2) }.into() ), @@ -1378,7 +1378,7 @@ AndExpression: ast::Expr = { ShiftExpression: ast::Expr = { > > => ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op, right: Box::new(e2) }.into() ), @@ -1392,7 +1392,7 @@ ShiftOp: ast::Operator = { ArithmeticExpression: ast::Expr = { > > => ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(a), op, right: Box::new(b) }.into() ), @@ -1406,7 +1406,7 @@ AddOp: ast::Operator = { Term: ast::Expr = { > > => ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(a), op, right: Box::new(b) }.into() ), @@ -1423,7 +1423,7 @@ MulOp: ast::Operator = { Factor: ast::Expr = { > => ast::Expr::new( - location, + location.. end_location, ast::ExprUnaryOp { operand: Box::new(e), op }.into() ), @@ -1438,7 +1438,7 @@ UnaryOp: ast::Unaryop = { Power: ast::Expr = { > "**" > => ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) }.into() ), @@ -1448,7 +1448,7 @@ Power: ast::Expr = { AtomExpr: ast::Expr = { "await" > => { ast::Expr::new( - location, + location.. end_location, ast::ExprAwait { value: Box::new(atom) }.into() ) @@ -1460,18 +1460,18 @@ AtomExpr2: ast::Expr = { Atom, > "(" ")" => { ast::Expr::new( - location, + location.. end_location, ast::ExprCall { func: Box::new(f), args: a.args, keywords: a.keywords }.into() ) }, > "[" "]" => ast::Expr::new( - location, + location.. end_location, ast::ExprSubscript { value: Box::new(e), slice: Box::new(s), ctx: ast::ExprContext::Load }.into() ), > "." => ast::Expr::new( - location, + location.. end_location, ast::ExprAttribute { value: Box::new(e), attr, ctx: ast::ExprContext::Load }.into() ), @@ -1488,7 +1488,7 @@ SubscriptList: ast::Expr = { } ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts: dims, ctx: ast::ExprContext::Load }.into(), ) @@ -1503,7 +1503,7 @@ Subscript: ast::Expr = { let upper = e2.map(Box::new); let step = e3.flatten().map(Box::new); ast::Expr::new( - location, + location.. end_location, ast::ExprSlice { lower, upper, step }.into() ) @@ -1517,26 +1517,26 @@ SliceOp: Option = { Atom: ast::Expr = { =>? Ok(parse_strings(s)?), => ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value, kind: None }.into() ), => ast::Expr::new( - location, + location.. end_location, ast::ExprName { id: name, ctx: ast::ExprContext::Load }.into() ), "[" "]" => { let elts = e.unwrap_or_default(); ast::Expr::new( - location, + location.. end_location, ast::ExprList { elts, ctx: ast::ExprContext::Load }.into() ) }, "[" "]" => { ast::Expr::new( - location, + location.. end_location, ast::ExprListComp { elt: Box::new(elt), generators }.into() ) @@ -1546,7 +1546,7 @@ Atom: ast::Expr = { elts.into_iter().next().unwrap() } else { ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts, ctx: ast::ExprContext::Load }.into() ) @@ -1564,21 +1564,21 @@ Atom: ast::Expr = { } else { let elts = left.into_iter().flatten().chain([mid]).chain(right).collect(); Ok(ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts, ctx: ast::ExprContext::Load }.into(), )) } }, "(" ")" => ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts: Vec::new(), ctx: ast::ExprContext::Load }.into() ), "(" ")" => e, "(" ")" => { ast::Expr::new( - location, + location.. end_location, ast::ExprGeneratorExp { elt: Box::new(elt), generators }.into() ) @@ -1596,14 +1596,14 @@ Atom: ast::Expr = { .map(|(k, v)| (k.map(|x| *x), v)) .unzip(); ast::Expr::new( - location, + location.. end_location, ast::ExprDict { keys, values }.into() ) }, "{" "}" => { ast::Expr::new( - location, + location.. end_location, ast::ExprDictComp { key: Box::new(e1.0), @@ -1613,21 +1613,21 @@ Atom: ast::Expr = { ) }, "{" "}" => ast::Expr::new( - location, + location.. end_location, ast::ExprSet { elts }.into() ), "{" "}" => { ast::Expr::new( - location, + location.. end_location, ast::ExprSetComp { elt: Box::new(elt), generators }.into() ) }, - "True" => ast::Expr::new(location, end_location, ast::ExprConstant { value: true.into(), kind: None }.into()), - "False" => ast::Expr::new(location, end_location, ast::ExprConstant { value: false.into(), kind: None }.into()), - "None" => ast::Expr::new(location, end_location, ast::ExprConstant { value: ast::Constant::None, kind: None }.into()), - "..." => ast::Expr::new(location, end_location, ast::ExprConstant { value: ast::Constant::Ellipsis, kind: None }.into()), + "True" => ast::Expr::new(location..end_location, ast::ExprConstant { value: true.into(), kind: None }.into()), + "False" => ast::Expr::new(location..end_location, ast::ExprConstant { value: false.into(), kind: None }.into()), + "None" => ast::Expr::new(location..end_location, ast::ExprConstant { value: ast::Constant::None, kind: None }.into()), + "..." => ast::Expr::new(location..end_location, ast::ExprConstant { value: ast::Constant::Ellipsis, kind: None }.into()), }; ListLiteralValues: Vec = { @@ -1679,7 +1679,7 @@ GenericList: ast::Expr = { elts.into_iter().next().unwrap() } else { ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts, ctx: ast::ExprContext::Load }.into() ) @@ -1690,7 +1690,7 @@ GenericList: ast::Expr = { // Test StarExpr: ast::Expr = { "*" > => ast::Expr::new( - location, + location.. end_location, ast::ExprStarred { value: Box::new(e), ctx: ast::ExprContext::Load }.into(), ) @@ -1725,8 +1725,7 @@ FunctionArgument: (Option<(crate::text_size::TextSize, crate::text_size::TextSiz => { let expr = match c { Some(c) => ast::Expr::new( - location, - end_location, + location..end_location, ast::ExprGeneratorExp { elt: Box::new(e), generators: c, @@ -1739,7 +1738,7 @@ FunctionArgument: (Option<(crate::text_size::TextSize, crate::text_size::TextSiz "=" > => (Some((location, end_location, Some(i))), e), "*" > => { let expr = ast::Expr::new( - location, + location.. end_location, ast::ExprStarred { value: Box::new(e), ctx: ast::ExprContext::Load }.into(), ); diff --git a/parser/src/python.rs b/parser/src/python.rs index b1337eb..7f13e61 100644 --- a/parser/src/python.rs +++ b/parser/src/python.rs @@ -1,5 +1,5 @@ // auto-generated: "lalrpop 0.19.10" -// sha3: 1e1b28f6c4d82d12bc29591853da3359916cff65c06bc361fa6d9dbdaca35fd2 +// sha3: a25f4d10cb8aa2890ee324b7101c2932913df2c17df807c69d75f30cdc2db2bd use crate::{ ast, lexer::{LexicalError, LexicalErrorType}, @@ -36809,7 +36809,7 @@ fn __action20< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtKind::Pass, ) @@ -36827,7 +36827,7 @@ fn __action21< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtDelete { targets: targets.into_iter().map(|expr| set_context(expr, ast::ExprContext::Del)).collect() }.into() ) @@ -36847,7 +36847,7 @@ fn __action22< // Just an expression, no assignment: if suffix.is_empty() { ast::Stmt::new( - location, + location.. end_location, ast::StmtExpr { value: Box::new(expression) }.into() ) @@ -36862,7 +36862,7 @@ fn __action22< let value = Box::new(values.into_iter().next().unwrap()); ast::Stmt::new( - location, + location.. end_location, ast::StmtAssign { targets, value, type_comment: None }.into() ) @@ -36882,7 +36882,7 @@ fn __action23< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtAugAssign { target: Box::new(set_context(target, ast::ExprContext::Store)), @@ -36907,7 +36907,7 @@ fn __action24< { let simple = matches!(target.node, ast::ExprKind::Name { .. }); ast::Stmt::new( - location, + location.. end_location, ast::StmtAnnAssign { target: Box::new(set_context(target, ast::ExprContext::Store)), @@ -37137,7 +37137,7 @@ fn __action48< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtKind::Break, ) @@ -37154,7 +37154,7 @@ fn __action49< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtKind::Continue, ) @@ -37172,7 +37172,7 @@ fn __action50< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtReturn { value: value.map(Box::new) }.into() ) @@ -37189,7 +37189,7 @@ fn __action51< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtExpr { value: Box::new(expression) }.into() ) @@ -37215,7 +37215,7 @@ fn __action53< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtRaise { exc: None, cause: None }.into() ) @@ -37234,7 +37234,7 @@ fn __action54< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtRaise { exc: Some(Box::new(t)), cause: c.map(|x| Box::new(x.1)) }.into() ) @@ -37252,7 +37252,7 @@ fn __action55< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtImport { names }.into() ) @@ -37273,7 +37273,7 @@ fn __action56< { let (level, module) = source; ast::Stmt::new( - location, + location.. end_location, ast::StmtImportFrom { level, @@ -37360,7 +37360,7 @@ fn __action63< { { // Star import all - vec![ast::Alias::new(location, end_location, ast::AliasData { name: "*".to_string(), asname: None })] + vec![ast::Alias::new(location..end_location, ast::AliasData { name: "*".to_string(), asname: None })] } } @@ -37401,7 +37401,7 @@ fn __action66< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtGlobal { names }.into() ) @@ -37419,7 +37419,7 @@ fn __action67< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtNonlocal { names }.into() ) @@ -37438,7 +37438,7 @@ fn __action68< { { ast::Stmt::new( - location, + location.. end_location, ast::StmtAssert { test: Box::new(test), @@ -37542,7 +37542,7 @@ fn __action77< .unwrap() .end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtMatch { subject: Box::new(subject), @@ -37575,7 +37575,7 @@ fn __action78< .unwrap() .end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtMatch { subject: Box::new(subject), @@ -37612,11 +37612,11 @@ fn __action79< let mut subjects = subjects; subjects.insert(0, subject); ast::Stmt::new( - location, + location.. end_location, ast::StmtMatch { subject: Box::new(ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts: subjects, @@ -37670,7 +37670,7 @@ fn __action82< ) -> ast::Pattern { ast::Pattern::new( - location, + location.. end_location, ast::PatternMatchSequence { patterns: vec![pattern] @@ -37693,7 +37693,7 @@ fn __action83< let mut patterns = patterns; patterns.insert(0, pattern); ast::Pattern::new( - location, + location.. end_location, ast::PatternMatchSequence { patterns @@ -37747,7 +37747,7 @@ fn __action87< })? } else { Ok(ast::Pattern::new( - location, + location.. end_location, ast::PatternMatchAs { pattern: Some(Box::new(pattern)), @@ -37780,7 +37780,7 @@ fn __action89< let mut patterns = patterns; patterns.insert(0, pattern); ast::Pattern::new( - location, + location.. end_location, ast::PatternMatchOr { patterns }.into() ) @@ -37796,7 +37796,7 @@ fn __action90< ) -> ast::Pattern { ast::Pattern::new( - location, + location.. end_location, node, ) @@ -37811,7 +37811,7 @@ fn __action91< ) -> ast::Pattern { ast::Pattern::new( - location, + location.. end_location, node, ) @@ -37826,7 +37826,7 @@ fn __action92< ) -> ast::Pattern { ast::Pattern::new( - location, + location.. end_location, node, ) @@ -37841,7 +37841,7 @@ fn __action93< ) -> ast::Pattern { ast::Pattern::new( - location, + location.. end_location, node, ) @@ -37856,7 +37856,7 @@ fn __action94< ) -> ast::Pattern { ast::Pattern::new( - location, + location.. end_location, node, ) @@ -37871,7 +37871,7 @@ fn __action95< ) -> ast::Pattern { ast::Pattern::new( - location, + location.. end_location, node, ) @@ -37886,7 +37886,7 @@ fn __action96< ) -> ast::Pattern { ast::Pattern::new( - location, + location.. end_location, node, ) @@ -37978,7 +37978,7 @@ fn __action102< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value, kind: None }.into() ) @@ -38003,7 +38003,7 @@ fn __action104< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprUnaryOp { op: ast::Unaryop::USub, @@ -38023,7 +38023,7 @@ fn __action105< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(left), @@ -38126,7 +38126,7 @@ fn __action113< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprName { id: name, ctx: ast::ExprContext::Load }.into(), ) @@ -38143,7 +38143,7 @@ fn __action114< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprAttribute { value: Box::new(name), @@ -38164,7 +38164,7 @@ fn __action115< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprAttribute { value: Box::new(e), @@ -38221,7 +38221,7 @@ fn __action120< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value: ast::Constant::None, @@ -38239,7 +38239,7 @@ fn __action121< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value: true.into(), @@ -38257,7 +38257,7 @@ fn __action122< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value: false.into(), @@ -38598,7 +38598,7 @@ fn __action138< // handle elif: for i in s2.into_iter().rev() { let x = ast::Stmt::new( - i.0, + i.0.. end_location, ast::StmtIf { test: Box::new(i.2), body: i.4, orelse: last }.into() ); @@ -38606,7 +38606,7 @@ fn __action138< } ast::Stmt::new( - location, + location.. end_location, ast::StmtIf { test: Box::new(test), body, orelse: last }.into() ) @@ -38632,7 +38632,7 @@ fn __action139< .unwrap() .end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtWhile { test: Box::new(test), @@ -38672,7 +38672,7 @@ fn __action140< } else { ast::StmtFor { target, iter, body, orelse, type_comment }.into() }; - ast::Stmt::new(location, end_location, node) + ast::Stmt::new(location..end_location, node) } } @@ -38699,7 +38699,7 @@ fn __action141< .or_else(|| handlers.last().map(|last| last.end())) .unwrap(); ast::Stmt::new( - location, + location.. end_location, ast::StmtTry { body, @@ -38734,7 +38734,7 @@ fn __action142< .or_else(|| handlers.last().map(|last| last.end())) .unwrap(); ast::Stmt::new( - location, + location.. end_location, ast::StmtTryStar { body, @@ -38762,7 +38762,7 @@ fn __action143< let finalbody = finally.2; let end_location = finalbody.last().unwrap().end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtTry { body, @@ -38788,7 +38788,7 @@ fn __action144< { let end_location = body.last().unwrap().end(); ast::Excepthandler::new( - location, + location.. end_location, ast::ExcepthandlerExceptHandler { type_: Some(Box::new(typ)), @@ -38813,7 +38813,7 @@ fn __action145< { let end_location = body.last().unwrap().end(); ast::Excepthandler::new( - location, + location.. end_location, ast::ExcepthandlerExceptHandler { type_: Some(Box::new(x.0)), @@ -38837,7 +38837,7 @@ fn __action146< { let end_location = body.last().unwrap().end(); ast::Excepthandler::new( - location, + location.. end_location, ast::ExcepthandlerExceptHandler { type_: typ.map(Box::new), @@ -38861,7 +38861,7 @@ fn __action147< { let end_location = body.last().unwrap().end(); ast::Excepthandler::new( - location, + location.. end_location, ast::ExcepthandlerExceptHandler { type_: Some(Box::new(x.0)), @@ -38891,7 +38891,7 @@ fn __action148< } else { ast::StmtWith { items, body, type_comment }.into() }; - ast::Stmt::new(location, end_location, node) + ast::Stmt::new(location..end_location, node) } } @@ -38979,7 +38979,7 @@ fn __action154< } else { ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment }.into() }; - ast::Stmt::new(location, end_location, node) + ast::Stmt::new(location..end_location, node) } } @@ -39017,7 +39017,7 @@ fn __action156< ) -> ast::Arg { ast::Arg::new( - location, + location.. end_location, ast::ArgData { arg, annotation: None, type_comment: None }, ) @@ -39034,7 +39034,7 @@ fn __action157< { { let annotation = a.map(|x| Box::new(x.1)); - ast::Arg::new(location, end_location, ast::ArgData { arg, annotation, type_comment: None }) + ast::Arg::new(location..end_location, ast::ArgData { arg, annotation, type_comment: None }) } } @@ -39049,7 +39049,7 @@ fn __action158< { { let annotation = a.map(|x| Box::new(x.1)); - ast::Arg::new(location, end_location, ast::ArgData { arg, annotation, type_comment: None }) + ast::Arg::new(location..end_location, ast::ArgData { arg, annotation, type_comment: None }) } } @@ -39072,7 +39072,7 @@ fn __action159< }; let end_location = body.last().unwrap().end(); ast::Stmt::new( - location, + location.. end_location, ast::StmtClassDef { name, @@ -39109,7 +39109,7 @@ fn __action161< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprYield { value: value.map(Box::new) }.into() ) @@ -39126,7 +39126,7 @@ fn __action162< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprYieldFrom { value: Box::new(e) }.into() ) @@ -39162,11 +39162,11 @@ fn __action165< { { ast::Expr::new( - location, + location.. value.end(), ast::ExprNamedExpr { target: Box::new(ast::Expr::new( - location, + location.. end_location, ast::ExprName { id, ctx: ast::ExprContext::Store }.into(), )), @@ -39203,7 +39203,7 @@ fn __action166< ))?; Ok(ast::Expr::new( - location, + location.. end_location, ast::ExprLambda { args: Box::new(p), @@ -39433,7 +39433,7 @@ fn __action189< } ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts: dims, ctx: ast::ExprContext::Load }.into(), ) @@ -39466,7 +39466,7 @@ fn __action191< let upper = e2.map(Box::new); let step = e3.flatten().map(Box::new); ast::Expr::new( - location, + location.. end_location, ast::ExprSlice { lower, upper, step }.into() ) @@ -39600,7 +39600,7 @@ fn __action204< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprStarred { value: Box::new(e), ctx: ast::ExprContext::Load }.into(), ) @@ -39682,8 +39682,7 @@ fn __action210< { let expr = match c { Some(c) => ast::Expr::new( - location, - end_location, + location..end_location, ast::ExprGeneratorExp { elt: Box::new(e), generators: c, @@ -39719,7 +39718,7 @@ fn __action212< { { let expr = ast::Expr::new( - location, + location.. end_location, ast::ExprStarred { value: Box::new(e), ctx: ast::ExprContext::Load }.into(), ); @@ -39840,7 +39839,7 @@ fn __action223< let mut values = vec![e1]; values.extend(e2.into_iter().map(|e| e.1)); ast::Expr::new( - location, + location.. end_location, ast::ExprBoolOp { op: ast::Boolop::Or, values }.into() ) @@ -39889,7 +39888,7 @@ fn __action227< elts.into_iter().next().unwrap() } else { ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts, ctx: ast::ExprContext::Load }.into() ) @@ -39925,7 +39924,7 @@ fn __action229< elts.into_iter().next().unwrap() } else { ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts, ctx: ast::ExprContext::Load }.into() ) @@ -39944,7 +39943,7 @@ fn __action230< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op: ast::Operator::BitOr, right: Box::new(e2) }.into() ) @@ -41016,7 +41015,7 @@ fn __action323< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Alias { - ast::Alias::new(location, end_location, ast::AliasData { name, asname: a.map(|a| a.1) }) + ast::Alias::new(location..end_location, ast::AliasData { name, asname: a.map(|a| a.1) }) } #[allow(clippy::too_many_arguments)] @@ -41080,7 +41079,7 @@ fn __action329< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Alias { - ast::Alias::new(location, end_location, ast::AliasData { name, asname: a.map(|a| a.1) }) + ast::Alias::new(location..end_location, ast::AliasData { name, asname: a.map(|a| a.1) }) } #[allow(clippy::too_many_arguments)] @@ -41163,7 +41162,7 @@ fn __action337< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprIfExp { test: Box::new(test), @@ -41730,7 +41729,7 @@ fn __action395< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprIfExp { test: Box::new(test), @@ -42085,7 +42084,7 @@ fn __action423< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op: ast::Operator::BitXor, right: Box::new(e2) }.into() ) @@ -42185,7 +42184,7 @@ fn __action432< let mut values = vec![e1]; values.extend(e2.into_iter().map(|e| e.1)); ast::Expr::new( - location, + location.. end_location, ast::ExprBoolOp { op: ast::Boolop::And, values }.into() ) @@ -42326,7 +42325,7 @@ fn __action446< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprUnaryOp { operand: Box::new(e), op: ast::Unaryop::Not }.into() ) @@ -42400,7 +42399,7 @@ fn __action453< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op: ast::Operator::BitAnd, right: Box::new(e2) }.into() ) @@ -42687,7 +42686,7 @@ fn __action481< let mut values = vec![e1]; values.extend(e2.into_iter().map(|e| e.1)); ast::Expr::new( - location, + location.. end_location, ast::ExprBoolOp { op: ast::Boolop::Or, values }.into() ) @@ -42868,7 +42867,7 @@ fn __action499< let mut values = vec![e1]; values.extend(e2.into_iter().map(|e| e.1)); ast::Expr::new( - location, + location.. end_location, ast::ExprBoolOp { op: ast::Boolop::And, values }.into() ) @@ -42933,7 +42932,7 @@ fn __action505< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op, right: Box::new(e2) }.into() ) @@ -42979,7 +42978,7 @@ fn __action509< { let (ops, comparators) = comparisons.into_iter().unzip(); ast::Expr::new( - location, + location.. end_location, ast::ExprCompare { left: Box::new(left), ops, comparators }.into() ) @@ -43035,7 +43034,7 @@ fn __action514< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(a), op, right: Box::new(b) }.into() ) @@ -43060,7 +43059,7 @@ fn __action516< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprUnaryOp { operand: Box::new(e), op: ast::Unaryop::Not }.into() ) @@ -43087,7 +43086,7 @@ fn __action518< { let (ops, comparators) = comparisons.into_iter().unzip(); ast::Expr::new( - location, + location.. end_location, ast::ExprCompare { left: Box::new(left), ops, comparators }.into() ) @@ -43114,7 +43113,7 @@ fn __action520< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(a), op, right: Box::new(b) }.into() ) @@ -43139,7 +43138,7 @@ fn __action522< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprUnaryOp { operand: Box::new(e), op }.into() ) @@ -43165,7 +43164,7 @@ fn __action524< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op: ast::Operator::BitOr, right: Box::new(e2) }.into() ) @@ -43191,7 +43190,7 @@ fn __action526< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op: ast::Operator::BitXor, right: Box::new(e2) }.into() ) @@ -43217,7 +43216,7 @@ fn __action528< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) }.into() ) @@ -43243,7 +43242,7 @@ fn __action530< { { ast::Expr::new( - location, + location.. end_location, ast::ExprAwait { value: Box::new(atom) }.into() ) @@ -43270,7 +43269,7 @@ fn __action532< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op: ast::Operator::BitAnd, right: Box::new(e2) }.into() ) @@ -43296,7 +43295,7 @@ fn __action534< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e1), op, right: Box::new(e2) }.into() ) @@ -43333,7 +43332,7 @@ fn __action537< { { ast::Expr::new( - location, + location.. end_location, ast::ExprCall { func: Box::new(f), args: a.args, keywords: a.keywords }.into() ) @@ -43352,7 +43351,7 @@ fn __action538< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprSubscript { value: Box::new(e), slice: Box::new(s), ctx: ast::ExprContext::Load }.into() ) @@ -43369,7 +43368,7 @@ fn __action539< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprAttribute { value: Box::new(e), attr, ctx: ast::ExprContext::Load }.into() ) @@ -43394,7 +43393,7 @@ fn __action541< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value, kind: None }.into() ) @@ -43409,7 +43408,7 @@ fn __action542< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprName { id: name, ctx: ast::ExprContext::Load }.into() ) @@ -43428,7 +43427,7 @@ fn __action543< { let elts = e.unwrap_or_default(); ast::Expr::new( - location, + location.. end_location, ast::ExprList { elts, ctx: ast::ExprContext::Load }.into() ) @@ -43448,7 +43447,7 @@ fn __action544< { { ast::Expr::new( - location, + location.. end_location, ast::ExprListComp { elt: Box::new(elt), generators }.into() ) @@ -43471,7 +43470,7 @@ fn __action545< elts.into_iter().next().unwrap() } else { ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts, ctx: ast::ExprContext::Load }.into() ) @@ -43504,7 +43503,7 @@ fn __action546< } else { let elts = left.into_iter().flatten().chain([mid]).chain(right).collect(); Ok(ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts, ctx: ast::ExprContext::Load }.into(), )) @@ -43522,7 +43521,7 @@ fn __action547< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts: Vec::new(), ctx: ast::ExprContext::Load }.into() ) @@ -43552,7 +43551,7 @@ fn __action549< { { ast::Expr::new( - location, + location.. end_location, ast::ExprGeneratorExp { elt: Box::new(elt), generators }.into() ) @@ -43595,7 +43594,7 @@ fn __action551< .map(|(k, v)| (k.map(|x| *x), v)) .unzip(); ast::Expr::new( - location, + location.. end_location, ast::ExprDict { keys, values }.into() ) @@ -43615,7 +43614,7 @@ fn __action552< { { ast::Expr::new( - location, + location.. end_location, ast::ExprDictComp { key: Box::new(e1.0), @@ -43637,7 +43636,7 @@ fn __action553< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprSet { elts }.into() ) @@ -43656,7 +43655,7 @@ fn __action554< { { ast::Expr::new( - location, + location.. end_location, ast::ExprSetComp { elt: Box::new(elt), generators }.into() ) @@ -43671,7 +43670,7 @@ fn __action555< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Expr { - ast::Expr::new(location, end_location, ast::ExprConstant { value: true.into(), kind: None }.into()) + ast::Expr::new(location..end_location, ast::ExprConstant { value: true.into(), kind: None }.into()) } #[allow(clippy::too_many_arguments)] @@ -43682,7 +43681,7 @@ fn __action556< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Expr { - ast::Expr::new(location, end_location, ast::ExprConstant { value: false.into(), kind: None }.into()) + ast::Expr::new(location..end_location, ast::ExprConstant { value: false.into(), kind: None }.into()) } #[allow(clippy::too_many_arguments)] @@ -43693,7 +43692,7 @@ fn __action557< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Expr { - ast::Expr::new(location, end_location, ast::ExprConstant { value: ast::Constant::None, kind: None }.into()) + ast::Expr::new(location..end_location, ast::ExprConstant { value: ast::Constant::None, kind: None }.into()) } #[allow(clippy::too_many_arguments)] @@ -43704,7 +43703,7 @@ fn __action558< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Expr { - ast::Expr::new(location, end_location, ast::ExprConstant { value: ast::Constant::Ellipsis, kind: None }.into()) + ast::Expr::new(location..end_location, ast::ExprConstant { value: ast::Constant::Ellipsis, kind: None }.into()) } #[allow(clippy::too_many_arguments)] @@ -43718,7 +43717,7 @@ fn __action559< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(a), op, right: Box::new(b) }.into() ) @@ -43744,7 +43743,7 @@ fn __action561< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(a), op, right: Box::new(b) }.into() ) @@ -43884,7 +43883,7 @@ fn __action575< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprUnaryOp { operand: Box::new(e), op }.into() ) @@ -43910,7 +43909,7 @@ fn __action577< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprBinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) }.into() ) @@ -43936,7 +43935,7 @@ fn __action579< { { ast::Expr::new( - location, + location.. end_location, ast::ExprAwait { value: Box::new(atom) }.into() ) @@ -43974,7 +43973,7 @@ fn __action582< { { ast::Expr::new( - location, + location.. end_location, ast::ExprCall { func: Box::new(f), args: a.args, keywords: a.keywords }.into() ) @@ -43993,7 +43992,7 @@ fn __action583< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprSubscript { value: Box::new(e), slice: Box::new(s), ctx: ast::ExprContext::Load }.into() ) @@ -44010,7 +44009,7 @@ fn __action584< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprAttribute { value: Box::new(e), attr, ctx: ast::ExprContext::Load }.into() ) @@ -44035,7 +44034,7 @@ fn __action586< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprConstant { value, kind: None }.into() ) @@ -44050,7 +44049,7 @@ fn __action587< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprName { id: name, ctx: ast::ExprContext::Load }.into() ) @@ -44069,7 +44068,7 @@ fn __action588< { let elts = e.unwrap_or_default(); ast::Expr::new( - location, + location.. end_location, ast::ExprList { elts, ctx: ast::ExprContext::Load }.into() ) @@ -44089,7 +44088,7 @@ fn __action589< { { ast::Expr::new( - location, + location.. end_location, ast::ExprListComp { elt: Box::new(elt), generators }.into() ) @@ -44121,7 +44120,7 @@ fn __action590< } else { let elts = left.into_iter().flatten().chain([mid]).chain(right).collect(); Ok(ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts, ctx: ast::ExprContext::Load }.into(), )) @@ -44139,7 +44138,7 @@ fn __action591< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprTuple { elts: Vec::new(), ctx: ast::ExprContext::Load }.into() ) @@ -44169,7 +44168,7 @@ fn __action593< { { ast::Expr::new( - location, + location.. end_location, ast::ExprGeneratorExp { elt: Box::new(elt), generators }.into() ) @@ -44212,7 +44211,7 @@ fn __action595< .map(|(k, v)| (k.map(|x| *x), v)) .unzip(); ast::Expr::new( - location, + location.. end_location, ast::ExprDict { keys, values }.into() ) @@ -44232,7 +44231,7 @@ fn __action596< { { ast::Expr::new( - location, + location.. end_location, ast::ExprDictComp { key: Box::new(e1.0), @@ -44254,7 +44253,7 @@ fn __action597< ) -> ast::Expr { ast::Expr::new( - location, + location.. end_location, ast::ExprSet { elts }.into() ) @@ -44273,7 +44272,7 @@ fn __action598< { { ast::Expr::new( - location, + location.. end_location, ast::ExprSetComp { elt: Box::new(elt), generators }.into() ) @@ -44288,7 +44287,7 @@ fn __action599< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Expr { - ast::Expr::new(location, end_location, ast::ExprConstant { value: true.into(), kind: None }.into()) + ast::Expr::new(location..end_location, ast::ExprConstant { value: true.into(), kind: None }.into()) } #[allow(clippy::too_many_arguments)] @@ -44299,7 +44298,7 @@ fn __action600< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Expr { - ast::Expr::new(location, end_location, ast::ExprConstant { value: false.into(), kind: None }.into()) + ast::Expr::new(location..end_location, ast::ExprConstant { value: false.into(), kind: None }.into()) } #[allow(clippy::too_many_arguments)] @@ -44310,7 +44309,7 @@ fn __action601< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Expr { - ast::Expr::new(location, end_location, ast::ExprConstant { value: ast::Constant::None, kind: None }.into()) + ast::Expr::new(location..end_location, ast::ExprConstant { value: ast::Constant::None, kind: None }.into()) } #[allow(clippy::too_many_arguments)] @@ -44321,7 +44320,7 @@ fn __action602< (_, end_location, _): (crate::text_size::TextSize, crate::text_size::TextSize, crate::text_size::TextSize), ) -> ast::Expr { - ast::Expr::new(location, end_location, ast::ExprConstant { value: ast::Constant::Ellipsis, kind: None }.into()) + ast::Expr::new(location..end_location, ast::ExprConstant { value: ast::Constant::Ellipsis, kind: None }.into()) } #[allow(clippy::too_many_arguments)] diff --git a/parser/src/string.rs b/parser/src/string.rs index 8fb7e63..8000d22 100644 --- a/parser/src/string.rs +++ b/parser/src/string.rs @@ -10,9 +10,7 @@ use crate::{ token::{StringKind, Tok}, }; use itertools::Itertools; -use rustpython_compiler_core::{ - text_size::{TextLen, TextSize}, -}; +use rustpython_compiler_core::text_size::{TextLen, TextSize}; // unicode_name2 does not expose `MAX_NAME_LENGTH`, so we replicate that constant here, fix #3798 const MAX_UNICODE_NAME: usize = 88; @@ -67,7 +65,7 @@ impl<'a> StringParser<'a> { #[inline] fn expr(&self, node: ExprKind) -> Expr { - Expr::new(self.start, self.end, node) + Expr::new(self.start..self.end, node) } fn parse_unicode_literal(&mut self, literal_number: usize) -> Result { @@ -624,8 +622,7 @@ pub(crate) fn parse_strings( } } return Ok(Expr::new( - initial_start, - last_end, + initial_start..last_end, ast::ExprConstant { value: Constant::Bytes(content), kind: None, @@ -648,8 +645,7 @@ pub(crate) fn parse_strings( } } return Ok(Expr::new( - initial_start, - last_end, + initial_start..last_end, ast::ExprConstant { value: Constant::Str(content.join("")), kind: initial_kind, @@ -664,8 +660,7 @@ pub(crate) fn parse_strings( let take_current = |current: &mut Vec| -> Expr { Expr::new( - initial_start, - last_end, + initial_start..last_end, ast::ExprConstant { value: Constant::Str(current.drain(..).join("")), kind: initial_kind.clone(), @@ -696,8 +691,7 @@ pub(crate) fn parse_strings( } Ok(Expr::new( - initial_start, - last_end, + initial_start..last_end, ast::ExprJoinedStr { values: deduped }.into(), )) } diff --git a/ruff_text_size/src/range.rs b/ruff_text_size/src/range.rs index 3204817..2703bf3 100644 --- a/ruff_text_size/src/range.rs +++ b/ruff_text_size/src/range.rs @@ -463,6 +463,13 @@ impl RangeBounds for TextRange { } } +impl From> for TextRange { + #[inline] + fn from(r: Range) -> Self { + TextRange::new(r.start, r.end) + } +} + impl From for Range where T: From, diff --git a/ruff_text_size/src/size.rs b/ruff_text_size/src/size.rs index 7b7765b..7002d5a 100644 --- a/ruff_text_size/src/size.rs +++ b/ruff_text_size/src/size.rs @@ -63,6 +63,30 @@ impl TextSize { pub fn of(text: T) -> TextSize { text.text_len() } + + /// Returns current raw `offset` as u32. + /// + /// # Examples + /// + /// ```rust + /// # use ruff_text_size::*; + /// assert_eq!(TextSize::from(4).to_u32(), 4); + /// ``` + pub fn to_u32(&self) -> u32 { + self.raw + } + + /// Returns current raw `offset` as usize. + /// + /// # Examples + /// + /// ```rust + /// # use ruff_text_size::*; + /// assert_eq!(TextSize::from(4).to_usize(), 4); + /// ``` + pub fn to_usize(&self) -> usize { + self.raw as usize + } } /// Methods to act like a primitive integer type, where reasonably applicable. @@ -91,7 +115,7 @@ impl From for TextSize { impl From for u32 { #[inline] fn from(value: TextSize) -> Self { - value.raw + value.to_u32() } } @@ -106,7 +130,7 @@ impl TryFrom for TextSize { impl From for usize { #[inline] fn from(value: TextSize) -> Self { - value.raw as usize + value.to_usize() } } diff --git a/scripts/update_asdl.sh b/scripts/update_asdl.sh new file mode 100755 index 0000000..841735e --- /dev/null +++ b/scripts/update_asdl.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +cd "$(dirname "$(dirname "$0")")" + +python ast/asdl_rs.py --generic-file ast/src/generic.rs --located-file ast/src/located.rs --module-file ../RustPython/vm/src/stdlib/ast/gen.rs ast/Python.asdl +rustfmt ast/src/generic.rs ast/src/located.rs ../RustPython/vm/src/stdlib/ast/gen.rs