Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/compiler/src/lexer/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type TokenType =
| "SCHEMA"
| "AGENT"
| "PROVIDER"
| "EDITION"
| "FLOW"
| "PRIVATE"
| "PARALLEL"
Expand Down Expand Up @@ -45,6 +46,7 @@ export interface Token {
export const KEYWORDS: Record<string, TokenType> = {
schema: "SCHEMA",
agent: "AGENT",
edition: "EDITION",
provider: "PROVIDER",
env: "ENV",
true: "BOOLEAN",
Expand Down
9 changes: 9 additions & 0 deletions packages/compiler/src/parser/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ export interface ProviderNode {
properties: ProviderPropertyNode[]; // Array of provider properties
}

//------------------------------------------------------------------
// Edition Supported (v0.6.0)
//------------------------------------------------------------------

export const SUPPORTED_EDITIONS = ["2026"] as const;
export type SamEdition = (typeof SUPPORTED_EDITIONS)[number]; // The edition of the Sam language, e.g., "2026"
export const DEFAULT_EDITION: SamEdition = "2026"; // The default edition of the Sam language

//------------------------------------------------------------------
// AST Node Types
//------------------------------------------------------------------
Expand All @@ -143,5 +151,6 @@ export type ASTNode = SchemaNode | AgentNode | FlowNode | ProviderNode;

export interface ProgramNode {
type: "Program";
edition: SamEdition; // The edition of the Sam language, e.g., "2026"
body: ASTNode[]; // Array of AST nodes, which can include schema declarations and other constructs
}
43 changes: 42 additions & 1 deletion packages/compiler/src/parser/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,4 +728,45 @@ test("should reject a non-boolean strict_schema provider property", () => {
expect(() => new Parser(tokenize(code)).parse()).toThrow(
/strict_schema/,
);
});
});
test("defaults the edition to 2026 when no directive is present", () => {
const ast = new Parser(tokenize(`flow main() {}`)).parse();

expect(ast.edition).toBe("2026");
});

test("accepts an edition directive at the top of the file", () => {
const ast = new Parser(
tokenize(`edition "2026"\n\nflow main() {}`),
).parse();

expect(ast.edition).toBe("2026");
});

test("rejects an unsupported edition", () => {
expect(() =>
new Parser(tokenize(`edition "2099"\n\nflow main() {}`)).parse(),
).toThrow(/Unsupported edition '2099'/);
});

test("rejects an edition directive below another declaration", () => {
const code = `
provider Groq {
protocol: "openai-compatible"
base_url: "https://api.groq.com/openai/v1"
}
edition "2026"
`;

expect(() => new Parser(tokenize(code)).parse()).toThrow(
/'edition' directive must appear only once at the top of the file \(line 6, column 5\)/,
);
});

test("reports the exact position of an edition directive that is not first", () => {
const code = `flow main() {}\nedition "2026"`;

expect(() => new Parser(tokenize(code)).parse()).toThrow(
/'edition' directive must appear only once at the top of the file \(line 2, column 1\)/,
);
});
40 changes: 38 additions & 2 deletions packages/compiler/src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { Token, TokenType } from "../lexer/tokens";
import type { ASTNode, ProgramNode } from "./ast";
import {
DEFAULT_EDITION,
SUPPORTED_EDITIONS,
type ASTNode,
type ProgramNode,
type SamEdition,
} from "./ast";
import { parseAgent } from "./parsers/parseAgent";
import { parseFlow } from "./parsers/parseFlow";
import { parseProvider } from "./parsers/parseProvider";
Expand All @@ -10,9 +16,11 @@ import { validateProgram } from "./validator";
export class Parser implements IParserContext {
private tokens: Token[];
private current: number;
public edition: SamEdition;

constructor(tokens: Token[]) {
constructor(tokens: Token[], edition: SamEdition = DEFAULT_EDITION) {
this.tokens = tokens;
this.edition = edition;
this.current = 0;
}

Expand Down Expand Up @@ -57,10 +65,20 @@ export class Parser implements IParserContext {
public parse(): ProgramNode {
const body: ASTNode[] = [];

// The edition directive, when present, must be the first token.
this.edition =
this.peek().type === "EDITION"
? this.parseHeaderEdition()
: DEFAULT_EDITION;

while (!this.isAtEnd()) {
const token = this.peek();

switch (token.type) {
case "EDITION":
throw new Error(
`SyntaxError: 'edition' directive must appear only once at the top of the file (line ${token.line}, column ${token.column})`,
);
case "SCHEMA":
body.push(parseSchema(this));
break;
Expand All @@ -83,10 +101,28 @@ export class Parser implements IParserContext {

const programNode: ProgramNode = {
type: "Program",
edition: this.edition,
body,
};

validateProgram(programNode);
return programNode;
}

/** Consumes `edition "<year>"`; the caller has already checked the token. */
private parseHeaderEdition(): SamEdition {
this.advance(); // Consume the "EDITION" token
const editionValueToken = this.consume(
"STRING",
'Expected a string value for the edition (e.g., edition "2026")',
);

const editionValue = editionValueToken.value;
if (!SUPPORTED_EDITIONS.includes(editionValue as SamEdition)) {
throw new Error(
`SyntaxError: Unsupported edition '${editionValue}' at line ${editionValueToken.line}, column ${editionValueToken.column}. Supported editions: ${SUPPORTED_EDITIONS.join(", ")}`,
);
}
return editionValue as SamEdition;
}
}
4 changes: 3 additions & 1 deletion packages/compiler/src/parser/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Token, TokenType } from "../lexer";
import type { Token, TokenType } from "../lexer/tokens";
import type { SamEdition } from "./ast";

export interface IParserContext {
peek(): Token;
advance(): Token;
isAtEnd(): boolean;
consume(type: TokenType, errorMessage: string): Token;
readonly edition: SamEdition; // The edition of the Sam language being used
}
Loading