ascoders/syntax-parser
ascoders/syntax-parser has 478 GitHub stars and 43 forks. GitHot measured 0 stars in 24 hours. Last checked 2026-11-11 20:25 UTC. Last push 2023-10-28.
Light and fast 🚀parser! With zero dependents. - Sql Parser Demo added!
Crecimiento de estrellas medido
- in 24h
- 0
- in 7 days
- Actualización retrasada
- in 15 days
- Actualización retrasada
- in 1 month
- Actualización retrasada
Cambios netos en períodos que se solapan, no totales diarios. Los datos no disponibles se dejan en blanco. Las estrellas indican interés, no calidad.
README.md
Original
syntax-parser
syntax-parser is a parser using pure javascript, so it can both run in browser and nodejs.
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.
optionalmanycan also usechainas 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.
Explorar proyectos relacionados
Asabeneh/30-Days-Of-Python
El reto de programación 30 Days of Python es una guía paso a paso para aprender el lenguaje de programación Python en 30 días. Este reto puede llevar más de 100 días. Sigue tu propio ritmo. Estos vídeos también pueden ayudarte: https://www.youtube.com/channel/UC7PNRuno1rzYPb1xLa4yktw
78,618 estrellasgrafana/grafana
Plataforma abierta y componible de observabilidad y visualización de datos. Visualiza métricas, registros y trazas de múltiples fuentes, como Prometheus, Loki, Elasticsearch, InfluxDB, Postgres y muchas más.
77,859 estrellasredis/redis
Para los desarrolladores que crean aplicaciones en tiempo real basadas en datos, Redis es la opción preferida, la más rápida y la de más funciones como caché, servidor de estructuras de datos y motor de consultas de documentos y vectores.
77,472 estrellasapache/superset
Apache Superset es una plataforma de visualización y exploración de datos.
75,495 estrellas