I am trying to write my own parser combinator library in Haskell, and I am struggling with how to parse identifiers. I have a datatype defined as follows which will be part of my AST.
data Expr = Var String | .... deriving (Show)
The purpose of this expression is to hold the names of variables as I parse them. The parser is defined as follows:
identifiers :: Parser Expr
identifiers = do
first <- string "_" <|> alphanum
rest <- many alphanum
guard $ notElem (first:rest) keywords
return $ Var (first:rest)
I was wondering where in the parsing do you take into account what value the identifier is bound to. For example, if you had number_of_results = 5, the parser will parse the name of the identifier, but how do you keep reference to what the value of the identifier is?
I was initially considering redefining the data type as follows:
data Expr = Var String Value | .... deriving (Show)
and then, in the parsing stage, continue parsing until I get to a value.
However, I am not too sure whether I should do it like this... Could someone suggest a solution to this problem?