Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
NotificationsYou must be signed in to change notification settings

streamich/jit-parser

Repository files navigation

Top-down recursive descent backtracking PEG scanner-less JIT parser combinator generator.

A high-performance parser library that compiles grammar definitions into efficient JavaScript parsing functions at runtime. It generates both Concrete Syntax Trees (CST) and Abstract Syntax Trees (AST) from textual input.

Table of Contents

Installation

npm install jit-parser

Quick Start

import{CodegenGrammar}from'jit-parser';import{ParseContext}from'jit-parser';// Define a simple grammarconstgrammar={start:'Value',cst:{Value:'hello'}};// Compile the grammar to JavaScriptconstparser=CodegenGrammar.compile(grammar);// Parse inputconstctx=newParseContext('hello',false);constcst=parser(ctx,0);console.log(cst);// CST node representing the parse result

Grammar Node Types

JIT Parser supports five main grammar node types for defining parsing rules. Grammar rules can be fully defined in JSON, making them language-agnostic and easy to serialize.

1. RefNode (Reference Node)

References a named node defined elsewhere in the grammar.

Interface:

typeRefNode<Nameextendsstring=string>={r:Name};

Syntax:

{r:'NodeName'}

Example:

constgrammar={start:'Program',cst:{Program:{r:'Statement'},Statement:'return;'}};

2. TerminalNode (Terminal Node)

Matches literal strings, regular expressions, or arrays of strings. Terminal nodes are leaf nodes in the parse tree.

Interface:

interfaceTerminalNode{type?:string;// Type name (default: "Text")t:RegExp|string|''|string[];// Pattern(s) to matchrepeat?:'*'|'+';// Repetition (only for string arrays)sample?:string;// Sample text for generationast?:AstNodeExpression;// AST transformation}// Shorthand: string, RegExp, or empty stringtypeTerminalNodeShorthand=RegExp|string|'';

Syntax:

// String literal'hello'// Regular expression/[a-z]+/// Array of alternatives{t:['true','false']}// With repetition{t:[' ','\t','\n'],repeat:'*'}// Full terminal node{t:/\d+/,type:'Number',sample:'123'}

Examples:

// Simple string terminalValue:'null'// RegExp terminalNumber:/\-?\d+(\.\d+)?/// Alternative stringsBoolean:{t:['true','false']}// Repeating whitespaceWS:{t:[' ','\t','\n'],repeat:'*'}

3. ProductionNode (Production Node)

Matches a sequence of grammar nodes in order. All nodes in the sequence must match for the production to succeed.

Interface:

interfaceProductionNode{p:GrammarNode[];// Sequence of nodes to matchtype?:string;// Type name (default: "Production")children?:Record<number,string>;// Child index to property mappingast?:AstNodeExpression;// AST transformation}// Shorthand: array of grammar nodestypeProductionNodeShorthand=GrammarNode[];

Syntax:

// Shorthand array['{',{r:'Content'},'}']// Full production node{p:['{',{r:'Content'},'}'],type:'Block',children:{1:'content'// Maps index 1 to 'content' property}}

Examples:

// Function call: func()FunctionCall:['func','(',')']// Object with named childrenObject:{p:['{',{r:'Members'},'}'],children:{1:'members'}}

4. UnionNode (Union Node)

Matches one of several alternative patterns. The first matching alternative is selected (ordered choice).

Interface:

interfaceUnionNode{u:GrammarNode[];// Array of alternative nodestype?:string;// Type name (default: "Union")ast?:AstNodeExpression;// AST transformation}

Syntax:

{u:[pattern1,pattern2,pattern3]}

Examples:

// Literal valuesLiteral:{u:['null','true','false',{r:'Number'},{r:'String'}]}// Statement typesStatement:{u:[{r:'IfStatement'},{r:'ReturnStatement'},{r:'ExpressionStatement'}]}

5. ListNode (List Node)

Matches zero or more repetitions of a pattern.

Interface:

interfaceListNode{l:GrammarNode;// Node to repeattype?:string;// Type name (default: "List")ast?:AstNodeExpression;// AST transformation}

Syntax:

{l:pattern}

Examples:

// Zero or more statementsStatements:{l:{r:'Statement'}}// Comma-separated listArguments:{l:{p:[',',{r:'Expression'}],ast:['$','/children/1']// Extract the expression, ignore comma}}

Tree Types

JIT Parser works with four types of tree structures:

1. Grammar Nodes

The grammar definition that describes the parsing rules. These are the node types described above that define how to parse input text.

2. CST (Concrete Syntax Tree)

The parse tree that contains every matched token and maintains the complete structure of the parsed input.

Interface:

interfaceCstNode{ptr:Pattern;// Reference to grammar patternpos:number;// Start position in inputend:number;// End position in inputchildren?:CstNode[];// Child nodes}

Example CST:

// For input: '{"foo": 123}'{ptr:ObjectPattern,pos:0,end:13,children:[{ptr:TextPattern,pos:0,end:1},// '{'{ptr:MembersPattern,pos:1,end:12,// '"foo": 123'children:[...]},{ptr:TextPattern,pos:12,end:13}// '}']}

3. AST (Abstract Syntax Tree)

A simplified tree structure derived from the CST, typically containing only semantically meaningful nodes.

Default AST Interface:

interfaceCanonicalAstNode{type:string;// Node typepos:number;// Start positionend:number;// End positionraw?:string;// Raw matched textchildren?:(CanonicalAstNode|unknown)[];// Child nodes}

Example AST:

// For input: '{"foo": 123}'{type:'Object',pos:0,end:13,children:[{type:'Entry',key:{type:'String',value:'foo'},value:{type:'Number',value:123}}]}

CST to AST Conversion Rules

  1. Default Conversion: Each CST node becomes an AST node withtype,pos,end, andchildren properties.

  2. AST Expressions: Useast property in grammar nodes to customize AST generation:

    • ast: null - Skip this node in AST
    • ast: ['$', '/children/0'] - Use first child's AST
    • ast: {...} - Custom JSON expression for transformation
  3. Children Mapping: Usechildren property to map CST child indices to AST properties:

    {children:{0:'key',// CST child 0 -> AST property 'key'2:'value'// CST child 2 -> AST property 'value'}}
  4. Type Override: Specify customtype property instead of default node type names.

4. Debug Trace Tree

If debug mode is enabled during compilation, the parser captures all grammar node tree paths that were attempted during parsing. This debug trace tree is useful for debugging parser behavior and improving parser performance by understanding which rules were tried and failed.

Interface:

interfaceTraceNode{type:string;// Grammar rule name that was attemptedpos:number;// Start position where rule was triedend?:number;// End position if rule succeededchildren?:TraceNode[];// Nested rule attemptssuccess:boolean;// Whether the rule matched successfully}

The debug trace captures the complete parsing process, including failed attempts, making it invaluable for understanding complex parsing scenarios and optimizing grammar rules.

Grammar Compilation

Grammars are compiled to efficient JavaScript functions that can parse input strings rapidly.

Basic Compilation

import{CodegenGrammar}from'jit-parser';constgrammar={start:'Value',cst:{Value:{r:'Number'},Number:/\d+/}};// Compile to parser functionconstparser=CodegenGrammar.compile(grammar);

Compilation Options

import{CodegenContext}from'jit-parser';constctx=newCodegenContext(true,// positions: Include pos/end in ASTtrue,// astExpressions: Process AST transformationsfalse// debug: Generate debug trace code);constparser=CodegenGrammar.compile(grammar,ctx);

Viewing Compiled Grammar

You can print the grammar structure by converting it to a string:

import{GrammarPrinter}from'jit-parser';constgrammarString=GrammarPrinter.print(grammar);console.log(grammarString);

Example output:

Value (reference)└─ Number (terminal): /\d+/

Complex Grammar Example

constjsonGrammar={start:'Value',cst:{WOpt:{t:[' ','\n','\t','\r'],repeat:'*',ast:null},Value:[{r:'WOpt'},{r:'TValue'},{r:'WOpt'}],TValue:{u:['null',{r:'Boolean'},{r:'Number'},{r:'String'},{r:'Object'},{r:'Array'}]},Boolean:{t:['true','false']},Number:/\-?\d+(\.\d+)?([eE][\+\-]?\d+)?/,String:/"[^"\\]*(?:\\.[^"\\]*)*"/,Object:['{',{r:'Members'},'}'],Members:{u:[{p:[{r:'Entry'},{l:{p:[',',{r:'Entry'}],ast:['$','/children/1']}}],ast:['concat',['push',[[]],['$','/children/0']],['$','/children/1']]},{r:'WOpt'}]},Entry:{p:[{r:'String'},':',{r:'Value'}],children:{0:'key',2:'value'}},Array:['[',{r:'Elements'},']']// ... more rules},ast:{Value:['$','/children/1'],// Extract middle child (TValue)Boolean:['==',['$','/raw'],'true'],// Convert to booleanNumber:['num',['$','/raw']]// Convert to number}};constparser=CodegenGrammar.compile(jsonGrammar);console.log(GrammarPrinter.print(jsonGrammar));

Debug Mode

Debug mode captures a trace of the parsing process, showing which grammar rules were attempted at each position.

Enabling Debug Mode

import{CodegenContext,ParseContext}from'jit-parser';// Enable debug during compilationconstdebugCtx=newCodegenContext(true,true,true);// debug = trueconstparser=CodegenGrammar.compile(grammar,debugCtx);// Create trace collectionconstrootTrace={pos:0,children:[]};constparseCtx=newParseContext('input text',false,[rootTrace]);// Parse with debug traceconstcst=parser(parseCtx,0);// Print debug traceimport{printTraceNode}from'jit-parser';console.log(printTraceNode(rootTrace,'','input text'));

Debug Trace Output

The debug trace shows:

  • Which grammar rules were attempted
  • At what positions in the input
  • Whether each attempt succeeded or failed
  • The hierarchical structure of rule attempts

Example trace output:

Root└─ Value 0:22 → ' {"foo": ["bar", 123]}'   ├─ WOpt 0:1 → " "   ├─ TValue 1:22 → '{"foo": ["bar", 123]}'   │  ├─ Null   │  ├─ Boolean     │  ├─ String   │  └─ Object 1:22 → '{"foo": ["bar", 123]}'   │     ├─ Text 1:2 → "{"   │     ├─ Members 2:21 → '"foo": ["bar", 123]'   │     │  └─ Production 2:21 → '"foo": ["bar", 123]'   │     │     ├─ Entry 2:21 → '"foo": ["bar", 123]'   │     │     │  ├─ String 2:7 → '"foo"'   │     │     │  ├─ Text 7:8 → ":"   │     │     │  └─ Value 8:21 → ' ["bar", 123]'    │     │     │     └─ ...   │     │     └─ List 21:21 → ""   │     └─ Text 21:22 → "}"   └─ WOpt 22:22 → ""

Examples

1. Simple Expression Parser

constexprGrammar={start:'Expression',cst:{Expression:{r:'Number'},Number:{t:/\d+/,type:'Number'}}};constparser=CodegenGrammar.compile(exprGrammar);constctx=newParseContext('42',true);constcst=parser(ctx,0);constast=cst.ptr.toAst(cst,'42');console.log(ast);// {type: 'Number', pos: 0, end: 2, raw: '42'}

2. JSON Parser

import{grammarasjsonGrammar}from'jit-parser/lib/grammars/json';constparser=CodegenGrammar.compile(jsonGrammar);constjson='{"name": "John", "age": 30}';constctx=newParseContext(json,true);constcst=parser(ctx,0);constast=cst.ptr.toAst(cst,json);console.log(ast);

3. Custom AST Transformation

constgrammar={start:'KeyValue',cst:{KeyValue:{p:[{r:'Key'},'=',{r:'Value'}],children:{0:'key',2:'value'},type:'Assignment'},Key:/[a-zA-Z]+/,Value:/\d+/},ast:{KeyValue:{type:'Assignment',key:['$','/children/0/raw'],value:['num',['$','/children/2/raw']]}}};

4. List Parsing

constlistGrammar={start:'List',cst:{List:['[',{r:'Items'},']'],Items:{u:[{p:[{r:'Item'},{l:{p:[',',{r:'Item'}],ast:['$','/children/1']}}],ast:['concat',['push',[[]],['$','/children/0']],['$','/children/1']]},''// Empty list]},Item:/\w+/}};

API Reference

Core Classes

CodegenGrammar

  • static compile(grammar: Grammar, ctx?: CodegenContext): Parser
  • compileRule(ruleName: string): Pattern

ParseContext

  • constructor(str: string, ast: boolean, trace?: RootTraceNode[])

CodegenContext

  • constructor(positions: boolean, astExpressions: boolean, debug: boolean)

GrammarPrinter

  • static print(grammar: Grammar, tab?: string): string

Utility Functions

printCst(cst: CstNode, tab: string, src: string): string

Print a formatted CST tree

printTraceNode(trace: RootTraceNode | ParseTraceNode, tab: string, src: string): string

Print a formatted debug trace

Type Definitions

See theGrammar Node Types section for complete interface definitions.


This parser generator provides a powerful and efficient way to build custom parsers with minimal code while maintaining high performance through JIT compilation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp