-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
257 additions
and
221 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{-# Language BlockArguments #-} | ||
module Advent.Format.Enum where | ||
|
||
import Language.Haskell.TH | ||
import Data.List | ||
import Data.Traversable | ||
|
||
enumCases :: String -> Q [(Name,String)] | ||
enumCases nameStr = | ||
do tyName <- maybe (fail ("Failed to find type named " ++ show nameStr)) pure | ||
=<< lookupTypeName nameStr | ||
|
||
info <- reify tyName | ||
cons <- | ||
case info of | ||
TyConI (DataD _ _ _ _ cons _) -> pure cons | ||
_ -> fail ("Failed to find data declaration for " ++ show nameStr) | ||
|
||
for cons \con -> | ||
case con of | ||
NormalC name [] | ||
| Just str <- stripPrefix nameStr (nameBase name) -> | ||
case str of | ||
'_' : symbolName -> | ||
do symbol <- processSymbolName symbolName | ||
pure (name, symbol) | ||
_ -> pure (name, str) | ||
_ -> fail ("Unsupported constructor: " ++ show con) | ||
|
||
processSymbolName :: String -> Q String | ||
processSymbolName str = | ||
case break ('_' ==) str of | ||
(name, rest) -> | ||
case lookup name symbolNames of | ||
Nothing -> fail ("Unknown symbol name: " ++ name) | ||
Just symbol -> | ||
case rest of | ||
[] -> pure [symbol] | ||
_:str' -> (symbol:) <$> processSymbolName str' | ||
|
||
symbolNames :: [(String, Char)] | ||
symbolNames = | ||
[ ("LT", '<') | ||
, ("GT", '>') | ||
, ("EQ", '=') | ||
, ("BANG", '!') | ||
, ("AT" , '@') | ||
, ("HASH", '#') | ||
, ("DOLLAR", '$') | ||
, ("PERCENT", '%') | ||
, ("CARET", '^') | ||
, ("AMPERSAND", '&') | ||
, ("STAR", '*') | ||
, ("PIPE", '|') | ||
, ("LPAREN", '(') | ||
, ("RPAREN", ')') | ||
, ("LBRACE", '{') | ||
, ("RBRACE", '}') | ||
, ("LBRACK", '[') | ||
, ("RBRACK", ']') | ||
, ("COLON", ':') | ||
, ("SEMI", ';') | ||
, ("QUESTION", '?') | ||
, ("SLASH", '/') | ||
, ("BACKSLASH", '\\') | ||
, ("UNDERSCORE", '_') | ||
, ("DASH", '-') | ||
, ("DOT", '.') | ||
, ("COMMA", ',') | ||
, ("PLUS", '+') | ||
, ("TILDE", '~') | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{-| | ||
Module : Advent.Format.Show | ||
Description : Rendering for the parser DSL | ||
Copyright : (c) Eric Mertens, 2018-2021 | ||
License : ISC | ||
Maintainer : [email protected] | ||
-} | ||
|
||
module Advent.Format.Show where | ||
|
||
import Advent.Format.Types (Format(..), Token(..)) | ||
|
||
-- | Render a parsed format string back to the input syntax. | ||
showFormat :: Int {- ^ surrounding precedence -} -> Format -> ShowS | ||
showFormat p fmt = | ||
case fmt of | ||
Many x -> showFormat 3 x . showChar '*' | ||
Some x -> showFormat 3 x . showChar '+' | ||
Gather x -> showFormat 3 x . showChar '!' | ||
SepBy x y -> showFormat 3 x . showChar '&' . showFormat 3 y | ||
Alt x y -> showParen (p > 1) $ showFormat 1 x . showChar '|' . showFormat 2 y | ||
Follow xs -> showParen (p > 2) $ \z -> foldr (showFormat 3) z xs | ||
Group x -> showFormat 3 x | ||
UnsignedInteger -> showString "%lu" | ||
SignedInteger -> showString "%ld" | ||
HexInteger -> showString "%lx" | ||
UnsignedInt -> showString "%u" | ||
SignedInt -> showString "%d" | ||
HexInt -> showString "%x" | ||
Word -> showString "%s" | ||
Char -> showString "%c" | ||
Letter -> showString "%a" | ||
Named n -> showChar '@' . showString n | ||
Literal x -> flip (foldr showLiteral) x | ||
|
||
-- | Render a literal character match back to input syntax. | ||
showLiteral :: Char -> ShowS | ||
showLiteral x | ||
| x == '\n' = showString "%n" | ||
| x `elem` "()&!*+%@" = showChar '%' . showChar x | ||
| otherwise = showChar x | ||
|
||
showToken :: Token -> String | ||
showToken t = | ||
case t of | ||
TOpenGroup -> "(" | ||
TCloseGroup -> ")" | ||
TAnyChar -> "%c" | ||
TAnyLetter -> "%a" | ||
TAnyWord -> "%s" | ||
TUnsignedInteger -> "%lu" | ||
TSignedInteger -> "%ld" | ||
THexInteger -> "%lx" | ||
TUnsignedInt -> "%u" | ||
TSignedInt -> "%d" | ||
THexInt -> "%x" | ||
TMany -> "*" | ||
TSome -> "+" | ||
TSepBy -> "&" | ||
TAlt -> "|" | ||
TAt x -> "@" ++ x | ||
TBang -> "!" | ||
TLiteral c -> showLiteral c "" |
Oops, something went wrong.