4.1 Source Code
| (require resyntax/grimoire/source) | package: resyntax |
In Resyntax, source code refers to source? values, which come in three types:
Source files, constructed with file-source, which don’t contain the code directly but refer to it by a local filesystem path.
Source strings, constructed with string-source, which contain the source code directly as a string and don’t exist anywhere on the local filesystem. (These are useful for testing Resyntax, and other scenarios where Resyntax needs to operate on code that doesn’t exist on disk.)
Modified sources, constructed by passing another (unmodified) source to modified-source along with a string representing what to replace the source’s contents with. A modified source contains both its new updated contents and a reference to the original source.
Resyntax’s basic architecture is to recursively take sources of any kind as input, produce modified-source? values as output, then re-analyze the modified sources again until no further modifications are desired. Then, Resyntax decides whether to commit those final modifications to the filesystem (as in resyntax fix) or merely display them to users (as in resyntax analyze). This recursive loop approach allows Resyntax to "look ahead" and produce a stack of dependent changes to commit in series without actually mutating the files on disk.
4.1.1 Basic Source Operations
procedure
(unmodified-source? v) → boolean?
v : any/c
procedure
(file-source? v) → boolean?
v : any/c
procedure
(file-source path) → file-source?
path : path-string?
procedure
(string-source? v) → boolean?
v : any/c
procedure
(string-source contents) → string-source?
contents : string?
procedure
(modified-source? v) → boolean?
v : any/c
procedure
(modified-source original new-contents) → modified-source?
original : unmodified-source? new-contents : string?
procedure
(source-name code) → (or/c path? symbol?)
code : source?
procedure
(source-path code) → (or/c path? #false)
code : source?
procedure
(source-directory code) → (or/c path? #false)
code : source?
procedure
(source-original code) → unmodified-source?
code : source?
procedure
(source->string code) → immutable-string?
code : source?
procedure
(with-input-from-source code proc) → any
code : source? proc : (-> any)
4.1.2 Parsing, Expanding, and Compiling Sources
The following operations allow treating source code values as inputs to Racket’s compiler. For file sources, this is roughly the same as reading the file into a syntax object using with-module-reading-parameterization and expanding that syntax object using expand. However, string sources and modified sources behave slightly differently, especially with regard to source location information on derived syntax objects:
A string source behaves as if it were an anonymous file with no well-defined location on the filesystem. Relative file path imports will not work correctly. Source location information will still be present with line and column numbers, but will claim to be located in a source named 'string instead of a file.
A modified source behaves the same as its wrapped unmodified source, except as if its contents were completely replaced. Source location information will be present, but will not correspond to positions within the original source as they will instead refer to positions in the modified contents. If the original source was a file source, accidentally using source locations from the modified source to make edits to the original file will produce malformed changes.
Note that while a file source is being read and expanded, the current directory is parameterized to the file source’s parent directory. This ensures that relative file path imports in source files can still be resolved regardless of what the current directory is before the source is read or expanded. This applies to both modified and unmodified file sources.
procedure
(source-read-language code) → (or/c module-path? #false)
code : source?
procedure
(source-read-syntax code) → syntax?
code : source?
procedure
(source-expand code) → syntax?
code : source?
procedure
(source-can-expand? code) → boolean?
code : source?
procedure
(source-text-of code stx) → immutable-string?
code : source? stx : syntax?
procedure
code : source?