New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

my-language

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

my-language

Create your own language or parser!

latest
Source
npmnpm
Version
1.0.3
Version published
Maintainers
1
Created
Source

Build Status

Create your own language!

TypeScript ready!

This is smth like ANTLR...

Install

npm i my-language

How to import?

TypeScript

import { fragment, group, or, any, and, maybe, parser, processor, ProcessorHandler } from "my-language/src";

JavaScript

const { fragment, group, or, any, and, maybe, parser, processor } = require("my-language");

or

import { fragment, group, or, any, and, maybe, parser, processor } from "my-language";

Example: calculator implementation

If you wanna use JS, just remove types from this example.

Gramma:

// Skippable fragments

const $skip = fragment(/[\s\t\n]+/s);

// Common fragments

const $number = fragment(/\d+(\.\d+)?/);
const $plus = fragment(/\+/);
const $minus = fragment(/\-/);
const $times = fragment(/\*/);
const $divide = fragment(/\//);
const $power = fragment(/\*\*/);
const $open = fragment(/\(/);
const $close = fragment(/\)/);

// Logical groups

const atom = group(or($number, and($open, () => expression, $close)));
const atomSigned = group(maybe($minus), atom);
const expressionPower = group(atomSigned, any($power, atomSigned));
const expressionTimes = group(expressionPower, any(or($times, $divide), expressionPower));
const expression = group(expressionTimes, any(or($plus, $minus), expressionTimes));

Parser:

const calculatorParser = parser($skip, expression);

Processor handler:

const calculator: ProcessorHandler<number> = (current) => {
    switch (current.type) {
        case atom: return current.has($number) ? Number(current.value) : calculator(current.get(expression));
        case atomSigned: return calculator(current.get(atom)) * (current.has($minus) ? -1 : 1);
    }

    const args = current.getChildren().map(calculator);
    const ops = current.getTokens().map((token) => token.definition);

    return args.reduce((a, b, i) => {
        switch (current.type) {
            case expression: return ops[i] === $plus ? a + b : a - b;
            case expressionTimes: return ops[i] === $times ? a * b : a / b;
            case expressionPower: return a ** b;
            default: return 0;
        }
    }, args.shift() || 0);
};

Create processor:

const calculatorProcessor = processor(calculator, calculatorParser);

Test smaple:

console.log(calculatorProcessor.process("10 * ((12 / 2 ** 2) * 5 + 6) - -10"));

Results:

[ 220 ]

Have a good work!

Keywords

parser

FAQs

Package last updated on 28 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts