baskell-0.1: src/ParserUtils.hs
{-------------------------------------------------------------------------------
Copyright: Bernie Pope 2004
Module: ParserUtils
Description: Utilities that support Baskell's parser.
Primary Authors: Bernie Pope
-------------------------------------------------------------------------------}
{-
This file is part of baskell.
baskell is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
baskell is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with baskell; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}
module ParserUtils
( Parser
, equals
, rightArrow
, comma
, semiColon
, singleQuoted
, leftRoundBracket
, rightRoundBracket
, leftSquareBracket
, rightSquareBracket
, backSlash
, exclamation
, colon
, doubleColon
, int
, word
)
where
import Text.ParserCombinators.Parsec
( GenParser
, token
, skipMany
, (<|>)
)
import Lexer
( Token (..)
, Symbol (..)
)
import AST
( Ident )
--------------------------------------------------------------------------------
type Parser a = GenParser Token () a
-- basic parser for token symbols. Needed by Parsec.
tokenParser :: (Symbol -> Maybe a) -> Parser a
tokenParser test
= token showToken posToken testToken
where
showToken (Token (pos,tok)) = show tok
posToken (Token (pos,tok)) = pos
testToken (Token (pos,tok)) = test tok
-- wrapper for tokenParser
simpleParser :: Symbol -> Parser ()
simpleParser tok
= tokenParser $
\next -> if next == tok
then Just ()
else Nothing
equals :: Parser ()
equals = simpleParser Equals
comma :: Parser ()
comma = simpleParser Comma
semiColon :: Parser ()
semiColon = simpleParser SemiColon
doubleColon :: Parser ()
doubleColon = simpleParser DoubleColon
colon :: Parser ()
colon = simpleParser Colon
singleQuoted :: Parser String
singleQuoted
= tokenParser getQuoted
where
getQuoted (SingleQuoted s) = Just s
getQuoted other = Nothing
leftRoundBracket :: Parser ()
leftRoundBracket = simpleParser LeftRoundBracket
rightRoundBracket :: Parser ()
rightRoundBracket = simpleParser RightRoundBracket
leftSquareBracket :: Parser ()
leftSquareBracket = simpleParser LeftSquareBracket
rightSquareBracket :: Parser ()
rightSquareBracket = simpleParser RightSquareBracket
backSlash :: Parser ()
backSlash = simpleParser BackSlash
exclamation :: Parser ()
exclamation = simpleParser Exclamation
dash :: Parser ()
dash = simpleParser Dash
gt :: Parser ()
gt = simpleParser GreaterThan
int :: Parser Int
int
= tokenParser number
where
number (Num n) = Just n
number other = Nothing
word :: Parser String
word
= tokenParser getWord
where
getWord (Word s) = Just s
getWord other = Nothing
rightArrow :: Parser ()
rightArrow = dash >> gt