Skip to content

Anillc/iberis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Another parser generator

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages