tools/PascalParser.hs
author koda
Fri, 26 Oct 2012 14:19:57 +0100
branch0.9.17
changeset 6410 fd08e4052360
parent 6397 6eb58ae8b510
child 6399 a904c735979c
permissions -rw-r--r--
close branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     1
module PascalParser where
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     2
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     3
import Text.Parsec.Expr
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     4
import Text.Parsec.Char
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     5
import Text.Parsec.Token
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     6
import Text.Parsec.Language
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     7
import Text.Parsec.Prim
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     8
import Text.Parsec.Combinator
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     9
import Text.Parsec.String
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    10
import Control.Monad
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    11
import Data.Char
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    12
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    13
data PascalUnit =
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    14
    Program Identifier Implementation
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    15
    | Unit Identifier Interface Implementation (Maybe Initialize) (Maybe Finalize)
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    16
    deriving Show
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    17
data Interface = Interface Uses TypesAndVars
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    18
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    19
data Implementation = Implementation Uses TypesAndVars
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    20
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    21
data Identifier = Identifier String
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    22
    deriving Show
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    23
data TypesAndVars = TypesAndVars [TypeVarDeclaration]
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    24
    deriving Show
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    25
data TypeVarDeclaration = TypeDeclaration Identifier TypeDecl
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    26
    | VarDeclaration Bool ([Identifier], TypeDecl) (Maybe InitExpression)
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
    27
    | FunctionDeclaration Identifier TypeDecl (Maybe Phrase)
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    28
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    29
data TypeDecl = SimpleType Identifier
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    30
    | RangeType Range
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    31
    | Sequence [Identifier]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    32
    | ArrayDecl Range TypeDecl
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    33
    | RecordType [TypeVarDeclaration]
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
    34
    | PointerTo TypeDecl
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
    35
    | String
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    36
    | UnknownType
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    37
    deriving Show
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    38
data Range = Range Identifier
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
    39
           | RangeFromTo InitExpression InitExpression
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    40
    deriving Show
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    41
data Initialize = Initialize String
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    42
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    43
data Finalize = Finalize String
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    44
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    45
data Uses = Uses [Identifier]
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    46
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    47
data Phrase = ProcCall Identifier [Expression]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    48
        | IfThenElse Expression Phrase (Maybe Phrase)
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    49
        | WhileCycle Expression Phrase
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    50
        | RepeatCycle Expression [Phrase]
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    51
        | ForCycle Identifier Expression Expression Phrase
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    52
        | WithBlock Reference Phrase
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    53
        | Phrases [Phrase]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    54
        | SwitchCase Expression [(Expression, Phrase)] (Maybe Phrase)
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    55
        | Assignment Reference Expression
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    56
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    57
data Expression = Expression String
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
    58
    | BuiltInFunCall [Expression] Reference
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    59
    | PrefixOp String Expression
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    60
    | PostfixOp String Expression
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    61
    | BinOp String Expression Expression
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    62
    | StringLiteral String
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    63
    | CharCode String
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    64
    | NumberLiteral String
6357
52cb4807a78c Something
unc0rr
parents: 6355
diff changeset
    65
    | FloatLiteral String
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    66
    | HexNumber String
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    67
    | Reference Reference
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
    68
    | Null
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    69
    deriving Show
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
    70
data Reference = ArrayElement [Expression] Reference
6316
ac23ba018ed2 Fix inifinite loops
unc0rr
parents: 6315
diff changeset
    71
    | FunCall [Expression] Reference
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    72
    | SimpleReference Identifier
6315
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
    73
    | Dereference Reference
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
    74
    | RecordField Reference Reference
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
    75
    | Address Reference
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    76
    deriving Show
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    77
data InitExpression = InitBinOp String InitExpression InitExpression
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    78
    | InitPrefixOp String InitExpression
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    79
    | InitReference Identifier
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    80
    | InitArray [InitExpression]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    81
    | InitRecord [(Identifier, InitExpression)]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    82
    | InitFloat String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    83
    | InitNumber String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    84
    | InitHexNumber String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    85
    | InitString String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    86
    | InitChar String
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
    87
    | BuiltInFunction String [InitExpression]
6391
bd5851ab3157 - Parse sets initialization
unc0rr
parents: 6388
diff changeset
    88
    | InitSet [Identifier]
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    89
    | InitNull
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    90
    deriving Show
6357
52cb4807a78c Something
unc0rr
parents: 6355
diff changeset
    91
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
    92
builtin = ["succ", "pred", "low", "high"]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    93
    
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    94
pascalLanguageDef
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    95
    = emptyDef
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    96
    { commentStart   = "(*"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    97
    , commentEnd     = "*)"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    98
    , commentLine    = "//"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    99
    , nestedComments = False
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   100
    , identStart     = letter <|> oneOf "_"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   101
    , identLetter    = alphaNum <|> oneOf "_."
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   102
    , reservedNames  = [
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   103
            "begin", "end", "program", "unit", "interface"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   104
            , "implementation", "and", "or", "xor", "shl"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   105
            , "shr", "while", "do", "repeat", "until", "case", "of"
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   106
            , "type", "var", "const", "out", "array", "packed"
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   107
            , "procedure", "function", "with", "for", "to"
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   108
            , "downto", "div", "mod", "record", "set", "nil"
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   109
            , "string", "shortstring"
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   110
            ] ++ builtin
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   111
    , reservedOpNames= [] 
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   112
    , caseSensitive  = False   
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   113
    }
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   114
    
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   115
caseInsensitiveString s = do
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   116
    mapM_ (\a -> satisfy (\b -> toUpper a == toUpper b)) s <?> s
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   117
    return s
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   118
    
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   119
pas = patch $ makeTokenParser pascalLanguageDef
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   120
    where
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   121
    patch tp = tp {stringLiteral = sl}
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   122
    sl = do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   123
        (char '\'')
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   124
        s <- (many $ noneOf "'")
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   125
        (char '\'')
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   126
        ss <- many $ do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   127
            (char '\'')
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   128
            s' <- (many $ noneOf "'")
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   129
            (char '\'')
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   130
            return $ '\'' : s'
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   131
        comments    
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   132
        return $ concat (s:ss)
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   133
    
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   134
comments = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   135
    spaces
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   136
    skipMany $ do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   137
        comment
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   138
        spaces
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   139
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   140
pascalUnit = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   141
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   142
    u <- choice [program, unit]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   143
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   144
    return u
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   145
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   146
comment = choice [
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   147
        char '{' >> manyTill anyChar (try $ char '}')
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   148
        , (try $ string "(*") >> manyTill anyChar (try $ string "*)")
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   149
        , (try $ string "//") >> manyTill anyChar (try newline)
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   150
        ]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   151
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   152
iD = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   153
    i <- liftM Identifier (identifier pas)
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   154
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   155
    return i
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   156
        
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   157
unit = do
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   158
    string "unit" >> comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   159
    name <- iD
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   160
    semi pas
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   161
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   162
    int <- interface
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   163
    impl <- implementation
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   164
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   165
    return $ Unit name int impl Nothing Nothing
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   166
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   167
    
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   168
reference = buildExpressionParser table term <?> "reference"
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   169
    where
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   170
    term = comments >> choice [
6386
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   171
        parens pas (reference >>= postfixes) >>= postfixes
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   172
        , char '@' >> reference >>= postfixes >>= return . Address
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   173
        , liftM SimpleReference iD >>= postfixes 
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   174
        ] <?> "simple reference"
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   175
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   176
    table = [ 
6386
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   177
            [Infix (try (char '.' >> notFollowedBy (char '.')) >> return RecordField) AssocLeft]
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   178
        ]
6386
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   179
    
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   180
    postfixes r = many postfix >>= return . foldl fp r
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   181
    postfix = choice [
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   182
            parens pas (option [] parameters) >>= return . FunCall
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   183
          , char '^' >> return Dereference
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   184
          , (brackets pas) (commaSep1 pas $ expression) >>= return . ArrayElement
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   185
        ]
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   186
    fp r f = f r
6316
ac23ba018ed2 Fix inifinite loops
unc0rr
parents: 6315
diff changeset
   187
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   188
    
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   189
varsDecl1 = varsParser sepEndBy1    
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   190
varsDecl = varsParser sepEndBy
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   191
varsParser m endsWithSemi = do
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   192
    vs <- m (aVarDecl endsWithSemi) (semi pas)
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   193
    return vs
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   194
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   195
aVarDecl endsWithSemi = do
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   196
    when (not endsWithSemi) $
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   197
        optional $ choice [
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   198
            try $ string "var"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   199
            , try $ string "const"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   200
            , try $ string "out"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   201
            ]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   202
    comments
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   203
    ids <- do
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   204
        i <- (commaSep1 pas) $ (try iD <?> "variable declaration")
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   205
        char ':'
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   206
        return i
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   207
    comments
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   208
    t <- typeDecl <?> "variable type declaration"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   209
    comments
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   210
    init <- option Nothing $ do
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   211
        char '='
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   212
        comments
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   213
        e <- initExpression
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   214
        comments
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   215
        return (Just e)
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   216
    return $ VarDeclaration False (ids, t) init
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   217
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   218
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   219
constsDecl = do
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
   220
    vs <- many1 (try (aConstDecl >>= \i -> semi pas >> return i) >>= \i -> comments >> return i)
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   221
    comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   222
    return vs
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   223
    where
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   224
    aConstDecl = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   225
        comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   226
        i <- iD <?> "const declaration"
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   227
        optional $ do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   228
            char ':'
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   229
            comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   230
            t <- typeDecl
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   231
            return ()
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   232
        char '='
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   233
        comments
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   234
        e <- initExpression
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   235
        comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   236
        return $ VarDeclaration False ([i], UnknownType) (Just e)
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   237
        
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   238
typeDecl = choice [
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   239
    char '^' >> typeDecl >>= return . PointerTo
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   240
    , try (string "shortstring") >> return String
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   241
    , arrayDecl
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   242
    , recordDecl
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   243
    , sequenceDecl >>= return . Sequence
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   244
    , try (identifier pas) >>= return . SimpleType . Identifier
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   245
    , rangeDecl >>= return . RangeType
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   246
    ] <?> "type declaration"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   247
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   248
    arrayDecl = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   249
        try $ string "array"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   250
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   251
        char '['
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   252
        r <- rangeDecl
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   253
        char ']'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   254
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   255
        string "of"
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   256
        comments
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   257
        t <- typeDecl
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   258
        return $ ArrayDecl r t
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   259
    recordDecl = do
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   260
        optional $ (try $ string "packed") >> comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   261
        try $ string "record"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   262
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   263
        vs <- varsDecl True
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   264
        string "end"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   265
        return $ RecordType vs
6310
31145a87811a Improve type declarations parsing
unc0rr
parents: 6307
diff changeset
   266
    sequenceDecl = (parens pas) $ (commaSep pas) iD
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   267
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   268
typesDecl = many (aTypeDecl >>= \t -> comments >> return t)
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   269
    where
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   270
    aTypeDecl = do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   271
        i <- try $ do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   272
            i <- iD <?> "type declaration"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   273
            comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   274
            char '='
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   275
            return i
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   276
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   277
        t <- typeDecl
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   278
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   279
        semi pas
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   280
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   281
        return $ TypeDeclaration i t
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   282
        
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   283
rangeDecl = choice [
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   284
    try $ rangeft
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   285
    , iD >>= return . Range
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   286
    ] <?> "range declaration"
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   287
    where
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   288
    rangeft = do
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   289
    e1 <- initExpression
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   290
    string ".."
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   291
    e2 <- initExpression
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   292
    return $ RangeFromTo e1 e2
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   293
    
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   294
typeVarDeclaration isImpl = (liftM concat . many . choice) [
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   295
    varSection,
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   296
    constSection,
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   297
    typeSection,
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   298
    funcDecl,
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   299
    procDecl
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   300
    ]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   301
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   302
    varSection = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   303
        try $ string "var"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   304
        comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   305
        v <- varsDecl1 True
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   306
        comments
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   307
        return v
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   308
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   309
    constSection = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   310
        try $ string "const"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   311
        comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   312
        c <- constsDecl
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   313
        comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   314
        return c
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   315
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   316
    typeSection = do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   317
        try $ string "type"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   318
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   319
        t <- typesDecl
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   320
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   321
        return t
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   322
        
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   323
    procDecl = do
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
   324
        try $ string "procedure"
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   325
        comments
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   326
        i <- iD
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   327
        optional $ do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   328
            char '('
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   329
            varsDecl False
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   330
            char ')'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   331
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   332
        char ';'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   333
        b <- if isImpl then
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   334
                do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   335
                comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   336
                optional $ typeVarDeclaration True
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   337
                comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   338
                liftM Just functionBody
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   339
                else
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   340
                return Nothing
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   341
        comments
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   342
        return $ [FunctionDeclaration i UnknownType b]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   343
        
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   344
    funcDecl = do
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
   345
        try $ string "function"
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   346
        comments
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   347
        i <- iD
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   348
        optional $ do
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   349
            char '('
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   350
            varsDecl False
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   351
            char ')'
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   352
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   353
        char ':'
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   354
        comments
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   355
        ret <- typeDecl
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   356
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   357
        char ';'
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   358
        comments
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   359
        b <- if isImpl then
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   360
                do
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   361
                optional $ typeVarDeclaration True
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   362
                comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   363
                liftM Just functionBody
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   364
                else
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   365
                return Nothing
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   366
        return $ [FunctionDeclaration i ret b]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   367
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   368
program = do
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   369
    string "program"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   370
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   371
    name <- iD
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   372
    (char ';')
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   373
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   374
    impl <- implementation
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   375
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   376
    return $ Program name impl
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   377
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   378
interface = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   379
    string "interface"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   380
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   381
    u <- uses
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   382
    comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   383
    tv <- typeVarDeclaration False
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   384
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   385
    return $ Interface u (TypesAndVars tv)
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   386
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   387
implementation = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   388
    string "implementation"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   389
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   390
    u <- uses
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   391
    comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   392
    tv <- typeVarDeclaration True
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   393
    string "end."
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   394
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   395
    return $ Implementation u (TypesAndVars tv)
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   396
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   397
expression = buildExpressionParser table term <?> "expression"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   398
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   399
    term = comments >> choice [
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   400
        builtInFunction expression >>= \(n, e) -> return $ BuiltInFunCall e (SimpleReference (Identifier n))
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   401
        , parens pas $ expression 
6357
52cb4807a78c Something
unc0rr
parents: 6355
diff changeset
   402
        , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . NumberLiteral . show) i
52cb4807a78c Something
unc0rr
parents: 6355
diff changeset
   403
        , try $ float pas >>= return . FloatLiteral . show
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   404
        , try $ integer pas >>= return . NumberLiteral . show
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   405
        , stringLiteral pas >>= return . StringLiteral
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   406
        , char '#' >> many digit >>= return . CharCode
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   407
        , char '$' >> many hexDigit >>= return . HexNumber
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   408
        , try $ string "nil" >> return Null
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   409
        , reference >>= return . Reference
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   410
        ] <?> "simple expression"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   411
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   412
    table = [ 
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   413
          [  Infix (char '*' >> return (BinOp "*")) AssocLeft
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   414
           , Infix (char '/' >> return (BinOp "/")) AssocLeft
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   415
           , Infix (try (string "div") >> return (BinOp "div")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   416
           , Infix (try (string "mod") >> return (BinOp "mod")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   417
          ]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   418
        , [  Infix (char '+' >> return (BinOp "+")) AssocLeft
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   419
           , Infix (char '-' >> return (BinOp "-")) AssocLeft
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   420
           , Prefix (char '-' >> return (PrefixOp "-"))
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   421
          ]
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   422
        , [  Infix (try (string "<>") >> return (BinOp "<>")) AssocNone
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   423
           , Infix (try (string "<=") >> return (BinOp "<=")) AssocNone
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   424
           , Infix (try (string ">=") >> return (BinOp ">=")) AssocNone
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   425
           , Infix (char '<' >> return (BinOp "<")) AssocNone
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   426
           , Infix (char '>' >> return (BinOp ">")) AssocNone
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   427
           , Infix (char '=' >> return (BinOp "=")) AssocNone
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   428
          ]
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   429
        , [  Infix (try $ string "and" >> return (BinOp "and")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   430
           , Infix (try $ string "or" >> return (BinOp "or")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   431
           , Infix (try $ string "xor" >> return (BinOp "xor")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   432
          ]
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   433
        , [  Infix (try $ string "shl" >> return (BinOp "shl")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   434
           , Infix (try $ string "shr" >> return (BinOp "shr")) AssocNone
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   435
          ]
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   436
        , [Prefix (try (string "not") >> return (PrefixOp "not"))]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   437
        ]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   438
    
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   439
phrasesBlock = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   440
    try $ string "begin"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   441
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   442
    p <- manyTill phrase (try $ string "end")
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   443
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   444
    return $ Phrases p
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   445
    
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   446
phrase = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   447
    o <- choice [
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   448
        phrasesBlock
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   449
        , ifBlock
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   450
        , whileCycle
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   451
        , repeatCycle
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   452
        , switchCase
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   453
        , withBlock
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   454
        , forCycle
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   455
        , (try $ reference >>= \r -> string ":=" >> return r) >>= \r -> expression >>= return . Assignment r
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   456
        , procCall
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   457
        ]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   458
    optional $ char ';'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   459
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   460
    return o
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   461
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   462
ifBlock = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   463
    try $ string "if"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   464
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   465
    e <- expression
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   466
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   467
    string "then"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   468
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   469
    o1 <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   470
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   471
    o2 <- optionMaybe $ do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   472
        try $ string "else"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   473
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   474
        o <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   475
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   476
        return o
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   477
    return $ IfThenElse e o1 o2
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   478
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   479
whileCycle = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   480
    try $ string "while"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   481
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   482
    e <- expression
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   483
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   484
    string "do"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   485
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   486
    o <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   487
    return $ WhileCycle e o
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   488
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   489
withBlock = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   490
    try $ string "with"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   491
    comments
6387
3dcb839b5904 Less code
unc0rr
parents: 6386
diff changeset
   492
    rs <- (commaSep1 pas) reference
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   493
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   494
    string "do"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   495
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   496
    o <- phrase
6387
3dcb839b5904 Less code
unc0rr
parents: 6386
diff changeset
   497
    return $ foldr WithBlock o rs
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   498
    
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   499
repeatCycle = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   500
    try $ string "repeat"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   501
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   502
    o <- many phrase
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   503
    string "until"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   504
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   505
    e <- expression
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   506
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   507
    return $ RepeatCycle e o
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   508
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   509
forCycle = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   510
    try $ string "for"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   511
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   512
    i <- iD
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   513
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   514
    string ":="
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   515
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   516
    e1 <- expression
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   517
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   518
    choice [string "to", string "downto"]
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   519
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   520
    e2 <- expression
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   521
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   522
    string "do"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   523
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   524
    p <- phrase
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   525
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   526
    return $ ForCycle i e1 e2 p
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   527
    
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   528
switchCase = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   529
    try $ string "case"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   530
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   531
    e <- expression
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   532
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   533
    string "of"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   534
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   535
    cs <- many1 aCase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   536
    o2 <- optionMaybe $ do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   537
        try $ string "else"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   538
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   539
        o <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   540
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   541
        return o
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   542
    string "end"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   543
    return $ SwitchCase e cs o2
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   544
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   545
    aCase = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   546
        e <- expression
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   547
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   548
        char ':'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   549
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   550
        p <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   551
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   552
        return (e, p)
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   553
    
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   554
procCall = do
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   555
    i <- iD
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   556
    p <- option [] $ (parens pas) parameters
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   557
    return $ ProcCall i p
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   558
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   559
parameters = (commaSep pas) expression <?> "parameters"
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   560
        
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   561
functionBody = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   562
    p <- phrasesBlock
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   563
    char ';'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   564
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   565
    return p
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   566
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   567
uses = liftM Uses (option [] u)
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   568
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   569
        u = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   570
            string "uses"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   571
            comments
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   572
            u <- (iD >>= \i -> comments >> return i) `sepBy1` (char ',' >> comments)
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   573
            char ';'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   574
            comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   575
            return u
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   576
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   577
initExpression = buildExpressionParser table term <?> "initialization expression"
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   578
    where
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   579
    term = comments >> choice [
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   580
        liftM (uncurry BuiltInFunction) $ builtInFunction initExpression 
6391
bd5851ab3157 - Parse sets initialization
unc0rr
parents: 6388
diff changeset
   581
        , try $ brackets pas (commaSep pas $ iD) >>= return . InitSet
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   582
        , try $ parens pas (commaSep pas $ initExpression) >>= return . InitArray
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   583
        , parens pas (semiSep pas $ recField) >>= return . InitRecord
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   584
        , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . InitNumber . show) i
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   585
        , try $ float pas >>= return . InitFloat . show
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   586
        , try $ integer pas >>= return . InitNumber . show
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   587
        , stringLiteral pas >>= return . InitString
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   588
        , char '#' >> many digit >>= return . InitChar
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   589
        , char '$' >> many hexDigit >>= return . InitHexNumber
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   590
        , try $ string "nil" >> return InitNull
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   591
        , iD >>= return . InitReference
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   592
        ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   593
        
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   594
    recField = do
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   595
        i <- iD
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   596
        spaces
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   597
        char ':'
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   598
        spaces
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   599
        e <- initExpression
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   600
        spaces
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   601
        return (i ,e)
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   602
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   603
    table = [ 
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   604
          [  Infix (char '*' >> return (InitBinOp "*")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   605
           , Infix (char '/' >> return (InitBinOp "/")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   606
           , Infix (try (string "div") >> return (InitBinOp "div")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   607
           , Infix (try (string "mod") >> return (InitBinOp "mod")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   608
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   609
        , [  Infix (char '+' >> return (InitBinOp "+")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   610
           , Infix (char '-' >> return (InitBinOp "-")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   611
           , Prefix (char '-' >> return (InitPrefixOp "-"))
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   612
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   613
        , [  Infix (try (string "<>") >> return (InitBinOp "<>")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   614
           , Infix (try (string "<=") >> return (InitBinOp "<=")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   615
           , Infix (try (string ">=") >> return (InitBinOp ">=")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   616
           , Infix (char '<' >> return (InitBinOp "<")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   617
           , Infix (char '>' >> return (InitBinOp ">")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   618
           , Infix (char '=' >> return (InitBinOp "=")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   619
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   620
        , [  Infix (try $ string "and" >> return (InitBinOp "and")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   621
           , Infix (try $ string "or" >> return (InitBinOp "or")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   622
           , Infix (try $ string "xor" >> return (InitBinOp "xor")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   623
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   624
        , [  Infix (try $ string "shl" >> return (InitBinOp "and")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   625
           , Infix (try $ string "shr" >> return (InitBinOp "or")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   626
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   627
        , [Prefix (try (string "not") >> return (InitPrefixOp "not"))]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   628
        ]
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   629
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   630
builtInFunction e = do
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   631
    name <- choice $ map (\s -> try $ caseInsensitiveString s >>= \i -> notFollowedBy alphaNum >> return i) builtin
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   632
    spaces
6397
6eb58ae8b510 Oops, function call has parens
unc0rr
parents: 6391
diff changeset
   633
    exprs <- parens pas $ commaSep1 pas $ e
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   634
    spaces
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   635
    return (name, exprs)