Skip to content

Instantly share code, notes, and snippets.

@StephenWakely
Last active April 29, 2019 22:03
Show Gist options
  • Save StephenWakely/e12a15db6988c5790763b3261eff6463 to your computer and use it in GitHub Desktop.
Save StephenWakely/e12a15db6988c5790763b3261eff6463 to your computer and use it in GitHub Desktop.
Variable parser
-- | A variable
-- x.y[3].z[2][9]
variableParser :: Parser Var
variableParser = do
pos <- getSourcePos
var <- parseSimpleVar pos <?> "Simple var"
go var
where
go :: Var -> Parser Var
go var = do
pos <- getSourcePos
next <- optional $
try ( parseFieldVar var pos <?> "field var" ) <|>
try ( parseSubscriptVar var pos <?> "subscript var" )
case next of
Nothing -> return var -- There is nothing more to this var
Just var' -> go var' -- There is more, continue parsing
parseFieldVar :: Var -> SourcePos -> Parser Var
parseFieldVar var pos = do
symbol "."
name <- identifier
return $ FieldVar var name pos
parseSubscriptVar :: Var -> SourcePos -> Parser Var
parseSubscriptVar var pos = do
exp <- squareBrackets $ parseExp
return $ SubscriptVar var exp pos
parseSimpleVar :: SourcePos -> Parser Var
parseSimpleVar pos = do
name <- identifier
return $ SimpleVar name pos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment