Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/new math example and more #80

Merged

Conversation

barona-mika-vilpas
Copy link
Collaborator

This PR adds the following features:

new math example

The previous math example was removed, see #58 . The new example essentially does the same thing but the implementation is different.
I had a lot of trouble writing this, but I think the implementation is now pretty good. It's also tested somewhat well.

fix maybe combinator return type

The previous type indicated a return type must be present, but in the implementation it's actually possible to not parse anything new and return the result of the previous parser. This is now reflected in the type.
This is a breaking change, but should not matter because the next release was going to have breaking changes anyway.

add many1 combinator

This is like many but it makes sure at least one match was made by the parser. The many combinator allows 0 matches.

add parjser.debug() method

This is a development time feature. You can now temporarily add the .debug() method to any parser like this:

string("a").expects("an 'a' character").debug();

When the parser has parsed something, the result is logged to the console. This allowed me to find errors in my math example and it was very useful.
Here is an example of the message:

consumed 'a' (length 1)
at position 0->1
👍🏻 (Ok) // note: these use the SHF system (😕😬💀, soft-hard-fatal)
{
    "input": "a",
    "userState": {},
    "position": 1,
    "stack": [],
    "value": "a",
    "kind": "OK"
}
{
    "expecting": "an 'a' character",
    "type": "string"
}

add constant type inference to many parsers

In typescript, the value "a" can be seen as having both types string and "a". By default the type checker infers the broader type string, but may be instructed to infer the narrower "constant" type "a" by using const str = "a" as const.

This can also be done when using generic types in functions with function foo<const T>(value: T) - in this case the generic type is inferred to the more narrow "a".

This is very useful when constructing parsers, I think. Now we can have this:

// it's very clear what the return type will be
const myparser: Parjser<["a", "b"]> = string("a").pipe(then(string("b")));
// it used to be inferred to Parjser<[string, string]>

This is also useful when working with type unions. Because of this extra type safety, you can avoid doing additional type casts altogether.
This should also be backward compatible.

example:

          expecting: "expecting one of: 'expecting '+'' (string), 'expecting '-'' (string)"
It was previously guaranteed to be non null/undefined, but it was a mistake
@coveralls
Copy link

Pull Request Test Coverage Report for Build 7142707813

  • 102 of 107 (95.33%) changed or added relevant lines in 6 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 90.333%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/lib/internal/combinators/many1.ts 29 30 96.67%
src/examples/math.ts 46 50 92.0%
Totals Coverage Status
Change from base Build 7097209228: 0.3%
Covered Lines: 1155
Relevant Lines: 1276

💛 - Coveralls

@barona-mika-vilpas barona-mika-vilpas merged commit ed00670 into GregRos:master Dec 8, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants