@baselineos/protocol-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,240 @@
1
+ /**
2
+ * Baseline Language PEG Grammar
3
+ * Parsing Expression Grammar for the Baseline prompt engineering language
4
+ *
5
+ * @license Apache-2.0
6
+ */
7
+
8
+ /**
9
+ * PEG Grammar Rules for Baseline Language
10
+ * These rules define how to parse .bl source files into AST nodes
11
+ */
12
+ export const baselineGrammarRules: Record<string, string> = {
13
+ // Program structure
14
+ program: `program = langBlock* frameBlock* coreBlock*`,
15
+
16
+ // Top-level blocks
17
+ langBlock: `langBlock = "lang" "{" (lexiconBlock / syntaxBlock / commandsBlock) "}"`,
18
+ frameBlock: `frameBlock = "frame" "{" (intentBlock / modeBlock / environmentBlock / scopeBlock / authorityBlock / basisBlock) "}"`,
19
+ coreBlock: `coreBlock = "core" "{" (fidelityBlock / specBlock / artifactBlock / productBlock / planBlock / promptBlock) "}"`,
20
+
21
+ // Lang: Lexicon
22
+ lexiconBlock: `lexiconBlock = "lexicon" "{" domain vocabulary template* pattern* "}"`,
23
+ domain: `domain = "domain" ":" identifier "{" description vocabulary patterns examples "}"`,
24
+ vocabulary: `vocabulary = "vocabulary" "{" term* synonym* antonym* definition* "}"`,
25
+ template: `template = "template" identifier "{" description pattern variables examples "}"`,
26
+ pattern: `pattern = "pattern" identifier "{" description regex examples usage "}"`,
27
+
28
+ // Lang: Syntax
29
+ syntaxBlock: `syntaxBlock = "syntax" "{" grammarRule* operator* precedenceRule* "}"`,
30
+ grammarRule: `grammarRule = "rule" identifier "{" pattern precedence associativity "}"`,
31
+ operator: `operator = "operator" symbol "{" precedence associativity description "}"`,
32
+ precedenceRule: `precedenceRule = "precedence" level "{" operators description "}"`,
33
+
34
+ // Lang: Commands
35
+ commandsBlock: `commandsBlock = "commands" "{" action* directive* operation* "}"`,
36
+ action: `action = "action" identifier "{" description parameters examples "}"`,
37
+ directive: `directive = "directive" identifier "{" description parameters examples "}"`,
38
+ operation: `operation = "operation" identifier "{" description parameters examples "}"`,
39
+
40
+ // Frame: Intent
41
+ intentBlock: `intentBlock = "intent" "{" goal* deliverable* successCriterion* constraint* "}"`,
42
+ goal: `goal = "goal" identifier "{" description priority deadline successCriteria "}"`,
43
+ deliverable: `deliverable = "deliverable" identifier "{" description type format quality "}"`,
44
+ successCriterion: `successCriterion = "criterion" identifier "{" description metric target unit "}"`,
45
+ constraint: `constraint = "constraint" identifier "{" description type value enforcement "}"`,
46
+
47
+ // Frame: Mode
48
+ modeBlock: `modeBlock = "mode" "{" executionPattern reasoning checkpoint* iterationRule* "}"`,
49
+ executionPattern: `executionPattern = "pattern" identifier "{" description steps checkpoints iterations "}"`,
50
+ reasoningMode: `reasoningMode = "reasoning" type "{" description parameters "}"`,
51
+ checkpoint: `checkpoint = "checkpoint" identifier "{" description validation actions "}"`,
52
+ iterationRule: `iterationRule = "iteration" identifier "{" condition maxIterations convergence "}"`,
53
+
54
+ // Frame: Environment
55
+ environmentBlock: `environmentBlock = "environment" "{" model* tool* runtime policy* "}"`,
56
+ model: `model = "model" identifier "{" type version capabilities constraints "}"`,
57
+ tool: `tool = "tool" identifier "{" description type parameters examples "}"`,
58
+ runtime: `runtime = "runtime" "{" environment version resources policies "}"`,
59
+ policy: `policy = "policy" identifier "{" description type rules enforcement "}"`,
60
+
61
+ // Frame: Scope
62
+ scopeBlock: `scopeBlock = "scope" "{" boundary* limit* exclusion* inclusion* "}"`,
63
+ boundary: `boundary = "boundary" identifier "{" description type value enforcement "}"`,
64
+ limit: `limit = "limit" identifier "{" description type value unit "}"`,
65
+ exclusion: `exclusion = "exclusion" identifier "{" description reason alternatives "}"`,
66
+ inclusion: `inclusion = "inclusion" identifier "{" description reason requirements "}"`,
67
+
68
+ // Frame: Authority
69
+ authorityBlock: `authorityBlock = "authority" "{" role* permission* accessControl* delegationRule* "}"`,
70
+ role: `role = "role" identifier "{" description permissions responsibilities "}"`,
71
+ permission: `permission = "permission" identifier "{" description scope level "}"`,
72
+ accessControl: `accessControl = "access" resource "{" permissions roles conditions "}"`,
73
+ delegationRule: `delegationRule = "delegation" identifier "{" description fromRole toRole conditions expiration "}"`,
74
+
75
+ // Frame: Basis
76
+ basisBlock: `basisBlock = "basis" "{" organization kpi* metric* context* "}"`,
77
+ organization: `organization = "organization" "{" name description structure culture values "}"`,
78
+ kpi: `kpi = "kpi" identifier "{" description metric target unit frequency "}"`,
79
+ metric: `metric = "metric" identifier "{" description type calculation unit "}"`,
80
+ context: `context = "context" identifier "{" description type value source "}"`,
81
+
82
+ // Core: Fidelity
83
+ fidelityBlock: `fidelityBlock = "fidelity" "{" precision verification quality constraints "}"`,
84
+ precisionLevel: `precisionLevel = "precision" level "{" description requirements "}"`,
85
+ verificationLevel: `verificationLevel = "verification" level "{" description methods "}"`,
86
+ qualityStandard: `qualityStandard = "quality" identifier "{" description criteria measurement "}"`,
87
+
88
+ // Core: Spec
89
+ specBlock: `specBlock = "spec" "{" requirement* contract* interface* validation* "}"`,
90
+ requirement: `requirement = "requirement" identifier "{" description type priority acceptance "}"`,
91
+ contract: `contract = "contract" identifier "{" description parties terms enforcement "}"`,
92
+ interface: `interface = "interface" identifier "{" description type methods properties "}"`,
93
+ validation: `validation = "validation" identifier "{" description type rules errorHandling "}"`,
94
+
95
+ // Core: Artifact
96
+ artifactBlock: `artifactBlock = "artifact" "{" output* receipt* signature* retentionPolicy* "}"`,
97
+ output: `output = "output" identifier "{" description type format quality "}"`,
98
+ receipt: `receipt = "receipt" identifier "{" description timestamp hash signature "}"`,
99
+ signature: `signature = "signature" "{" algorithm value timestamp key "}"`,
100
+ retentionPolicy: `retentionPolicy = "retention" identifier "{" description duration conditions disposal "}"`,
101
+
102
+ // Core: Product
103
+ productBlock: `productBlock = "product" "{" deliverable* document* result* outcome* "}"`,
104
+ document: `document = "document" identifier "{" description type format content "}"`,
105
+ result: `result = "result" identifier "{" description type value quality "}"`,
106
+ outcome: `outcome = "outcome" identifier "{" description type success metrics "}"`,
107
+
108
+ // Core: Plan
109
+ planBlock: `planBlock = "plan" "{" roadmap timeline milestone* dependency* "}"`,
110
+ roadmap: `roadmap = "roadmap" "{" name description phases timeline milestones "}"`,
111
+ timeline: `timeline = "timeline" "{" start end phases dependencies "}"`,
112
+ milestone: `milestone = "milestone" identifier "{" description date deliverables successCriteria "}"`,
113
+ dependency: `dependency = "dependency" identifier "{" description from to type critical "}"`,
114
+
115
+ // Core: Prompt (IR)
116
+ promptBlock: `promptBlock = "prompt" "{" ir instruction* context* constraint* "}"`,
117
+ intermediateRepresentation: `intermediateRepresentation = "ir" "{" version blocks metadata optimizations "}"`,
118
+ instruction: `instruction = "instruction" identifier "{" description type parameters examples "}"`,
119
+
120
+ // Utility elements
121
+ parameter: `parameter = "parameter" identifier "{" type description required default validation "}"`,
122
+ term: `term = "term" identifier "{" definition synonyms antonyms examples "}"`,
123
+ synonym: `synonym = "synonym" term "{" alternatives context "}"`,
124
+ antonym: `antonym = "antonym" term "{" opposites context "}"`,
125
+ definition: `definition = "definition" term "{" meaning context examples "}"`,
126
+ variable: `variable = "variable" identifier "{" type description required default "}"`,
127
+ step: `step = "step" identifier "{" description action validation next "}"`,
128
+ convergenceCriteria: `convergenceCriteria = "convergence" "{" metric threshold tolerance maxIterations "}"`,
129
+ capability: `capability = "capability" identifier "{" description parameters examples "}"`,
130
+ resource: `resource = "resource" "{" type name capacity unit available "}"`,
131
+ rule: `rule = "rule" identifier "{" description condition action priority "}"`,
132
+ condition: `condition = "condition" identifier "{" description expression parameters "}"`,
133
+ structure: `structure = "structure" "{" type levels departments reporting "}"`,
134
+ culture: `culture = "culture" "{" values principles practices norms "}"`,
135
+ value: `value = "value" identifier "{" description importance examples "}"`,
136
+ phase: `phase = "phase" identifier "{" description start end deliverables milestones "}"`,
137
+ optimization: `optimization = "optimization" identifier "{" description type impact implementation "}"`,
138
+ errorHandling: `errorHandling = "errorHandling" "{" strategy fallback logging userNotification "}"`,
139
+ method: `method = "method" identifier "{" description parameters returnType examples "}"`,
140
+ property: `property = "property" identifier "{" description type required default "}"`,
141
+ acceptanceCriteria: `acceptanceCriteria = "criteria" identifier "{" description test passCondition priority "}"`,
142
+
143
+ // Primitives
144
+ identifier: `identifier = [a-zA-Z_][a-zA-Z0-9_]*`,
145
+ description: `description = "description" ":" string`,
146
+ string: `string = '"' [^"]* '"'`,
147
+ number: `number = [0-9]+ ("." [0-9]+)?`,
148
+ boolean: `boolean = "true" / "false"`,
149
+ date: `date = [0-9]{4}-[0-9]{2}-[0-9]{2}`,
150
+ time: `time = [0-9]{2}:[0-9]{2}:[0-9]{2}`,
151
+ datetime: `datetime = date "T" time`,
152
+ array: `array = "[" (valueElement ("," valueElement)*)? "]"`,
153
+ object: `object = "{" (pair ("," pair)*)? "}"`,
154
+ pair: `pair = key ":" valueElement`,
155
+ key: `key = identifier / string`,
156
+ valueElement: `valueElement = string / number / boolean / date / time / datetime / array / object / identifier`,
157
+ };
158
+
159
+ /**
160
+ * Grammar validation utilities
161
+ */
162
+ export class GrammarValidator {
163
+ static validateRule(rule: string): boolean {
164
+ const stack: string[] = [];
165
+ const pairs: Record<string, string> = { '(': ')', '[': ']', '{': '}' };
166
+
167
+ for (const char of rule) {
168
+ if (pairs[char]) {
169
+ stack.push(char);
170
+ } else if (Object.values(pairs).includes(char)) {
171
+ const open = stack.pop();
172
+ if (!open || pairs[open] !== char) {
173
+ return false;
174
+ }
175
+ }
176
+ }
177
+ return stack.length === 0;
178
+ }
179
+
180
+ static extractReferencedRules(rule: string): string[] {
181
+ const referencedRules: string[] = [];
182
+ const rulePattern = /[a-zA-Z_][a-zA-Z0-9_]*/g;
183
+ let match: RegExpExecArray | null;
184
+ while ((match = rulePattern.exec(rule)) !== null) {
185
+ const ruleName = match[0];
186
+ if (!['true', 'false', 'null'].includes(ruleName)) {
187
+ referencedRules.push(ruleName);
188
+ }
189
+ }
190
+ return referencedRules;
191
+ }
192
+
193
+ static hasRecursion(ruleName: string, grammar: Record<string, string>): boolean {
194
+ const visited = new Set<string>();
195
+ const recursionStack = new Set<string>();
196
+
197
+ const check = (name: string): boolean => {
198
+ if (recursionStack.has(name)) return true;
199
+ if (visited.has(name)) return false;
200
+
201
+ visited.add(name);
202
+ recursionStack.add(name);
203
+
204
+ const ruleContent = grammar[name];
205
+ if (ruleContent) {
206
+ if (ruleContent.includes(name)) return true;
207
+ for (const ref of this.extractReferencedRules(ruleContent)) {
208
+ if (check(ref)) return true;
209
+ }
210
+ }
211
+
212
+ recursionStack.delete(name);
213
+ return false;
214
+ };
215
+
216
+ return check(ruleName);
217
+ }
218
+
219
+ static generateSummary(grammar: Record<string, string>) {
220
+ const rules = Object.keys(grammar);
221
+ return {
222
+ totalRules: rules.length,
223
+ ruleTypes: {
224
+ structural: rules.filter(r => r.includes('Block') || r.includes('Program')),
225
+ lexical: rules.filter(r => r.includes('Lexicon') || r.includes('Vocabulary')),
226
+ syntactic: rules.filter(r => r.includes('Syntax') || r.includes('Grammar')),
227
+ semantic: rules.filter(r => r.includes('Intent') || r.includes('Mode')),
228
+ execution: rules.filter(r => r.includes('Core') || r.includes('Prompt')),
229
+ },
230
+ hasRecursion: rules.some(r => this.hasRecursion(r, grammar)),
231
+ validationStatus: rules.every(r => this.validateRule(grammar[r])),
232
+ };
233
+ }
234
+ }
235
+
236
+ export const baselineGrammar = {
237
+ rules: baselineGrammarRules,
238
+ validator: GrammarValidator,
239
+ summary: () => GrammarValidator.generateSummary(baselineGrammarRules),
240
+ };
@@ -0,0 +1,420 @@
1
+ /**
2
+ * Baseline Language Parser
3
+ * PEG-based parser for Baseline Language syntax
4
+ *
5
+ * @license Apache-2.0
6
+ */
7
+
8
+ import type {
9
+ BaselineProgram,
10
+ ProgramMetadata,
11
+ BlockMetadata,
12
+ Priority,
13
+ } from '../types/index.js';
14
+
15
+ // ─── Token Types ────────────────────────────────────────────────────
16
+
17
+ export type TokenType = 'IDENTIFIER' | 'STRING' | 'NUMBER' | 'OPERATOR' | 'EOF';
18
+
19
+ export interface Token {
20
+ type: TokenType;
21
+ value: string;
22
+ position: number;
23
+ }
24
+
25
+ // ─── Block Content Types ────────────────────────────────────────────
26
+
27
+ export interface LangBlockContent {
28
+ type: 'lexicon';
29
+ domain: string;
30
+ vocabulary: unknown[];
31
+ templates: unknown[];
32
+ patterns: unknown[];
33
+ }
34
+
35
+ export interface FrameBlockContent {
36
+ type: 'intent';
37
+ goals: unknown[];
38
+ deliverables: unknown[];
39
+ successCriteria: unknown[];
40
+ constraints: unknown[];
41
+ }
42
+
43
+ export interface CoreBlockContent {
44
+ type: 'fidelity';
45
+ precision: string;
46
+ verification: string;
47
+ quality: unknown[];
48
+ constraints: unknown[];
49
+ }
50
+
51
+ export type ParsedBlock = {
52
+ type: 'lang' | 'frame' | 'core';
53
+ content: LangBlockContent | FrameBlockContent | CoreBlockContent;
54
+ metadata: BlockMetadata;
55
+ };
56
+
57
+ // ─── Parser ─────────────────────────────────────────────────────────
58
+
59
+ export class BaselineParser {
60
+ private tokens: Token[] = [];
61
+ private currentPosition = 0;
62
+ private grammar: unknown = null;
63
+
64
+ async initialize(): Promise<void> {
65
+ await this.initializeGrammar();
66
+ }
67
+
68
+ /**
69
+ * Parse Baseline Language source code
70
+ */
71
+ async parse(source: string): Promise<BaselineProgram> {
72
+ try {
73
+ this.tokens = this.tokenize(source);
74
+ this.currentPosition = 0;
75
+ const program = await this.parseProgram();
76
+ return program;
77
+ } catch (error) {
78
+ const errorMessage = error instanceof Error ? error.message : String(error);
79
+ throw new Error(`Parsing failed: ${errorMessage}`);
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Tokenize source code into tokens
85
+ */
86
+ tokenize(source: string): Token[] {
87
+ const tokens: Token[] = [];
88
+ let position = 0;
89
+
90
+ while (position < source.length) {
91
+ const char = source[position];
92
+
93
+ // Skip whitespace
94
+ if (/\s/.test(char)) {
95
+ position++;
96
+ continue;
97
+ }
98
+
99
+ // Handle single-line comments
100
+ if (char === '/' && source[position + 1] === '/') {
101
+ const commentEnd = source.indexOf('\n', position);
102
+ if (commentEnd === -1) {
103
+ position = source.length;
104
+ } else {
105
+ position = commentEnd + 1;
106
+ }
107
+ continue;
108
+ }
109
+
110
+ // Handle multi-line comments
111
+ if (char === '/' && source[position + 1] === '*') {
112
+ const commentEnd = source.indexOf('*/', position);
113
+ if (commentEnd === -1) {
114
+ throw new Error('Unterminated multi-line comment');
115
+ }
116
+ position = commentEnd + 2;
117
+ continue;
118
+ }
119
+
120
+ // Handle identifiers
121
+ if (/[a-zA-Z_]/.test(char)) {
122
+ const identifier = this.extractIdentifier(source, position);
123
+ tokens.push({
124
+ type: 'IDENTIFIER',
125
+ value: identifier,
126
+ position,
127
+ });
128
+ position += identifier.length;
129
+ continue;
130
+ }
131
+
132
+ // Handle strings
133
+ if (char === '"') {
134
+ const str = this.extractString(source, position);
135
+ tokens.push({
136
+ type: 'STRING',
137
+ value: str,
138
+ position,
139
+ });
140
+ position += str.length + 2; // +2 for quotes
141
+ continue;
142
+ }
143
+
144
+ // Handle numbers
145
+ if (/[0-9]/.test(char)) {
146
+ const num = this.extractNumber(source, position);
147
+ tokens.push({
148
+ type: 'NUMBER',
149
+ value: num,
150
+ position,
151
+ });
152
+ position += num.length;
153
+ continue;
154
+ }
155
+
156
+ // Handle operators and punctuation
157
+ if (this.isOperator(char) || this.isPunctuation(char)) {
158
+ tokens.push({
159
+ type: 'OPERATOR',
160
+ value: char,
161
+ position,
162
+ });
163
+ position++;
164
+ continue;
165
+ }
166
+
167
+ throw new Error(`Unknown character at position ${position}: ${char}`);
168
+ }
169
+
170
+ // Add EOF token
171
+ tokens.push({
172
+ type: 'EOF',
173
+ value: '',
174
+ position,
175
+ });
176
+
177
+ return tokens;
178
+ }
179
+
180
+ /**
181
+ * Parse program structure
182
+ */
183
+ private async parseProgram(): Promise<BaselineProgram> {
184
+ const blocks: ParsedBlock[] = [];
185
+
186
+ while (this.currentToken().type !== 'EOF') {
187
+ const block = await this.parseBlock();
188
+ if (block) {
189
+ blocks.push(block);
190
+ }
191
+ }
192
+
193
+ const metadata: ProgramMetadata = {
194
+ version: '1.0.0',
195
+ created: new Date(),
196
+ author: 'Baseline Lang',
197
+ description: 'Generated Baseline Language program',
198
+ tags: ['baseline', 'lang', 'generated'],
199
+ };
200
+
201
+ return {
202
+ blocks: blocks as unknown as BaselineProgram['blocks'],
203
+ metadata,
204
+ };
205
+ }
206
+
207
+ /**
208
+ * Parse individual block
209
+ */
210
+ private async parseBlock(): Promise<ParsedBlock | null> {
211
+ const token = this.currentToken();
212
+ if (token.type === 'IDENTIFIER') {
213
+ switch (token.value) {
214
+ case 'lang':
215
+ return await this.parseLangBlock();
216
+ case 'frame':
217
+ return await this.parseFrameBlock();
218
+ case 'core':
219
+ return await this.parseCoreBlock();
220
+ default:
221
+ throw new Error(`Unknown block type: ${token.value}`);
222
+ }
223
+ }
224
+ return null;
225
+ }
226
+
227
+ /**
228
+ * Parse language block
229
+ */
230
+ private async parseLangBlock(): Promise<ParsedBlock> {
231
+ this.expect('IDENTIFIER', 'lang');
232
+ this.expect('OPERATOR', '{');
233
+ const content = await this.parseLangBlockContent();
234
+ this.expect('OPERATOR', '}');
235
+
236
+ return {
237
+ type: 'lang',
238
+ content,
239
+ metadata: this.createBlockMetadata('lang'),
240
+ };
241
+ }
242
+
243
+ /**
244
+ * Parse frame block
245
+ */
246
+ private async parseFrameBlock(): Promise<ParsedBlock> {
247
+ this.expect('IDENTIFIER', 'frame');
248
+ this.expect('OPERATOR', '{');
249
+ const content = await this.parseFrameBlockContent();
250
+ this.expect('OPERATOR', '}');
251
+
252
+ return {
253
+ type: 'frame',
254
+ content,
255
+ metadata: this.createBlockMetadata('frame'),
256
+ };
257
+ }
258
+
259
+ /**
260
+ * Parse core block
261
+ */
262
+ private async parseCoreBlock(): Promise<ParsedBlock> {
263
+ this.expect('IDENTIFIER', 'core');
264
+ this.expect('OPERATOR', '{');
265
+ const content = await this.parseCoreBlockContent();
266
+ this.expect('OPERATOR', '}');
267
+
268
+ return {
269
+ type: 'core',
270
+ content,
271
+ metadata: this.createBlockMetadata('core'),
272
+ };
273
+ }
274
+
275
+ /**
276
+ * Parse language block content
277
+ * TODO: Full implementation should parse lexicon, syntax, and commands
278
+ */
279
+ private async parseLangBlockContent(): Promise<LangBlockContent> {
280
+ while (this.currentPosition < this.tokens.length && this.currentToken().type !== 'EOF') {
281
+ const token = this.currentToken();
282
+ if (token.type === 'OPERATOR' && token.value === '}') {
283
+ break;
284
+ }
285
+ this.currentPosition++;
286
+ }
287
+
288
+ return {
289
+ type: 'lexicon',
290
+ domain: 'default',
291
+ vocabulary: [],
292
+ templates: [],
293
+ patterns: [],
294
+ };
295
+ }
296
+
297
+ /**
298
+ * Parse frame block content
299
+ * TODO: Full implementation should parse intent, mode, environment, etc.
300
+ */
301
+ private async parseFrameBlockContent(): Promise<FrameBlockContent> {
302
+ while (this.currentPosition < this.tokens.length && this.currentToken().type !== 'EOF') {
303
+ const token = this.currentToken();
304
+ if (token.type === 'OPERATOR' && token.value === '}') {
305
+ break;
306
+ }
307
+ this.currentPosition++;
308
+ }
309
+
310
+ return {
311
+ type: 'intent',
312
+ goals: [],
313
+ deliverables: [],
314
+ successCriteria: [],
315
+ constraints: [],
316
+ };
317
+ }
318
+
319
+ /**
320
+ * Parse core block content
321
+ * TODO: Full implementation should parse fidelity, spec, artifact, etc.
322
+ */
323
+ private async parseCoreBlockContent(): Promise<CoreBlockContent> {
324
+ while (this.currentPosition < this.tokens.length && this.currentToken().type !== 'EOF') {
325
+ const token = this.currentToken();
326
+ if (token.type === 'OPERATOR' && token.value === '}') {
327
+ break;
328
+ }
329
+ this.currentPosition++;
330
+ }
331
+
332
+ return {
333
+ type: 'fidelity',
334
+ precision: 'medium',
335
+ verification: 'standard',
336
+ quality: [],
337
+ constraints: [],
338
+ };
339
+ }
340
+
341
+ // ─── Helper Methods ─────────────────────────────────────────────────
342
+
343
+ private extractIdentifier(source: string, position: number): string {
344
+ let end = position;
345
+ while (end < source.length && /[a-zA-Z0-9_]/.test(source[end])) {
346
+ end++;
347
+ }
348
+ return source.substring(position, end);
349
+ }
350
+
351
+ private extractString(source: string, position: number): string {
352
+ let end = position + 1;
353
+ while (end < source.length && source[end] !== '"') {
354
+ if (source[end] === '\\') {
355
+ end += 2; // Skip escaped character
356
+ } else {
357
+ end++;
358
+ }
359
+ }
360
+ if (end >= source.length) {
361
+ throw new Error('Unterminated string');
362
+ }
363
+ return source.substring(position + 1, end);
364
+ }
365
+
366
+ private extractNumber(source: string, position: number): string {
367
+ let end = position;
368
+ while (end < source.length && /[0-9]/.test(source[end])) {
369
+ end++;
370
+ }
371
+ if (end < source.length && source[end] === '.') {
372
+ end++;
373
+ while (end < source.length && /[0-9]/.test(source[end])) {
374
+ end++;
375
+ }
376
+ }
377
+ return source.substring(position, end);
378
+ }
379
+
380
+ private isOperator(char: string): boolean {
381
+ return /[+\-*/=<>!&|]/.test(char);
382
+ }
383
+
384
+ private isPunctuation(char: string): boolean {
385
+ return /[(){}[\],;:.]/.test(char);
386
+ }
387
+
388
+ private currentToken(): Token {
389
+ return this.tokens[this.currentPosition];
390
+ }
391
+
392
+ private expect(type: TokenType, value?: string): void {
393
+ const token = this.currentToken();
394
+ if (token.type !== type) {
395
+ throw new Error(`Expected ${type}, got ${token.type}`);
396
+ }
397
+ if (value && token.value !== value) {
398
+ throw new Error(`Expected ${value}, got ${token.value}`);
399
+ }
400
+ this.currentPosition++;
401
+ }
402
+
403
+ private createBlockMetadata(type: string): BlockMetadata {
404
+ return {
405
+ id: `${type}_${Date.now()}`,
406
+ name: `${type} block`,
407
+ description: `Generated ${type} block`,
408
+ version: '1.0.0',
409
+ created: new Date(),
410
+ modified: new Date(),
411
+ tags: [type, 'generated'],
412
+ priority: 'medium' as Priority,
413
+ };
414
+ }
415
+
416
+ private async initializeGrammar(): Promise<void> {
417
+ // Initialize PEG grammar
418
+ // In full implementation, this would load and validate grammar rules
419
+ }
420
+ }