Skip to content
/ drivel Public

typescript parser generator using chevrotain

Notifications You must be signed in to change notification settings

shantlr/drivel

Repository files navigation

Drivel

WIP

Typescript Parser generator based on chevrotain

Installation

npm install -g drivel

Usage

drivel gen ./grammar ./src/parser

Grammar synthax

Grammar can be defined using a yaml like synthax

Rules

The rules sections define the differents rules to follow during parsing. Currently as drivel is using generating a parser using chevrotain, the rules are expected to be left recursive.

You can directly inline string literal token A start rule is required as a parsing starting point

rules:
  start: "hello" "world"

Modifier suffix

The following suffix can be added after a token:

  • ?: Pattern can either be matched 0 or 1 time
  • *: Pattern can be matched 0 or more times
  • +: Pattern can be matched 1 or more times
rules:
  start: "hello"? "world"*

Parenthesis

You can define a sub sequence by wrapping mulitple instructions inside parenthesis

rules:
  start: ("hello" "world")+

Tokens

You can define tokens that follow custom pattern using either regex a string literals

tokens:
  singleQuoteString:
    pattern: /'(?:[^'\\]|\\.)*'/
  doubleQuoteString:
    pattern: /"(?:[^"\\]|\\.)*"/
  trueLiteral:
    pattern: true
  falseLiteral:
    pattern: false
  integer:
    pattern: /[\d]+/

Tokens are then usable inside rules

rules:
  start: value
  value: singleQuoteString | doubleQuoteString | trueLiteral | falseLiteral | integer

Output

Default rule output

Named element

rules:
  start: value:"hello" "world"

Grammar examples

calculator

Releases

No releases published

Packages

No packages published