-
Notifications
You must be signed in to change notification settings - Fork 5
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
Refactoring of the string path syntax parser #115
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
aaa477e
recursive toPath
hgwood e003363
:hammer: smaller functions
hgwood a710ba7
:hammer: simpler parseQuotedBracketNotation
hgwood bf2983c
:hammer: parseBareBracketNotation using regexp
hgwood 305867a
:ok_hand: :hammer: use functions more aligned with intent
hgwood aec005f
:bulb: :art: jsdoc and split long lines
hgwood bac0c2a
:hammer: moar regexes?
hgwood e00cd8e
:hammer: only regexes! :smiling_imp:
hgwood 03cefdc
:hammer: better lisibility (?) using a helper match function
hgwood 6e40a70
:sparkles: path syntax: leading dot now ignored
hgwood 1108250
:hammer: renamed vars to avoid shadowing
hgwood 22d17d0
:hammer: path syntax parser: match function spreads results into down…
hgwood 4293b87
:rotating_light: fix lint
nlepage d524544
:bulb: fix jsdoc
nlepage b9d42fc
:ok_hand: :hammer: explicit regexp parser creation
hgwood e46f9b0
:ok_hand: :hammer: parser combinators
hgwood 7096afc
:hammer: adapting to new code organization
hgwood 5c15985
:rewind: reverting merge mistakes
hgwood 5c5f7d0
:bulb: fix jsdoc
nlepage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @typedef {function(string): T | null} Parser<T> | ||
*/ | ||
|
||
const maybeMap = (maybe, fn) => maybe === null ? maybe : fn(maybe) | ||
|
||
/** | ||
* Creates a parser from a regular expression by matching the input string with | ||
* the regular expression, returning the resulting match object. | ||
* @function | ||
* @param {RegExp} regexp the regular expression | ||
* @return {Parser<string[]>} the resulting parser | ||
*/ | ||
export const regexp = regexp => str => maybeMap(str.match(regexp), match => match.slice(1)) | ||
|
||
/** | ||
* Returns a new parser that will return <code>null</code> if a predicate about | ||
* the result of another parser does not hold. If the predicate holds then | ||
* the new parser returns the result of the other parser unchanged. | ||
* @function | ||
* @param {Parser<T>} parser parser to filter | ||
* @param {function(*): boolean} predicate predicate to use | ||
* @return {Parser<T>} resulting parser | ||
*/ | ||
export const filter = (parser, predicate) => str => maybeMap(parser(str), parsed => predicate(parsed) ? parsed : null) | ||
|
||
/** | ||
* Returns a new parser which will post-process the result of another parser. | ||
* @function | ||
* @param {Parser<T>} parser parser for which to process the result | ||
* @param {function(T): R} mapper function to transform the result of the parser | ||
* @return {Parser<R>} resulting parser | ||
*/ | ||
export const map = (parser, mapper) => str => maybeMap(parser(str), mapper) | ||
|
||
/** | ||
* Returns a new parser that attempts parsing with a first parser then falls | ||
* back to a second parser if the first returns <code>null</code>. | ||
* @function | ||
* @param {Parser<A>} parser the first parser | ||
* @param {Parser<B>} other the second parser | ||
* @return {Parser<A | B>} resulting parser | ||
*/ | ||
export const fallback = (parser, other) => str => { | ||
const parsed = parser(str) | ||
if (parsed !== null) return parsed | ||
return other(str) | ||
} | ||
|
||
/** | ||
* Chains a list of parsers together using <code>fallback</code>. | ||
* @function | ||
* @param {Array<Parser<*>>} parsers a list of parsers to try in order | ||
* @return {Parser<*>} resulting parser | ||
*/ | ||
export const race = parsers => parsers.reduce((chainedParser, parser) => fallback(chainedParser, parser)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could test each parser so it will be easier to work on them individually later ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea but could we do this in a later PR ? The current tests cover all of the code so I think it's OK to merge this as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @nlepage. An issue could be filed to make sure it's not forgotten.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened an issue about this topic.