: Haskell, .
import Text.Parsec
import qualified Text.Parsec.Token as T
import Text.Parsec.String ( Parser )
import Text.Parsec.Language (haskellDef)
lexer = T.makeTokenParser haskellDef
whiteSpace :: Parser ()
whiteSpace = T.whiteSpace lexer
lexeme = T.lexeme lexer
mainParser = do whiteSpace
ds <- many digit
eof
return ds
.
Mukeshs-MacBook-Pro:Compilers mukeshtiwari$ ghci stmp.hs
GHCi, version 7.6.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( stmp.hs, interpreted )
Ok, modules loaded: Main.
*Main> parse mainParser "" "1"
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package bytestring-0.10.0.0 ... linking ... done.
Loading package transformers-0.3.0.0 ... linking ... done.
Loading package mtl-2.1.2 ... linking ... done.
Loading package text-0.11.2.3 ... linking ... done.
Loading package parsec-3.1.3 ... linking ... done.
Right "1"
*Main> parse mainParser "" "12"
Right "12"
*Main> parse mainParser "" "123"
Right "123"
*Main> parse mainParser "" " 123"
Right "123"
*Main> parse mainParser "" " 123"
Right "123"
*Main> parse mainParser "" " 123"
Right "123"
. .
*Main> parse mainParser "" "123 "
Left (line 1, column 4):
unexpected ' '
expecting digit or end of input
Oops! - . ? , , , , ? whiteSpace, ( ), , , -, . ( ) eof, . , ? , whiteSpace , (Ignore < * ).
import Text.Parsec
import qualified Text.Parsec.Token as T
import Text.Parsec.String ( Parser )
import Text.Parsec.Language (haskellDef)
import Control.Applicative ( (<*) )
lexer = T.makeTokenParser haskellDef
whiteSpace :: Parser ()
whiteSpace = T.whiteSpace lexer
lexeme = T.lexeme lexer
mainParser = do whiteSpace
ds <- many ( digit <* whiteSpace )
eof
return ds
*Main> parse mainParser "" " 31312 "
Right "31312"
*Main> parse mainParser "" " 3131 2 "
Right "31312"
*Main> parse mainParser "" " 313 1 2 "
Right "31312"
*Main> parse mainParser "" " 3 1 3 1 2 "
Right "31312"
*Main> parse mainParser "" " 31 3 1 2 "
Right "31312"
. , . whiteSpace, ( (digit < * whiteSpace)). , whiteSpace , . lexeme, lexeme p p whiteSpace, lexeme , .