Mercurial > projects > dang
annotate parser/Parser.d @ 150:6c5a3c0bb4fb
Make switch work again
Also added locations to statements (only filled out for switch)
Added a verification pass
Removed some comments
author | Anders Halager <halager@gmail.com> |
---|---|
date | Mon, 21 Jul 2008 20:35:03 +0200 |
parents | 8c09fdaa724e |
children | aeeef0dea14e |
rev | line source |
---|---|
1 | 1 module parser.Parser; |
2 | |
3 import lexer.Lexer, | |
4 lexer.Token; | |
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 | 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 | 14 import tango.io.Stdout, |
15 Integer = tango.text.convert.Integer; | |
16 | |
17 class Parser | |
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 | 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 | 32 { |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
84
diff
changeset
|
33 this.sm = sm; |
1 | 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 | 36 |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
37 Module m; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
38 if ( isa(Tok.Module) ) |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
39 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
40 Token _module = next; |
99
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 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
47 SLoc loc = peek.location; |
99
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 | 50 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
51 auto nes = parseAttributeInit; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
52 while( !isa(Tok.EOF) ) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
53 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
54 while ( peek.isAttribute ) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
55 nes ~= parseAttribute(nes[$-1]); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
56 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
57 foreach (d; parseDeclDef(nes[$-1].a)) |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
58 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
|
59 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
60 nes = parseAttributeScope(nes); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
61 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
62 |
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
|
63 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
|
64 } |
1 | 65 |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
66 private: |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
67 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
|
68 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
69 if ( isa (Tok.Import) ) |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
70 return parseImports(); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
71 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
72 return [parseDecl(a)]; |
1 | 73 } |
74 | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
75 Decl parseDecl(Attribute att) |
1 | 76 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
77 Token t = peek; |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
78 |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
79 if (t.isBasicType || t.isIdentifier) |
1 | 80 { |
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
|
81 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
|
82 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
|
83 int len = peekParseType; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
84 if (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
|
85 { |
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
|
86 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
|
87 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
|
88 iden = Id(require(Tok.Identifier)); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
89 if ( isa(Tok.Seperator) ) |
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
|
90 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
91 Token sep = 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
|
92 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
|
93 } |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
94 else if ( isa(Tok.Assign) ) |
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
|
95 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
96 Token assign = 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
|
97 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
|
98 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
|
99 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
|
100 } |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
101 else if ( isa(Tok.OpenParentheses) ) |
136 | 102 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
|
103 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
|
104 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
|
105 return null; |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
106 } |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
107 t = peek(len); |
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
|
108 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
|
109 .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
|
110 while(len--) |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
111 next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
112 while(peek.type != Tok.Identifier) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
113 next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
114 type = Id(peek); |
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
|
115 goto parseDeclAfterInvalidType; |
1 | 116 } |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
117 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
|
118 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
119 Id type = Id(next); |
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
|
120 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
|
121 |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
122 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
|
123 } |
136 | 124 else if (t.type == Tok.Class) |
125 { | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
126 Id type = Id(next); |
136 | 127 Id iden = Id(require(Tok.Identifier)); |
128 | |
129 return parseClass(type, iden, att); | |
130 } | |
131 else if (t.type == Tok.Interface) | |
132 { | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
133 Id type = Id(next); |
136 | 134 Id iden = Id(require(Tok.Identifier)); |
135 | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
136 return parseInterface(type, iden, att); |
136 | 137 } |
92
771ac63898e2
A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents:
89
diff
changeset
|
138 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
|
139 .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
|
140 .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
|
141 .fatal(ExitLevel.Parser); |
1 | 142 } |
143 | |
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
|
144 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
|
145 { |
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
|
146 Extern e = Extern.D; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
147 if(peek(1).type != Tok.OpenParentheses) |
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
|
148 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
|
149 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
150 next; next; |
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
|
151 |
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
|
152 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
|
153 |
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
|
154 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
|
155 { |
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
|
156 case "C": |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
157 if (peek(0).type == Tok.Plus && |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
158 peek(1).type == Tok.Plus) |
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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 } |
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
|
177 |
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
|
178 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
|
179 |
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
|
180 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
|
181 } |
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
|
182 |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
183 /** |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
184 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
|
185 */ |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
186 Decl[] parseImports() |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
187 { |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
188 Token _import = require(Tok.Import); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
189 SmallArray!(Decl) res; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
190 void addToRes(Decl d) { res ~= d; } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
191 |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
192 bool done = false; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
193 while (!done && !isa(Tok.Seperator)) |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
194 { |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
195 ModuleName mod = parseModuleName(); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
196 Token tok = peek; |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
197 switch (tok.type) |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
198 { |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
199 case Tok.Comma: |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
200 // import A, B.C; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
201 // parse another module-name |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
202 next(); |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
203 res ~= action.actOnImport(_import, mod, null); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
204 break; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
205 case Tok.Assign: |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
206 // import B = A.A; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
207 // ^- must be a single identifier |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
208 // renamed import |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
209 if (mod.packages.length != 0) |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
210 { |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
211 SLoc loc = mod.packages[0].tok.location; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
212 messages.report(RenameMustBeSingleIdent, loc); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
213 } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
214 //if (isStatic) |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
215 // error("Static imports cannot be renamed"); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
216 next(); |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
217 Id name = mod.id; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
218 mod = parseModuleName(); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
219 // create from mod and rename to `name` |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
220 res ~= action.actOnImport(_import, mod, &name); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
221 break; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
222 case Tok.Colon: |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
223 // import A : a; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
224 // selective imports, potentially import A : print = a |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
225 next(); |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
226 Decl d = action.actOnImport(_import, mod, null); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
227 // do-while on a comma: |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
228 // add explicit symbol |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
229 do |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
230 { |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
231 Id sym = parseIdentifier(); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
232 Id dummy; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
233 Id* name = null; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
234 if (skip(Tok.Assign)) |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
235 { |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
236 dummy = sym; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
237 name = &dummy; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
238 sym = parseIdentifier(); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
239 } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
240 action.addSelectiveImport(d, sym, name); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
241 |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
242 } while (skip(Tok.Comma)); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
243 require(Tok.Seperator); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
244 res ~= d; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
245 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
|
246 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
|
247 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
|
248 break; |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
249 default: |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
250 goto Lerror; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
251 } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
252 res ~= action.actOnImport(_import, mod, null); |
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 |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
255 require(Tok.Seperator); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
256 return res.safe(); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
257 Lerror: |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
258 while (!isa (Tok.Seperator)) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
259 next(); |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
260 return res.safe(); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
261 } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
262 |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
263 /** |
136 | 264 Parse interface |
265 */ | |
266 Decl parseInterface(Id type, Id iden, Attribute att) | |
267 { | |
268 auto decl = action.actOnDeclarator(type, iden, null, att); | |
269 | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
270 if (peek.type == Tok.Colon) |
136 | 271 // SuperInterfaces |
272 { | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
273 next; // Remove colon. |
136 | 274 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
275 Id identifier; |
136 | 276 |
277 // The identifier | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
278 identifier = Id(require(Tok.Identifier)); |
136 | 279 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
280 action.actOnInterfaceBaseClass(decl, identifier); |
136 | 281 |
282 // We should now have an optional list of items, each starting ',' | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
283 while (peek.type == Tok.Comma) |
136 | 284 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
285 next; // Remove comma |
136 | 286 |
287 // The identifier | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
288 identifier = Id(require(Tok.Identifier)); |
136 | 289 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
290 action.actOnInterfaceBaseClass(decl, identifier); |
136 | 291 } |
292 } | |
293 | |
294 require(Tok.OpenBrace); | |
295 | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
296 auto nes = parseAttributeInit; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
297 while( !isa(Tok.EOF) && !isa(Tok.CloseBrace) ) |
136 | 298 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
299 while ( peek.isAttribute ) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
300 nes ~= parseAttribute(nes[$-1]); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
301 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
302 auto m_decl = parseDecl(nes[$-1].a); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
303 action.actOnInterfaceMember(decl, m_decl); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
304 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
305 nes = parseAttributeScope(nes); |
136 | 306 } |
307 | |
308 require(Tok.CloseBrace); | |
309 | |
310 return decl; | |
311 } | |
312 | |
313 /** | |
314 Parse class | |
315 */ | |
316 Decl parseClass(Id type, Id iden, Attribute att) | |
317 { | |
318 auto decl = action.actOnDeclarator(type, iden, null, att); | |
319 | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
320 if (peek.type == Tok.Colon) |
136 | 321 // BaseClassList - Super class and interfaces(in that order) |
322 { | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
323 next; // Remove colon. |
136 | 324 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
325 Token protection; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
326 Id identifier; |
136 | 327 |
328 // First we expect an optional protection level. | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
329 if (peek.isBaseClassProtection) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
330 protection = next; |
136 | 331 // Then the identifier |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
332 identifier = Id(require(Tok.Identifier)); |
136 | 333 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
334 action.actOnClassBaseClass(decl, identifier); |
136 | 335 |
336 // We should now have an optional list of items, each starting ',' | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
337 while (peek.type == Tok.Comma) |
136 | 338 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
339 next; // Remove comma |
136 | 340 |
341 // First we expect an optional protection level. | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
342 if (peek.isBaseClassProtection) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
343 protection = next; |
136 | 344 // Then the identifier |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
345 identifier = Id(require(Tok.Identifier)); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
346 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
347 action.actOnClassBaseClass(decl, identifier); |
136 | 348 } |
349 } | |
350 | |
351 require(Tok.OpenBrace); | |
352 | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
353 auto nes = parseAttributeInit; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
354 while( !isa(Tok.EOF) && !isa(Tok.CloseBrace) ) |
136 | 355 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
356 while ( peek.isAttribute ) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
357 nes ~= parseAttribute(nes[$-1]); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
358 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
359 auto m_decl = parseDecl(nes[$-1].a); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
360 action.actOnClassMember(decl, m_decl); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
361 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
362 nes = parseAttributeScope(nes); |
136 | 363 } |
364 | |
365 require(Tok.CloseBrace); | |
366 | |
367 return decl; | |
368 } | |
369 | |
370 /** | |
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
|
371 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
|
372 */ |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
373 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
|
374 { |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
375 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
|
376 |
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
|
377 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
|
378 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
379 auto nes = parseAttributeInit; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
380 while( !isa(Tok.EOF) && !isa(Tok.CloseBrace) ) |
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
|
381 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
382 while ( peek.isAttribute ) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
383 nes ~= parseAttribute(nes[$-1]); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
384 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
385 auto m_decl = parseDecl(nes[$-1].a); |
102
cd066f3b539a
Parsing methods in structs - error on semantics though.
Anders Johnsen <skabet@gmail.com>
parents:
100
diff
changeset
|
386 action.actOnStructMember(decl, m_decl); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
387 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
388 nes = parseAttributeScope(nes); |
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
|
389 } |
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
|
390 |
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
|
391 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
|
392 |
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
|
393 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
|
394 } |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
395 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
396 Att[] parseAttributeInit() |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
397 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
398 Att[] nes; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
399 nes ~= Att(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
400 nes[0].nested = Scope; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
401 return nes; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
402 } |
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
|
403 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
404 Att[] parseAttributeScope(Att[] nes) |
1 | 405 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
406 while ( nes[$-1].nested == Single ) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
407 nes.length = nes.length - 1; |
1 | 408 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
409 while ( isa(Tok.CloseBrace) && nes.length > 1) |
1 | 410 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
411 while ( nes.length > 1 ) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
412 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
413 if( nes[$-1].nested == Scope ) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
414 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
415 nes.length = nes.length - 1; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
416 next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
417 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
418 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
419 nes.length = nes.length - 1; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
420 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
421 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
422 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
423 return nes; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
424 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
425 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
426 Att parseAttribute(Att last) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
427 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
428 Att _parseAttribute(Att last) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
429 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
430 Att a = last; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
431 a.nested = Single; |
5
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
432 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
433 switch(peek.type) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
434 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
435 case Tok.Public: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
436 a.a.setProtection(Protection.Public); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
437 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
438 case Tok.Private: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
439 a.a.setProtection(Protection.Private); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
440 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
441 case Tok.Package: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
442 a.a.setProtection(Protection.Package); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
443 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
444 case Tok.Protected: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
445 a.a.setProtection(Protection.Protected); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
446 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
447 case Tok.Export: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
448 a.a.setProtection(Protection.Export); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
449 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
450 case Tok.Static: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
451 a.a.setStatic; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
452 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
453 case Tok.Final: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
454 a.a.setFinal; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
455 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
456 case Tok.Const: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
457 a.a.setConst; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
458 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
459 case Tok.Abstract: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
460 a.a.setAbstract; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
461 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
462 case Tok.Override: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
463 a.a.setOverride; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
464 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
465 case Tok.Depracted: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
466 a.a.setDepracted; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
467 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
468 case Tok.Auto: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
469 a.a.setAuto; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
470 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
471 case Tok.Extern: |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
472 Extern e = parseLinkageType; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
473 a.a.setExtern(e); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
474 break; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
475 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
476 next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
477 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
478 return a; |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
479 } |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
480 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
481 Att a = _parseAttribute(last); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
482 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
483 while (peek.isAttribute) |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
484 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
485 a = parseAttribute(a); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
486 } |
45 | 487 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
488 if (peek.type == Tok.Colon) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
489 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
490 a.nested = All; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
491 next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
492 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
493 else if (peek.type == Tok.OpenBrace) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
494 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
495 a.nested = Scope; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
496 next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
497 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
498 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
499 return a; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
500 } |
45 | 501 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
502 enum : uint { Single, Scope, All } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
503 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
504 struct Att |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
505 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
506 Attribute a; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
507 uint nested; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
508 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
509 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
510 /** |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
511 Parse statements. |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
512 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
513 This is the place to attack! |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
514 */ |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
515 Stmt parseStatement() |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
516 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
517 Token t = peek; |
45 | 518 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
519 if (t.isReturn) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
520 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
521 Token ret = next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
522 Exp exp; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
523 if (peek.type != Tok.Seperator) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
524 exp = parseExpression(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
525 require(Tok.Seperator); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
526 return action.actOnReturnStmt(ret, exp); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
527 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
528 /* |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
529 if (cond) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
530 single statement | compound statement |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
531 [else |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
532 single statement | compound statement] |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
533 */ |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
534 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
535 else if (t.isIf) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
536 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
537 Token _if = next(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
538 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
539 require(Tok.OpenParentheses); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
540 Exp cond = parseExpression(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
541 require(Tok.CloseParentheses); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
542 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
543 Stmt thenB = parseSingleOrCompoundStatement(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
544 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
545 // if there is no else part we use the if as token, to have |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
546 // something than can be passed along |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
547 Token _else = _if; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
548 Stmt elseB; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
549 if (peek.type == Tok.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
|
550 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
551 _else = 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
|
552 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
|
553 } |
45 | 554 |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
555 return action.actOnIfStmt(_if, cond, thenB, _else, elseB); |
5
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
556 |
47
b0a691727a0c
Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents:
46
diff
changeset
|
557 /* |
b0a691727a0c
Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents:
46
diff
changeset
|
558 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
|
559 single statement | compound statement |
47
b0a691727a0c
Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents:
46
diff
changeset
|
560 */ |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
561 } |
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
562 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
|
563 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
564 Token _while = 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
|
565 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
|
566 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
|
567 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
|
568 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
|
569 return action.actOnWhileStmt(_while, cond, bodyStmt); |
5
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
570 |
48
b6c1dc30ca4b
Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents:
47
diff
changeset
|
571 /* |
b6c1dc30ca4b
Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents:
47
diff
changeset
|
572 One of four things: |
b6c1dc30ca4b
Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents:
47
diff
changeset
|
573 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
|
574 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
|
575 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
|
576 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
|
577 |
b6c1dc30ca4b
Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents:
47
diff
changeset
|
578 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
|
579 */ |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
580 } |
146 | 581 else if (t.isFor) |
582 { | |
583 Token _for = next; | |
584 require(Tok.OpenParentheses); | |
585 Stmt init = parseStatement(); | |
586 | |
587 Exp cond; | |
588 if ( !isa(Tok.Seperator)) | |
589 cond = parseExpression(); | |
590 require(Tok.Seperator); | |
591 | |
592 Exp incre; | |
593 if ( !isa(Tok.CloseParentheses)) | |
594 incre = parseExpression(); | |
595 require(Tok.CloseParentheses); | |
596 | |
597 Stmt bodyStmt = parseSingleOrCompoundStatement(); | |
598 return action.actOnForStmt(_for, init, cond, incre, bodyStmt); | |
599 | |
600 /* | |
601 One of four things: | |
602 A declaration of a function/variable `type id ...` | |
603 A direct assignment `id = exp;` | |
604 An indirect assignment `id.id = exp` | |
605 Some sort of free standing expression | |
606 | |
607 The assignments should be handled as binary expressions? | |
608 */ | |
609 } | |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
610 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
|
611 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
612 Token iden = peek; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
613 Token n = peek(1); |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
614 // 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
|
615 // 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
|
616 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
|
617 { |
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
618 int len = peekParseType; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
619 if(peek(len).type == Tok.Identifier && len != 0) |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
620 return action.actOnDeclStmt(parseVarDecl()); |
84 | 621 |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
622 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
|
623 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
|
624 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
|
625 } |
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
626 |
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
627 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
|
628 return action.actOnDeclStmt(parseVarDecl()); |
76
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
629 |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
630 // 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
|
631 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
|
632 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
|
633 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
|
634 } |
150 | 635 else if (t.isSwitch) |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
636 { |
150 | 637 next(); |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
638 require(Tok.OpenParentheses); |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
639 auto target = parseExpression(); |
150 | 640 auto res = action.actOnStartOfSwitchStmt(t, target); |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
641 require(Tok.CloseParentheses); |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
642 require(Tok.OpenBrace); |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
643 while (true) |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
644 { |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
645 Stmt[] statements; |
150 | 646 if (isa(Tok.Default)) |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
647 { |
150 | 648 Token _default = next(); |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
649 require(Tok.Colon); |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
650 statements.length = 0; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
651 while (peek.type != Tok.Case |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
652 && peek.type != Tok.Default |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
653 && peek.type != Tok.CloseBrace) |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
654 statements ~= parseStatement(); |
150 | 655 action.actOnDefaultStmt(res, _default, statements); |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
656 continue; |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
657 } |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
658 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
659 Token _case = peek; |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
660 if (_case.type != Tok.Case) |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
661 break; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
662 next(); |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
663 |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
664 Exp[] literals; |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
665 do |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
666 { |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
667 Exp e = parseExpression(); |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
668 literals ~= e; |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
669 } |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
670 while (skip(Tok.Comma)); |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
671 require(Tok.Colon); |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
672 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
673 while (peek.type != Tok.Case |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
674 && peek.type != Tok.Default |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
675 && peek.type != Tok.CloseBrace) |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
676 statements ~= parseStatement(); |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
677 |
150 | 678 action.actOnCaseStmt(res, _case, literals, statements); |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
679 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
680 if (peek.type == Tok.CloseBrace) |
143
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
681 break; |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
682 } |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
683 require(Tok.CloseBrace); |
d76cc5cad4fc
Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents:
140
diff
changeset
|
684 return res; |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
685 } |
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
686 else |
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
687 { |
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
688 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
|
689 { |
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
690 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
|
691 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
|
692 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
|
693 } |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
694 messages.report(UnexpectedBeginStmt, peek.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
|
695 return null; |
1 | 696 } |
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
|
697 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
|
698 return null; |
1 | 699 } |
700 | |
76
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
701 Decl parseVarDecl() |
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
702 { |
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
703 // manually hardcoded to only support "type id [= exp];" |
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
704 // 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
|
705 Id type = parseType; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
706 Id id = Id(next); |
76
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
707 Exp init; |
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
708 if (skip(Tok.Assign)) |
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
709 init = parseExpression(); |
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
710 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
|
711 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
|
712 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
|
713 return d; |
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
714 } |
9171f04dd9ee
Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents:
75
diff
changeset
|
715 |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
716 /** |
48
b6c1dc30ca4b
Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents:
47
diff
changeset
|
717 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
|
718 */ |
136 | 719 Decl parseFunc(ref Id type, ref Id name, Attribute att) |
1 | 720 { |
136 | 721 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
|
722 parseFuncArgs(func); |
1 | 723 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
724 if(peek.type == Tok.Seperator) |
82
06dda301ea61
Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents:
81
diff
changeset
|
725 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
726 next; |
82
06dda301ea61
Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents:
81
diff
changeset
|
727 return func; |
06dda301ea61
Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents:
81
diff
changeset
|
728 } |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
729 Stmt stmt = parseCompoundStatement(); |
1 | 730 |
48
b6c1dc30ca4b
Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents:
47
diff
changeset
|
731 return action.actOnEndOfFunction(func, stmt); |
1 | 732 } |
733 | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
734 /** |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
735 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
|
736 |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
737 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
|
738 */ |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
739 void parseFuncArgs(Decl func) |
1 | 740 { |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
741 require(Tok.OpenParentheses); // Remove the "(" token. |
1 | 742 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
743 while(peek.type != Tok.CloseParentheses) |
1 | 744 { |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
745 auto t = parseType(); |
82
06dda301ea61
Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents:
81
diff
changeset
|
746 Id i; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
747 if(peek.type == Tok.Identifier) |
82
06dda301ea61
Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents:
81
diff
changeset
|
748 i = parseIdentifier(); |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
749 action.addFuncArg(func, t, i); |
1 | 750 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
751 if(peek.type == Tok.Comma) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
752 next; |
1 | 753 } |
754 | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
755 require(Tok.CloseParentheses); // Remove the ")" |
1 | 756 } |
757 | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
758 /** |
45 | 759 Parse either a block, or a single statement as allowed after if, while |
760 and for. | |
761 */ | |
762 Stmt parseSingleOrCompoundStatement() | |
763 { | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
764 if (peek.type == Tok.OpenBrace) |
45 | 765 return parseCompoundStatement(); |
766 return parseStatement(); | |
767 } | |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
768 |
45 | 769 /** |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
770 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
|
771 current token. |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
772 |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
773 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
|
774 */ |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
775 Stmt parseCompoundStatement() |
1 | 776 { |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
777 Token lbrace = require(Tok.OpenBrace); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
778 SmallArray!(Stmt, 32) stmts; // Try to use the stack only |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
779 while (peek.type != Tok.CloseBrace) |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
780 stmts ~= parseStatement(); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
781 Token rbrace = require(Tok.CloseBrace); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
782 return action.actOnCompoundStmt(lbrace, rbrace, stmts.unsafe()); |
1 | 783 } |
784 | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
785 Id parseIdentifier() |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
786 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
787 Token tok = next; |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
788 |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
789 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
|
790 return Id(tok); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
791 |
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
|
792 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
|
793 .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
|
794 .arg(Tok.Identifier); |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
795 } |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
796 |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
797 ModuleName parseModuleName() |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
798 { |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
799 auto id = parseIdentifier(); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
800 ModuleName mod; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
801 while (skip(Tok.Dot)) |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
802 { |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
803 mod.packages ~= id; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
804 if (peek.type != Tok.Identifier) { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
805 messages.report(ExpectedIdAfterPackage, peek.location); |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
806 goto Lerror; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
807 } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
808 id = parseIdentifier(); |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
809 } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
810 mod.id = id; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
811 return mod; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
812 Lerror: |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
813 while (!skip(Tok.Seperator)) |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
814 next(); |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
815 return mod; |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
816 } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
817 |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
818 |
75 | 819 /** |
820 Parse a type - this includes pointer and array(at some point) types. | |
821 */ | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
822 Id parseType() |
1 | 823 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
824 Token type = next; |
1 | 825 |
75 | 826 Id currentType; |
827 | |
828 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
|
829 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
|
830 |
75 | 831 currentType = Id(type); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
832 type = peek; |
75 | 833 |
81 | 834 while(type.type == Tok.Star || type.type == Tok.OpenBracket) |
75 | 835 { |
81 | 836 if(type.type == Tok.Star) |
837 { | |
838 currentType = PointerId(currentType); | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
839 next; |
81 | 840 } |
841 else | |
842 { | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
843 next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
844 if(peek.type == Tok.Integer) |
136 | 845 currentType = StaticArrayId( |
846 currentType, | |
847 action.actOnNumericConstant( | |
848 require(Tok.Integer))); | |
81 | 849 require(Tok.CloseBracket); |
850 | |
851 } | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
852 type = peek; |
75 | 853 } |
854 | |
855 return currentType; | |
1 | 856 } |
857 | |
84 | 858 int peekParseType() |
859 { | |
860 int i; | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
861 Token type = peek(i); |
84 | 862 |
863 Id currentType; | |
864 | |
865 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
|
866 return 0; |
84 | 867 |
868 currentType = Id(type); | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
869 type = peek(++i); |
84 | 870 |
871 while(type.type == Tok.Star || type.type == Tok.OpenBracket) | |
872 { | |
873 if(type.type == Tok.Star) | |
874 { | |
875 i++; | |
876 } | |
877 else | |
878 { | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
879 if(peek(i++).type != Tok.OpenBracket) |
84 | 880 return 0; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
881 if(peek(i).type == Tok.Integer) |
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
|
882 { |
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
|
883 i++; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
884 if(peek(i++).type != Tok.CloseBracket) |
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
|
885 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
|
886 } |
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
|
887 else |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
888 if(peek(i++).type != Tok.CloseBracket) |
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
|
889 return 0; |
84 | 890 |
891 } | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
892 type = peek(i); |
84 | 893 } |
894 | |
895 return i; | |
896 } | |
897 | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
898 private: |
1 | 899 // -- Expression parsing -- // |
83
9e90694f5da0
Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents:
82
diff
changeset
|
900 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
|
901 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
902 switch(peek.type) |
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
|
903 { |
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
|
904 case Tok.Dot: |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
905 switch(peek(1).type) |
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
|
906 { |
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
|
907 case Tok.Identifier: |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
908 Token op = next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
909 Id member = Id(next); |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
910 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
|
911 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
|
912 default: |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
913 Token t = 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
|
914 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
|
915 } |
83
9e90694f5da0
Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents:
82
diff
changeset
|
916 case Tok.OpenBracket: |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
917 Token open = next; |
83
9e90694f5da0
Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents:
82
diff
changeset
|
918 Exp index = parseExpression(); |
9e90694f5da0
Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents:
82
diff
changeset
|
919 Token close = require(Tok.CloseBracket); |
9e90694f5da0
Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents:
82
diff
changeset
|
920 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
|
921 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
|
922 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
|
923 } |
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
|
924 } |
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
|
925 |
1 | 926 Exp parseExpression(int p = 0) |
927 { | |
928 auto exp = P(); | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
929 Token n = peek(); |
1 | 930 BinOp* op = null; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
931 while ((op = binary(n.type)) != null && op.prec >= p) |
1 | 932 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
933 next(); |
1 | 934 int q = op.leftAssoc? 1 + op.prec : op.prec; |
935 auto exp2 = parseExpression(q); | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
936 exp = action.actOnBinaryOp(n.location, op.operator, exp, exp2); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
937 n = peek(); |
1 | 938 } |
939 | |
940 return exp; | |
941 } | |
942 | |
943 Exp P() | |
944 { | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
945 Token n = next(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
946 if (auto op = unary(n.type)) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
947 return action.actOnUnaryOp(n, parseExpression(op.prec)); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
948 else if (n.type == Tok.OpenParentheses) |
1 | 949 { |
950 auto e = parseExpression(0); | |
951 require(Tok.CloseParentheses); | |
952 return e; | |
953 } | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
954 else if (n.type == Tok.Identifier) |
1 | 955 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
956 Exp value = action.actOnIdentifierExp(Id(n)); |
83
9e90694f5da0
Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents:
82
diff
changeset
|
957 Exp iden = parsePostfixExp(value); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
958 switch(peek.type) |
1 | 959 { |
960 case Tok.OpenParentheses: | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
961 Token lp = next; |
48
b6c1dc30ca4b
Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents:
47
diff
changeset
|
962 SmallArray!(Exp, 8) args; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
963 while(peek.type != Tok.CloseParentheses) |
1 | 964 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
965 if(peek.type == Tok.Comma) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
966 next; |
1 | 967 args ~= parseExpression(); |
968 } | |
969 | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
970 Token rp = next(); |
48
b6c1dc30ca4b
Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents:
47
diff
changeset
|
971 return action.actOnCallExpr(iden, lp, args.unsafe(), rp); |
1 | 972 |
973 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
|
974 return iden; |
1 | 975 } |
976 } | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
977 else if (n.type == Tok.Cast) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
978 return parseCast(n); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
979 else if (n.type == Tok.Integer) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
980 return action.actOnNumericConstant(n); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
981 else if (n.type == Tok.String) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
982 return action.actOnStringExp(n); |
1 | 983 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
984 messages.report(ExpectedExp, n.location) |
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
|
985 .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
|
986 return null; |
1 | 987 } |
988 | |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
84
diff
changeset
|
989 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
|
990 { |
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
|
991 require(Tok.OpenParentheses); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
992 auto n = next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
993 if(!n.isBasicType && !n.isIdentifier) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
994 messages.report(ExpectedCastType, n.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
|
995 |
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
|
996 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
|
997 auto exp = P(); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
998 return action.actOnCastExpr(_cast, Id(n), 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
|
999 } |
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
|
1000 |
1 | 1001 struct UnOp |
1002 { | |
1003 Tok tokenType; | |
1004 int prec; | |
1005 } | |
1006 | |
78
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
76
diff
changeset
|
1007 static const UnOp[] _unary = |
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
76
diff
changeset
|
1008 [ |
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
76
diff
changeset
|
1009 {Tok.Minus, 4}, |
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
76
diff
changeset
|
1010 {Tok.Star, 4} |
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
76
diff
changeset
|
1011 ]; |
1 | 1012 UnOp* unary(Tok t) |
1013 { | |
1014 foreach (ref op; _unary) | |
1015 if (op.tokenType == t) | |
1016 return &op; | |
1017 return null; | |
1018 } | |
1019 | |
1020 struct BinOp | |
1021 { | |
1022 Tok tokenType; | |
1023 int prec; | |
1024 bool leftAssoc; | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
1025 Operator operator; |
1 | 1026 } |
1027 | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
1028 static const BinOp[] _binary = |
1 | 1029 [ |
126
c3b24e7e8cf8
Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents:
123
diff
changeset
|
1030 {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
|
1031 {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
|
1032 {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
|
1033 {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
|
1034 {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
|
1035 {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
|
1036 |
116
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1037 // =, += etc. 1 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1038 // (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
|
1039 // ||, 2 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1040 // &&, 3 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1041 // |, 4 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1042 // &, 5 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1043 // ^, 6 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1044 // ==, !=, is, !is, 7 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1045 // <, <= etc, 7 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1046 // in, 7 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1047 // <<, >>, >>>, 8 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1048 // +, -, ~, 9 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1049 // *, /, %, 10 |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1050 // unary operators here |
0cd8d6ab3f89
Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents:
104
diff
changeset
|
1051 |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
1052 {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
|
1053 {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
|
1054 |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
1055 {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
|
1056 {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
|
1057 {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
|
1058 {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
|
1059 |
74
192da4976daa
Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents:
71
diff
changeset
|
1060 {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
|
1061 {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
|
1062 |
74
192da4976daa
Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents:
71
diff
changeset
|
1063 {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
|
1064 {Tok.Slash, 5, true, Operator.Div}, |
123 | 1065 {Tok.Percent, 5, true, Operator.Mod}, |
1066 | |
1067 {Tok.LeftShift, 8, true, Operator.LeftShift}, | |
1068 {Tok.RightShift, 8, true, Operator.RightShift}, | |
1069 {Tok.UnsignedRightShift, 8, true, Operator.UnsignedRightShift} | |
1 | 1070 ]; |
1071 BinOp* binary(Tok t) | |
1072 { | |
1073 foreach (ref op; _binary) | |
1074 if (op.tokenType == t) | |
1075 return &op; | |
1076 return null; | |
1077 } | |
1078 | |
1079 private: | |
1080 | |
36
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
33
diff
changeset
|
1081 Token require(Tok t) |
1 | 1082 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1083 if (peek().type != t) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1084 messages.report(UnexpectedTokSingle, peek.location) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1085 .arg(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
|
1086 .arg(t); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1087 return next(); |
36
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
33
diff
changeset
|
1088 } |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
33
diff
changeset
|
1089 |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
33
diff
changeset
|
1090 bool skip(Tok t) |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
33
diff
changeset
|
1091 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1092 if (peek().type != t) |
36
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
33
diff
changeset
|
1093 return false; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1094 next(); |
36
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
33
diff
changeset
|
1095 return true; |
1 | 1096 } |
1097 | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1098 bool isa(Tok t) |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
1099 { |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1100 return peek.type == t; |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
1101 } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
1102 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1103 Token next() |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1104 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1105 return lexer.next; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1106 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1107 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1108 Token peek(int i = 0) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1109 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1110 return lexer.peek(i); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1111 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1112 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
143
diff
changeset
|
1113 Lexer lexer; |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
84
diff
changeset
|
1114 SourceManager sm; |
1 | 1115 } |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
1116 |