Preprocessor (internal/preproc)
Sits between the lexer and the parser. It expands include file splices and passes use (library) and import (module) statements through unchanged, after checking them for common mistakes.
Algorithm
- Strip trivia tokens (comments, blank lines) so the recognizers can rely on adjacent tokens being meaningful.
- Walk the token stream.
- When
INCLUDE STRING SEMIis found (a textual file splice):- Verify the string ends in
.j. - Resolve the path (relative to the current file's directory, or absolute if it starts with
/) to its canonical, symlink-free form. - Reject it if that path is already on the current include chain (circular include).
- Read the file (refused above
limits.MaxSourceBytes), check its own# pragma-jennifer-*requirement header, lex it (with file-tagged tokens) and preprocess it recursively. Each file is read and tokenized once per run (a per-path cache). Include recursion is capped atlimits.MaxNestingDepthand the total spliced tokens atmaxSplicedTokens(2,000,000), so an exponential include chain is a positioned error rather than an out-of-memory kill. - Splice the result (dropping the trailing
EOF) at this point.
- Verify the string ends in
- When
IMPORT ...is found (a module import): pass the tokens through unchanged.importis a statement handled by the parser (ModuleImportStmt) and the interpreter's module loader, not a textual splice. Only the unquoted-path mistake is checked here (validateModuleImport). - When
USE IDENT SEMIis found: pass it through unchanged. The parser turns it into anImportStmtnode. - Report common mistakes:
| Input | Error |
|---|---|
include IDENT; (library form) | `include` is for files; use `use NAME;` for system libraries |
include IDENT.j; (unquoted file) | file splices take a string literal: `include "NAME.j";` |
include "foo.go"; | include path "foo.go" must end with `.j` |
import IDENT; (library form) | `import` takes a quoted module path (`import "NAME.j";`); for a system library use `use NAME;` |
import IDENT.j; (unquoted path) | module paths are quoted: `import "NAME.j";` |
use IDENT.j; (file form) | `use` is for system libraries; for files use `include "NAME.j";` |
Edge cases
- An
includepath must literally end in.j. includepaths may contain/for subdirectories. Absolute paths are accepted as-is; relative paths resolve from the including file's directory.- Circular includes are detected by tracking the canonical paths on the current chain, so a symlink and its target count as one file. A module
importcycle is detected separately, by the loader ininternal/interpreter; the preprocessor never opens an imported module.