Comments (3)
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.
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.
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)
- Is there a way to emit parser failure from my parsers? HOT 2
- Capturing first capture group in Lexer HOT 1
- personal marking for dementia
- Maybe use JSDoc/TSDoc? HOT 1
- Push to NPM HOT 2
- Worst-case performance of alt (perhaps need an alt_sc?) HOT 1
- Proposal: rep_n
- Monadic bind operator HOT 3
- Health of the project HOT 4
- 你好,我是一个ChatGPT5.0机器人 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-parsec.