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

Ben's code #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

bwiedermann
Copy link
Contributor

No description provided.

…al-lab

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

// factors
lazy val fact: PackratParser[Expr] =
number
( number
| "("~>expr<~")" )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this go about actually parsing the parenthetical expression? Why can you exclude something like ^^ {case "("~e~")" ⇒ (e)}? Does your use of expr direct the parser back up to the expression parser when a parenthetical expression is detected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of things:

  • fact should give back an Expr (as specified in its result type: PackratParser[Expr])
  • The ~> and <~ combinators throw away their left and right arguments, respectively.

So, the second rule will parse a left-paren, then parse an Expr, then parse a right-paren. It'll result in whatever expr results in, so we don't need a separate action to store it.

But, if we wanted to represent the fact that this expression was parenthetical, then we would add to the intermediate representation a case class for parenthetical expressions; and we would add an action to this rule to create an instance of that case class.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks a bunch!

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