0.0.3 • Published 1 year ago

iberis v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

iberis

import { Grammar, accept, s } from 'iberis'

const g = new Grammar<string | RegExp>('sum')
const t = s.template(g)
t`sum     -> sum /[+-]/ product`    .bind((x, op, y) => op.text === '+' ? x + y : x - y)
t`sum     -> product`
t`product -> product /[*\/]/ factor`.bind((x, op, y) => op.text === '*' ? x * y : x / y)
t`product -> factor`
t`factor  -> '(' sum ')'`           .bind((_, sum) => sum)
t`factor  -> /\d+(?:\.\d+)?/`       .bind((num) => +num.text)
t`factor  -> /"(?:[^"\\]|\\.)*"/`   .bind((str) => str.text.substring(1, str.text.length - 1).replaceAll('\\\\', '\\').replaceAll('\\"', '"'))

const input = '233 * (114 + 514) / 1919.810 + "www"'
const root = g.parse(s.lexer(input), s.equals)
console.log(accept(root[0]))
// output: 76.2179590688662www
0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago