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