All Projects β†’ ascoders β†’ Syntax Parser

ascoders / Syntax Parser

Light and fast πŸš€parser! With zero dependents. - Sql Parser Demo added!

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Syntax Parser

Fslexyacc
Lexer and parser generators for F#
Stars: ✭ 148 (-53.31%)
Mutual labels:  lexer, parser
Monkey Rust
An interpreter for the Monkey programming language written in Rust
Stars: ✭ 174 (-45.11%)
Mutual labels:  lexer, parser
Gelatin
Transform text files to XML, JSON, or YAML
Stars: ✭ 150 (-52.68%)
Mutual labels:  lexer, parser
Plyara
Parse YARA rules and operate over them more easily.
Stars: ✭ 108 (-65.93%)
Mutual labels:  lexer, parser
Cub
The Cub Programming Language
Stars: ✭ 198 (-37.54%)
Mutual labels:  lexer, parser
Csly
a C# embeddable lexer and parser generator (.Net core)
Stars: ✭ 129 (-59.31%)
Mutual labels:  lexer, parser
Lioness
The Lioness Programming Language
Stars: ✭ 155 (-51.1%)
Mutual labels:  lexer, parser
Charly Vm
Fibers, Closures, C-Module System | NaN-boxing, bytecode-VM written in C++
Stars: ✭ 66 (-79.18%)
Mutual labels:  lexer, parser
Diagon
Interactive ASCII art diagram generators. 🌟
Stars: ✭ 189 (-40.38%)
Mutual labels:  lexer, parser
Snapdragon
snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory.
Stars: ✭ 180 (-43.22%)
Mutual labels:  lexer, parser
Nodable
a node-able bidirectionnal expression editor.
Stars: ✭ 103 (-67.51%)
Mutual labels:  lexer, parser
Sql Parser
A validating SQL lexer and parser with a focus on MySQL dialect.
Stars: ✭ 284 (-10.41%)
Mutual labels:  sql, parser
Graphql Go Tools
Tools to write high performance GraphQL applications using Go/Golang.
Stars: ✭ 96 (-69.72%)
Mutual labels:  lexer, parser
Works For Me
Collection of developer toolkits
Stars: ✭ 131 (-58.68%)
Mutual labels:  lexer, parser
Rs Monkey Lang
Monkey Programming Language written in Rust.
Stars: ✭ 80 (-74.76%)
Mutual labels:  lexer, parser
Grmtools
Rust grammar tool libraries and binaries
Stars: ✭ 153 (-51.74%)
Mutual labels:  lexer, parser
Logos
Create ridiculously fast Lexers
Stars: ✭ 1,001 (+215.77%)
Mutual labels:  lexer, parser
Csstree
A tool set for CSS including fast detailed parser, walker, generator and lexer based on W3C specs and browser implementations
Stars: ✭ 1,121 (+253.63%)
Mutual labels:  lexer, parser
Participle
A parser library for Go
Stars: ✭ 2,302 (+626.18%)
Mutual labels:  lexer, parser
Jsqlparser
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes. The generated hierarchy can be navigated using the Visitor Pattern
Stars: ✭ 3,405 (+974.13%)
Mutual labels:  sql, parser

syntax-parser

syntax-parser is a parser using pure javascript, so it can both run in browser and nodejs.

CircleCI Status NPM Version Code Coverage

syntax-parser supports:

  • lexer.
  • parser.

Lexer

createLexer can help you create a lexer.

Example

import { createLexer } from 'syntax-parser';

const myLexer = createLexer([
  {
    type: 'whitespace',
    regexes: [/^(\s+)/],
    ignore: true
  },
  {
    type: 'word',
    regexes: [/^([a-zA-Z0-9]+)/]
  },
  {
    type: 'operator',
    regexes: [/^(\+)/]
  }
]);

myLexer('a + b');
// [
//   { "type": "word", "value": "a", "position": [0, 1] },
//   { "type": "operator", "value": "+", "position": [2, 3] },
//   { "type": "word", "value": "b", "position": [4, 5] }
// ]

type

Token type name, you can use any value here, and you will use it in the parser stage.

regexes

Regexes that use to be matched for each Token type.

ignore

The matching Token will not be added to the Token result queue.

In general, whitespace can be ignored in syntax parsing.

Parser

createParser can help you create a parser. Parser requires a lexer.

import { createParser, chain, matchTokenType, many } from 'syntax-parser';

const root = () => chain(addExpr)(ast => ast[0]);

const addExpr = () =>
  chain(matchTokenType('word'), many(addPlus))(ast => ({
    left: ast[0].value,
    operator: ast[1] && ast[1][0].operator,
    right: ast[1] && ast[1][0].term
  }));

const addPlus = () =>
  chain('+', root)(ast => ({
    operator: ast[0].value,
    term: ast[1]
  }));

const myParser = createParser(
  root, // Root grammar.
  myLexer // Created in lexer example.
);

myParser('a + b');
// ast:
// [{
//   "left": "a",
//   "operator": "+",
//   "right": {
//     "left": "b",
//     "operator": null,
//     "right": null
//   }
// }]

chain

Basic grammatical element, support four parameters:

string

String means match token:

chain('select', 'table'); // Match 'select table'

array

Array means 'or':

chain('select', ['table', 'chart']); // Match both 'select table' and 'select chart'

matchTokenType

matchTokenType allow you match Token type defined in lexer.

chain('select', matchTokenType('word')); // Match 'select [any word!]'

function

It's easy to call another chain function:

const a = () => chain('select', b);
const b = () => chain('table');

many/optional

Just as literal meaning:

const a = () => chain('select', optional('table')); // Match both 'select' and 'select table'
const b = () => chain('select', many(',', matchTokenType('word'))); // Match both 'select' and 'select a' and 'select a, b' .. and so on.

optional many can also use chain as parameter. many(chain(..))

The last callback allow partial redefin of local ast:

chain('select', 'table')(
  ast => ast[0] // return 'select'
);

Tests

npm test

Monaco Editor Sql Editor

If you want to see this demo, run this command:

npm run docs

Then select demo Monaco Editor.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].