tools/PascalParser.hs
changeset 6388 14718b2685a3
parent 6387 3dcb839b5904
child 6391 bd5851ab3157
equal deleted inserted replaced
6387:3dcb839b5904 6388:14718b2685a3
    34     | PointerTo TypeDecl
    34     | PointerTo TypeDecl
    35     | String
    35     | String
    36     | UnknownType
    36     | UnknownType
    37     deriving Show
    37     deriving Show
    38 data Range = Range Identifier
    38 data Range = Range Identifier
    39            | RangeFromTo Expression Expression
    39            | RangeFromTo InitExpression InitExpression
    40     deriving Show
    40     deriving Show
    41 data Initialize = Initialize String
    41 data Initialize = Initialize String
    42     deriving Show
    42     deriving Show
    43 data Finalize = Finalize String
    43 data Finalize = Finalize String
    44     deriving Show
    44     deriving Show
    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     | BuiltInFunCall [Expression] Reference
    58     | PrefixOp String Expression
    59     | PrefixOp String Expression
    59     | PostfixOp String Expression
    60     | PostfixOp String Expression
    60     | BinOp String Expression Expression
    61     | BinOp String Expression Expression
    61     | StringLiteral String
    62     | StringLiteral String
    62     | CharCode String
    63     | CharCode String
    66     | Reference Reference
    67     | Reference Reference
    67     | Null
    68     | Null
    68     deriving Show
    69     deriving Show
    69 data Reference = ArrayElement [Expression] Reference
    70 data Reference = ArrayElement [Expression] Reference
    70     | FunCall [Expression] Reference
    71     | FunCall [Expression] Reference
    71     | BuiltInFunCall [Expression] Reference
       
    72     | SimpleReference Identifier
    72     | SimpleReference Identifier
    73     | Dereference Reference
    73     | Dereference Reference
    74     | RecordField Reference Reference
    74     | RecordField Reference Reference
    75     | Address Reference
    75     | Address Reference
    76     deriving Show
    76     deriving Show
    82     | InitFloat String
    82     | InitFloat String
    83     | InitNumber String
    83     | InitNumber String
    84     | InitHexNumber String
    84     | InitHexNumber String
    85     | InitString String
    85     | InitString String
    86     | InitChar String
    86     | InitChar String
       
    87     | BuiltInFunction String [InitExpression]
    87     | InitNull
    88     | InitNull
    88     deriving Show
    89     deriving Show
    89 
    90 
       
    91 builtin = ["succ", "pred", "low", "high"]
    90     
    92     
    91 pascalLanguageDef
    93 pascalLanguageDef
    92     = emptyDef
    94     = emptyDef
    93     { commentStart   = "(*"
    95     { commentStart   = "(*"
    94     , commentEnd     = "*)"
    96     , commentEnd     = "*)"
   101             , "implementation", "and", "or", "xor", "shl"
   103             , "implementation", "and", "or", "xor", "shl"
   102             , "shr", "while", "do", "repeat", "until", "case", "of"
   104             , "shr", "while", "do", "repeat", "until", "case", "of"
   103             , "type", "var", "const", "out", "array", "packed"
   105             , "type", "var", "const", "out", "array", "packed"
   104             , "procedure", "function", "with", "for", "to"
   106             , "procedure", "function", "with", "for", "to"
   105             , "downto", "div", "mod", "record", "set", "nil"
   107             , "downto", "div", "mod", "record", "set", "nil"
   106             , "string", "shortstring"--, "succ", "pred", "low"
   108             , "string", "shortstring"
   107             --, "high"
   109             ] ++ builtin
   108             ]
       
   109     , reservedOpNames= [] 
   110     , reservedOpNames= [] 
   110     , caseSensitive  = False   
   111     , caseSensitive  = False   
   111     }
   112     }
       
   113     
       
   114 caseInsensitiveString s = do
       
   115     mapM_ (\a -> satisfy (\b -> toUpper a == toUpper b)) s <?> s
       
   116     return s
   112     
   117     
   113 pas = patch $ makeTokenParser pascalLanguageDef
   118 pas = patch $ makeTokenParser pascalLanguageDef
   114     where
   119     where
   115     patch tp = tp {stringLiteral = sl}
   120     patch tp = tp {stringLiteral = sl}
   116     sl = do
   121     sl = do
   278     try $ rangeft
   283     try $ rangeft
   279     , iD >>= return . Range
   284     , iD >>= return . Range
   280     ] <?> "range declaration"
   285     ] <?> "range declaration"
   281     where
   286     where
   282     rangeft = do
   287     rangeft = do
   283     e1 <- expression
   288     e1 <- initExpression
   284     string ".."
   289     string ".."
   285     e2 <- expression
   290     e2 <- initExpression
   286     return $ RangeFromTo e1 e2
   291     return $ RangeFromTo e1 e2
   287     
   292     
   288 typeVarDeclaration isImpl = (liftM concat . many . choice) [
   293 typeVarDeclaration isImpl = (liftM concat . many . choice) [
   289     varSection,
   294     varSection,
   290     constSection,
   295     constSection,
   389     return $ Implementation u (TypesAndVars tv)
   394     return $ Implementation u (TypesAndVars tv)
   390 
   395 
   391 expression = buildExpressionParser table term <?> "expression"
   396 expression = buildExpressionParser table term <?> "expression"
   392     where
   397     where
   393     term = comments >> choice [
   398     term = comments >> choice [
   394         parens pas $ expression 
   399         builtInFunction expression >>= \(n, e) -> return $ BuiltInFunCall e (SimpleReference (Identifier n))
       
   400         , parens pas $ expression 
   395         , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . NumberLiteral . show) i
   401         , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . NumberLiteral . show) i
   396         , try $ float pas >>= return . FloatLiteral . show
   402         , try $ float pas >>= return . FloatLiteral . show
   397         , try $ integer pas >>= return . NumberLiteral . show
   403         , try $ integer pas >>= return . NumberLiteral . show
   398         , stringLiteral pas >>= return . StringLiteral
   404         , stringLiteral pas >>= return . StringLiteral
   399         , char '#' >> many digit >>= return . CharCode
   405         , char '#' >> many digit >>= return . CharCode
   568             return u
   574             return u
   569 
   575 
   570 initExpression = buildExpressionParser table term <?> "initialization expression"
   576 initExpression = buildExpressionParser table term <?> "initialization expression"
   571     where
   577     where
   572     term = comments >> choice [
   578     term = comments >> choice [
   573         try $ parens pas (commaSep pas $ initExpression) >>= return . InitArray
   579         liftM (uncurry BuiltInFunction) $ builtInFunction initExpression 
       
   580         , try $ parens pas (commaSep pas $ initExpression) >>= return . InitArray
   574         , parens pas (semiSep pas $ recField) >>= return . InitRecord
   581         , parens pas (semiSep pas $ recField) >>= return . InitRecord
   575         , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . InitNumber . show) i
   582         , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . InitNumber . show) i
   576         , try $ float pas >>= return . InitFloat . show
   583         , try $ float pas >>= return . InitFloat . show
       
   584         , try $ integer pas >>= return . InitNumber . show
   577         , stringLiteral pas >>= return . InitString
   585         , stringLiteral pas >>= return . InitString
   578         , char '#' >> many digit >>= return . InitChar
   586         , char '#' >> many digit >>= return . InitChar
   579         , char '$' >> many hexDigit >>= return . InitHexNumber
   587         , char '$' >> many hexDigit >>= return . InitHexNumber
   580         , try $ string "nil" >> return InitNull
   588         , try $ string "nil" >> return InitNull
   581         , iD >>= return . InitReference
   589         , iD >>= return . InitReference
   614         , [  Infix (try $ string "shl" >> return (InitBinOp "and")) AssocNone
   622         , [  Infix (try $ string "shl" >> return (InitBinOp "and")) AssocNone
   615            , Infix (try $ string "shr" >> return (InitBinOp "or")) AssocNone
   623            , Infix (try $ string "shr" >> return (InitBinOp "or")) AssocNone
   616           ]
   624           ]
   617         , [Prefix (try (string "not") >> return (InitPrefixOp "not"))]
   625         , [Prefix (try (string "not") >> return (InitPrefixOp "not"))]
   618         ]
   626         ]
   619     
   627 
       
   628 builtInFunction e = do
       
   629     name <- choice $ map (\s -> try $ caseInsensitiveString s >>= \i -> notFollowedBy alphaNum >> return i) builtin
       
   630     spaces
       
   631     exprs <- many1 e
       
   632     spaces
       
   633     return (name, exprs)