oh my god

This commit is contained in:
crumbtoo
2024-01-15 11:11:43 -07:00
parent 9a4f24ec10
commit c0236dc079
2 changed files with 337 additions and 34 deletions

View File

@@ -1,5 +1,5 @@
{
{-# LANGUAGE ViewPatterns, LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module Rlp.Lex
@@ -39,20 +39,10 @@ $nl = [\n\r]
$white_no_nl = $white # $nl
$namechar = [$alpha $digit \' \#]
$reservedsym = [\(\)\,\;\[\]\`\{\}]
$asciisym = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~\:]
$namesym = $asciisym # \;
@reservedop =
"=" | \\ | "->" | "::" | "|"
@varname = $lower $namechar*
@varname = $lower $namechar*
@conname = $upper $namechar*
@varsym = $namesym+
@consym = \: $namesym*
@decimal = $digit+
@digits = $digit+
rlp :-
@@ -65,8 +55,8 @@ rlp :-
{
\n { beginPush bol }
@varname { tokenWith TokenVarName }
@decimal { tokenWith (TokenLitInt . readInt) }
@reservedop { tokenWith readReservedOp }
@digits { tokenWith (TokenLitInt . readInt) }
"=" { constToken TokenEquals }
}
-- control characters
@@ -96,14 +86,6 @@ rlp :-
{
readReservedOp :: Text -> RlpToken
readReservedOp = \case
"=" -> TokenEquals
"\\" -> TokenLambda
"->" -> TokenArrow
"::" -> TokenHasType
s -> error (show s)
-- | @andBegin@, with the subtle difference that the start code is set
-- /after/ the action
thenBegin :: LexerAction a -> Int -> LexerAction a
@@ -145,12 +127,6 @@ alexGetByte inp = case inp ^. aiBytes of
getInput :: P AlexInput
getInput = use psInput
takeInput :: Int -> AlexInput -> Text
takeInput n inp = T.cons c cs
where
c = inp ^. aiPrevChar
cs = T.take (max 0 (n-1)) $ inp ^. aiSource
getLexState :: P Int
getLexState = use (psLexState . singular _head)
@@ -172,8 +148,8 @@ constToken t inp l = do
tokenWith :: (Text -> RlpToken) -> LexerAction (Located RlpToken)
tokenWith tf inp l = do
pos <- getPos
let t = takeInput l inp
pure (Located (pos,l) (tf t))
let t = tf (T.take l $ inp ^. aiSource)
pure (Located (pos,l) t)
getPos :: P Position
getPos = use (psInput . aiPos)
@@ -200,10 +176,10 @@ initParseState s = ParseState
}
initAlexInput :: Text -> AlexInput
initAlexInput t = AlexInput
{ _aiPrevChar = c
initAlexInput s = AlexInput
{ _aiPrevChar = '\0'
, _aiSource = s
, _aiBytes = b
, _aiBytes = []
, _aiPos = (1,1)
}