tools/PascalParser.hs
changeset 6443 23364a5fcc86
parent 6426 2d44f6561e72
child 6444 eddc1e9bcd81
equal deleted inserted replaced
6442:e6e409d9d6db 6443:23364a5fcc86
    26 data TypesAndVars = TypesAndVars [TypeVarDeclaration]
    26 data TypesAndVars = TypesAndVars [TypeVarDeclaration]
    27     deriving Show
    27     deriving Show
    28 data TypeVarDeclaration = TypeDeclaration Identifier TypeDecl
    28 data TypeVarDeclaration = TypeDeclaration Identifier TypeDecl
    29     | VarDeclaration Bool ([Identifier], TypeDecl) (Maybe InitExpression)
    29     | VarDeclaration Bool ([Identifier], TypeDecl) (Maybe InitExpression)
    30     | FunctionDeclaration Identifier TypeDecl [TypeVarDeclaration] (Maybe (TypesAndVars, Phrase))
    30     | FunctionDeclaration Identifier TypeDecl [TypeVarDeclaration] (Maybe (TypesAndVars, Phrase))
       
    31     | OperatorDeclaration String Identifier TypeDecl [TypeVarDeclaration] (Maybe (TypesAndVars, Phrase))
    31     deriving Show
    32     deriving Show
    32 data TypeDecl = SimpleType Identifier
    33 data TypeDecl = SimpleType Identifier
    33     | RangeType Range
    34     | RangeType Range
    34     | Sequence [Identifier]
    35     | Sequence [Identifier]
    35     | ArrayDecl (Maybe Range) TypeDecl
    36     | ArrayDecl (Maybe Range) TypeDecl
   171     comments
   172     comments
   172     return vs
   173     return vs
   173     where
   174     where
   174     aConstDecl = do
   175     aConstDecl = do
   175         comments
   176         comments
   176         i <- iD <?> "const declaration"
   177         i <- iD
   177         optional $ do
   178         optional $ do
   178             char ':'
   179             char ':'
   179             comments
   180             comments
   180             t <- typeDecl
   181             t <- typeDecl
   181             comments
   182             comments
   293     
   294     
   294 typeVarDeclaration isImpl = (liftM concat . many . choice) [
   295 typeVarDeclaration isImpl = (liftM concat . many . choice) [
   295     varSection,
   296     varSection,
   296     constSection,
   297     constSection,
   297     typeSection,
   298     typeSection,
   298     funcDecl
   299     funcDecl,
       
   300     operatorDecl
   299     ]
   301     ]
   300     where
   302     where
   301     varSection = do
   303     varSection = do
   302         try $ string "var"
   304         try $ string "var"
   303         comments
   305         comments
   304         v <- varsDecl1 True
   306         v <- varsDecl1 True <?> "variable declaration"
   305         comments
   307         comments
   306         return v
   308         return v
   307 
   309 
   308     constSection = do
   310     constSection = do
   309         try $ string "const"
   311         try $ string "const"
   310         comments
   312         comments
   311         c <- constsDecl
   313         c <- constsDecl <?> "const declaration"
   312         comments
   314         comments
   313         return c
   315         return c
   314 
   316 
   315     typeSection = do
   317     typeSection = do
   316         try $ string "type"
   318         try $ string "type"
   317         comments
   319         comments
   318         t <- typesDecl
   320         t <- typesDecl <?> "type declaration"
   319         comments
   321         comments
   320         return t
   322         return t
       
   323         
       
   324     operatorDecl = do
       
   325         try $ string "operator"
       
   326         comments
       
   327         i <- manyTill anyChar space
       
   328         comments
       
   329         vs <- parens pas $ varsDecl False
       
   330         comments
       
   331         rid <- iD
       
   332         comments
       
   333         char ':'
       
   334         comments
       
   335         ret <- typeDecl
       
   336         comments
       
   337         return ret
       
   338         char ';'
       
   339         comments
       
   340         forward <- liftM isJust $ optionMaybe (try (string "forward;") >> comments)
       
   341         many functionDecorator
       
   342         b <- if isImpl && (not forward) then
       
   343                 liftM Just functionBody
       
   344                 else
       
   345                 return Nothing
       
   346         return $ [OperatorDeclaration i rid ret vs b]
       
   347 
   321         
   348         
   322     funcDecl = do
   349     funcDecl = do
   323         fp <- try (string "function") <|> try (string "procedure")
   350         fp <- try (string "function") <|> try (string "procedure")
   324         comments
   351         comments
   325         i <- iD
   352         i <- iD
   340         b <- if isImpl && (not forward) then
   367         b <- if isImpl && (not forward) then
   341                 liftM Just functionBody
   368                 liftM Just functionBody
   342                 else
   369                 else
   343                 return Nothing
   370                 return Nothing
   344         return $ [FunctionDeclaration i ret vs b]
   371         return $ [FunctionDeclaration i ret vs b]
       
   372         
   345     functionDecorator = choice [
   373     functionDecorator = choice [
   346         try $ string "inline;"
   374         try $ string "inline;"
   347         , try $ string "cdecl;"
   375         , try $ string "cdecl;"
   348         , try (string "external") >> comments >> iD >> optional (string "name" >> comments >> stringLiteral pas)>> string ";"
   376         , try (string "external") >> comments >> iD >> optional (string "name" >> comments >> stringLiteral pas)>> string ";"
   349         ] >> comments
   377         ] >> comments
       
   378         
       
   379         
   350 program = do
   380 program = do
   351     string "program"
   381     string "program"
   352     comments
   382     comments
   353     name <- iD
   383     name <- iD
   354     (char ';')
   384     (char ';')