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