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

support for polymorphic token types #2

Closed
trofi opened this issue Dec 25, 2012 · 3 comments
Closed

support for polymorphic token types #2

trofi opened this issue Dec 25, 2012 · 3 comments

Comments

@trofi
Copy link
Contributor

trofi commented Dec 25, 2012

One of my co-workers attempted to make a polymorphic token type
for generated parsers (handy to try various target types)

http://code.haskell.org/~slyfox/M.y

and even succeded in it with the following hack:

{-# OPTIONS  -cpp #-}
...
#define HappyAbsSyn (HappyAbsSyn_ ta tb)
...
%tokentype { Token ta tb }

What do you think of adding direct support for polymorphic token types to happy?

Thanks!

@Ericson2314
Copy link
Collaborator

Unfortunately http://code.haskell.org/~slyfox/M.y is dead now, so it's a bit hard for me to see what this is about.

@trofi
Copy link
Contributor Author

trofi commented Mar 19, 2021

Let's inline it here:

{

module M
    ( parse_regexps
    ) where

import           Data.Char

#define HappyAbsSyn (HappyAbsSyn_ ta tb)

}

%name      parser
%tokentype { Token ta tb }
%error     { parseError }
%token
    name          { TokenName $$ }
    int           { TokenInt $$ }

%%

Expr :: { [Token ta tb] }
    : name    { [TokenName $1] }
    | int     { [TokenInt  $1] }


{

data Token ta tb
    = TokenName ta
    | TokenInt  tb


instance (Show ta, Show tb) => Show (Token ta tb) where
    show (TokenName ta) = show ta
    show (TokenInt  tb) = show tb


class Eatable ta where
    fromString :: String -> ta


parseError :: [Token ta tb] -> c
parseError e = error "Parse error"


lexer :: (Eatable ta, Eatable tb) => String -> [Token ta tb]
lexer [] = []
lexer (c : cs)
      | isSpace c = lexer cs
      | isAlpha c = lex_name (c : cs)
      | isDigit c = lex_int  (c : cs)


lex_name :: (Eatable ta, Eatable tb) => String -> [Token ta tb]
lex_name cs =
    let (nm, rest) = span isAlpha cs
    in  TokenName (fromString nm) : lexer rest


lex_int :: (Eatable ta, Eatable tb) => String -> [Token ta tb]
lex_int cs =
    let (num, rest) = span isDigit cs
    in  TokenInt (fromString num) : lexer rest


--------------------------------------------------------------------------------
parse_regexps :: (Eatable ta, Eatable tb) => String -> [Token ta tb]
parse_regexps = parser . lexer

}

@Ericson2314
Copy link
Collaborator

OK thanks. I will try to investigate that soon.

@trofi trofi closed this as completed Feb 29, 2024
@trofi trofi closed this as not planned Won't fix, can't repro, duplicate, stale Feb 29, 2024
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

No branches or pull requests

2 participants