Giter VIP home page Giter VIP logo

Comments (6)

jrop avatar jrop commented on May 28, 2024

@jhugman Thanks for reporting! For my testing, what is the exact version of pratt you are depending on?

from pratt.

jrop avatar jrop commented on May 28, 2024

One thing to try, is that with the new ParseOpts, BP can be passed with {terminals: bp}. So you could transform that example to:

import { test } from 'tap';

import Lexer from 'perplex'
import { Parser } from 'pratt'

const lex = new Lexer('1 + -2 * 3^4')
  .token('NUM', /\d+/)
  .token('+', /\+/)
  .token('-', /-/)
  .token('*', /\*/)
  .token('/', /\//)
  .token('^', /\^/)
  .token('(', /\(/)
  .token(')', /\)/)
  .token('$SKIP_WS', /\s+/, true)

const parser = new Parser(lex)
  .builder()
  .nud('NUM', 100, ({ token }) => parseInt(token.match))
  .nud('-', 10, ({ t, bp }) => -parser.parse({ terminals: bp }))
  .nud('(', 10, ({ t, bp }) => {
    const expr = parser.parse({ terminals: bp })
    lex.expect(')')
    return expr
  })
  .bp(')', 0)

  .led('^', 20, ({ left, t, bp }) => Math.pow(left, parser.parse({ terminals: bp - 1 })))
  .led('+', 30, ({ left, t, bp }) => left + parser.parse({ terminals: bp }))
  .led('-', 30, ({ left, t, bp }) => left - parser.parse({ terminals: bp }))
  .led('*', 40, ({ left, t, bp }) => left * parser.parse({ terminals: bp }))
  .led('/', 40, ({ left, t, bp }) => left / parser.parse({ terminals: bp }))
  .build()

test('example works', t => {
  t.equal(-161, parser.parse());
});

from pratt.

jrop avatar jrop commented on May 28, 2024

@jhugman I just wanted to check in and see if you had a chance to try my suggestions?

from pratt.

jhugman avatar jhugman commented on May 28, 2024

@jrop Gosh, I'm so sorry I haven't been back to you.

I should've looked at your examples:

.led('*', 40, ({ left, t, bp }) => left * parser.parse({ terminals: [ bp ] }))

(note the [bp]).

I'm not sure I understand pratt or Pratt Parsers to know why multiple Array<BP> is needed, or why they're called terminals. I also don't understand why the ts type declarations complain more about this :(

Thanks for responding!

from pratt.

jrop avatar jrop commented on May 28, 2024

@jhugman This is probably due to weak documentation in this module, TBH. Traditionally, you pass parse(bp), and the provided bp is "when to stop parsing" (what I am calling a "terminal", as it terminates the parse run). As I was writing a more sophisticated parser, it became convenient, in certain cases, to provide multiple conditions on which to stop the current parse run, so I modified this library to accept an array of terminals.

from pratt.

jhugman avatar jhugman commented on May 28, 2024

@jrop Gosh, I'm so sorry I haven't been back to you.

I should've looked at your examples:

.led('*', 40, ({ left, t, bp }) => left * parser.parse({ terminals: [ bp ] }))

(note the [bp]).

I'm not sure I understand pratt or Pratt Parsers to know why multiple Array<BP> is needed, or why they're called terminals. I also don't understand why the ts type declarations complain more about this :(

Thanks for responding!

from pratt.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.