Skip to content

Commit 7da9298

Browse files
committed
context docs
1 parent d1d6f63 commit 7da9298

36 files changed

Lines changed: 18036 additions & 185 deletions

.context/Specifications.md

Lines changed: 1807 additions & 0 deletions
Large diffs are not rendered by default.

.context/parser/Compiler.md

Lines changed: 573 additions & 0 deletions
Large diffs are not rendered by default.

.context/parser/Exception.md

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
# Exception
2+
3+
The Exception class extends the Exception class from `@stackpress/lib` to provide enhanced error handling specific to the idea parser library. It includes position information and better error reporting for parsing failures.
4+
5+
```typescript
6+
import { Exception } from '@stackpress/idea-parser';
7+
```
8+
9+
## Overview
10+
11+
Exception is a specialized error class that extends the base Exception class with additional functionality for parser-specific error handling. It automatically includes position information when parsing fails, making it easier to identify and fix syntax errors in schema files.
12+
13+
## Usage Examples
14+
15+
### Basic Error Handling
16+
17+
```typescript
18+
import { parse, Exception } from '@stackpress/idea-parser';
19+
20+
try {
21+
const result = parse('invalid schema syntax');
22+
} catch (error) {
23+
if (error instanceof Exception) {
24+
console.log('Parser error:', error.message);
25+
console.log('Error position:', error.start, '-', error.end);
26+
console.log('Stack trace:', error.stack);
27+
}
28+
}
29+
```
30+
31+
### Position Information
32+
33+
Exception includes position information to help locate errors in the source code:
34+
35+
```typescript
36+
import { EnumTree, Exception } from '@stackpress/idea-parser';
37+
38+
try {
39+
// Missing closing brace
40+
EnumTree.parse('enum Status { ACTIVE "Active"');
41+
} catch (error) {
42+
if (error instanceof Exception) {
43+
console.log('Error message:', error.message);
44+
console.log('Error starts at character:', error.start);
45+
console.log('Error ends at character:', error.end);
46+
47+
// Can be used for syntax highlighting in editors
48+
const errorRange = { start: error.start, end: error.end };
49+
}
50+
}
51+
```
52+
53+
### Common Error Scenarios
54+
55+
#### Syntax Errors
56+
57+
```typescript
58+
try {
59+
parse('enum Status { ACTIVE "Active"'); // Missing closing brace
60+
} catch (error) {
61+
console.log(error.message); // "Unexpected end of input expecting }"
62+
}
63+
```
64+
65+
#### Invalid Token Types
66+
67+
```typescript
68+
try {
69+
parse('model user { id String }'); // Invalid - should be capitalized
70+
} catch (error) {
71+
console.log(error.message); // "Expected CapitalIdentifier but got something else"
72+
}
73+
```
74+
75+
#### Unknown References
76+
77+
```typescript
78+
try {
79+
parse('model User { name String @field.input(UnknownProp) }');
80+
} catch (error) {
81+
console.log(error.message); // "Unknown reference UnknownProp"
82+
}
83+
```
84+
85+
#### Duplicate Declarations
86+
87+
```typescript
88+
try {
89+
parse(`
90+
enum Status { ACTIVE "Active" }
91+
enum Status { INACTIVE "Inactive" }
92+
`);
93+
} catch (error) {
94+
console.log(error.message); // "Duplicate Status"
95+
}
96+
```
97+
98+
## Integration with AST
99+
100+
All AST classes throw Exception when parsing fails:
101+
102+
```typescript
103+
import { SchemaTree, EnumTree, ModelTree, Exception } from '@stackpress/idea-parser';
104+
105+
// Any parsing operation can throw Exception
106+
try {
107+
const schema = SchemaTree.parse(schemaCode);
108+
const enumAST = EnumTree.parse(enumCode);
109+
const modelAST = ModelTree.parse(modelCode);
110+
} catch (error) {
111+
if (error instanceof Exception) {
112+
// Handle parser-specific errors
113+
console.error('Parsing failed:', error.message);
114+
} else {
115+
// Handle other types of errors
116+
console.error('Unexpected error:', error);
117+
}
118+
}
119+
```
120+
121+
## Error Recovery
122+
123+
While Exception indicates parsing failure, you can implement error recovery strategies:
124+
125+
```typescript
126+
import { parse, Exception } from '@stackpress/idea-parser';
127+
128+
function parseWithFallback(code: string, fallbackCode?: string) {
129+
try {
130+
return parse(code);
131+
} catch (error) {
132+
if (error instanceof Exception && fallbackCode) {
133+
console.warn('Primary parsing failed, trying fallback:', error.message);
134+
return parse(fallbackCode);
135+
}
136+
throw error; // Re-throw if no fallback or different error type
137+
}
138+
}
139+
```
140+
141+
## Language Server Integration
142+
143+
Exception's position information makes it ideal for language server implementations:
144+
145+
```typescript
146+
import { parse, Exception } from '@stackpress/idea-parser';
147+
148+
function validateSchema(code: string) {
149+
try {
150+
parse(code);
151+
return { valid: true, errors: [] };
152+
} catch (error) {
153+
if (error instanceof Exception) {
154+
return {
155+
valid: false,
156+
errors: [{
157+
message: error.message,
158+
range: {
159+
start: error.start,
160+
end: error.end
161+
},
162+
severity: 'error'
163+
}]
164+
};
165+
}
166+
throw error;
167+
}
168+
}
169+
```
170+
171+
## Inherited Features
172+
173+
Since Exception extends the base Exception class from `@stackpress/lib`, it inherits all the enhanced error handling features:
174+
175+
- Template-based error messages
176+
- Enhanced stack trace parsing
177+
- Position information support
178+
- HTTP status code integration
179+
- Validation error aggregation
180+
181+
For more details on the base Exception functionality, refer to the [@stackpress/lib Exception documentation](https://github.com/stackpress/lib#exception).
182+
183+
## Best Practices
184+
185+
### Always Check Error Type
186+
187+
```typescript
188+
try {
189+
parse(schemaCode);
190+
} catch (error) {
191+
if (error instanceof Exception) {
192+
// Handle parser errors specifically
193+
handleParserError(error);
194+
} else {
195+
// Handle other errors (network, file system, etc.)
196+
handleGenericError(error);
197+
}
198+
}
199+
```
200+
201+
### Use Position Information
202+
203+
```typescript
204+
function highlightError(code: string, error: Exception) {
205+
const lines = code.split('\n');
206+
let currentPos = 0;
207+
208+
for (let i = 0; i < lines.length; i++) {
209+
const lineEnd = currentPos + lines[i].length;
210+
211+
if (error.start >= currentPos && error.start <= lineEnd) {
212+
const lineStart = error.start - currentPos;
213+
const lineEnd = Math.min(error.end - currentPos, lines[i].length);
214+
215+
console.log(`Line ${i + 1}: ${lines[i]}`);
216+
console.log(' '.repeat(lineStart + 8) + '^'.repeat(lineEnd - lineStart));
217+
break;
218+
}
219+
220+
currentPos = lineEnd + 1; // +1 for newline
221+
}
222+
}
223+
```
224+
225+
### Provide Helpful Error Messages
226+
227+
```typescript
228+
function parseWithContext(code: string, filename?: string) {
229+
try {
230+
return parse(code);
231+
} catch (error) {
232+
if (error instanceof Exception) {
233+
const context = filename ? ` in ${filename}` : '';
234+
throw new Exception(
235+
`Parse error${context}: ${error.message}`,
236+
error.code
237+
).withPosition(error.start, error.end);
238+
}
239+
throw error;
240+
}
241+
}
242+
```

0 commit comments

Comments
 (0)