annotate parser/Parser.d @ 143:d76cc5cad4fc

Added partial support for switches. Added support for extern(C) in CodeGen.
author Anders Johnsen <skabet@gmail.com>
date Mon, 21 Jul 2008 01:05:20 +0200
parents 927ae00bd9d2
children 6e6355fb5f0f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1 module parser.Parser;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
2
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
3 import lexer.Lexer,
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
4 lexer.Token;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
5
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
6 import parser.Action;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
7
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
8 import basic.Message,
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
9 basic.Attribute;
21
0fb2d13dce37 Now working with gdc also (gdc use reverse paremeter validating on function calls)
johnsen@johnsen-laptop
parents: 12
diff changeset
10
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
11 import basic.SmallArray,
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
12 basic.SourceManager;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
13
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
14 import tango.io.Stdout,
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
15 Integer = tango.text.convert.Integer;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
16
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
17 class Parser
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
18 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
19 Action action;
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
20 MessageHandler messages;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
21 alias Object Exp;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
22 alias Object Stmt;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
23 alias Object Decl;
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
24 alias Object Module;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
25
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
26 this(MessageHandler messages)
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
27 {
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
28 this.messages = messages;
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
29 }
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
30
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
31 Module parse(SourceManager sm, Lexer lexer, Action act)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
32 {
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
33 this.sm = sm;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
34 this.lexer = lexer;
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
35 this.action = act;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
36
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
37 Module m;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
38 if (lexer.peek.type == Tok.Module)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
39 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
40 Token _module = lexer.next;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
41 ModuleName name = parseModuleName();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
42 m = action.actOnModule(_module, sm.getText(name.asRange()));
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
43 require(Tok.Seperator);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
44 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
45 else
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
46 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
47 SLoc loc = lexer.peek.location;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
48 m = action.actOnImplicitModule(loc, sm.getFile(loc));
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
49 }
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
50
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
51 Attribute att;
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
52 while (lexer.peek.type != Tok.EOF)
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
53 foreach (d; parseDeclDef(&att))
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
54 action.actOnModuleDecl(m, d);
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
55
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
56 return m;
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
57 }
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
58
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
59 private:
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
60 Decl[] parseDeclDef(Attribute* a)
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
61 {
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
62 Token t = lexer.peek;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
63 if (t.type == Tok.Import)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
64 return parseImports();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
65 else
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
66 return [parseDecl(a)];
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
67 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
68
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
69 Decl parseDecl(Attribute* all_res)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
70 {
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
71 Token t = lexer.peek;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
72
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
73 Attribute att = *all_res;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
74
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
75 while ( t.isAttribute )
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
76 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
77 Attribute* a = &att;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
78 bool sco;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
79 if(lexer.peek(1).type == Tok.Colon)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
80 sco = true;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
81
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
82 LSwitch: switch(t.type)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
83 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
84 case Tok.Public:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
85 a.setProtection(Protection.Public);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
86 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
87 case Tok.Private:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
88 a.setProtection(Protection.Private);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
89 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
90 case Tok.Package:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
91 a.setProtection(Protection.Package);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
92 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
93 case Tok.Protected:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
94 a.setProtection(Protection.Protected);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
95 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
96 case Tok.Export:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
97 a.setProtection(Protection.Export);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
98 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
99 case Tok.Static:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
100 a.setStatic;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
101 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
102 case Tok.Final:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
103 a.setFinal;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
104 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
105 case Tok.Const:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
106 a.setConst;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
107 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
108 case Tok.Abstract:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
109 a.setAbstract;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
110 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
111 case Tok.Override:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
112 a.setOverride;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
113 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
114 case Tok.Depracted:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
115 a.setDepracted;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
116 break;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
117 case Tok.Auto:
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
118 a.setAuto;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
119 break;
140
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
120 case Tok.Extern:
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
121 Extern e = parseLinkageType;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
122 a.setExtern(e);
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
123 break;
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
124 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
125 if(sco)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
126 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
127 sco = false;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
128 a = all_res;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
129 lexer.next;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
130 goto LSwitch;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
131 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
132 lexer.next;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
133 t = lexer.peek;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
134 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
135
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
136 if (t.isBasicType || t.isIdentifier)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
137 {
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
138 Id type;
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
139 Id iden;
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
140 int len = peekParseType;
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
141 if(lexer.peek(len).type == Tok.Identifier && len != 0)
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
142 {
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
143 type = parseType;
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
144 parseDeclAfterInvalidType:
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
145 iden = Id(require(Tok.Identifier));
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
146 Token next = lexer.peek();
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
147 if (next.type == Tok.Seperator)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
148 {
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
149 Token sep = lexer.next();
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
150 return action.actOnDeclarator(type, iden, null, att);
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
151 }
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
152 else if (next.type == Tok.Assign)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
153 {
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
154 Token assign = lexer.next();
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
155 Exp exp = parseExpression();
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
156 require(Tok.Seperator);
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
157 return action.actOnDeclarator(type, iden, exp, att);
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
158 }
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
159 else if (next.type == Tok.OpenParentheses)
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
160 return parseFunc(type, iden, att);
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
161 else
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
162 messages.report(UnexpectedTok, next.location).arg(next.getType);
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
163 return null;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
164 }
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
165 t = lexer.peek(len);
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
166 messages.report(InvalidDeclType, t.location)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
167 .arg(sm.getText(t.asRange));
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
168 while(len--)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
169 lexer.next;
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
170 while(lexer.peek.type != Tok.Identifier)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
171 lexer.next;
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
172 type = Id(lexer.peek);
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
173 goto parseDeclAfterInvalidType;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
174 }
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
175 else if (t.type == Tok.Struct)
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
176 {
53
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
177 Id type = Id(lexer.next);
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
178 Id iden = Id(require(Tok.Identifier));
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
179
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
180 return parseStruct(type, iden, att);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
181 }
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
182 else if (t.type == Tok.Class)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
183 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
184 Id type = Id(lexer.next);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
185 Id iden = Id(require(Tok.Identifier));
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
186
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
187 return parseClass(type, iden, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
188 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
189 else if (t.type == Tok.Interface)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
190 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
191 Id type = Id(lexer.next);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
192 Id iden = Id(require(Tok.Identifier));
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
193
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
194 return parseClass(type, iden, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
195 }
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
196 messages.report(UnexpectedTok, t.location)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
197 .arg(t.getType)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
198 .arg(Tok.Identifier)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
199 .fatal(ExitLevel.Parser);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
200 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
201
140
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
202 Extern parseLinkageType()
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
203 {
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
204 Extern e = Extern.D;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
205 if(lexer.peek(1).type != Tok.OpenParentheses)
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
206 return e;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
207
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
208 lexer.next; lexer.next;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
209
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
210 Token t = require(Tok.Identifier);
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
211
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
212 switch(sm.getText(t.asRange))
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
213 {
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
214 case "C":
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
215 if (lexer.peek(0).type == Tok.Plus &&
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
216 lexer.peek(1).type == Tok.Plus)
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
217 e = Extern.CPlusPlus;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
218 else
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
219 e = Extern.C;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
220 break;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
221 case "D":
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
222 break;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
223 case "Windows":
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
224 e = Extern.Windows;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
225 break;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
226 case "Pascal":
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
227 e = Extern.Pascal;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
228 break;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
229 case "System":
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
230 e = Extern.System;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
231 break;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
232 default:
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
233 messages.report(UnexpectedLinkType, t.location);
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
234 }
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
235
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
236 require(Tok.CloseParentheses);
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
237
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
238 return e;
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
239 }
927ae00bd9d2 Added support for extern keyword. Being ignored atm though. Also changed ast/Module, so that you can get a list of only vars, functions or structs.
Anders Johnsen <skabet@gmail.com>
parents: 136
diff changeset
240
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
241 /**
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
242 Parse a series of imports belonging to a single import token.
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
243 */
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
244 Decl[] parseImports()
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
245 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
246 Token _import = require(Tok.Import);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
247 SmallArray!(Decl) res;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
248 void addToRes(Decl d) { res ~= d; }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
249
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
250 bool done = false;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
251 while (!done && !on_a(Tok.Seperator))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
252 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
253 ModuleName mod = parseModuleName();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
254 Token tok = lexer.peek;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
255 switch (tok.type)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
256 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
257 case Tok.Comma:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
258 // import A, B.C;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
259 // parse another module-name
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
260 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
261 res ~= action.actOnImport(_import, mod, null);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
262 break;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
263 case Tok.Assign:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
264 // import B = A.A;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
265 // ^- must be a single identifier
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
266 // renamed import
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
267 if (mod.packages.length != 0)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
268 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
269 SLoc loc = mod.packages[0].tok.location;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
270 messages.report(RenameMustBeSingleIdent, loc);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
271 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
272 //if (isStatic)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
273 // error("Static imports cannot be renamed");
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
274 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
275 Id name = mod.id;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
276 mod = parseModuleName();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
277 // create from mod and rename to `name`
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
278 res ~= action.actOnImport(_import, mod, &name);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
279 break;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
280 case Tok.Colon:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
281 // import A : a;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
282 // selective imports, potentially import A : print = a
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
283 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
284 Decl d = action.actOnImport(_import, mod, null);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
285 // do-while on a comma:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
286 // add explicit symbol
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
287 do
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
288 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
289 Id sym = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
290 Id dummy;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
291 Id* name = null;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
292 if (skip(Tok.Assign))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
293 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
294 dummy = sym;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
295 name = &dummy;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
296 sym = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
297 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
298 action.addSelectiveImport(d, sym, name);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
299
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
300 } while (skip(Tok.Comma));
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
301 require(Tok.Seperator);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
302 res ~= d;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
303 return res.safe();
100
5f258eaf9517 Loading modules in. Just need to add them to the scope of the "main" Module now.
Anders Johnsen <skabet@gmail.com>
parents: 99
diff changeset
304 case Tok.Seperator:
5f258eaf9517 Loading modules in. Just need to add them to the scope of the "main" Module now.
Anders Johnsen <skabet@gmail.com>
parents: 99
diff changeset
305 done = true;
5f258eaf9517 Loading modules in. Just need to add them to the scope of the "main" Module now.
Anders Johnsen <skabet@gmail.com>
parents: 99
diff changeset
306 break;
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
307 default:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
308 goto Lerror;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
309 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
310 res ~= action.actOnImport(_import, mod, null);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
311 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
312
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
313 require(Tok.Seperator);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
314 return res.safe();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
315 Lerror:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
316 while (!on_a (Tok.Seperator))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
317 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
318 return res.safe();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
319 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
320
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
321 /**
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
322 Parse interface
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
323 */
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
324 Decl parseInterface(Id type, Id iden, Attribute att)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
325 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
326 auto decl = action.actOnDeclarator(type, iden, null, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
327
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
328 if (lexer.peek.type == Tok.Colon)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
329 // SuperInterfaces
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
330 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
331 lexer.next; // Remove colon.
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
332
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
333 Token identifier;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
334
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
335 // The identifier
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
336 identifier = require(Tok.Identifier);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
337
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
338 // FIXME: Register Interface here
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
339
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
340 // We should now have an optional list of items, each starting ','
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
341 while (lexer.peek.type == Tok.Comma)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
342 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
343 lexer.next; // Remove comma
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
344
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
345 // The identifier
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
346 identifier = require(Tok.Identifier);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
347
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
348 // FIXME: Register Interface here
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
349 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
350 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
351
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
352 require(Tok.OpenBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
353
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
354 Attribute a;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
355 while(lexer.peek.isBasicType || lexer.peek.isIdentifier || lexer.peek.isAttribute)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
356 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
357 auto m_decl = parseDecl(&a);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
358 action.actOnStructMember(decl, m_decl);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
359 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
360
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
361 require(Tok.CloseBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
362
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
363 return decl;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
364 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
365
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
366 /**
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
367 Parse class
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
368 */
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
369 Decl parseClass(Id type, Id iden, Attribute att)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
370 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
371 auto decl = action.actOnDeclarator(type, iden, null, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
372
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
373 if (lexer.peek.type == Tok.Colon)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
374 // BaseClassList - Super class and interfaces(in that order)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
375 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
376 lexer.next; // Remove colon.
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
377
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
378 Token protection, identifier;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
379
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
380 // First we expect an optional protection level.
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
381 if (lexer.peek.isBaseClassProtection)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
382 protection = lexer.next;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
383 // Then the identifier
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
384 identifier = require(Tok.Identifier);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
385
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
386 // FIXME: Register Interface here
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
387
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
388 // We should now have an optional list of items, each starting ','
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
389 while (lexer.peek.type == Tok.Comma)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
390 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
391 lexer.next; // Remove comma
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
392
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
393 // First we expect an optional protection level.
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
394 if (lexer.peek.isBaseClassProtection)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
395 protection = lexer.next;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
396 // Then the identifier
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
397 identifier = require(Tok.Identifier);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
398 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
399 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
400
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
401 require(Tok.OpenBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
402
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
403 Attribute a;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
404 while(lexer.peek.isBasicType || lexer.peek.isIdentifier || lexer.peek.isAttribute)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
405 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
406 auto m_decl = parseDecl(&a);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
407 action.actOnStructMember(decl, m_decl); // FIXME: Should call actOnClassMember
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
408 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
409
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
410 require(Tok.CloseBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
411
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
412 return decl;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
413 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
414
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
415 /**
53
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
416 Parse struct
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
417 */
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
418 Decl parseStruct(Id type, Id iden, Attribute att)
53
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
419 {
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
420 auto decl = action.actOnDeclarator(type, iden, null, att);
53
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
421
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
422 require(Tok.OpenBrace);
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
423
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
424 Attribute a;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
425 while(lexer.peek.isBasicType || lexer.peek.isIdentifier || lexer.peek.isAttribute)
53
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
426 {
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
427 auto m_decl = parseDecl(&a);
102
cd066f3b539a Parsing methods in structs - error on semantics though.
Anders Johnsen <skabet@gmail.com>
parents: 100
diff changeset
428 action.actOnStructMember(decl, m_decl);
53
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
429 }
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
430
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
431 require(Tok.CloseBrace);
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
432
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
433 return decl;
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
434 }
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
435
53
da551f90e03f Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents: 51
diff changeset
436 /**
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
437 Parse statements.
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
438
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
439 This is the place to attack!
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
440 */
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
441 Stmt parseStatement()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
442 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
443 Token t = lexer.peek;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
444
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
445 if (t.isReturn)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
446 {
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
447 Token ret = lexer.next;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
448 Exp exp;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
449 if (lexer.peek.type != Tok.Seperator)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
450 exp = parseExpression();
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
451 require(Tok.Seperator);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
452 return action.actOnReturnStmt(ret, exp);
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
453
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
454 /*
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
455 if (cond)
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
456 single statement | compound statement
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
457 [else
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
458 single statement | compound statement]
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
459 */
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
460 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
461 else if (t.isIf)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
462 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
463 Token _if = lexer.next();
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
464
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
465 require(Tok.OpenParentheses);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
466 Exp cond = parseExpression();
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
467 require(Tok.CloseParentheses);
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
468
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
469 Stmt thenB = parseSingleOrCompoundStatement();
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
470
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
471 // if there is no else part we use the if as token, to have
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
472 // something than can be passed along
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
473 Token _else = _if;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
474 Stmt elseB;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
475 if (lexer.peek.type == Tok.Else)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
476 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
477 _else = lexer.next;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
478 elseB = parseSingleOrCompoundStatement();
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
479 }
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
480
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
481 return action.actOnIfStmt(_if, cond, thenB, _else, elseB);
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
482
47
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
483 /*
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
484 while (cond)
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
485 single statement | compound statement
47
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
486 */
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
487 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
488 else if (t.isWhile)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
489 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
490 Token _while = lexer.next;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
491 require(Tok.OpenParentheses);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
492 Exp cond = parseExpression();
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
493 require(Tok.CloseParentheses);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
494 Stmt bodyStmt = parseSingleOrCompoundStatement();
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
495 return action.actOnWhileStmt(_while, cond, bodyStmt);
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
496
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
497 /*
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
498 One of four things:
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
499 A declaration of a function/variable `type id ...`
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
500 A direct assignment `id = exp;`
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
501 An indirect assignment `id.id = exp`
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
502 Some sort of free standing expression
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
503
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
504 The assignments should be handled as binary expressions?
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
505 */
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
506 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
507 else if (t.isBasicType || t.isIdentifier)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
508 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
509 Token iden = lexer.peek;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
510 Token n = lexer.peek(1);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
511 // Must be an decl, if we start with a basic type, or two
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
512 // identifiers in a row
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
513 if ( n.type == Tok.Star || n.type == Tok.OpenBracket)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
514 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
515 int len = peekParseType;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
516 if(lexer.peek(len).type == Tok.Identifier && len != 0)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
517 return action.actOnDeclStmt(parseVarDecl());
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
518
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
519 Exp exp = parseExpression();
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
520 require(Tok.Seperator);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
521 return action.actOnExprStmt(exp);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
522 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
523
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
524 if (n.isIdentifier())
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
525 return action.actOnDeclStmt(parseVarDecl());
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
526
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
527 // Expression: a.b, a = b, a(b) etc.
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
528 Exp exp = parseExpression();
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
529 require(Tok.Seperator);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
530 return action.actOnExprStmt(exp);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
531 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
532 else if(t.isSwitch)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
533 {
143
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
534 lexer.next;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
535 require(Tok.OpenParentheses);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
536 auto target = parseExpression();
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
537 auto res = action.actOnStartOfSwitchStmt(target);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
538 require(Tok.CloseParentheses);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
539 require(Tok.OpenBrace);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
540 while (true)
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
541 {
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
542 Stmt[] statements;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
543 if (skip(Tok.Default))
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
544 {
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
545 require(Tok.Colon);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
546 statements.length = 0;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
547 while (lexer.peek.type != Tok.Case
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
548 && lexer.peek.type != Tok.Default
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
549 && lexer.peek.type != Tok.CloseBrace)
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
550 statements ~= parseStatement();
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
551 action.actOnDefaultStmt(res, statements);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
552 continue;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
553 }
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
554
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
555 Token _case = lexer.peek;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
556 if (_case.type != Tok.Case)
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
557 break;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
558 lexer.next();
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
559
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
560 Exp[] literals;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
561 do
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
562 {
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
563 Exp e = parseExpression();
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
564 // IntegerLit lit = cast(IntegerLit)e;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
565 // if (lit is null)
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
566 // messages.report(CaseValueMustBeInt, lexer.peek.location).arg(lexer.next.getType);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
567 // else
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
568 literals ~= e;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
569 }
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
570 while (skip(Tok.Comma));
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
571 require(Tok.Colon);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
572
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
573 while (lexer.peek.type != Tok.Case
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
574 && lexer.peek.type != Tok.Default
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
575 && lexer.peek.type != Tok.CloseBrace)
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
576 statements ~= parseStatement();
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
577
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
578 action.actOnCaseStmt(res, literals, statements);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
579
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
580 if (lexer.peek.type == Tok.CloseBrace)
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
581 break;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
582 }
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
583 require(Tok.CloseBrace);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
584 return res;
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
585 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
586 else
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
587 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
588 if (t.type == Tok.Star)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
589 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
590 auto exp = parseExpression();
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
591 require(Tok.Seperator);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
592 return action.actOnExprStmt(exp);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
593 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
594 messages.report(UnexpectedBeginStmt, lexer.peek.location).arg(lexer.next.getType);
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
595 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
596 }
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
597 messages.report(UnexpectedTok, t.location);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
598 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
599 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
600
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
601 Decl parseVarDecl()
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
602 {
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
603 // manually hardcoded to only support "type id [= exp];"
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
604 // as that is the only thing the codegen understands
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
605 Id type = parseType;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
606 Id id = Id(lexer.next);
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
607 Exp init;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
608 if (skip(Tok.Assign))
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
609 init = parseExpression();
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
610 require(Tok.Seperator);
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
611 Attribute att;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
612 Decl d = action.actOnDeclarator(type, id, init, att);
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
613 return d;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
614 }
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
615
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
616 /**
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
617 Parses a function/method given the already parsed return type and name
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
618 */
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
619 Decl parseFunc(ref Id type, ref Id name, Attribute att)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
620 {
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
621 Decl func = action.actOnStartOfFunctionDef(type, name, att);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
622 parseFuncArgs(func);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
623
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
624 if(lexer.peek.type == Tok.Seperator)
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
625 {
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
626 lexer.next;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
627 return func;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
628 }
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
629 Stmt stmt = parseCompoundStatement();
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
630
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
631 return action.actOnEndOfFunction(func, stmt);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
632 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
633
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
634 /**
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
635 Parse the function arguments, assumes current token is (.
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
636
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
637 Both the intitial paren and the ending paren is consumed.
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
638 */
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
639 void parseFuncArgs(Decl func)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
640 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
641 require(Tok.OpenParentheses); // Remove the "(" token.
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
642
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
643 while(lexer.peek.type != Tok.CloseParentheses)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
644 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
645 auto t = parseType();
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
646 Id i;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
647 if(lexer.peek.type == Tok.Identifier)
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
648 i = parseIdentifier();
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
649 action.addFuncArg(func, t, i);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
650
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
651 if(lexer.peek.type == Tok.Comma)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
652 lexer.next;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
653 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
654
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
655 require(Tok.CloseParentheses); // Remove the ")"
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
656 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
657
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
658 /**
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
659 Parse either a block, or a single statement as allowed after if, while
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
660 and for.
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
661 */
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
662 Stmt parseSingleOrCompoundStatement()
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
663 {
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
664 if (lexer.peek.type == Tok.OpenBrace)
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
665 return parseCompoundStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
666 return parseStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
667 }
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
668
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
669 /**
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
670 Parses a function-body or similar, expects an opening brace to be the
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
671 current token.
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
672
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
673 Will consume both the starting { and ending }
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
674 */
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
675 Stmt parseCompoundStatement()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
676 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
677 Token lbrace = require(Tok.OpenBrace);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
678 SmallArray!(Stmt, 32) stmts; // Try to use the stack only
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
679 while (lexer.peek.type != Tok.CloseBrace)
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
680 stmts ~= parseStatement();
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
681 Token rbrace = require(Tok.CloseBrace);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
682 return action.actOnCompoundStmt(lbrace, rbrace, stmts.unsafe());
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
683 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
684
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
685 Id parseIdentifier()
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
686 {
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
687 Token tok = lexer.next;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
688
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
689 if (tok.type is Tok.Identifier)
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
690 return Id(tok);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
691
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
692 messages.report(UnexpectedTokSingle, tok.location)
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
693 .arg(tok.getType)
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
694 .arg(Tok.Identifier);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
695 }
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
696
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
697 ModuleName parseModuleName()
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
698 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
699 auto id = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
700 ModuleName mod;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
701 while (skip(Tok.Dot))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
702 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
703 mod.packages ~= id;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
704 if (lexer.peek.type != Tok.Identifier) {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
705 messages.report(ExpectedIdAfterPackage, lexer.peek.location);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
706 goto Lerror;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
707 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
708 id = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
709 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
710 mod.id = id;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
711 return mod;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
712 Lerror:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
713 while (!skip(Tok.Seperator))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
714 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
715 return mod;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
716 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
717
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
718
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
719 /**
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
720 Parse a type - this includes pointer and array(at some point) types.
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
721 */
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
722 Id parseType()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
723 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
724 Token type = lexer.next;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
725
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
726 Id currentType;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
727
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
728 if ( !(type.isBasicType || type.type == Tok.Identifier) )
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
729 messages.report(InvalidType, type.location);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
730
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
731 currentType = Id(type);
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
732 type = lexer.peek;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
733
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
734 while(type.type == Tok.Star || type.type == Tok.OpenBracket)
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
735 {
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
736 if(type.type == Tok.Star)
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
737 {
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
738 currentType = PointerId(currentType);
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
739 lexer.next;
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
740 }
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
741 else
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
742 {
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
743 lexer.next;
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
744 if(lexer.peek.type == Tok.Integer)
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
745 currentType = StaticArrayId(
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
746 currentType,
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
747 action.actOnNumericConstant(
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
748 require(Tok.Integer)));
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
749 require(Tok.CloseBracket);
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
750
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
751 }
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
752 type = lexer.peek;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
753 }
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
754
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
755 return currentType;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
756 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
757
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
758 int peekParseType()
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
759 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
760 int i;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
761 Token type = lexer.peek(i);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
762
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
763 Id currentType;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
764
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
765 if ( !(type.isBasicType || type.type == Tok.Identifier) )
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
766 return 0;
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
767
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
768 currentType = Id(type);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
769 type = lexer.peek(++i);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
770
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
771 while(type.type == Tok.Star || type.type == Tok.OpenBracket)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
772 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
773 if(type.type == Tok.Star)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
774 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
775 i++;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
776 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
777 else
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
778 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
779 if(lexer.peek(i++).type != Tok.OpenBracket)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
780 return 0;
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
781 if(lexer.peek(i).type == Tok.Integer)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
782 {
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
783 i++;
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
784 if(lexer.peek(i++).type != Tok.CloseBracket)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
785 return 0;
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
786 }
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
787 else
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
788 if(lexer.peek(i++).type != Tok.CloseBracket)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
789 return 0;
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
790
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
791 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
792 type = lexer.peek(i);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
793 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
794
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
795 return i;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
796 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
797
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
798 private:
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
799 // -- Expression parsing -- //
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
800 Exp parsePostfixExp(Exp target)
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
801 {
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
802 switch(lexer.peek.type)
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
803 {
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
804 case Tok.Dot:
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
805 switch(lexer.peek(1).type)
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
806 {
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
807 case Tok.Identifier:
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
808 Token op = lexer.next;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
809 Id member = Id(lexer.next);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
810 Exp exp = action.actOnMemberReference(target, op.location, member);
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
811 return parsePostfixExp(exp);
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
812 default:
30
3147a52d1247 Ooops.. should have compiled before commit.. now works again
Anders Halager <halager@gmail.com>
parents: 29
diff changeset
813 Token t = lexer.peek(1);
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
814 messages.report(ExpectedIdAfterDot, t.location);
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
815 }
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
816 case Tok.OpenBracket:
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
817 Token open = lexer.next;
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
818 Exp index = parseExpression();
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
819 Token close = require(Tok.CloseBracket);
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
820 return action.actOnIndexEpr(target, open, index, close);
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
821 default:
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
822 return target;
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
823 }
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
824 }
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
825
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
826 Exp parseExpression(int p = 0)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
827 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
828 auto exp = P();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
829 Token next = lexer.peek();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
830 BinOp* op = null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
831 while ((op = binary(next.type)) != null && op.prec >= p)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
832 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
833 lexer.next();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
834 int q = op.leftAssoc? 1 + op.prec : op.prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
835 auto exp2 = parseExpression(q);
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
836 exp = action.actOnBinaryOp(next.location, op.operator, exp, exp2);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
837 next = lexer.peek();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
838 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
839
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
840 return exp;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
841 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
842
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
843 Exp P()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
844 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
845 Token next = lexer.next();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
846 if (auto op = unary(next.type))
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
847 return action.actOnUnaryOp(next, parseExpression(op.prec));
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
848 else if (next.type == Tok.OpenParentheses)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
849 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
850 auto e = parseExpression(0);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
851 require(Tok.CloseParentheses);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
852 return e;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
853 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
854 else if (next.type == Tok.Identifier)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
855 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
856 Exp value = action.actOnIdentifierExp(Id(next));
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
857 Exp iden = parsePostfixExp(value);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
858 switch(lexer.peek.type)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
859 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
860 case Tok.OpenParentheses:
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
861 Token lp = lexer.next;
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
862 SmallArray!(Exp, 8) args;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
863 while(lexer.peek.type != Tok.CloseParentheses)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
864 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
865 if(lexer.peek.type == Tok.Comma)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
866 lexer.next;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
867 args ~= parseExpression();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
868 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
869
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
870 Token rp = lexer.next();
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
871 return action.actOnCallExpr(iden, lp, args.unsafe(), rp);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
872
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
873 default:
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
874 return iden;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
875 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
876 }
68
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
877 else if (next.type == Tok.Cast)
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
878 return parseCast(next);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
879 else if (next.type == Tok.Integer)
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
880 return action.actOnNumericConstant(next);
104
7ff4bc2accf2 Lexing more types of strings. Now all's left is to parse the string in the AST.
Anders Johnsen <skabet@gmail.com>
parents: 102
diff changeset
881 else if (next.type == Tok.String)
7ff4bc2accf2 Lexing more types of strings. Now all's left is to parse the string in the AST.
Anders Johnsen <skabet@gmail.com>
parents: 102
diff changeset
882 return action.actOnStringExp(next);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
883
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
884 messages.report(ExpectedExp, next.location)
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
885 .fatal(ExitLevel.Parser);
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
886 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
887 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
888
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
889 Exp parseCast(ref Token _cast)
68
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
890 {
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
891 require(Tok.OpenParentheses);
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
892 auto next = lexer.next;
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
893 if(!next.isBasicType && !next.isIdentifier)
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
894 messages.report(ExpectedCastType, next.location);
68
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
895
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
896 require(Tok.CloseParentheses);
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
897 auto exp = P();
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
898 return action.actOnCastExpr(_cast, Id(next), exp);
68
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
899 }
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
900
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
901 struct UnOp
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
902 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
903 Tok tokenType;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
904 int prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
905 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
906
78
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
907 static const UnOp[] _unary =
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
908 [
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
909 {Tok.Minus, 4},
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
910 {Tok.Star, 4}
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
911 ];
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
912 UnOp* unary(Tok t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
913 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
914 foreach (ref op; _unary)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
915 if (op.tokenType == t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
916 return &op;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
917 return null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
918 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
919
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
920 struct BinOp
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
921 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
922 Tok tokenType;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
923 int prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
924 bool leftAssoc;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
925 Operator operator;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
926 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
927
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
928 static const BinOp[] _binary =
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
929 [
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
930 {Tok.Assign, 1, false, Operator.Assign},
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
931 {Tok.PlusAssign, 1, false, Operator.AddAssign},
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
932 {Tok.MinusAssign, 1, false, Operator.SubAssign},
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
933 {Tok.StarAssign, 1, false, Operator.MulAssign},
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
934 {Tok.SlashAssign, 1, false, Operator.DivAssign},
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
935 {Tok.PercentAssign, 1, false, Operator.ModAssign},
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
936
116
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
937 // =, += etc. 1
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
938 // (need special-case for the ternary operator at this level)
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
939 // ||, 2
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
940 // &&, 3
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
941 // |, 4
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
942 // &, 5
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
943 // ^, 6
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
944 // ==, !=, is, !is, 7
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
945 // <, <= etc, 7
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
946 // in, 7
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
947 // <<, >>, >>>, 8
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
948 // +, -, ~, 9
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
949 // *, /, %, 10
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
950 // unary operators here
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
951
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
952 {Tok.Eq, 2, true, Operator.Eq},
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
953 {Tok.Ne, 2, true, Operator.Ne},
10
2f493057cf17 Some support for the rest of the boolean operators
Anders Halager <halager@gmail.com>
parents: 7
diff changeset
954
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
955 {Tok.Lt, 2, true, Operator.Lt},
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
956 {Tok.Le, 2, true, Operator.Le},
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
957 {Tok.Gt, 2, true, Operator.Gt},
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
958 {Tok.Ge, 2, true, Operator.Ge},
10
2f493057cf17 Some support for the rest of the boolean operators
Anders Halager <halager@gmail.com>
parents: 7
diff changeset
959
74
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
960 {Tok.Plus, 3, true, Operator.Add},
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
961 {Tok.Minus, 3, true, Operator.Sub},
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
962
74
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
963 {Tok.Star, 5, true, Operator.Mul},
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
964 {Tok.Slash, 5, true, Operator.Div},
123
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
965 {Tok.Percent, 5, true, Operator.Mod},
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
966
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
967 {Tok.LeftShift, 8, true, Operator.LeftShift},
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
968 {Tok.RightShift, 8, true, Operator.RightShift},
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
969 {Tok.UnsignedRightShift, 8, true, Operator.UnsignedRightShift}
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
970 ];
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
971 BinOp* binary(Tok t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
972 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
973 foreach (ref op; _binary)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
974 if (op.tokenType == t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
975 return &op;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
976 return null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
977 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
978
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
979 private:
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
980
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
981 Token require(Tok t)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
982 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
983 if (lexer.peek().type != t)
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
984 messages.report(UnexpectedTokSingle, lexer.peek.location)
29
41d23f2762c3 Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
985 .arg(lexer.peek.getType)
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
986 .arg(t);
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
987 return lexer.next();
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
988 }
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
989
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
990 bool skip(Tok t)
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
991 {
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
992 if (lexer.peek().type != t)
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
993 return false;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
994 lexer.next();
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
995 return true;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
996 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
997
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
998 bool on_a(Tok t)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
999 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
1000 return lexer.peek.type == t;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
1001 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
1002
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1003 Lexer lexer;
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
1004 SourceManager sm;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1005 }
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
1006