Parser API
The SentriFlow parser transforms network configuration text into a structured AST (Abstract Syntax Tree) that can be traversed and validated. This page covers the parser API for programmatic integration.
Installation
npm install @sentriflow/coreBasic Usage
import { SchemaAwareParser } from '@sentriflow/core';
const config = `
hostname Router1
!
interface GigabitEthernet0/0
ip address 192.168.1.1 255.255.255.0
no shutdown
`;
const parser = new SchemaAwareParser();
const ast = parser.parse(config);
// ast is an array of ConfigNode objects
console.log(ast.length);SchemaAwareParser
The SchemaAwareParser class is the primary parser for SentriFlow. It automatically detects the vendor format and parses configuration text into an AST.
Constructor
const parser = new SchemaAwareParser();parse()
Parse configuration text into an AST.
Signature
class SchemaAwareParser {
parse(config: string, options?: ParseOptions): ConfigNode[]
}Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
config | string | Yes | Configuration text to parse |
options | ParseOptions | No | Parser options |
ParseOptions
interface ParseOptions {
/** Include comments in AST */
includeComments?: boolean;
/** Include empty lines as nodes */
includeEmpty?: boolean;
/** Starting line number (for partial parsing) */
startLine?: number;
/** Enable strict mode (fail on unknown commands) */
strict?: boolean;
}Returns
Returns an array of ConfigNode objects representing the parsed configuration.
Example
import { SchemaAwareParser } from '@sentriflow/core';
const config = `
! This is a comment
hostname Router1
!
interface GigabitEthernet0/0
description Uplink to Core
ip address 192.168.1.1 255.255.255.0
no shutdown
`;
const parser = new SchemaAwareParser();
// Parse with default options (auto-detects vendor)
const ast = parser.parse(config);
// Parse with comments included
const astWithComments = parser.parse(config, {
includeComments: true,
});
// Parse in strict mode
try {
const strictAst = parser.parse(config, { strict: true });
} catch (error) {
console.error('Parse error:', error.message);
}detectVendor()
Auto-detect vendor from configuration content.
Signature
function detectVendor(config: string): VendorSchema | undefinedDetection Heuristics
The vendor is detected based on:
- Header comments (e.g.,
! sentriflow: vendor=cisco-ios) - Command patterns (e.g.,
setcommands indicate Juniper) - Section structure
- Keyword frequency
Example
import { detectVendor, SchemaAwareParser } from '@sentriflow/core';
const config = `
hostname Router1
interface GigabitEthernet0/0
ip address 192.168.1.1 255.255.255.0
`;
const vendorSchema = detectVendor(config);
if (vendorSchema) {
console.log(`Detected vendor: ${vendorSchema.name}`);
}
// Note: SchemaAwareParser auto-detects vendor, so you typically
// don't need to call detectVendor() manually
const parser = new SchemaAwareParser();
const ast = parser.parse(config);IncrementalParser
For scenarios where configuration is being edited in real-time (such as in an IDE), the IncrementalParser provides efficient incremental parsing capabilities.
Basic Usage
import { IncrementalParser } from '@sentriflow/core';
const parser = new IncrementalParser();
// Initial parse
let ast = parser.parse(config);
// After edits, incrementally update the AST
ast = parser.update(newConfig, changes);The IncrementalParser is optimized for scenarios with frequent small edits, such as IDE integrations. For one-time parsing of complete configurations, use SchemaAwareParser instead.
Supported Vendors
The parser automatically detects and handles configurations from these vendors:
| Vendor | Format | Notes |
|---|---|---|
cisco-ios | Hierarchical | IOS, IOS-XE, IOS-XR |
cisco-nxos | Hierarchical | Nexus switches |
juniper-junos | Set/Hierarchical | Set and hierarchical formats |
paloalto-panos | Set commands | Set command format |
arista-eos | Hierarchical | EOS configuration |
vyos | Hierarchical | VyOS/EdgeOS |
fortinet-fortigate | Hierarchical | FortiOS config |
Error Handling
The parser throws errors for invalid configurations when strict mode is enabled:
import { SchemaAwareParser } from '@sentriflow/core';
const parser = new SchemaAwareParser();
try {
const ast = parser.parse(config, { strict: true });
} catch (error) {
console.error(`Parse error: ${error.message}`);
}By default, the parser operates in lenient mode and will attempt to parse configurations even if they contain unrecognized commands. Use strict: true to catch all parsing issues.
Performance Considerations
- Use
SchemaAwareParserfor one-time parsing of complete configurations - Use
IncrementalParserfor IDE integrations with frequent edits - Consider
includeComments: falseif comments aren’t needed for your use case - The parser automatically optimizes for the detected vendor format
Next Steps
- ConfigNode API - Working with AST nodes
- Rule Engine API - Running validation rules
- TypeScript Rules - Writing custom rules