tools/PascalParser.hs
changeset 6310 31145a87811a
parent 6307 25cfd9f4a567
child 6315 1f7a7a330c59
equal deleted inserted replaced
6309:82e846e5d502 6310:31145a87811a
    63     | CharCode String
    63     | CharCode String
    64     | NumberLiteral String
    64     | NumberLiteral String
    65     | HexNumber String
    65     | HexNumber String
    66     | Address Reference
    66     | Address Reference
    67     | Reference Reference
    67     | Reference Reference
       
    68     | Dereference Expression
       
    69     | RecordField Expression Expression
    68     | Null
    70     | Null
    69     deriving Show
    71     deriving Show
    70 data Reference = ArrayElement Identifier Expression
    72 data Reference = ArrayElement Identifier Expression
    71     | SimpleReference Identifier
    73     | SimpleReference Identifier
    72     | RecordField Reference Reference
       
    73     | Dereference Reference
       
    74     deriving Show
    74     deriving Show
    75     
    75     
    76 pascalLanguageDef
    76 pascalLanguageDef
    77     = emptyDef
    77     = emptyDef
    78     { commentStart   = "(*"
    78     { commentStart   = "(*"
   150         , try $ iD >>= \i -> (brackets pas) expression >>= return . ArrayElement i
   150         , try $ iD >>= \i -> (brackets pas) expression >>= return . ArrayElement i
   151         , iD >>= return . SimpleReference
   151         , iD >>= return . SimpleReference
   152         ] <?> "simple reference"
   152         ] <?> "simple reference"
   153 
   153 
   154     table = [ 
   154     table = [ 
   155         [Postfix (char '^' >> return Dereference)]
       
   156         , [Infix (char '.' >> return RecordField) AssocLeft]
       
   157         ]
   155         ]
   158     
   156     
   159 varsDecl1 = varsParser sepEndBy1    
   157 varsDecl1 = varsParser sepEndBy1    
   160 varsDecl = varsParser sepEndBy
   158 varsDecl = varsParser sepEndBy
   161 varsParser m endsWithSemi = do
   159 varsParser m endsWithSemi = do
   209     char '^' >> typeDecl >>= return . PointerTo
   207     char '^' >> typeDecl >>= return . PointerTo
   210     , try (string "shortstring") >> return String
   208     , try (string "shortstring") >> return String
   211     , arrayDecl
   209     , arrayDecl
   212     , recordDecl
   210     , recordDecl
   213     , rangeDecl >>= return . RangeType
   211     , rangeDecl >>= return . RangeType
   214     , seqenceDecl >>= return . Sequence
   212     , sequenceDecl >>= return . Sequence
   215     , identifier pas >>= return . SimpleType . Identifier
   213     , identifier pas >>= return . SimpleType . Identifier
   216     ] <?> "type declaration"
   214     ] <?> "type declaration"
   217     where
   215     where
   218     arrayDecl = do
   216     arrayDecl = do
   219         try $ string "array"
   217         try $ string "array"
   231         try $ string "record"
   229         try $ string "record"
   232         comments
   230         comments
   233         vs <- varsDecl True
   231         vs <- varsDecl True
   234         string "end"
   232         string "end"
   235         return $ RecordType vs
   233         return $ RecordType vs
   236     seqenceDecl = (parens pas) $ (commaSep pas) iD
   234     sequenceDecl = (parens pas) $ (commaSep pas) iD
   237 
   235 
   238 typesDecl = many (aTypeDecl >>= \t -> comments >> return t)
   236 typesDecl = many (aTypeDecl >>= \t -> comments >> return t)
   239     where
   237     where
   240     aTypeDecl = do
   238     aTypeDecl = do
   241         i <- try $ do
   239         i <- try $ do
   401           ]
   399           ]
   402         , [  Infix (try $ string "shl" >> return (BinOp "and")) AssocNone
   400         , [  Infix (try $ string "shl" >> return (BinOp "and")) AssocNone
   403            , Infix (try $ string "shr" >> return (BinOp "or")) AssocNone
   401            , Infix (try $ string "shr" >> return (BinOp "or")) AssocNone
   404           ]
   402           ]
   405         , [Prefix (try (string "not") >> return (PrefixOp "not"))]
   403         , [Prefix (try (string "not") >> return (PrefixOp "not"))]
       
   404         , [Postfix (char '^' >> return Dereference)]
       
   405         , [Infix (try (char '.' >> notFollowedBy (char '.')) >> return RecordField) AssocLeft]
   406         ]
   406         ]
   407     
   407     
   408 phrasesBlock = do
   408 phrasesBlock = do
   409     try $ string "begin"
   409     try $ string "begin"
   410     comments
   410     comments