tools/PascalParser.hs
changeset 6317 83b93a2d2741
parent 6316 ac23ba018ed2
child 6355 734fed7aefd3
equal deleted inserted replaced
6316:ac23ba018ed2 6317:83b93a2d2741
    63     | NumberLiteral String
    63     | NumberLiteral String
    64     | HexNumber String
    64     | HexNumber String
    65     | Reference Reference
    65     | Reference Reference
    66     | Null
    66     | Null
    67     deriving Show
    67     deriving Show
    68 data Reference = ArrayElement Identifier Expression
    68 data Reference = ArrayElement [Expression] Reference
    69     | FunCall [Expression] Reference
    69     | FunCall [Expression] Reference
    70     | SimpleReference Identifier
    70     | SimpleReference Identifier
    71     | Dereference Reference
    71     | Dereference Reference
    72     | RecordField Reference Reference
    72     | RecordField Reference Reference
    73     | Address Reference
    73     | Address Reference
   145     
   145     
   146 reference = buildExpressionParser table term <?> "reference"
   146 reference = buildExpressionParser table term <?> "reference"
   147     where
   147     where
   148     term = comments >> choice [
   148     term = comments >> choice [
   149         parens pas reference 
   149         parens pas reference 
   150         , try $ iD >>= \i -> (brackets pas) expression >>= return . ArrayElement i
       
   151         , char '@' >> reference >>= return . Address
   150         , char '@' >> reference >>= return . Address
   152         , iD >>= return . SimpleReference
   151         , iD >>= return . SimpleReference
   153         ] <?> "simple reference"
   152         ] <?> "simple reference"
   154 
   153 
   155     table = [ 
   154     table = [ 
   156             [Postfix $ (parens pas) (option [] parameters) >>= return . FunCall]
   155             [Postfix $ (parens pas) (option [] parameters) >>= return . FunCall]
   157           , [Postfix (char '^' >> return Dereference)]
   156           , [Postfix (char '^' >> return Dereference)]
       
   157           , [Postfix $ (brackets pas) (commaSep1 pas $ expression) >>= return . ArrayElement]
   158           , [Infix (try (char '.' >> notFollowedBy (char '.')) >> return RecordField) AssocLeft]
   158           , [Infix (try (char '.' >> notFollowedBy (char '.')) >> return RecordField) AssocLeft]
   159         ]
   159         ]
   160 
   160 
   161     
   161     
   162 varsDecl1 = varsParser sepEndBy1    
   162 varsDecl1 = varsParser sepEndBy1    
   188         return (Just e)
   188         return (Just e)
   189     return $ VarDeclaration False (ids, t) init
   189     return $ VarDeclaration False (ids, t) init
   190 
   190 
   191 
   191 
   192 constsDecl = do
   192 constsDecl = do
   193     vs <- many (try (aConstDecl >>= \i -> semi pas >> return i) >>= \i -> comments >> return i)
   193     vs <- many1 (try (aConstDecl >>= \i -> semi pas >> return i) >>= \i -> comments >> return i)
   194     comments
   194     comments
   195     return vs
   195     return vs
   196     where
   196     where
   197     aConstDecl = do
   197     aConstDecl = do
   198         comments
   198         comments
   292         t <- typesDecl
   292         t <- typesDecl
   293         comments
   293         comments
   294         return t
   294         return t
   295         
   295         
   296     procDecl = do
   296     procDecl = do
   297         string "procedure"
   297         try $ string "procedure"
   298         comments
   298         comments
   299         i <- iD
   299         i <- iD
   300         optional $ do
   300         optional $ do
   301             char '('
   301             char '('
   302             varsDecl False
   302             varsDecl False
   313                 return Nothing
   313                 return Nothing
   314         comments
   314         comments
   315         return $ [FunctionDeclaration i UnknownType b]
   315         return $ [FunctionDeclaration i UnknownType b]
   316         
   316         
   317     funcDecl = do
   317     funcDecl = do
   318         string "function"
   318         try $ string "function"
   319         comments
   319         comments
   320         i <- iD
   320         i <- iD
   321         optional $ do
   321         optional $ do
   322             char '('
   322             char '('
   323             varsDecl False
   323             varsDecl False