{"action":"created","comment":{"url":"https://api.github.com/repos/pest-parser/pest/pulls/comments/238075751","pull_request_review_id":180537272,"id":238075751,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIzODA3NTc1MQ==","diff_hunk":"@@ -79,137 +91,322 @@ impl<R: RuleType> BitOr for Operator<R> {\n     }\n }\n \n-/// List of operators and precedences, which can perform [precedence climbing][1] on infix\n-/// expressions contained in a [`Pairs`]. The token pairs contained in the `Pairs` should start\n-/// with a *primary* pair and then alternate between an *operator* and a *primary*.\n+/// Struct containing operators and precedences, which can perform [precedence climbing][1] on\n+/// primary, prefix, suffix and infix expressions over [`Pairs`]. The tokens in [`Pairs`]\n+/// should alternate in the sequence:\n+/// `prefix* ~ primary ~ suffix* ~ (infix ~ prefix* ~ primary ~ suffix*)*`\n+///\n+/// # Example\n+///\n+/// The following pest grammar defines a calculator which is valid for precedence climbing:\n+///\n+/// ```pest\n+/// WHITESPACE   =  _{ \" \" | \"\\t\" | NEWLINE }\n+/// \n+/// program      =   { SOI ~ expr ~ EOI }\n+///   expr       =   { prefix* ~ primary ~ suffix* ~ (infix ~ prefix* ~ primary ~ suffix)* }\n+///     infix    =  _{ add | sub | mul | div | pow }\n+///       add    =   { \"+\" } # Addition\n+///       sub    =   { \"-\" } # Subtraction\n+///       mul    =   { \"*\" } # Multiplication\n+///       div    =   { \"/\" } # Division\n+///       pow    =   { \"^\" } # Exponentiation\n+///     prefix   =  _{ neg }\n+///       neg    =   { \"-\" } # Negation\n+///     suffix   =  _{ fac }\n+///       fac    =   { \"!\" } # Factorial\n+///     primary  =  _{ int | \"(\" ~ expr ~ \")\" }\n+///       int    =  @{ ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* }\n+/// ```\n+///\n+/// Below is a [`PrecClimber`] that is able to climb an `expr` in the above grammar. The order\n+/// of precedence corresponds to the order in which [`op`] is called. Thus, `mul` will\n+/// have higher precedence than `add`. Operators can also be chained with `|` to give them equal\n+/// precedence. In the current version, prefix operators precede suffix, and suffix","path":"pest/src/prec_climber.rs","position":null,"original_position":150,"commit_id":"b67321e03a6b02974e0f9093d61d34f8896ca33c","original_commit_id":"ba220d296c3cdf500e821ea7fc560e4e19a40c88","user":{"login":"segeljakt","id":15143039,"node_id":"MDQ6VXNlcjE1MTQzMDM5","avatar_url":"https://avatars3.githubusercontent.com/u/15143039?v=4","gravatar_id":"","url":"https://api.github.com/users/segeljakt","html_url":"https://github.com/segeljakt","followers_url":"https://api.github.com/users/segeljakt/followers","following_url":"https://api.github.com/users/segeljakt/following{/other_user}","gists_url":"https://api.github.com/users/segeljakt/gists{/gist_id}","starred_url":"https://api.github.com/users/segeljakt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/segeljakt/subscriptions","organizations_url":"https://api.github.com/users/segeljakt/orgs","repos_url":"https://api.github.com/users/segeljakt/repos","events_url":"https://api.github.com/users/segeljakt/events{/privacy}","received_events_url":"https://api.github.com/users/segeljakt/received_events","type":"User","site_admin":false},"body":"The algorithm should now be able to deal with prefix/postfix operator precedence","created_at":"2018-12-01T20:31:01Z","updated_at":"2018-12-01T20:31:01Z","html_url":"https://github.com/pest-parser/pest/pull/344#discussion_r238075751","pull_request_url":"https://api.github.com/repos/pest-parser/pest/pulls/344","author_association":"NONE","_links":{"self":{"href":"https://api.github.com/repos/pest-parser/pest/pulls/comments/238075751"},"html":{"href":"https://github.com/pest-parser/pest/pull/344#discussion_r238075751"},"pull_request":{"href":"https://api.github.com/repos/pest-parser/pest/pulls/344"}},"in_reply_to_id":236074120},"pull_request":{"url":"https://api.github.com/repos/pest-parser/pest/pulls/344","id":233280356,"node_id":"MDExOlB1bGxSZXF1ZXN0MjMzMjgwMzU2","html_url":"https://github.com/pest-parser/pest/pull/344","diff_url":"https://github.com/pest-parser/pest/pull/344.diff","patch_url":"https://github.com/pest-parser/pest/pull/344.patch","issue_url":"https://api.github.com/repos/pest-parser/pest/issues/344","number":344,"state":"open","locked":false,"title":"Unary prefix/suffix operator support for PrecClimber","user":{"login":"segeljakt","id":15143039,"node_id":"MDQ6VXNlcjE1MTQzMDM5","avatar_url":"https://avatars3.githubusercontent.com/u/15143039?v=4","gravatar_id":"","url":"https://api.github.com/users/segeljakt","html_url":"https://github.com/segeljakt","followers_url":"https://api.github.com/users/segeljakt/followers","following_url":"https://api.github.com/users/segeljakt/following{/other_user}","gists_url":"https://api.github.com/users/segeljakt/gists{/gist_id}","starred_url":"https://api.github.com/users/segeljakt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/segeljakt/subscriptions","organizations_url":"https://api.github.com/users/segeljakt/orgs","repos_url":"https://api.github.com/users/segeljakt/repos","events_url":"https://api.github.com/users/segeljakt/events{/privacy}","received_events_url":"https://api.github.com/users/segeljakt/received_events","type":"User","site_admin":false},"body":"Adds a new `PrattParser`, which works similar to `PrecClimber` but extends it with unary (prefix and suffix) operator handling.\r\n\r\nTo try it out, checkout this PR and run `cargo run --example=calc` in the terminal.\r\n\r\nGiven a grammar:\r\n\r\n```pest\r\nWHITESPACE   =  _{ \" \" | \"\\t\" | NEWLINE }\r\n\r\nprogram      =   { SOI ~ expr ~ EOI }\r\n  expr       =   { prefix* ~ primary ~ postfix* ~ (infix ~ prefix* ~ primary ~ postfix* )* }\r\n    infix    =  _{ add | sub | mul | div | pow }\r\n      add    =   { \"+\" } // Addition\r\n      sub    =   { \"-\" } // Subtraction\r\n      mul    =   { \"*\" } // Multiplication\r\n      div    =   { \"/\" } // Division\r\n      pow    =   { \"^\" } // Exponentiation\r\n    prefix   =  _{ neg }\r\n      neg    =   { \"-\" } // Negation\r\n    postfix  =  _{ fac }\r\n      fac    =   { \"!\" } // Factorial\r\n    primary  =  _{ int | \"(\" ~ expr ~ \")\" }\r\n      int    =  @{ (ASCII_NONZERO_DIGIT ~ ASCII_DIGIT+ | ASCII_DIGIT) }\r\n```\r\n\r\nYou can now define a `PrattParser`:\r\n\r\n```rust\r\nlet pratt = PrattParser::new()\r\n      .op(Op::infix(Rule::add, Left) | Op::infix(Rule::sub, Left))\r\n      .op(Op::infix(Rule::mul, Left) | Op::infix(Rule::div, Left))\r\n      .op(Op::infix(Rule::pow, Right))\r\n      .op(Op::postfix(Rule::fac))\r\n      .op(Op::prefix(Rule::neg));\r\n```\r\n\r\nAnd use it like:\r\n\r\n```rust\r\nfn parse_to_i32(pairs: Pairs<Rule>, pratt: &PrattParser<Rule>) -> i128 {\r\n  pratt\r\n      .map_primary(|primary| match primary.as_rule() {\r\n          Rule::int  => primary.as_str().parse().unwrap(),\r\n          Rule::expr => parse_to_i32(primary.into_inner(), pratt),\r\n          _          => unreachable!(),\r\n      })\r\n      .map_prefix(|op, rhs| match op.as_rule() {\r\n          Rule::neg  => -rhs,\r\n          _          => unreachable!(),\r\n      })\r\n      .map_postfix(|lhs, op| match op.as_rule() {\r\n          Rule::fac  => (1..lhs+1).product(),\r\n          _          => unreachable!(),\r\n      })\r\n      .map_infix(|lhs, op, rhs| match op.as_rule() {\r\n          Rule::add  => lhs + rhs,\r\n          Rule::sub  => lhs - rhs,\r\n          Rule::mul  => lhs * rhs,\r\n          Rule::div  => lhs / rhs,\r\n          Rule::pow  => (1..rhs+1).map(|_| lhs).product(),\r\n          _          => unreachable!(),\r\n      })\r\n      .parse(pairs)\r\n}\r\n```\r\n\r\nSome things to consider:\r\n* Does not support ternary operators.\r\n* Precedence climbing like before uses recursion, though I'm not sure how deep you need to go for this to become a problem.\r\n* The current naming (`prefix`/`suffix`/`infix`) could be misinterpreted as binary operators with `prefix`/`suffix`/`infix` notation. Does it work, or should I name things more explicitly, e.g., `unary_prefix`/`unary_suffix`/`binary_infix`?","created_at":"2018-11-23T21:16:11Z","updated_at":"2018-12-01T20:31:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0265a8891f480d4811bdf847a7f40aa5f21b5411","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/pest-parser/pest/pulls/344/commits","review_comments_url":"https://api.github.com/repos/pest-parser/pest/pulls/344/comments","review_comment_url":"https://api.github.com/repos/pest-parser/pest/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pest-parser/pest/issues/344/comments","statuses_url":"https://api.github.com/repos/pest-parser/pest/statuses/b67321e03a6b02974e0f9093d61d34f8896ca33c","head":{"label":"segeljakt:prec-climber-arity","ref":"prec-climber-arity","sha":"b67321e03a6b02974e0f9093d61d34f8896ca33c","user":{"login":"segeljakt","id":15143039,"node_id":"MDQ6VXNlcjE1MTQzMDM5","avatar_url":"https://avatars3.githubusercontent.com/u/15143039?v=4","gravatar_id":"","url":"https://api.github.com/users/segeljakt","html_url":"https://github.com/segeljakt","followers_url":"https://api.github.com/users/segeljakt/followers","following_url":"https://api.github.com/users/segeljakt/following{/other_user}","gists_url":"https://api.github.com/users/segeljakt/gists{/gist_id}","starred_url":"https://api.github.com/users/segeljakt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/segeljakt/subscriptions","organizations_url":"https://api.github.com/users/segeljakt/orgs","repos_url":"https://api.github.com/users/segeljakt/repos","events_url":"https://api.github.com/users/segeljakt/events{/privacy}","received_events_url":"https://api.github.com/users/segeljakt/received_events","type":"User","site_admin":false},"repo":{"id":158407216,"node_id":"MDEwOlJlcG9zaXRvcnkxNTg0MDcyMTY=","name":"pest","full_name":"segeljakt/pest","private":false,"owner":{"login":"segeljakt","id":15143039,"node_id":"MDQ6VXNlcjE1MTQzMDM5","avatar_url":"https://avatars3.githubusercontent.com/u/15143039?v=4","gravatar_id":"","url":"https://api.github.com/users/segeljakt","html_url":"https://github.com/segeljakt","followers_url":"https://api.github.com/users/segeljakt/followers","following_url":"https://api.github.com/users/segeljakt/following{/other_user}","gists_url":"https://api.github.com/users/segeljakt/gists{/gist_id}","starred_url":"https://api.github.com/users/segeljakt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/segeljakt/subscriptions","organizations_url":"https://api.github.com/users/segeljakt/orgs","repos_url":"https://api.github.com/users/segeljakt/repos","events_url":"https://api.github.com/users/segeljakt/events{/privacy}","received_events_url":"https://api.github.com/users/segeljakt/received_events","type":"User","site_admin":false},"html_url":"https://github.com/segeljakt/pest","description":"The Elegant Parser","fork":true,"url":"https://api.github.com/repos/segeljakt/pest","forks_url":"https://api.github.com/repos/segeljakt/pest/forks","keys_url":"https://api.github.com/repos/segeljakt/pest/keys{/key_id}","collaborators_url":"https://api.github.com/repos/segeljakt/pest/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/segeljakt/pest/teams","hooks_url":"https://api.github.com/repos/segeljakt/pest/hooks","issue_events_url":"https://api.github.com/repos/segeljakt/pest/issues/events{/number}","events_url":"https://api.github.com/repos/segeljakt/pest/events","assignees_url":"https://api.github.com/repos/segeljakt/pest/assignees{/user}","branches_url":"https://api.github.com/repos/segeljakt/pest/branches{/branch}","tags_url":"https://api.github.com/repos/segeljakt/pest/tags","blobs_url":"https://api.github.com/repos/segeljakt/pest/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/segeljakt/pest/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/segeljakt/pest/git/refs{/sha}","trees_url":"https://api.github.com/repos/segeljakt/pest/git/trees{/sha}","statuses_url":"https://api.github.com/repos/segeljakt/pest/statuses/{sha}","languages_url":"https://api.github.com/repos/segeljakt/pest/languages","stargazers_url":"https://api.github.com/repos/segeljakt/pest/stargazers","contributors_url":"https://api.github.com/repos/segeljakt/pest/contributors","subscribers_url":"https://api.github.com/repos/segeljakt/pest/subscribers","subscription_url":"https://api.github.com/repos/segeljakt/pest/subscription","commits_url":"https://api.github.com/repos/segeljakt/pest/commits{/sha}","git_commits_url":"https://api.github.com/repos/segeljakt/pest/git/commits{/sha}","comments_url":"https://api.github.com/repos/segeljakt/pest/comments{/number}","issue_comment_url":"https://api.github.com/repos/segeljakt/pest/issues/comments{/number}","contents_url":"https://api.github.com/repos/segeljakt/pest/contents/{+path}","compare_url":"https://api.github.com/repos/segeljakt/pest/compare/{base}...{head}","merges_url":"https://api.github.com/repos/segeljakt/pest/merges","archive_url":"https://api.github.com/repos/segeljakt/pest/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/segeljakt/pest/downloads","issues_url":"https://api.github.com/repos/segeljakt/pest/issues{/number}","pulls_url":"https://api.github.com/repos/segeljakt/pest/pulls{/number}","milestones_url":"https://api.github.com/repos/segeljakt/pest/milestones{/number}","notifications_url":"https://api.github.com/repos/segeljakt/pest/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/segeljakt/pest/labels{/name}","releases_url":"https://api.github.com/repos/segeljakt/pest/releases{/id}","deployments_url":"https://api.github.com/repos/segeljakt/pest/deployments","created_at":"2018-11-20T15:00:50Z","updated_at":"2018-11-20T15:07:25Z","pushed_at":"2018-12-01T20:20:46Z","git_url":"git://github.com/segeljakt/pest.git","ssh_url":"git@github.com:segeljakt/pest.git","clone_url":"https://github.com/segeljakt/pest.git","svn_url":"https://github.com/segeljakt/pest","homepage":"https://pest.rs","size":1440,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":"NOASSERTION","url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"pest-parser:master","ref":"master","sha":"49847de8f41badc14c353c8f221a92fc5e376ee8","user":{"login":"pest-parser","id":26044607,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI2MDQ0NjA3","avatar_url":"https://avatars0.githubusercontent.com/u/26044607?v=4","gravatar_id":"","url":"https://api.github.com/users/pest-parser","html_url":"https://github.com/pest-parser","followers_url":"https://api.github.com/users/pest-parser/followers","following_url":"https://api.github.com/users/pest-parser/following{/other_user}","gists_url":"https://api.github.com/users/pest-parser/gists{/gist_id}","starred_url":"https://api.github.com/users/pest-parser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pest-parser/subscriptions","organizations_url":"https://api.github.com/users/pest-parser/orgs","repos_url":"https://api.github.com/users/pest-parser/repos","events_url":"https://api.github.com/users/pest-parser/events{/privacy}","received_events_url":"https://api.github.com/users/pest-parser/received_events","type":"Organization","site_admin":false},"repo":{"id":56964647,"node_id":"MDEwOlJlcG9zaXRvcnk1Njk2NDY0Nw==","name":"pest","full_name":"pest-parser/pest","private":false,"owner":{"login":"pest-parser","id":26044607,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI2MDQ0NjA3","avatar_url":"https://avatars0.githubusercontent.com/u/26044607?v=4","gravatar_id":"","url":"https://api.github.com/users/pest-parser","html_url":"https://github.com/pest-parser","followers_url":"https://api.github.com/users/pest-parser/followers","following_url":"https://api.github.com/users/pest-parser/following{/other_user}","gists_url":"https://api.github.com/users/pest-parser/gists{/gist_id}","starred_url":"https://api.github.com/users/pest-parser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pest-parser/subscriptions","organizations_url":"https://api.github.com/users/pest-parser/orgs","repos_url":"https://api.github.com/users/pest-parser/repos","events_url":"https://api.github.com/users/pest-parser/events{/privacy}","received_events_url":"https://api.github.com/users/pest-parser/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/pest-parser/pest","description":"The Elegant Parser","fork":false,"url":"https://api.github.com/repos/pest-parser/pest","forks_url":"https://api.github.com/repos/pest-parser/pest/forks","keys_url":"https://api.github.com/repos/pest-parser/pest/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pest-parser/pest/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pest-parser/pest/teams","hooks_url":"https://api.github.com/repos/pest-parser/pest/hooks","issue_events_url":"https://api.github.com/repos/pest-parser/pest/issues/events{/number}","events_url":"https://api.github.com/repos/pest-parser/pest/events","assignees_url":"https://api.github.com/repos/pest-parser/pest/assignees{/user}","branches_url":"https://api.github.com/repos/pest-parser/pest/branches{/branch}","tags_url":"https://api.github.com/repos/pest-parser/pest/tags","blobs_url":"https://api.github.com/repos/pest-parser/pest/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pest-parser/pest/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pest-parser/pest/git/refs{/sha}","trees_url":"https://api.github.com/repos/pest-parser/pest/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pest-parser/pest/statuses/{sha}","languages_url":"https://api.github.com/repos/pest-parser/pest/languages","stargazers_url":"https://api.github.com/repos/pest-parser/pest/stargazers","contributors_url":"https://api.github.com/repos/pest-parser/pest/contributors","subscribers_url":"https://api.github.com/repos/pest-parser/pest/subscribers","subscription_url":"https://api.github.com/repos/pest-parser/pest/subscription","commits_url":"https://api.github.com/repos/pest-parser/pest/commits{/sha}","git_commits_url":"https://api.github.com/repos/pest-parser/pest/git/commits{/sha}","comments_url":"https://api.github.com/repos/pest-parser/pest/comments{/number}","issue_comment_url":"https://api.github.com/repos/pest-parser/pest/issues/comments{/number}","contents_url":"https://api.github.com/repos/pest-parser/pest/contents/{+path}","compare_url":"https://api.github.com/repos/pest-parser/pest/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pest-parser/pest/merges","archive_url":"https://api.github.com/repos/pest-parser/pest/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pest-parser/pest/downloads","issues_url":"https://api.github.com/repos/pest-parser/pest/issues{/number}","pulls_url":"https://api.github.com/repos/pest-parser/pest/pulls{/number}","milestones_url":"https://api.github.com/repos/pest-parser/pest/milestones{/number}","notifications_url":"https://api.github.com/repos/pest-parser/pest/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pest-parser/pest/labels{/name}","releases_url":"https://api.github.com/repos/pest-parser/pest/releases{/id}","deployments_url":"https://api.github.com/repos/pest-parser/pest/deployments","created_at":"2016-04-24T09:50:43Z","updated_at":"2018-12-01T18:19:45Z","pushed_at":"2018-12-01T20:20:47Z","git_url":"git://github.com/pest-parser/pest.git","ssh_url":"git@github.com:pest-parser/pest.git","clone_url":"https://github.com/pest-parser/pest.git","svn_url":"https://github.com/pest-parser/pest","homepage":"https://pest.rs","size":1396,"stargazers_count":1114,"watchers_count":1114,"language":"Rust","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":64,"mirror_url":null,"archived":false,"open_issues_count":47,"license":{"key":"other","name":"Other","spdx_id":"NOASSERTION","url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":64,"open_issues":47,"watchers":1114,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pest-parser/pest/pulls/344"},"html":{"href":"https://github.com/pest-parser/pest/pull/344"},"issue":{"href":"https://api.github.com/repos/pest-parser/pest/issues/344"},"comments":{"href":"https://api.github.com/repos/pest-parser/pest/issues/344/comments"},"review_comments":{"href":"https://api.github.com/repos/pest-parser/pest/pulls/344/comments"},"review_comment":{"href":"https://api.github.com/repos/pest-parser/pest/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pest-parser/pest/pulls/344/commits"},"statuses":{"href":"https://api.github.com/repos/pest-parser/pest/statuses/b67321e03a6b02974e0f9093d61d34f8896ca33c"}},"author_association":"NONE"}}
{"actor":{"display_login":"segeljakt"}}