This commit is contained in:
crumbtoo
2024-01-15 10:35:11 -07:00
parent 3dfadc17ec
commit bdf74ac6c9
2 changed files with 40 additions and 12 deletions

View File

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

View File

@@ -44,11 +44,15 @@ data RlpToken
| TokenConSym Name | TokenConSym Name
-- keywords -- keywords
| TokenData | TokenData
| TokenPipe
| TokenLet | TokenLet
| TokenIn | TokenIn
-- control symbols -- reserved ops
| TokenArrow
| TokenPipe
| TokenHasType
| TokenLambda
| TokenEquals | TokenEquals
-- control symbols
| TokenSemicolon | TokenSemicolon
| TokenLBrace | TokenLBrace
| TokenRBrace | TokenRBrace