Nomos Compiler Library #22
Closed
pewpewpotato
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
The compiler turns Nomos source files into a deterministic configuration snapshot. It is a small, stable Go library consumed by the CLI and other tools.
This README documents the compiler expectations and, importantly, the parser contract the compiler relies on. The compiler depends on
libs/parserfor a stable AST and precise parse errors — read theParser contractsection below for details you must rely on when implementing compilation, error reporting and diagnostics.Goals
source,importand inlinereferenceconstructs via pluggable provider APIs.Files accepted
.cslextension and directories containing.cslfiles. Callers pass a path (file or directory) viaOptions.Path. The compiler must traverse directories in a deterministic order.Relationship to other projects
apps/command-line(the CLI).libs/parserfor tokenizing/parsing and AST construction.Public API (contract)
Keep the surface minimal and stable. Example public types used by consumers (CLI):
Notes:
Snapshotis format-agnostic; the caller chooses JSON/YAML/HCL rendering.Parser contract (what compiler relies on)
The compiler must consume the parser output and error types in specific ways. The parser (in
libs/parser) exposes a stable set of entry points and AST shapes — the compiler must treat parsing errors as lower-level, recoverable diagnostics and use AST node spans to produce rich error messages.Key parser behaviours the compiler relies on:
Public parse entry points:
parser.ParseFile(path string) (*ast.AST, error)— convenience top-level function.parser.Parse(r io.Reader, filename string) (*ast.AST, error)— parse from an arbitrary reader.parser.NewParser()and(*Parser).Parse*allow reusing pooled parser instances.AST shape:
*ast.ASTcontainsStatements []ast.Stmtand aSourceSpan.ast.SourceDecl,ast.ImportStmt,ast.SectionDecl(sections with map entries).ast.Expr. Supported value expressions currently include*ast.StringLiteraland*ast.ReferenceExprfor inline references.ReferenceExprhasAlias string,Path []stringand carries aSourceSpan.Inline references:
key: reference:alias:dot.pathproduces anast.ReferenceExprassigned as the entry value.reference:statements (legacy) are rejected by the parser with aSyntaxErrorand a migration hint — the compiler should not expect top-level reference statements.Source spans and position information:
SourceSpanwith filename, start/end line and column.EndColis inclusive (points to last byte of the token).Parse errors:
*parser.ParseErrorwith kindsLexError,SyntaxError, orIOErrorand includes filename/line/column and an optional snippet.parser.FormatParseError(err, sourceText)to render caret-marked user-friendly messages.Validation scope:
:after keywords, properly-terminated strings, alias is a string literal).Implication for the compiler:
ReferenceExprnodes as value placeholders to be resolved by providers/import resolution. Do not attempt to interpretreference:syntax as a top-level statement — it's only valid as a value.SourceSpanandFormatParseErroroutput so messages are consistent with parser diagnostics.Composition and resolution semantics
ReferenceExpr) are resolved after imports/values from providers are materialized, allowing cross-file linking and importing.Providers and sources
sourcedeclarations in the AST map to provider instances by alias and type.Errors and diagnostics
ParseErrorfor syntax/lexing faults. For semantic errors, return structured errors that includeSourceSpanwhen possible.fmt.Errorf("...: %w", err)) to preserve root causes.Example usage (compiler consumer)
Testing strategy
testdata/and assert snapshots. Keep tests deterministic and avoid external network calls.Versioning
libs/compiler/vX.Y.Zand follow semantic versioning. Breaking API changes require a major version bump.Notes / Follow-ups
SourceSpansemantics (1-indexed, byte-based columns, inclusive end column) are important when slicing source text for display — prefer usingparser.FormatParseErrorfor consistent caret rendering.All reactions