Moduleparsec-3.1.18.0Haskell2010
Text.ParserCombinators.Parsec.Token
Parsec compatibility module
- 4 types
- 1 value
- Packageparsec-3.1.18.0
- Exports5
- LanguageHaskell2010
- LicenceBSD-2-Clause
- SourceToken.hs
The GenLanguageDef type is a record that contains all parameterizable
features of the Text.Parsec.Token module. The module Text.Parsec.Language
contains some default definitions.
Constructors
LanguageDefcommentStart :: StringDescribes the start of a block comment. Use the empty string if the language doesn't support block comments. For example "/*".
commentEnd :: StringDescribes the end of a block comment. Use the empty string if the language doesn't support block comments. For example "*/".
commentLine :: StringDescribes the start of a line comment. Use the empty string if the language doesn't support line comments. For example "//".
nestedComments :: BoolSet to True if the language supports nested block comments.
identStart :: ParsecT s u m CharThis parser should accept any start characters of identifiers. For example
letter <|> char '_'.identLetter :: ParsecT s u m CharThis parser should accept any legal tail characters of identifiers. For example
alphaNum <|> char '_'.opStart :: ParsecT s u m CharThis parser should accept any start characters of operators. For example
oneOf ":!#$%&*+./<=>?@\\^|-~"opLetter :: ParsecT s u m CharThis parser should accept any legal tail characters of operators. Note that this parser should even be defined if the language doesn't support user-defined operators, or otherwise the reservedOp parser won't work correctly.
reservedNames :: [String]The list of reserved identifiers.
reservedOpNames :: [String]The list of reserved operators.
caseSensitive :: BoolSet to True if the language is case sensitive.
The type of the record that holds lexical parsers that work on
s streams with state u over a monad m.
Constructors
TokenParseridentifier :: ParsecT s u m StringThis lexeme parser parses a legal identifier. Returns the identifier string. This parser will fail on identifiers that are reserved words. Legal identifier (start) characters and reserved words are defined in the LanguageDef that is passed to makeTokenParser. An
identifieris treated as a single token using try.reserved :: String -> ParsecT s u m ()The lexeme parser
reserved nameparsessymbol name, but it also checks that thenameis not a prefix of a valid identifier. Areservedword is treated as a single token using try.operator :: ParsecT s u m StringThis lexeme parser parses a legal operator. Returns the name of the operator. This parser will fail on any operators that are reserved operators. Legal operator (start) characters and reserved operators are defined in the LanguageDef that is passed to makeTokenParser. An
operatoris treated as a single token using try.reservedOp :: String -> ParsecT s u m ()The lexeme parser
reservedOp nameparsessymbol name, but it also checks that thenameis not a prefix of a valid operator. AreservedOpis treated as a single token using try.charLiteral :: ParsecT s u m CharThis lexeme parser parses a single literal character. Returns the literal character value. This parsers deals correctly with escape sequences. The literal character is parsed according to the grammar rules defined in the Haskell report (which matches most programming languages quite closely).
stringLiteral :: ParsecT s u m StringThis lexeme parser parses a literal string. Returns the literal string value. This parsers deals correctly with escape sequences and gaps. The literal string is parsed according to the grammar rules defined in the Haskell report (which matches most programming languages quite closely).
natural :: ParsecT s u m IntegerThis lexeme parser parses a natural number (a non-negative whole number). Returns the value of the number. The number can be specified in decimal, hexadecimal or octal. The number is parsed according to the grammar rules in the Haskell report.
integer :: ParsecT s u m IntegerThis lexeme parser parses an integer (a whole number). This parser is like natural except that it can be prefixed with sign (i.e. '-' or '+'). Returns the value of the number. The number can be specified in decimal, hexadecimal or octal. The number is parsed according to the grammar rules in the Haskell report.
float :: ParsecT s u m DoubleThis lexeme parser parses a floating point value. Returns the value of the number. The number is parsed according to the grammar rules defined in the Haskell report.
naturalOrFloat :: ParsecT s u m (Either Integer Double)decimal :: ParsecT s u m IntegerParses a non-negative whole number in the decimal system. Returns the value of the number.
hexadecimal :: ParsecT s u m IntegerParses a non-negative whole number in the hexadecimal system. The number should be prefixed with "x" or "X". Returns the value of the number.
octal :: ParsecT s u m IntegerParses a non-negative whole number in the octal system. The number should be prefixed with "o" or "O". Returns the value of the number.
symbol :: String -> ParsecT s u m StringLexeme parser
symbol sparses stringsand skips trailing white space.lexeme :: forall a. ParsecT s u m a -> ParsecT s u m alexeme pfirst applies parserpand then the whiteSpace parser, returning the value ofp. Every lexical token (lexeme) is defined usinglexeme, this way every parse starts at a point without white space. Parsers that uselexemeare called lexeme parsers in this document.The only point where the whiteSpace parser should be called explicitly is the start of the main parser in order to skip any leading white space.
mainParser = do{ whiteSpace ; ds <- many (lexeme digit) ; eof ; return (sum ds) }whiteSpace :: ParsecT s u m ()Parses any white space. White space consists of zero or more occurrences of a space, a line comment or a block (multi line) comment. Block comments may be nested. How comments are started and ended is defined in the LanguageDef that is passed to makeTokenParser.
parens :: forall a. ParsecT s u m a -> ParsecT s u m aLexeme parser
parens pparsespenclosed in parenthesis, returning the value ofp.braces :: forall a. ParsecT s u m a -> ParsecT s u m aLexeme parser
braces pparsespenclosed in braces ('{' and '}'), returning the value ofp.angles :: forall a. ParsecT s u m a -> ParsecT s u m aLexeme parser
angles pparsespenclosed in angle brackets ('<' and '>'), returning the value ofp.brackets :: forall a. ParsecT s u m a -> ParsecT s u m aLexeme parser
brackets pparsespenclosed in brackets ('[' and ']'), returning the value ofp.squares :: forall a. ParsecT s u m a -> ParsecT s u m aDEPRECATED: Use brackets.
semi :: ParsecT s u m StringLexeme parser |semi| parses the character ';' and skips any trailing white space. Returns the string ";".
comma :: ParsecT s u m StringLexeme parser
commaparses the character ',' and skips any trailing white space. Returns the string ",".colon :: ParsecT s u m StringLexeme parser
colonparses the character ':' and skips any trailing white space. Returns the string ":".dot :: ParsecT s u m StringLexeme parser
dotparses the character '.' and skips any trailing white space. Returns the string ".".semiSep :: forall a. ParsecT s u m a -> ParsecT s u m [a]Lexeme parser
semiSep pparses zero or more occurrences ofpseparated by semi. Returns a list of values returned byp.semiSep1 :: forall a. ParsecT s u m a -> ParsecT s u m [a]Lexeme parser
semiSep1 pparses one or more occurrences ofpseparated by semi. Returns a list of values returned byp.commaSep :: forall a. ParsecT s u m a -> ParsecT s u m [a]Lexeme parser
commaSep pparses zero or more occurrences ofpseparated by comma. Returns a list of values returned byp.commaSep1 :: forall a. ParsecT s u m a -> ParsecT s u m [a]Lexeme parser
commaSep1 pparses one or more occurrences ofpseparated by comma. Returns a list of values returned byp.
The expression makeTokenParser language creates a GenTokenParser
record that contains lexical parsers that are
defined using the definitions in the language record.
The use of this function is quite stylized - one imports the appropriate language definition and selects the lexical parsers that are needed from the resulting GenTokenParser.
module Main where
import Text.Parsec
import qualified Text.Parsec.Token as P
import Text.Parsec.Language (haskellDef)
-- The parser
...
expr = parens expr
<|> identifier
<|> ...
-- The lexer
lexer = P.makeTokenParser haskellDef
parens = P.parens lexer
braces = P.braces lexer
identifier = P.identifier lexer
reserved = P.reserved lexer
...