tools/PascalParser.hs
changeset 6315 1f7a7a330c59
parent 6310 31145a87811a
child 6316 ac23ba018ed2
equal deleted inserted replaced
6314:b4fc5b863f5f 6315:1f7a7a330c59
    53         | Phrases [Phrase]
    53         | Phrases [Phrase]
    54         | SwitchCase Expression [(Expression, Phrase)] (Maybe Phrase)
    54         | SwitchCase Expression [(Expression, Phrase)] (Maybe Phrase)
    55         | Assignment Reference Expression
    55         | Assignment Reference Expression
    56     deriving Show
    56     deriving Show
    57 data Expression = Expression String
    57 data Expression = Expression String
    58     | FunCall Reference [Expression]
       
    59     | PrefixOp String Expression
    58     | PrefixOp String Expression
    60     | PostfixOp String Expression
    59     | PostfixOp String Expression
    61     | BinOp String Expression Expression
    60     | BinOp String Expression Expression
    62     | StringLiteral String
    61     | StringLiteral String
    63     | CharCode String
    62     | CharCode String
    64     | NumberLiteral String
    63     | NumberLiteral String
    65     | HexNumber String
    64     | HexNumber String
       
    65     | Reference Reference
       
    66     | Null
       
    67     deriving Show
       
    68 data Reference = ArrayElement Identifier Expression
       
    69     | FunCall Reference [Expression]
       
    70     | SimpleReference Identifier
       
    71     | Dereference Reference
       
    72     | RecordField Reference Reference
    66     | Address Reference
    73     | Address Reference
    67     | Reference Reference
       
    68     | Dereference Expression
       
    69     | RecordField Expression Expression
       
    70     | Null
       
    71     deriving Show
       
    72 data Reference = ArrayElement Identifier Expression
       
    73     | SimpleReference Identifier
       
    74     deriving Show
    74     deriving Show
    75     
    75     
    76 pascalLanguageDef
    76 pascalLanguageDef
    77     = emptyDef
    77     = emptyDef
    78     { commentStart   = "(*"
    78     { commentStart   = "(*"
   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
   150         , try $ iD >>= \i -> (brackets pas) expression >>= return . ArrayElement i
       
   151         , try $ funCall
       
   152         , try $ reference >>= \r -> char '^' >> return (Dereference r)
       
   153         , char '@' >> reference >>= return . Address
   151         , iD >>= return . SimpleReference
   154         , iD >>= return . SimpleReference
   152         ] <?> "simple reference"
   155         ] <?> "simple reference"
   153 
   156 
   154     table = [ 
   157     table = [ 
       
   158           [Infix (try (char '.' >> notFollowedBy (char '.')) >> return RecordField) AssocLeft]
   155         ]
   159         ]
   156     
   160     
   157 varsDecl1 = varsParser sepEndBy1    
   161 varsDecl1 = varsParser sepEndBy1    
   158 varsDecl = varsParser sepEndBy
   162 varsDecl = varsParser sepEndBy
   159 varsParser m endsWithSemi = do
   163 varsParser m endsWithSemi = do
   368         parens pas $ expression 
   372         parens pas $ expression 
   369         , try $ integer pas >>= return . NumberLiteral . show
   373         , try $ integer pas >>= return . NumberLiteral . show
   370         , stringLiteral pas >>= return . StringLiteral
   374         , stringLiteral pas >>= return . StringLiteral
   371         , char '#' >> many digit >>= return . CharCode
   375         , char '#' >> many digit >>= return . CharCode
   372         , char '$' >> many hexDigit >>= return . HexNumber
   376         , char '$' >> many hexDigit >>= return . HexNumber
   373         , char '@' >> reference >>= return . Address
       
   374         , try $ string "nil" >> return Null
   377         , try $ string "nil" >> return Null
   375         , try $ funCall
       
   376         , reference >>= return . Reference
   378         , reference >>= return . Reference
   377         ] <?> "simple expression"
   379         ] <?> "simple expression"
   378 
   380 
   379     table = [ 
   381     table = [ 
   380           [  Infix (char '*' >> return (BinOp "*")) AssocLeft
   382           [  Infix (char '*' >> return (BinOp "*")) AssocLeft
   399           ]
   401           ]
   400         , [  Infix (try $ string "shl" >> return (BinOp "and")) AssocNone
   402         , [  Infix (try $ string "shl" >> return (BinOp "and")) AssocNone
   401            , Infix (try $ string "shr" >> return (BinOp "or")) AssocNone
   403            , Infix (try $ string "shr" >> return (BinOp "or")) AssocNone
   402           ]
   404           ]
   403         , [Prefix (try (string "not") >> return (PrefixOp "not"))]
   405         , [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