annotate parser/Parser.d @ 146:8c09fdaa724e

Parsing for-loop.
author Anders Johnsen <skabet@gmail.com>
date Mon, 21 Jul 2008 18:16:50 +0200
parents 6e6355fb5f0f
children 6ec686d9c87d 6c5a3c0bb4fb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1 module parser.Parser;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
2
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
3 import lexer.Lexer,
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
4 lexer.Token;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
5
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
6 import parser.Action;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
7
126
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
8 import basic.Message,
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
9 basic.Attribute;
21
0fb2d13dce37 Now working with gdc also (gdc use reverse paremeter validating on function calls)
johnsen@johnsen-laptop
parents: 12
diff changeset
10
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
11 import basic.SmallArray,
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
12 basic.SourceManager;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
13
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
14 import tango.io.Stdout,
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
15 Integer = tango.text.convert.Integer;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
16
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
17 class Parser
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
18 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
19 Action action;
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
20 MessageHandler messages;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
21 alias Object Exp;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
22 alias Object Stmt;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
23 alias Object Decl;
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
24 alias Object Module;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
25
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
26 this(MessageHandler messages)
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
27 {
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
28 this.messages = messages;
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
29 }
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
30
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
31 Module parse(SourceManager sm, Lexer lexer, Action act)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
32 {
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
33 this.sm = sm;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
34 this.lexer = lexer;
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
35 this.action = act;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
36
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
37 Module m;
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
73 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
74
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
75 Decl parseDecl(Attribute att)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
124 else if (t.type == Tok.Class)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
125 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
126 Id type = Id(next);
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
127 Id iden = Id(require(Tok.Identifier));
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
128
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
129 return parseClass(type, iden, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
130 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
131 else if (t.type == Tok.Interface)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
132 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
133 Id type = Id(next);
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
134 Id iden = Id(require(Tok.Identifier));
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
135
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
136 return parseInterface(type, iden, att);
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
137 }
92
771ac63898e2 A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents: 89
diff changeset
138 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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
142 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
264 Parse interface
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
265 */
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
266 Decl parseInterface(Id type, Id iden, Attribute att)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
267 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
268 auto decl = action.actOnDeclarator(type, iden, null, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
269
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
270 if (peek.type == Tok.Colon)
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
271 // SuperInterfaces
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
272 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
273 next; // Remove colon.
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
274
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
275 Id identifier;
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
276
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
277 // The identifier
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
278 identifier = Id(require(Tok.Identifier));
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
279
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
280 action.actOnInterfaceBaseClass(decl, identifier);
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
281
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
284 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
285 next; // Remove comma
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
286
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
287 // The identifier
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
288 identifier = Id(require(Tok.Identifier));
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
289
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
290 action.actOnInterfaceBaseClass(decl, identifier);
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
291 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
292 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
293
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
294 require(Tok.OpenBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
306 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
307
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
308 require(Tok.CloseBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
309
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
310 return decl;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
311 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
312
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
313 /**
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
314 Parse class
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
315 */
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
316 Decl parseClass(Id type, Id iden, Attribute att)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
317 {
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
318 auto decl = action.actOnDeclarator(type, iden, null, att);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
319
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
320 if (peek.type == Tok.Colon)
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
321 // BaseClassList - Super class and interfaces(in that order)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
322 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
323 next; // Remove colon.
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
327
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
333
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
334 action.actOnClassBaseClass(decl, identifier);
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
335
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
338 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
339 next; // Remove comma
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
340
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
348 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
349 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
350
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
351 require(Tok.OpenBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
363 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
364
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
365 require(Tok.CloseBrace);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
366
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
367 return decl;
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
368 }
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
369
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
408
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
409 while ( isa(Tok.CloseBrace) && nes.length > 1)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
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
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
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
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
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
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
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
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
581 else if (t.isFor)
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
582 {
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
583 Token _for = next;
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
584 require(Tok.OpenParentheses);
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
585 Stmt init = parseStatement();
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
586
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
587 Exp cond;
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
588 if ( !isa(Tok.Seperator))
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
589 cond = parseExpression();
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
590 require(Tok.Seperator);
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
591
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
592 Exp incre;
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
593 if ( !isa(Tok.CloseParentheses))
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
594 incre = parseExpression();
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
595 require(Tok.CloseParentheses);
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
596
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
597 Stmt bodyStmt = parseSingleOrCompoundStatement();
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
598 return action.actOnForStmt(_for, init, cond, incre, bodyStmt);
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
599
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
600 /*
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
601 One of four things:
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
602 A declaration of a function/variable `type id ...`
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
603 A direct assignment `id = exp;`
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
604 An indirect assignment `id.id = exp`
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
605 Some sort of free standing expression
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
606
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
607 The assignments should be handled as binary expressions?
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
608 */
8c09fdaa724e Parsing for-loop.
Anders Johnsen <skabet@gmail.com>
parents: 144
diff changeset
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
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
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 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
635 else if(t.isSwitch)
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
636 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
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();
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
640 auto res = action.actOnStartOfSwitchStmt(target);
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;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
646 if (skip(Tok.Default))
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
647 {
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
648 require(Tok.Colon);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
649 statements.length = 0;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
650 while (peek.type != Tok.Case
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
651 && peek.type != Tok.Default
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
652 && peek.type != Tok.CloseBrace)
143
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
653 statements ~= parseStatement();
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
654 action.actOnDefaultStmt(res, statements);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
655 continue;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
656 }
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
657
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
658 Token _case = peek;
143
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
659 if (_case.type != Tok.Case)
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
660 break;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
661 next();
143
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
662
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
663 Exp[] literals;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
664 do
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
665 {
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
666 Exp e = parseExpression();
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
667 // IntegerLit lit = cast(IntegerLit)e;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
668 // if (lit is null)
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
669 // messages.report(CaseValueMustBeInt, peek.location).arg(next.getType);
143
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
670 // else
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
671 literals ~= e;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
672 }
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
673 while (skip(Tok.Comma));
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
674 require(Tok.Colon);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
675
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
676 while (peek.type != Tok.Case
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
677 && peek.type != Tok.Default
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
678 && peek.type != Tok.CloseBrace)
143
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
679 statements ~= parseStatement();
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
680
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
681 action.actOnCaseStmt(res, literals, statements);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
682
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
683 if (peek.type == Tok.CloseBrace)
143
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
684 break;
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
685 }
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
686 require(Tok.CloseBrace);
d76cc5cad4fc Added partial support for switches.
Anders Johnsen <skabet@gmail.com>
parents: 140
diff changeset
687 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
688 }
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
689 else
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
690 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
691 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
692 {
c3b24e7e8cf8 Carius changes to the parser. Parsing attributes, lexing many keywords(not all yet).
Anders Johnsen <skabet@gmail.com>
parents: 123
diff changeset
693 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
694 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
695 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
696 }
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
697 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
698 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
699 }
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
700 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
701 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
702 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
703
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
704 Decl parseVarDecl()
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
705 {
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
706 // manually hardcoded to only support "type id [= exp];"
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
707 // 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
708 Id type = parseType;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
709 Id id = Id(next);
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
710 Exp init;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
711 if (skip(Tok.Assign))
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
712 init = parseExpression();
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
713 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
714 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
715 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
716 return d;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
717 }
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
718
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
719 /**
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
720 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
721 */
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
722 Decl parseFunc(ref Id type, ref Id name, Attribute att)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
723 {
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
724 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
725 parseFuncArgs(func);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
726
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
727 if(peek.type == Tok.Seperator)
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
728 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
729 next;
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
730 return func;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
731 }
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
732 Stmt stmt = parseCompoundStatement();
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
733
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
734 return action.actOnEndOfFunction(func, stmt);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
735 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
736
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
737 /**
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
738 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
739
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
740 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
741 */
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
742 void parseFuncArgs(Decl func)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
743 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
744 require(Tok.OpenParentheses); // Remove the "(" token.
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
745
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
746 while(peek.type != Tok.CloseParentheses)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
747 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
748 auto t = parseType();
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
749 Id i;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
750 if(peek.type == Tok.Identifier)
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
751 i = parseIdentifier();
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
752 action.addFuncArg(func, t, i);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
753
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
754 if(peek.type == Tok.Comma)
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
755 next;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
756 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
757
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
758 require(Tok.CloseParentheses); // Remove the ")"
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
759 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
760
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
761 /**
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
762 Parse either a block, or a single statement as allowed after if, while
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
763 and for.
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
764 */
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
765 Stmt parseSingleOrCompoundStatement()
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
766 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
767 if (peek.type == Tok.OpenBrace)
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
768 return parseCompoundStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
769 return parseStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
770 }
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
771
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
772 /**
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
773 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
774 current token.
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
775
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
776 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
777 */
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
778 Stmt parseCompoundStatement()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
779 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
780 Token lbrace = require(Tok.OpenBrace);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
781 SmallArray!(Stmt, 32) stmts; // Try to use the stack only
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
782 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
783 stmts ~= parseStatement();
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
784 Token rbrace = require(Tok.CloseBrace);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
785 return action.actOnCompoundStmt(lbrace, rbrace, stmts.unsafe());
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
786 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
787
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
788 Id parseIdentifier()
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
789 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
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
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
792 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
793 return Id(tok);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
794
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
795 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
796 .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
797 .arg(Tok.Identifier);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
798 }
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
799
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
800 ModuleName parseModuleName()
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
801 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
802 auto id = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
803 ModuleName mod;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
804 while (skip(Tok.Dot))
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
805 {
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
806 mod.packages ~= id;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
807 if (peek.type != Tok.Identifier) {
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
808 messages.report(ExpectedIdAfterPackage, peek.location);
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
809 goto Lerror;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
810 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
811 id = parseIdentifier();
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
812 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
813 mod.id = id;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
814 return mod;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
815 Lerror:
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
816 while (!skip(Tok.Seperator))
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
817 next();
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
818 return mod;
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
819 }
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
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
822 /**
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
823 Parse a type - this includes pointer and array(at some point) types.
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
824 */
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
825 Id parseType()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
826 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
827 Token type = next;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
828
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
829 Id currentType;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
830
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
831 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
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
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
834 currentType = Id(type);
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
835 type = peek;
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
836
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
837 while(type.type == Tok.Star || type.type == Tok.OpenBracket)
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
838 {
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
839 if(type.type == Tok.Star)
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
840 {
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
841 currentType = PointerId(currentType);
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
842 next;
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
843 }
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
844 else
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
845 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
846 next;
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
847 if(peek.type == Tok.Integer)
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
848 currentType = StaticArrayId(
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
849 currentType,
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
850 action.actOnNumericConstant(
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 126
diff changeset
851 require(Tok.Integer)));
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
852 require(Tok.CloseBracket);
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
853
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
854 }
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
855 type = peek;
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
856 }
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
857
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
858 return currentType;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
859 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
860
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
861 int peekParseType()
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
862 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
863 int i;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
864 Token type = peek(i);
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
865
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
866 Id currentType;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
867
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
868 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
869 return 0;
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
870
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
871 currentType = Id(type);
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
872 type = peek(++i);
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
873
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
874 while(type.type == Tok.Star || type.type == Tok.OpenBracket)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
875 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
876 if(type.type == Tok.Star)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
877 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
878 i++;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
879 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
880 else
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
881 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
882 if(peek(i++).type != Tok.OpenBracket)
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
883 return 0;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
884 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
885 {
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 i++;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
887 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
888 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
889 }
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
890 else
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
891 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
892 return 0;
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
893
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
894 }
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
895 type = peek(i);
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
896 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
897
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
898 return i;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
899 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
900
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
901 private:
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
902 // -- Expression parsing -- //
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
903 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
904 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
905 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
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.Dot:
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
908 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
909 {
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
910 case Tok.Identifier:
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
911 Token op = next;
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
912 Id member = Id(next);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
913 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
914 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
915 default:
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
916 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
917 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
918 }
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
919 case Tok.OpenBracket:
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
920 Token open = next;
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
921 Exp index = parseExpression();
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
922 Token close = require(Tok.CloseBracket);
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
923 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
924 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
925 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
926 }
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
927 }
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
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
929 Exp parseExpression(int p = 0)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
930 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
931 auto exp = P();
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
932 Token n = peek();
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
933 BinOp* op = null;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
934 while ((op = binary(n.type)) != null && op.prec >= p)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
935 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
936 next();
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
937 int q = op.leftAssoc? 1 + op.prec : op.prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
938 auto exp2 = parseExpression(q);
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
939 exp = action.actOnBinaryOp(n.location, op.operator, exp, exp2);
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
940 n = peek();
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
941 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
942
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
943 return exp;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
944 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
945
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
946 Exp P()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
947 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
948 Token n = next();
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
949 if (auto op = unary(n.type))
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
950 return action.actOnUnaryOp(n, parseExpression(op.prec));
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
951 else if (n.type == Tok.OpenParentheses)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
952 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
953 auto e = parseExpression(0);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
954 require(Tok.CloseParentheses);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
955 return e;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
956 }
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
957 else if (n.type == Tok.Identifier)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
958 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
959 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
960 Exp iden = parsePostfixExp(value);
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
961 switch(peek.type)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
962 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
963 case Tok.OpenParentheses:
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
964 Token lp = next;
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
965 SmallArray!(Exp, 8) args;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
966 while(peek.type != Tok.CloseParentheses)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
967 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
968 if(peek.type == Tok.Comma)
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
969 next;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
970 args ~= parseExpression();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
971 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
972
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
973 Token rp = next();
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
974 return action.actOnCallExpr(iden, lp, args.unsafe(), rp);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
975
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
976 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
977 return iden;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
978 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
979 }
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
980 else if (n.type == Tok.Cast)
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
981 return parseCast(n);
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
982 else if (n.type == Tok.Integer)
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
983 return action.actOnNumericConstant(n);
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
984 else if (n.type == Tok.String)
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
985 return action.actOnStringExp(n);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
986
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
987 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
988 .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
989 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
990 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
991
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
992 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
993 {
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
994 require(Tok.OpenParentheses);
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
995 auto n = next;
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
996 if(!n.isBasicType && !n.isIdentifier)
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
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
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 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
1000 auto exp = P();
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1001 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
1002 }
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
1003
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1004 struct UnOp
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1005 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1006 Tok tokenType;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1007 int prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1008 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1009
78
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
1010 static const UnOp[] _unary =
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
1011 [
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
1012 {Tok.Minus, 4},
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
1013 {Tok.Star, 4}
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
1014 ];
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1015 UnOp* unary(Tok t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1016 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1017 foreach (ref op; _unary)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1018 if (op.tokenType == t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1019 return &op;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1020 return null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1021 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1022
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1023 struct BinOp
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1024 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1025 Tok tokenType;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1026 int prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1027 bool leftAssoc;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
1028 Operator operator;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1029 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1030
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
1031 static const BinOp[] _binary =
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1032 [
126
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.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
1034 {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
1035 {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
1036 {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
1037 {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
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
116
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1040 // =, += etc. 1
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1041 // (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
1042 // ||, 2
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1043 // &&, 3
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1044 // |, 4
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1045 // &, 5
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1046 // ^, 6
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1047 // ==, !=, is, !is, 7
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1048 // <, <= etc, 7
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1049 // in, 7
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1050 // <<, >>, >>>, 8
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1051 // +, -, ~, 9
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1052 // *, /, %, 10
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
diff changeset
1053 // unary operators here
0cd8d6ab3f89 Add in the types for float and co.
Anders Halager <halager@gmail.com>
parents: 104
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.Eq, 2, true, Operator.Eq},
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
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
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
1058 {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
1059 {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
1060 {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
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
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.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
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
74
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
1066 {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
1067 {Tok.Slash, 5, true, Operator.Div},
123
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
1068 {Tok.Percent, 5, true, Operator.Mod},
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
1069
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
1070 {Tok.LeftShift, 8, true, Operator.LeftShift},
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
1071 {Tok.RightShift, 8, true, Operator.RightShift},
6a5f745d351c Parsing <<, >> and >>>.
Anders Johnsen <skabet@gmail.com>
parents: 116
diff changeset
1072 {Tok.UnsignedRightShift, 8, true, Operator.UnsignedRightShift}
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1073 ];
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1074 BinOp* binary(Tok t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1075 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1076 foreach (ref op; _binary)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1077 if (op.tokenType == t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1078 return &op;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1079 return null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1080 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1081
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1082 private:
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1083
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
1084 Token require(Tok t)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1085 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1086 if (peek().type != t)
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1087 messages.report(UnexpectedTokSingle, peek.location)
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1088 .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
1089 .arg(t);
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1090 return next();
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
1091 }
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
1092
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
1093 bool skip(Tok t)
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
1094 {
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1095 if (peek().type != t)
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
1096 return false;
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1097 next();
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
1098 return true;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1099 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1100
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1101 bool isa(Tok t)
99
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 return peek.type == t;
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
1104 }
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
1105
144
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1106 Token next()
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 return lexer.next;
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
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1111 Token peek(int i = 0)
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 return lexer.peek(i);
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1114 }
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1115
6e6355fb5f0f - Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents: 143
diff changeset
1116 Lexer lexer;
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
1117 SourceManager sm;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1118 }
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
1119