tools/PascalParser.hs
changeset 6444 eddc1e9bcd81
parent 6443 23364a5fcc86
child 6450 14224c9b4594
equal deleted inserted replaced
6443:23364a5fcc86 6444:eddc1e9bcd81
    65     | PrefixOp String Expression
    65     | PrefixOp String Expression
    66     | PostfixOp String Expression
    66     | PostfixOp String Expression
    67     | BinOp String Expression Expression
    67     | BinOp String Expression Expression
    68     | StringLiteral String
    68     | StringLiteral String
    69     | CharCode String
    69     | CharCode String
       
    70     | HexCharCode String
    70     | NumberLiteral String
    71     | NumberLiteral String
    71     | FloatLiteral String
    72     | FloatLiteral String
    72     | HexNumber String
    73     | HexNumber String
    73     | Reference Reference
    74     | Reference Reference
    74     | Null
    75     | Null
   173     return vs
   174     return vs
   174     where
   175     where
   175     aConstDecl = do
   176     aConstDecl = do
   176         comments
   177         comments
   177         i <- iD
   178         i <- iD
   178         optional $ do
   179         t <- optionMaybe $ do
   179             char ':'
   180             char ':'
   180             comments
   181             comments
   181             t <- typeDecl
   182             t <- typeDecl
   182             comments
   183             comments
   183             return ()
   184             return t
   184         char '='
   185         char '='
   185         comments
   186         comments
   186         e <- initExpression
   187         e <- initExpression
   187         comments
   188         comments
   188         return $ VarDeclaration False ([i], UnknownType) (Just e)
   189         return $ VarDeclaration False ([i], fromMaybe UnknownType t) (Just e)
   189         
   190         
   190 typeDecl = choice [
   191 typeDecl = choice [
   191     char '^' >> typeDecl >>= return . PointerTo
   192     char '^' >> typeDecl >>= return . PointerTo
   192     , try (string "shortstring") >> return (String 255)
   193     , try (string "shortstring") >> return (String 255)
   193     , try (string "string") >> optionMaybe (brackets pas $ integer pas) >>= return . String . fromMaybe 255
   194     , try (string "string") >> optionMaybe (brackets pas $ integer pas) >>= return . String . fromMaybe 255
   371         return $ [FunctionDeclaration i ret vs b]
   372         return $ [FunctionDeclaration i ret vs b]
   372         
   373         
   373     functionDecorator = choice [
   374     functionDecorator = choice [
   374         try $ string "inline;"
   375         try $ string "inline;"
   375         , try $ string "cdecl;"
   376         , try $ string "cdecl;"
       
   377         , try $ string "overload;"
   376         , try (string "external") >> comments >> iD >> optional (string "name" >> comments >> stringLiteral pas)>> string ";"
   378         , try (string "external") >> comments >> iD >> optional (string "name" >> comments >> stringLiteral pas)>> string ";"
   377         ] >> comments
   379         ] >> comments
   378         
   380         
   379         
   381         
   380 program = do
   382 program = do
   420         , parens pas $ expression 
   422         , parens pas $ expression 
   421         , try $ natural pas >>= \i -> notFollowedBy (char '.') >> (return . NumberLiteral . show) i
   423         , try $ natural pas >>= \i -> notFollowedBy (char '.') >> (return . NumberLiteral . show) i
   422         , try $ float pas >>= return . FloatLiteral . show
   424         , try $ float pas >>= return . FloatLiteral . show
   423         , try $ natural pas >>= return . NumberLiteral . show
   425         , try $ natural pas >>= return . NumberLiteral . show
   424         , stringLiteral pas >>= return . StringLiteral
   426         , stringLiteral pas >>= return . StringLiteral
       
   427         , try (string "#$") >> many hexDigit >>= \c -> comments >> return (HexCharCode c)
   425         , char '#' >> many digit >>= \c -> comments >> return (CharCode c)
   428         , char '#' >> many digit >>= \c -> comments >> return (CharCode c)
   426         , char '$' >> many hexDigit >>=  \h -> comments >> return (HexNumber h)
   429         , char '$' >> many hexDigit >>=  \h -> comments >> return (HexNumber h)
   427         , char '-' >> expression >>= return . PrefixOp "-"
   430         , char '-' >> expression >>= return . PrefixOp "-"
   428         , try $ string "nil" >> return Null
   431         , try $ string "nil" >> return Null
   429         , reference >>= return . Reference
   432         , reference >>= return . Reference
   456         ]
   459         ]
   457     
   460     
   458 phrasesBlock = do
   461 phrasesBlock = do
   459     try $ string "begin"
   462     try $ string "begin"
   460     comments
   463     comments
   461     p <- manyTill phrase (try $ string "end")
   464     p <- manyTill phrase (try $ string "end" >> notFollowedBy alphaNum)
   462     comments
   465     comments
   463     return $ Phrases p
   466     return $ Phrases p
   464     
   467     
   465 phrase = do
   468 phrase = do
   466     o <- choice [
   469     o <- choice [
   605         , parens pas (semiSep pas $ recField) >>= return . InitRecord
   608         , parens pas (semiSep pas $ recField) >>= return . InitRecord
   606         , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . InitNumber . show) i
   609         , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . InitNumber . show) i
   607         , try $ float pas >>= return . InitFloat . show
   610         , try $ float pas >>= return . InitFloat . show
   608         , try $ integer pas >>= return . InitNumber . show
   611         , try $ integer pas >>= return . InitNumber . show
   609         , stringLiteral pas >>= return . InitString
   612         , stringLiteral pas >>= return . InitString
   610         , char '#' >> many digit >>= return . InitChar
   613         , char '#' >> many digit >>= \c -> comments >> return (InitChar c)
   611         , char '$' >> many hexDigit >>= return . InitHexNumber
   614         , char '$' >> many hexDigit >>= \h -> comments >> return (InitHexNumber h)
   612         , try $ string "nil" >> return InitNull
   615         , try $ string "nil" >> return InitNull
   613         , iD >>= return . InitReference
   616         , iD >>= return . InitReference
   614         ]
   617         ]
   615         
   618         
   616     recField = do
   619     recField = do