Giter Site home page Giter Site logo

Nongreedy rules about ts-parsec HOT 3 CLOSED

microsoft avatar microsoft commented on May 22, 2024
Nongreedy rules

from ts-parsec.

Comments (3)

ZihanChen-MSFT avatar ZihanChen-MSFT commented on May 22, 2024

I think it works like rep which gives you all results and rep_sc gives you the "most greedy" result. Do you have a more specific requirement?

from ts-parsec.

peterbud avatar peterbud commented on May 22, 2024

Consider the following input:

Dear customer, here is the statement of your account as of 01.01.2023. more text to discard...

Account statement EUR

The following transaction were recorded... even more text to discard

01.12.2022 Transfer 100
03.12.2022 Transfer 300

Sincerely, more text to discard

The first requirement to be able to consume any token in a non-greedy way, like at the beginning of the input.
I have not seen any function which would match any token, I have created a function which does that - I have called as any - maybe there are better way to do that?

Then I have tried the following lexar/parser:

export enum TokenKind {
  DMY,
  Number,
  Header,
  Other,
  Space,
}

export const lexer = buildLexer([
  [true, /^\d{2}\.\d{2}\.\d{4}/g, TokenKind.DMY],
  [true, /^(\d*\.?)*(,\d*)?( *-)?/g, TokenKind.Number],
  [true, /^account statement/gi, TokenKind.Header],

  [true, /^\S+/g, TokenKind.Other], // all other words
  [false, /^\s+/g, TokenKind.Space], // ignore whitespace
])

const STATEMENT = rule<TokenKind, unknown>()
STATEMENT.setPattern(
  seq(
    rep(any()),
    tok(TokenKind.Header),
    rep(any()),
    rep(seq(tok(TokenKind.DMY), str("Transfer"), tok(TokenKind.Number))),
  ),
)

The problem is that rep() is greedy in a sense that it will match all tokens, although what I'm looking for is to match until it finds the first Header token. Then again I'd need to consume all tokens until I find the first date token which marks the beginning of the transactions.

What would be your suggestion for this problem?

from ts-parsec.

ZihanChen-MSFT avatar ZihanChen-MSFT commented on May 22, 2024

I feel like this is overusing, that's why there is no any. I would just split the text into lines first and match each line and keep all succeeded ones. And I think regular expression is simpler for this particular input.

from ts-parsec.

Related Issues (11)

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.