Skip to content

proc_macro support for reading files/strings to spanned TokenStream #55904

Description

@mystor

Currently, as far as I am aware, there is no way to get spans for tokens parsed from a source other than the macro's callsite, and no good way to tell rustc & cargo a procedural macro depends on another file. It would be nice for macros which want to load other files to enable this.

It'd be nice to support some mechanism for loading a file as a TokenStream. Ideally this would take the form of a method in the crate directly parsing a file / string to a TokenStream. For example:

  1. A function to load a file as a TokenStream with accurate span info etc, e.g.

    fn lex_file(path: &Path) -> io::Result<TokenStream>;
  2. A function to parse a string with a given filename. This might need an additional mechanism to register the source file as a dependency, e.g.

    fn lex_str(src: &str, filename: &Path) -> Result<TokenStream, LexErr>;
    fn add_path_dependency(path: &Path) -> ...;

To make it generally useful & able to implement something like include!() we'd probably also want/need something like #54725 to get the file to load relative to.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-proc-macrosArea: Procedural macros

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions