annotate parser/Parser.d @ 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.
author Anders Johnsen <skabet@gmail.com>
date Sun, 20 Jul 2008 23:23:56 +0200
parents 2be29b296081
children d76cc5cad4fc
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
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
136
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
137 if (t.isBasicType || t.isIdentifier)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
138 {
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
139 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
140 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
141 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
142 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
143 {
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
144 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
145 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
146 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
147 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
148 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
149 {
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
150 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
151 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
152 }
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 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
154 {
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 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
156 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
157 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
158 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
159 }
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
160 else if (next.type == Tok.OpenParentheses)
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
161 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
162 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
163 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
164 return null;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
165 }
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
166 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
167 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
168 .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
169 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
170 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
171 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
172 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
173 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
174 goto parseDeclAfterInvalidType;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
175 }
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
176 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
177 {
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
178 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
179 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
180
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
181 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
182 }
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
183 else if (t.type == Tok.Class)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
184 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
185 Id type = Id(lexer.next);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
186 Id iden = Id(require(Tok.Identifier));
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
187
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
188 return parseClass(type, iden, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
189 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
190 else if (t.type == Tok.Interface)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
191 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
192 Id type = Id(lexer.next);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
193 Id iden = Id(require(Tok.Identifier));
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
194
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
195 return parseClass(type, iden, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
196 }
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
197 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
198 .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
199 .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
200 .fatal(ExitLevel.Parser);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
201 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
202
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
203 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
204 {
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 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
206 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
207 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
208
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 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
210
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 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
212
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 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
214 {
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 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
216 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
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234 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
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
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 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
238
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 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
240 }
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
241
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
242 /**
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
243 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
244 */
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
245 Decl[] parseImports()
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
246 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
247 Token _import = require(Tok.Import);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
248 SmallArray!(Decl) res;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
249 void addToRes(Decl d) { res ~= d; }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
250
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
251 bool done = false;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
252 while (!done && !on_a(Tok.Seperator))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
253 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
254 ModuleName mod = parseModuleName();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
255 Token tok = lexer.peek;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
256 switch (tok.type)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
257 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
258 case Tok.Comma:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
259 // import A, B.C;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
260 // parse another module-name
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
261 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
262 res ~= action.actOnImport(_import, mod, null);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
263 break;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
264 case Tok.Assign:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
265 // import B = A.A;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
266 // ^- must be a single identifier
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
267 // renamed import
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
268 if (mod.packages.length != 0)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
269 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
270 SLoc loc = mod.packages[0].tok.location;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
271 messages.report(RenameMustBeSingleIdent, loc);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
272 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
273 //if (isStatic)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
274 // error("Static imports cannot be renamed");
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
275 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
276 Id name = mod.id;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
277 mod = parseModuleName();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
278 // create from mod and rename to `name`
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
279 res ~= action.actOnImport(_import, mod, &name);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
280 break;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
281 case Tok.Colon:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
282 // import A : a;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
283 // selective imports, potentially import A : print = a
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
284 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
285 Decl d = action.actOnImport(_import, mod, null);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
286 // do-while on a comma:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
287 // add explicit symbol
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
288 do
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
289 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
290 Id sym = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
291 Id dummy;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
292 Id* name = null;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
293 if (skip(Tok.Assign))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
294 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
295 dummy = sym;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
296 name = &dummy;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
297 sym = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
298 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
299 action.addSelectiveImport(d, sym, name);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
300
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
301 } while (skip(Tok.Comma));
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
302 require(Tok.Seperator);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
303 res ~= d;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
304 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
305 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
306 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
307 break;
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
308 default:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
309 goto Lerror;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
310 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
311 res ~= action.actOnImport(_import, mod, null);
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
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
314 require(Tok.Seperator);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
315 return res.safe();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
316 Lerror:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
317 while (!on_a (Tok.Seperator))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
318 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
319 return res.safe();
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
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
322 /**
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
323 Parse interface
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
324 */
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
325 Decl parseInterface(Id type, Id iden, Attribute att)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
326 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
327 auto decl = action.actOnDeclarator(type, iden, null, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
328
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
329 if (lexer.peek.type == Tok.Colon)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
330 // SuperInterfaces
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
331 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
332 lexer.next; // Remove colon.
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
333
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
334 Token identifier;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
335
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
336 // The identifier
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
337 identifier = require(Tok.Identifier);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
338
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
339 // FIXME: Register Interface here
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
340
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
341 // We should now have an optional list of items, each starting ','
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
342 while (lexer.peek.type == Tok.Comma)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
343 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
344 lexer.next; // Remove comma
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
345
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
346 // The identifier
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
347 identifier = require(Tok.Identifier);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
348
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
349 // FIXME: Register Interface here
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
353 require(Tok.OpenBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
354
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
355 Attribute a;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
356 while(lexer.peek.isBasicType || lexer.peek.isIdentifier || lexer.peek.isAttribute)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
357 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
358 auto m_decl = parseDecl(&a);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
359 action.actOnStructMember(decl, m_decl);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
360 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
361
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
362 require(Tok.CloseBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
363
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
364 return decl;
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 /**
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
368 Parse class
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
369 */
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
370 Decl parseClass(Id type, Id iden, Attribute att)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
371 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
372 auto decl = action.actOnDeclarator(type, iden, null, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
373
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
374 if (lexer.peek.type == Tok.Colon)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
375 // BaseClassList - Super class and interfaces(in that order)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
376 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
377 lexer.next; // Remove colon.
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
378
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
379 Token protection, identifier;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
380
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
381 // First we expect an optional protection level.
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
382 if (lexer.peek.isBaseClassProtection)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
383 protection = lexer.next;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
384 // Then the identifier
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
385 identifier = require(Tok.Identifier);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
386
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
387 // FIXME: Register Interface here
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
388
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
389 // We should now have an optional list of items, each starting ','
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
390 while (lexer.peek.type == Tok.Comma)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
391 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
392 lexer.next; // Remove comma
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
393
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
394 // First we expect an optional protection level.
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
395 if (lexer.peek.isBaseClassProtection)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
396 protection = lexer.next;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
397 // Then the identifier
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
398 identifier = require(Tok.Identifier);
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
402 require(Tok.OpenBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
403
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
404 Attribute a;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
405 while(lexer.peek.isBasicType || lexer.peek.isIdentifier || lexer.peek.isAttribute)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
406 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
407 auto m_decl = parseDecl(&a);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
408 action.actOnStructMember(decl, m_decl); // FIXME: Should call actOnClassMember
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
409 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
410
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
411 require(Tok.CloseBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
412
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
413 return decl;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
414 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
415
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
416 /**
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
417 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
418 */
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
419 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
420 {
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
421 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
422
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 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
424
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
425 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
426 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
427 {
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
428 auto m_decl = parseDecl(&a);
102
cd066f3b539a Parsing methods in structs - error on semantics though.
Anders Johnsen <skabet@gmail.com>
parents: 100
diff changeset
429 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
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
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 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
433
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 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
435 }
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
436
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
437 /**
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
438 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
439
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
440 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
441 */
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
442 Stmt parseStatement()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
443 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
444 Token t = lexer.peek;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
445
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
446 if (t.isReturn)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
447 {
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
448 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
449 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
450 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
451 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
452 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
453 return action.actOnReturnStmt(ret, exp);
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
454
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
455 /*
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
456 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
457 single statement | compound statement
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
458 [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
459 single statement | compound statement]
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
460 */
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
461 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
462 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
463 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
464 Token _if = lexer.next();
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
465
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
466 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
467 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
468 require(Tok.CloseParentheses);
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
469
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
470 Stmt thenB = parseSingleOrCompoundStatement();
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
471
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
472 // 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
473 // 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
474 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
475 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
476 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
477 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
478 _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
479 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
480 }
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
481
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
482 return action.actOnIfStmt(_if, cond, thenB, _else, elseB);
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
483
47
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
484 /*
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
485 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
486 single statement | compound statement
47
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
487 */
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
488 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
489 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
490 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
491 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
492 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
493 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
494 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
495 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
496 return action.actOnWhileStmt(_while, cond, bodyStmt);
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
497
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
498 /*
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
499 One of four things:
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
500 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
501 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
502 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
503 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
504
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
505 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
506 */
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
507 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
508 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
509 {
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 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
511 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
512 // 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
513 // 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
514 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
515 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
516 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
517 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
518 return action.actOnDeclStmt(parseVarDecl());
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
519
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
520 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
521 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
522 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
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
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
525 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
526 return action.actOnDeclStmt(parseVarDecl());
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
527
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
528 // 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
529 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
530 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
531 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
532 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
533 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
534 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
535 messages.report(UnexpectedTok, 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
536 return null;
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
537 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
538 else
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
539 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
540 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
541 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
542 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
543 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
544 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
545 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
546 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
547 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
548 }
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
549 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
550 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
551 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
552
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
553 Decl parseVarDecl()
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
554 {
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
555 // manually hardcoded to only support "type id [= exp];"
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
556 // 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
557 Id type = parseType;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
558 Id id = Id(lexer.next);
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
559 Exp init;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
560 if (skip(Tok.Assign))
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
561 init = parseExpression();
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
562 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
563 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
564 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
565 return d;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
566 }
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
567
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
568 /**
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
569 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
570 */
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
571 Decl parseFunc(ref Id type, ref Id name, Attribute att)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
572 {
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
573 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
574 parseFuncArgs(func);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
575
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
576 if(lexer.peek.type == Tok.Seperator)
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
577 {
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
578 lexer.next;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
579 return func;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
580 }
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
581 Stmt stmt = parseCompoundStatement();
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
582
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
583 return action.actOnEndOfFunction(func, stmt);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
584 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
585
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
586 /**
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
587 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
588
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
589 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
590 */
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
591 void parseFuncArgs(Decl func)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
592 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
593 require(Tok.OpenParentheses); // Remove the "(" token.
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
594
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
595 while(lexer.peek.type != Tok.CloseParentheses)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
596 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
597 auto t = parseType();
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
598 Id i;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
599 if(lexer.peek.type == Tok.Identifier)
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
600 i = parseIdentifier();
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
601 action.addFuncArg(func, t, i);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
602
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
603 if(lexer.peek.type == Tok.Comma)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
604 lexer.next;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
605 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
606
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
607 require(Tok.CloseParentheses); // Remove the ")"
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
608 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
609
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
610 /**
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
611 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
612 and for.
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
613 */
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
614 Stmt parseSingleOrCompoundStatement()
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
615 {
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
616 if (lexer.peek.type == Tok.OpenBrace)
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
617 return parseCompoundStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
618 return parseStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
619 }
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
620
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
621 /**
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
622 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
623 current token.
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
624
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
625 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
626 */
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
627 Stmt parseCompoundStatement()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
628 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
629 Token lbrace = require(Tok.OpenBrace);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
630 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
631 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
632 stmts ~= parseStatement();
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
633 Token rbrace = require(Tok.CloseBrace);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
634 return action.actOnCompoundStmt(lbrace, rbrace, stmts.unsafe());
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
635 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
636
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
637 Id parseIdentifier()
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 Token tok = lexer.next;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
640
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
641 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
642 return Id(tok);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
643
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
644 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
645 .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
646 .arg(Tok.Identifier);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
647 }
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
648
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
649 ModuleName parseModuleName()
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
650 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
651 auto id = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
652 ModuleName mod;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
653 while (skip(Tok.Dot))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
654 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
655 mod.packages ~= id;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
656 if (lexer.peek.type != Tok.Identifier) {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
657 messages.report(ExpectedIdAfterPackage, lexer.peek.location);
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
658 goto Lerror;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
659 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
660 id = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
661 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
662 mod.id = id;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
663 return mod;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
664 Lerror:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
665 while (!skip(Tok.Seperator))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
666 lexer.next();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
667 return mod;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
668 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
669
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
670
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
671 /**
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
672 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
673 */
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
674 Id parseType()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
675 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
676 Token type = lexer.next;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
677
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
678 Id currentType;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
679
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
680 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
681 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
682
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
683 currentType = Id(type);
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
684 type = lexer.peek;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
685
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
686 while(type.type == Tok.Star || type.type == Tok.OpenBracket)
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
687 {
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
688 if(type.type == Tok.Star)
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
689 {
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
690 currentType = PointerId(currentType);
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
691 lexer.next;
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
692 }
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
693 else
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
694 {
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
695 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
696 if(lexer.peek.type == Tok.Integer)
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
697 currentType = StaticArrayId(
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
698 currentType,
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
699 action.actOnNumericConstant(
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
700 require(Tok.Integer)));
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
701 require(Tok.CloseBracket);
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
702
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
703 }
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
704 type = lexer.peek;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
705 }
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
706
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
707 return currentType;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
708 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
709
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
710 int peekParseType()
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
711 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
712 int i;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
713 Token type = lexer.peek(i);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
714
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
715 Id currentType;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
716
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
717 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
718 return 0;
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
719
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
720 currentType = Id(type);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
721 type = lexer.peek(++i);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
722
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
723 while(type.type == Tok.Star || type.type == Tok.OpenBracket)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
724 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
725 if(type.type == Tok.Star)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
726 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
727 i++;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
728 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
729 else
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
730 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
731 if(lexer.peek(i++).type != Tok.OpenBracket)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
732 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
733 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
734 {
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
735 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
736 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
737 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
738 }
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
739 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
740 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
741 return 0;
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
742
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
743 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
744 type = lexer.peek(i);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
745 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
746
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
747 return i;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
748 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
749
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
750 private:
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
751 // -- Expression parsing -- //
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
752 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
753 {
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
754 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
755 {
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
756 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
757 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
758 {
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
759 case Tok.Identifier:
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
760 Token op = lexer.next;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
761 Id member = Id(lexer.next);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
762 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
763 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
764 default:
30
3147a52d1247 Ooops.. should have compiled before commit.. now works again
Anders Halager <halager@gmail.com>
parents: 29
diff changeset
765 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
766 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
767 }
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
768 case Tok.OpenBracket:
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
769 Token open = lexer.next;
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
770 Exp index = parseExpression();
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
771 Token close = require(Tok.CloseBracket);
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
772 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
773 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
774 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
775 }
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
776 }
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
777
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
778 Exp parseExpression(int p = 0)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
779 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
780 auto exp = P();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
781 Token next = lexer.peek();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
782 BinOp* op = null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
783 while ((op = binary(next.type)) != null && op.prec >= p)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
784 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
785 lexer.next();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
786 int q = op.leftAssoc? 1 + op.prec : op.prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
787 auto exp2 = parseExpression(q);
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
788 exp = action.actOnBinaryOp(next.location, op.operator, exp, exp2);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
789 next = lexer.peek();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
790 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
791
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
792 return exp;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
793 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
794
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
795 Exp P()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
796 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
797 Token next = lexer.next();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
798 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
799 return action.actOnUnaryOp(next, parseExpression(op.prec));
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
800 else if (next.type == Tok.OpenParentheses)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
801 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
802 auto e = parseExpression(0);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
803 require(Tok.CloseParentheses);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
804 return e;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
805 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
806 else if (next.type == Tok.Identifier)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
807 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
808 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
809 Exp iden = parsePostfixExp(value);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
810 switch(lexer.peek.type)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
811 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
812 case Tok.OpenParentheses:
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
813 Token lp = lexer.next;
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
814 SmallArray!(Exp, 8) args;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
815 while(lexer.peek.type != Tok.CloseParentheses)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
816 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
817 if(lexer.peek.type == Tok.Comma)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
818 lexer.next;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
819 args ~= parseExpression();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
820 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
821
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
822 Token rp = lexer.next();
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
823 return action.actOnCallExpr(iden, lp, args.unsafe(), rp);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
824
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
825 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
826 return iden;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
827 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
828 }
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
829 else if (next.type == Tok.Cast)
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
830 return parseCast(next);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
831 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
832 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
833 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
834 return action.actOnStringExp(next);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
835
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
836 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
837 .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
838 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
839 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
840
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
841 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
842 {
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
843 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
844 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
845 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
846 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
847
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
848 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
849 auto exp = P();
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
850 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
851 }
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
852
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
853 struct UnOp
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
854 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
855 Tok tokenType;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
856 int prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
857 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
858
78
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
859 static const UnOp[] _unary =
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
860 [
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
861 {Tok.Minus, 4},
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
862 {Tok.Star, 4}
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
863 ];
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
864 UnOp* unary(Tok t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
865 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
866 foreach (ref op; _unary)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
867 if (op.tokenType == t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
868 return &op;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
869 return null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
870 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
871
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
872 struct BinOp
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
873 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
874 Tok tokenType;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
875 int prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
876 bool leftAssoc;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
877 Operator operator;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
878 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
879
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
880 static const BinOp[] _binary =
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
881 [
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
882 {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
883 {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
884 {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
885 {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
886 {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
887 {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
888
116
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
889 // =, += etc. 1
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
890 // (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
891 // ||, 2
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
892 // &&, 3
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
893 // |, 4
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
894 // &, 5
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
895 // ^, 6
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
896 // ==, !=, is, !is, 7
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
897 // <, <= etc, 7
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
898 // in, 7
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
899 // <<, >>, >>>, 8
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
900 // +, -, ~, 9
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
901 // *, /, %, 10
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
902 // unary operators here
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
903
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
904 {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
905 {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
906
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
907 {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
908 {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
909 {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
910 {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
911
74
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
912 {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
913 {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
914
74
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
915 {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
916 {Tok.Slash, 5, true, Operator.Div},
123
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
917 {Tok.Percent, 5, true, Operator.Mod},
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
918
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
919 {Tok.LeftShift, 8, true, Operator.LeftShift},
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
920 {Tok.RightShift, 8, true, Operator.RightShift},
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
921 {Tok.UnsignedRightShift, 8, true, Operator.UnsignedRightShift}
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
922 ];
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
923 BinOp* binary(Tok t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
924 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
925 foreach (ref op; _binary)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
926 if (op.tokenType == t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
927 return &op;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
928 return null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
929 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
930
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
931 private:
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
932
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
933 Token require(Tok t)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
934 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
935 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
936 messages.report(UnexpectedTokSingle, lexer.peek.location)
29
41d23f2762c3 Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
937 .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
938 .arg(t);
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
939 return lexer.next();
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
940 }
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
941
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
942 bool skip(Tok t)
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
943 {
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
944 if (lexer.peek().type != t)
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
945 return false;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
946 lexer.next();
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
947 return true;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
948 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
949
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
950 bool on_a(Tok t)
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
951 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
952 return lexer.peek.type == t;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
953 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
954
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
955 Lexer lexer;
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
956 SourceManager sm;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
957 }
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
958