Skip to Content
APIParser API: Parse Network Configs into Structured AST

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/core

Basic 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

ParameterTypeRequiredDescription
configstringYesConfiguration text to parse
optionsParseOptionsNoParser 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 | undefined

Detection Heuristics

The vendor is detected based on:

  • Header comments (e.g., ! sentriflow: vendor=cisco-ios)
  • Command patterns (e.g., set commands 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:

VendorFormatNotes
cisco-iosHierarchicalIOS, IOS-XE, IOS-XR
cisco-nxosHierarchicalNexus switches
juniper-junosSet/HierarchicalSet and hierarchical formats
paloalto-panosSet commandsSet command format
arista-eosHierarchicalEOS configuration
vyosHierarchicalVyOS/EdgeOS
fortinet-fortigateHierarchicalFortiOS 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 SchemaAwareParser for one-time parsing of complete configurations
  • Use IncrementalParser for IDE integrations with frequent edits
  • Consider includeComments: false if comments aren’t needed for your use case
  • The parser automatically optimizes for the detected vendor format

Next Steps

Last updated on