tools/PascalParser.hs
author unc0rr
Sun, 13 Nov 2011 13:46:26 +0300
changeset 6355 734fed7aefd3
parent 6317 83b93a2d2741
child 6357 52cb4807a78c
permissions -rw-r--r--
Introduce initialization expressions
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
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    39
           | RangeFromTo Expression 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
    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
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
    58
    | PrefixOp String Expression
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    59
    | 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
    60
    | BinOp String Expression Expression
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    61
    | StringLiteral String
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    62
    | CharCode String
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    63
    | NumberLiteral String
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    64
    | HexNumber String
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    65
    | Reference Reference
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
    66
    | Null
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    67
    deriving Show
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
    68
data Reference = ArrayElement [Expression] Reference
6316
ac23ba018ed2 Fix inifinite loops
unc0rr
parents: 6315
diff changeset
    69
    | FunCall [Expression] Reference
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    70
    | SimpleReference Identifier
6315
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
    71
    | Dereference Reference
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
    72
    | RecordField Reference Reference
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
    73
    | Address Reference
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    74
    deriving Show
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    75
data InitExpression = InitBinOp String InitExpression InitExpression
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    76
    | InitPrefixOp String InitExpression
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    77
    | InitReference Identifier
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    78
    | InitArray [InitExpression]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    79
    | InitRecord [(Identifier, InitExpression)]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    80
    | InitFloat String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    81
    | InitNumber String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    82
    | InitHexNumber String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    83
    | InitString String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    84
    | InitChar String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    85
    | InitNull
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    86
    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
    87
    
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
    88
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
    89
    = 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
    90
    { 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
    91
    , 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
    92
    , 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
    93
    , 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
    94
    , 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
    95
    , 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
    96
    , 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
    97
            "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
    98
            , "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
    99
            , "shr", "while", "do", "repeat", "until", "case", "of"
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   100
            , "type", "var", "const", "out", "array", "packed"
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   101
            , "procedure", "function", "with", "for", "to"
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   102
            , "downto", "div", "mod", "record", "set", "nil"
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   103
            , "string", "shortstring"
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   104
            ]
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
   105
    , 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
   106
    , 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
   107
    }
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
   108
    
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   109
pas = patch $ makeTokenParser pascalLanguageDef
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   110
    where
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   111
    patch tp = tp {stringLiteral = sl}
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   112
    sl = do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   113
        (char '\'')
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   114
        s <- (many $ noneOf "'")
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   115
        (char '\'')
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   116
        ss <- many $ do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   117
            (char '\'')
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   118
            s' <- (many $ noneOf "'")
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   119
            (char '\'')
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   120
            return $ '\'' : s'
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   121
        comments    
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   122
        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
   123
    
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
   124
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
   125
    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
   126
    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
   127
        comment
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   128
        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
   129
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
   130
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
   131
    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
   132
    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
   133
    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
   134
    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
   135
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
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
   137
        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
   138
        , (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
   139
        , (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
   140
        ]
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
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   142
iD = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   143
    i <- liftM Identifier (identifier pas)
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   144
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   145
    return i
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   146
        
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
   147
unit = do
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   148
    string "unit" >> comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   149
    name <- iD
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   150
    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
   151
    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
   152
    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
   153
    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
   154
    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
   155
    return $ Unit name int impl Nothing Nothing
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   156
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   157
    
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   158
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
   159
    where
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   160
    term = comments >> choice [
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   161
        parens pas reference 
6315
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
   162
        , char '@' >> reference >>= return . Address
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   163
        , iD >>= return . SimpleReference
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   164
        ] <?> "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
   165
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   166
    table = [ 
6316
ac23ba018ed2 Fix inifinite loops
unc0rr
parents: 6315
diff changeset
   167
            [Postfix $ (parens pas) (option [] parameters) >>= return . FunCall]
ac23ba018ed2 Fix inifinite loops
unc0rr
parents: 6315
diff changeset
   168
          , [Postfix (char '^' >> return Dereference)]
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
   169
          , [Postfix $ (brackets pas) (commaSep1 pas $ expression) >>= return . ArrayElement]
6316
ac23ba018ed2 Fix inifinite loops
unc0rr
parents: 6315
diff changeset
   170
          , [Infix (try (char '.' >> notFollowedBy (char '.')) >> return RecordField) AssocLeft]
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   171
        ]
6316
ac23ba018ed2 Fix inifinite loops
unc0rr
parents: 6315
diff changeset
   172
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   173
    
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   174
varsDecl1 = varsParser sepEndBy1    
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   175
varsDecl = varsParser sepEndBy
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   176
varsParser m endsWithSemi = do
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   177
    vs <- m (aVarDecl endsWithSemi) (semi pas)
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   178
    return vs
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   179
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   180
aVarDecl endsWithSemi = do
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   181
    when (not endsWithSemi) $
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   182
        optional $ choice [
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   183
            try $ string "var"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   184
            , try $ string "const"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   185
            , try $ string "out"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   186
            ]
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
   187
    comments
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   188
    ids <- do
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   189
        i <- (commaSep1 pas) $ (try iD <?> "variable declaration")
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   190
        char ':'
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   191
        return i
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   192
    comments
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   193
    t <- typeDecl <?> "variable type declaration"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   194
    comments
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   195
    init <- option Nothing $ do
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   196
        char '='
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   197
        comments
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   198
        e <- initExpression
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   199
        comments
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   200
        return (Just e)
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   201
    return $ VarDeclaration False (ids, t) init
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   202
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   203
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   204
constsDecl = do
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
   205
    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
   206
    comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   207
    return vs
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   208
    where
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   209
    aConstDecl = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   210
        comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   211
        i <- iD <?> "const declaration"
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   212
        optional $ do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   213
            char ':'
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   214
            comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   215
            t <- typeDecl
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   216
            return ()
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   217
        char '='
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   218
        comments
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   219
        e <- initExpression
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   220
        comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   221
        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
   222
        
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
   223
typeDecl = choice [
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   224
    char '^' >> typeDecl >>= return . PointerTo
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   225
    , try (string "shortstring") >> return String
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   226
    , arrayDecl
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   227
    , recordDecl
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   228
    , sequenceDecl >>= return . Sequence
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   229
    , 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
   230
    , 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
   231
    ] <?> "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
   232
    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
   233
    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
   234
        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
   235
        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
   236
        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
   237
        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
   238
        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
   239
        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
   240
        string "of"
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   241
        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
   242
        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
   243
        return $ ArrayDecl r t
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   244
    recordDecl = do
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   245
        optional $ (try $ string "packed") >> comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   246
        try $ string "record"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   247
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   248
        vs <- varsDecl True
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   249
        string "end"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   250
        return $ RecordType vs
6310
31145a87811a Improve type declarations parsing
unc0rr
parents: 6307
diff changeset
   251
    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
   252
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   253
typesDecl = many (aTypeDecl >>= \t -> comments >> return t)
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   254
    where
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   255
    aTypeDecl = do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   256
        i <- try $ do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   257
            i <- iD <?> "type declaration"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   258
            comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   259
            char '='
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   260
            return i
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   261
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   262
        t <- typeDecl
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   263
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   264
        semi pas
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   265
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   266
        return $ TypeDeclaration i t
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   267
        
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
   268
rangeDecl = choice [
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   269
    try $ rangeft
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   270
    , 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
   271
    ] <?> "range declaration"
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   272
    where
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   273
    rangeft = do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   274
    e1 <- expression
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   275
    string ".."
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   276
    e2 <- expression
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   277
    return $ RangeFromTo e1 e2
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   278
    
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   279
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
   280
    varSection,
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   281
    constSection,
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   282
    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
   283
    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
   284
    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
   285
    ]
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
    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
   287
    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
   288
        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
   289
        comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   290
        v <- varsDecl1 True
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   291
        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
   292
        return v
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   293
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   294
    constSection = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   295
        try $ string "const"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   296
        comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   297
        c <- constsDecl
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   298
        comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   299
        return c
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   300
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   301
    typeSection = do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   302
        try $ string "type"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   303
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   304
        t <- typesDecl
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   305
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   306
        return t
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   307
        
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
   308
    procDecl = do
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
   309
        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
   310
        comments
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   311
        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
   312
        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
   313
            char '('
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   314
            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
   315
            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
   316
        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
   317
        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
   318
        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
   319
                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
   320
                comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   321
                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
   322
                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
   323
                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
   324
                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
   325
                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
   326
        comments
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   327
        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
   328
        
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
   329
    funcDecl = do
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
   330
        try $ string "function"
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   331
        comments
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   332
        i <- iD
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   333
        optional $ do
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   334
            char '('
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   335
            varsDecl False
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   336
            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
   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
        char ':'
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   339
        comments
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   340
        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
   341
        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
   342
        char ';'
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   343
        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
   344
        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
   345
                do
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   346
                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
   347
                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
   348
                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
   349
                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
   350
                return Nothing
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   351
        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
   352
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
program = do
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   354
    string "program"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   355
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   356
    name <- iD
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   357
    (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
   358
    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
   359
    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
   360
    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
   361
    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
   362
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
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
   364
    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
   365
    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
   366
    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
   367
    comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   368
    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
   369
    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
   370
    return $ Interface u (TypesAndVars tv)
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   371
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
   372
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
   373
    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
   374
    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
   375
    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
   376
    comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   377
    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
   378
    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
   379
    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
   380
    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
   381
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
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
   383
    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
   384
    term = comments >> 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
   385
        parens pas $ expression 
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   386
        , try $ integer pas >>= return . NumberLiteral . show
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   387
        , stringLiteral pas >>= return . StringLiteral
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   388
        , char '#' >> many digit >>= return . CharCode
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   389
        , char '$' >> many hexDigit >>= return . HexNumber
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   390
        , try $ string "nil" >> return Null
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   391
        , 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
   392
        ] <?> "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
   393
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
    table = [ 
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   395
          [  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
   396
           , Infix (char '/' >> return (BinOp "/")) AssocLeft
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   397
           , Infix (try (string "div") >> return (BinOp "div")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   398
           , Infix (try (string "mod") >> return (BinOp "mod")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   399
          ]
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
   400
        , [  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
   401
           , Infix (char '-' >> return (BinOp "-")) AssocLeft
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   402
           , Prefix (char '-' >> return (PrefixOp "-"))
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   403
          ]
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   404
        , [  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
   405
           , 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
   406
           , 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
   407
           , 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
   408
           , 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
   409
           , Infix (char '=' >> return (BinOp "=")) AssocNone
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   410
          ]
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   411
        , [  Infix (try $ string "and" >> return (BinOp "and")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   412
           , Infix (try $ string "or" >> return (BinOp "or")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   413
           , Infix (try $ string "xor" >> return (BinOp "xor")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   414
          ]
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   415
        , [  Infix (try $ string "shl" >> return (BinOp "shl")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   416
           , Infix (try $ string "shr" >> return (BinOp "shr")) AssocNone
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   417
          ]
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   418
        , [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
   419
        ]
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
   420
    
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
   421
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
   422
    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
   423
    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
   424
    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
   425
    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
   426
    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
   427
    
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
   428
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
   429
    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
   430
        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
   431
        , 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
   432
        , whileCycle
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   433
        , 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
   434
        , switchCase
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   435
        , withBlock
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   436
        , forCycle
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   437
        , (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
   438
        , 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
   439
        ]
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
    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
   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
    return o
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   443
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
   444
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
   445
    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
   446
    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
   447
    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
   448
    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
   449
    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
   450
    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
   451
    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
   452
    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
   453
    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
   454
        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
   455
        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
   456
        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
   457
        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
   458
        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
   459
    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
   460
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
   461
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
   462
    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
   463
    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
   464
    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
   465
    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
   466
    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
   467
    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
   468
    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
   469
    return $ WhileCycle e o
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   470
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   471
withBlock = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   472
    try $ string "with"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   473
    comments
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   474
    r <- reference
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   475
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   476
    string "do"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   477
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   478
    o <- phrase
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   479
    return $ WithBlock r o
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   480
    
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   481
repeatCycle = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   482
    try $ string "repeat"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   483
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   484
    o <- many phrase
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   485
    string "until"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   486
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   487
    e <- expression
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   488
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   489
    return $ RepeatCycle e o
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   490
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   491
forCycle = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   492
    try $ string "for"
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
    i <- iD
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
    string ":="
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   497
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   498
    e1 <- expression
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   499
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   500
    choice [string "to", string "downto"]
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
    e2 <- expression
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   503
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   504
    string "do"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   505
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   506
    p <- phrase
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   507
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   508
    return $ ForCycle i e1 e2 p
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   509
    
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
   510
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
   511
    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
   512
    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
   513
    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
   514
    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
   515
    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
   516
    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
   517
    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
   518
    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
   519
        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
   520
        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
   521
        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
   522
        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
   523
        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
   524
    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
   525
    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
   526
    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
   527
    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
   528
        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
   529
        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
   530
        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
   531
        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
   532
        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
   533
        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
   534
        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
   535
    
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
procCall = do
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   537
    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
   538
    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
   539
    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
   540
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   541
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
   542
        
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
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
   544
    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
   545
    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
   546
    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
   547
    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
   548
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
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
   550
    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
   551
        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
   552
            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
   553
            comments
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   554
            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
   555
            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
   556
            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
   557
            return u
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   558
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   559
initExpression = buildExpressionParser table term <?> "initialization expression"
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   560
    where
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   561
    term = comments >> choice [
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   562
        try $ parens pas (commaSep pas $ initExpression) >>= return . InitArray
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   563
        , parens pas (semiSep pas $ recField) >>= return . InitRecord
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   564
        , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . InitNumber . show) i
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   565
        , try $ float pas >>= return . InitFloat . show
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   566
        , stringLiteral pas >>= return . InitString
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   567
        , char '#' >> many digit >>= return . InitChar
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   568
        , char '$' >> many hexDigit >>= return . InitHexNumber
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   569
        , try $ string "nil" >> return InitNull
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   570
        , iD >>= return . InitReference
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   571
        ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   572
        
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   573
    recField = do
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   574
        i <- iD
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   575
        spaces
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   576
        char ':'
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   577
        spaces
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   578
        e <- initExpression
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   579
        spaces
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   580
        return (i ,e)
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   581
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   582
    table = [ 
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   583
          [  Infix (char '*' >> return (InitBinOp "*")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   584
           , Infix (char '/' >> return (InitBinOp "/")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   585
           , Infix (try (string "div") >> return (InitBinOp "div")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   586
           , Infix (try (string "mod") >> return (InitBinOp "mod")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   587
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   588
        , [  Infix (char '+' >> return (InitBinOp "+")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   589
           , Infix (char '-' >> return (InitBinOp "-")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   590
           , Prefix (char '-' >> return (InitPrefixOp "-"))
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   591
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   592
        , [  Infix (try (string "<>") >> return (InitBinOp "<>")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   593
           , Infix (try (string "<=") >> return (InitBinOp "<=")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   594
           , Infix (try (string ">=") >> return (InitBinOp ">=")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   595
           , Infix (char '<' >> return (InitBinOp "<")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   596
           , Infix (char '>' >> return (InitBinOp ">")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   597
           , Infix (char '=' >> return (InitBinOp "=")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   598
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   599
        , [  Infix (try $ string "and" >> return (InitBinOp "and")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   600
           , Infix (try $ string "or" >> return (InitBinOp "or")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   601
           , Infix (try $ string "xor" >> return (InitBinOp "xor")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   602
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   603
        , [  Infix (try $ string "shl" >> return (InitBinOp "and")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   604
           , Infix (try $ string "shr" >> return (InitBinOp "or")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   605
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   606
        , [Prefix (try (string "not") >> return (InitPrefixOp "not"))]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   607
        ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   608