1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2013 Academy of Motion Picture Arts and Sciences
3 // ("A.M.P.A.S."). Portions contributed by others as indicated.
4 // All rights reserved.
5 //
6 // A worldwide, royalty-free, non-exclusive right to copy, modify, create
7 // derivatives, and use, in source and binary forms, is hereby granted,
8 // subject to acceptance of this license. Performance of any of the
9 // aforementioned acts indicates acceptance to be bound by the following
10 // terms and conditions:
11 //
12 //  * Copies of source code, in whole or in part, must retain the
13 //    above copyright notice, this list of conditions and the
14 //    Disclaimer of Warranty.
15 //
16 //  * Use in binary form must retain the above copyright notice,
17 //    this list of conditions and the Disclaimer of Warranty in the
18 //    documentation and/or other materials provided with the distribution.
19 //
20 //  * Nothing in this license shall be deemed to grant any rights to
21 //    trademarks, copyrights, patents, trade secrets or any other
22 //    intellectual property of A.M.P.A.S. or any contributors, except
23 //    as expressly stated herein.
24 //
25 //  * Neither the name "A.M.P.A.S." nor the name of any other
26 //    contributors to this software may be used to endorse or promote
27 //    products derivative of or based on this software without express
28 //    prior written permission of A.M.P.A.S. or the contributors, as
29 //    appropriate.
30 //
31 // This license shall be construed pursuant to the laws of the State of
32 // California, and any disputes related thereto shall be subject to the
33 // jurisdiction of the courts therein.
34 //
35 // Disclaimer of Warranty: THIS SOFTWARE IS PROVIDED BY A.M.P.A.S. AND
36 // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
37 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
38 // FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO
39 // EVENT SHALL A.M.P.A.S., OR ANY CONTRIBUTORS OR DISTRIBUTORS, BE LIABLE
40 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, RESITUTIONARY,
41 // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
46 // THE POSSIBILITY OF SUCH DAMAGE.
47 //
48 // WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THE ACADEMY
49 // SPECIFICALLY DISCLAIMS ANY REPRESENTATIONS OR WARRANTIES WHATSOEVER
50 // RELATED TO PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS IN THE ACADEMY
51 // COLOR ENCODING SYSTEM, OR APPLICATIONS THEREOF, HELD BY PARTIES OTHER
52 // THAN A.M.P.A.S., WHETHER DISCLOSED OR UNDISCLOSED.
53 ///////////////////////////////////////////////////////////////////////////
54 
55 
56 #ifndef INCLUDED_CTL_PARSER_H
57 #define INCLUDED_CTL_PARSER_H
58 
59 //-----------------------------------------------------------------------------
60 //
61 //	class Parser -- the parser for the color transformation language.
62 //	The parser converts the sequence of tokens that is generated by
63 //	the lexical analyzer into an abstract syntax tree.
64 //	The parser also computes the value types of all expressions, and
65 //	it simplifies expressions by evaluating constant subexpressions.
66 //
67 //-----------------------------------------------------------------------------
68 
69 #include <CtlLex.h>
70 #include <CtlSyntaxTree.h>
71 #include <CtlType.h>
72 #include <vector>
73 
74 namespace Ctl {
75 
76 class SymbolTable;
77 class Interpreter;
78 
79 
80 class Parser
81 {
82   public:
83 
84      Parser (LContext &lcontext, Interpreter &interpreter);
85     ~Parser ();
86 
87     SyntaxNodePtr	parseInput ();
88 
interpreter()89     Interpreter &	interpreter ()		{return _interpreter;}
90 
91   private:
92 
93     enum AllocationMode
94     {
95 	AM_STATIC,
96 	AM_AUTO
97     };
98 
99     void		parseCtlVersion ();
100     void		parseImportList ();
101     void		parseImportStatement ();
102     SyntaxNodePtr	parseModuleBody ();
103     ModuleNodePtr	parseFunctionOrConstList ();
104     FunctionNodePtr	parseFunction ();
105 
106     void		parseParameterList
107 				(ParamVector &parameters,
108 				 const std::string &funcName);
109 
110     void		parseParameter
111 				(ParamVector &parameters,
112 				 const std::string &funcName,
113 				 bool &foundDefaults);
114 
115     StatementNodePtr	parseCompoundStatement();
116     StatementNodePtr	parseStatement();
117 
118     StatementNodePtr	parseVariableDefinition
119 				(AllocationMode mode,
120 				 DataTypePtr baseType = 0);
121 
122     StatementNodePtr    parseStructDefinition (AllocationMode mode);
123     StatementNodePtr	parseExprVariableDefinitionOrAssign();
124     StatementNodePtr	parseAssignment (ExprNodePtr lhs);
125     StatementNodePtr	parseSimpleAssignment (ExprNodePtr lhs);
126     StatementNodePtr	parseExprStatement (ExprNodePtr expr);
127     StatementNodePtr	parseSimpleExprStatement (ExprNodePtr expr);
128     StatementNodePtr	parseForStatement();
129     StatementNodePtr	parseForUpdateStatement();
130     StatementNodePtr	parseIfStatement();
131     StatementNodePtr	parsePrintStatement();
132     StatementNodePtr	parseNullStatement();
133     StatementNodePtr	parseReturnStatement();
134     StatementNodePtr	parseWhileStatement();
135     ExprNodePtr		parseExpression();
136     ExprNodePtr		parseOrExpression ();
137     ExprNodePtr		parseAndExpression ();
138     ExprNodePtr		parseBitXorExpression ();
139     ExprNodePtr		parseBitOrExpression ();
140     ExprNodePtr		parseBitAndExpression ();
141     ExprNodePtr		parseEqualityExpression ();
142     ExprNodePtr		parseRelationalExpression ();
143     ExprNodePtr		parseShiftExpression ();
144     ExprNodePtr		parseAdditiveExpression ();
145     ExprNodePtr		parseMultiplicativeExpression ();
146     ExprNodePtr		parseUnaryExpression ();
147     ExprNodePtr		parsePrimaryExpression ();
148     ExprNodePtr         parseMemberArrayExpression (ExprNodePtr lhs);
149     std::string		parseScopedString ();
150     NameNodePtr		parseScopedName ();
151     int	          	parseExprList (ExprNodeVector &arguments);
152 
153     bool                parseInitializer
154 				(ExprNodePtr &initialValue,
155 				 DataTypePtr dataType,
156 				 SizeVector &sizes);
157 
158     bool                parseInitializerRecursive
159 				(DataTypePtr dataType,
160 				 ExprNodeVector &elements,
161 				 SizeVector &s,
162 				 int depth);
163 
164     DataTypePtr		parseBaseType ();
165     bool		parseVaryingHint ();
166     void		parseArraySize (SizeVector &sizes);
167 
168     ExprNodePtr		evaluateExpression
169 				(ExprNodePtr expr,
170 				 TypePtr targetType);
171 
172     VariableNodePtr	variableDefinitionNoInit
173 				(AllocationMode mode,
174 				 int lineNumber,
175 				 const std::string &name,
176 				 const DataTypePtr &baseType,
177 				 const SizeVector &declArraySizes);
178 
179     VariableNodePtr	variableDefinitionImport
180 				(AllocationMode mode,
181 				 int lineNumber,
182 				 const std::string &name,
183 				 bool isConst,
184 				 const DataTypePtr &baseType,
185 				 const SizeVector &declArraySizes,
186 				 ExprNodePtr &initialValue);
187 
188     VariableNodePtr	variableDefinitionCurlyBraces
189 				(AllocationMode mode,
190 				 int lineNumber,
191 				 const std::string &name,
192 				 bool isConst,
193 				 const DataTypePtr &baseType,
194 				 const SizeVector &declArraySizes,
195 				 ExprNodePtr &initialValue);
196 
197     VariableNodePtr	variableDefinitionAssignExpr
198 				(AllocationMode mode,
199 				 int lineNumber,
200 				 const std::string &name,
201 				 bool isConst,
202 				 const DataTypePtr &baseType,
203 				 const SizeVector &declArraySizes,
204 				 ExprNodePtr &initialValue);
205 
206     VariableNodePtr	variableDefinitionExprSideEffect
207 				(AllocationMode mode,
208 				 int lineNumber,
209 				 const std::string &name,
210 				 bool isConst,
211 				 const DataTypePtr &baseType,
212 				 const SizeVector &declArraySizes);
213 
token()214     const Token &	token () const		{return _lex.token();}
fileName()215     const std::string &	fileName () const	{return _lcontext.fileName();}
symtab()216     SymbolTable &	symtab () const		{return _lcontext.symtab();}
module()217     const Module *	module () const		{return _lcontext.module();}
218     int			currentLineNumber () const;
219 
220     int			tokenIntValue () const;
221     float		tokenFloatValue () const;
222     const std::string &	tokenStringValue () const;
223 
next()224     void		next ()			{_lex.next();}
225 
226     void		match (Token expectedToken);
227     void		recover (Token expectedToken);
228     void		giveUp ();
229     void		syntaxError ();
230     void		staticVariableError ();
231     void		foundError (int lineNumber, Error error);
numErrors()232     int			numErrors () const	{return _lcontext.numErrors();}
233 
234     void		duplicateName (const std::string &name,
235 				       int lineNumber,
236 				       const std::string &fileName);
237 
238     void		undefinedName (const std::string &name);
239 
240     Lex			_lex;
241     LContext &		_lcontext;
242     Interpreter &	_interpreter;
243 
244     StatementNodePtr    _firstConst;
245     StatementNodePtr    _lastConst;
246 };
247 
248 
249 void loadModuleRecursive (Parser &parser, const std::string &moduleName);
250 
251 
252 } // namespace Ctl
253 
254 #endif
255