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

Add a eof terminal #37

Closed
int-index opened this issue Oct 22, 2018 · 5 comments
Closed

Add a eof terminal #37

int-index opened this issue Oct 22, 2018 · 5 comments

Comments

@int-index
Copy link

Is it possible to have

eof :: Prod r e t ()

that matches the end of input?

I have a grammar that accepts expressions in parentheses:

    exprInBrackets <- rule $
      ExprInBrackets
        <$> leftBrack
        <*> ntExpr
        <*> rightBrack
        <?> "parenthesized expression"

And, given ([2]) it will produce the following value:

let inner = ExprInBrackets "[" (ExprNum 2) "]"
in ExprInBrackets "(" inner ")"

I'd like to modify it like this:

    exprInBrackets <- rule $
      ExprInBrackets
        <$> leftParen
        <*> ntExpr
        <*> (rightParen <|> ("" <$ eof))
        <?> "parenthesized expression"

so that expressions with unbalanced parentheses can be parsed. Given ([2 it would produce

let inner = ExprInBrackets "[" (ExprNum 2) ""
in ExprInBrackets "(" inner ""

cc @artemohanjanyan

@ollef
Copy link
Owner

ollef commented Oct 22, 2018

It seems like it should be possible to add as a primitive, but might add some complexity to the implementation.

There are also some questions that need to be answered like whether eof *> eof means the same as eof and whether it can be done in a backward compatible way.

Could you get away with adding an EOF token to your token type instead?

@int-index
Copy link
Author

int-index commented Oct 22, 2018

whether eof *> eof means the same as eof

For the use case I described, it should be the same.

Could you get away with adding an EOF token to your token type instead?

No. Let's say I want to parse (2 tokenised as [OpenPar, Num 2, Eof] – this will work. However, ((2 tokenised as [OpenPar, OpenPar, Num 2, Eof], will not.

@int-index
Copy link
Author

whether it can be done in a backward compatible way.

I don't see how this functionality would break any existing parser.

@ollef
Copy link
Owner

ollef commented Oct 22, 2018 via email

@ollef
Copy link
Owner

ollef commented Nov 28, 2018

Turns out this is possible using existing functionality: #40.

@ollef ollef closed this as completed Nov 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants