as_parser.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_parser.h
  25. //
  26. // This class parses the script code and builds a tree for compilation
  27. //
  28. /*
  29. TYPEDEF = 'typedef' REALTYPE IDENTIFIER ';'
  30. ENUM = 'enum' IDENTIFIER '{' ENUMELEMENT? (',' ENUMELEMENT)* '}'
  31. ENUMELEMENT = IDENTIFIER ('=' EXPRESSION)
  32. SCRIPT = (FUNCTION | GLOBVAR | IMPORT | STRUCT | INTERFACE | TYPEDEF | ENUM)*
  33. TYPE = 'const'? DATATYPE
  34. TYPEMOD = ('&' ('in' | 'out' | 'inout')?)?
  35. FUNCDEF = 'funcdef' FUNCSIG ';'
  36. FUNCSIG = TYPE TYPEMOD IDENTIFIER PARAMLIST
  37. FUNCTION = FUNCSIG BLOCK
  38. IMPORT = 'import' FUNCSIG 'from' STRING ';'
  39. INTERFACE = 'interface' IDENTIFIER '{' (FUNCSIG ';')* '}' ';'
  40. GLOBVAR = TYPE IDENTIFIER ('=' (INITLIST | ASSIGNMENT))? (',' IDENTIFIER ('=' (INITLIST | ASSIGNMENT))?)* ';'
  41. DATATYPE = REALTYPE | IDENTIFIER
  42. REALTYPE = 'void' | 'bool' | 'float' | 'int' | 'uint' | 'bits'
  43. PARAMLIST = '(' (TYPE TYPEMOD IDENTIFIER? (',' TYPE TYPEMOD IDENTIFIER?)*)? ')'
  44. BLOCK = '{' (DECLARATION | STATEMENT)* '}'
  45. DECLARATION = TYPE IDENTIFIER ('=' (INITLIST | ASSIGNMENT))? (',' IDENTIFIER ('=' (INITLIST | ASSIGNMENT))?)* ';'
  46. STATEMENT = BLOCK | IF | WHILE | DOWHILE | RETURN | EXPRSTATEMENT | BREAK | CONTINUE
  47. BREAK = 'break' ';'
  48. CONTINUE = 'continue' ';'
  49. EXPRSTATEMENT = ASSIGNMENT? ';'
  50. FOR = 'for' '(' (DECLARATION | EXPRSTATEMENT) EXPRSTATEMENT ASSIGNMENT? ')' STATEMENT
  51. IF = 'if' '(' ASSIGNMENT ')' STATEMENT ('else' STATEMENT)?
  52. WHILE = 'while' '(' ASSIGNMENT ')' STATEMENT
  53. DOWHILE = 'do' STATEMENT 'while' '(' ASSIGNMENT ')' ';'
  54. RETURN = 'return' ASSIGNMENT? ';'
  55. ASSIGNMENT = CONDITION (ASSIGNOP ASSIGNMENT)?
  56. CONDITION = EXPRESSION ('?' ASSIGNMENT ':' ASSIGNMENT)?
  57. EXPRESSION = TERM (OP TERM)*
  58. TERM = PRE* VALUE POST*
  59. VALUE = '(' ASSIGNMENT ')' | CONSTANT | IDENTIFIER | FUNCTIONCALL | CONVERSION | CAST
  60. PRE = '-' | '+' | 'not' | '++' | '--' | '~'
  61. POST = '++' | '--' | ('.' | '->') (IDENTIFIER | FUNCTIONCALL) | '[' ASSIGNMENT ']'
  62. FUNCTIONCALL = IDENTIFIER ARGLIST
  63. ARGLIST = '(' (ASSIGNMENT (',' ASSIGNMENT)*)? ')'
  64. CONSTANT = "abc" | 123 | 123.1 | 'true' | 'false' | 0xFFFF
  65. OP = 'and' | 'or' |
  66. '==' | '!=' | '<' | '<=' | '>=' | '>' |
  67. '+' | '-' | '*' | '/' | '%' | '|' | '&' | '^' | '<<' | '>>' | '>>>'
  68. ASSIGNOP = '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '|=' | '&=' | '^=' | '<<=' | '>>=' | '>>>='
  69. CONVERSION = TYPE '(' ASSIGNMENT ')'
  70. INITLIST = '{' ((INITLIST | ASSIGNMENT)? (',' (INITLIST | ASSIGNMENT)?)*)? '}'
  71. CAST = 'cast' '<' TYPE '>' '(' ASSIGNMENT ')'
  72. */
  73. #ifndef AS_PARSER_H
  74. #define AS_PARSER_H
  75. #include "as_scriptnode.h"
  76. #include "as_scriptcode.h"
  77. #include "as_builder.h"
  78. #include "as_tokenizer.h"
  79. BEGIN_AS_NAMESPACE
  80. class asCParser
  81. {
  82. public:
  83. asCParser(asCBuilder *builder);
  84. ~asCParser();
  85. int ParseScript(asCScriptCode *script);
  86. int ParseFunctionDefinition(asCScriptCode *script);
  87. int ParsePropertyDeclaration(asCScriptCode *script);
  88. int ParseVirtualPropertyDeclaration(asCScriptCode *script, bool asMethod);
  89. int ParseDataType(asCScriptCode *script);
  90. int ParseTemplateDecl(asCScriptCode *script);
  91. int ParseStatementBlock(asCScriptCode *script, asCScriptNode *block);
  92. int ParseGlobalVarInit(asCScriptCode *script, asCScriptNode *init);
  93. int ParseExpression(asCScriptCode *script);
  94. asCScriptNode *GetScriptNode();
  95. protected:
  96. void Reset();
  97. void GetToken(sToken *token);
  98. void RewindTo(const sToken *token);
  99. void Error(const char *text, sToken *token);
  100. asCScriptNode *ParseImport();
  101. asCScriptNode *ParseFunctionDefinition();
  102. asCScriptNode *ParseScript();
  103. asCScriptNode *ParseType(bool allowConst, bool allowVariableType = false);
  104. asCScriptNode *ParseTypeMod(bool isParam);
  105. asCScriptNode *ParseFunction(bool isMethod = false);
  106. asCScriptNode *ParseFuncDef();
  107. asCScriptNode *ParseGlobalVar();
  108. asCScriptNode *ParseParameterList();
  109. asCScriptNode *SuperficiallyParseStatementBlock();
  110. asCScriptNode *SuperficiallyParseGlobalVarInit();
  111. asCScriptNode *ParseStatementBlock();
  112. asCScriptNode *ParseDeclaration();
  113. asCScriptNode *ParseStatement();
  114. asCScriptNode *ParseExpressionStatement();
  115. asCScriptNode *ParseSwitch();
  116. asCScriptNode *ParseCase();
  117. asCScriptNode *ParseIf();
  118. asCScriptNode *ParseFor();
  119. asCScriptNode *ParseWhile();
  120. asCScriptNode *ParseDoWhile();
  121. asCScriptNode *ParseReturn();
  122. asCScriptNode *ParseBreak();
  123. asCScriptNode *ParseContinue();
  124. asCScriptNode *ParseAssignment();
  125. asCScriptNode *ParseAssignOperator();
  126. asCScriptNode *ParseCondition();
  127. asCScriptNode *ParseExpression();
  128. asCScriptNode *ParseExprTerm();
  129. asCScriptNode *ParseExprOperator();
  130. asCScriptNode *ParseExprPreOp();
  131. asCScriptNode *ParseExprPostOp();
  132. asCScriptNode *ParseExprValue();
  133. asCScriptNode *ParseArgList();
  134. asCScriptNode *ParseDataType(bool allowVariableType = false);
  135. asCScriptNode *ParseRealType();
  136. asCScriptNode *ParseIdentifier();
  137. asCScriptNode *ParseConstant();
  138. asCScriptNode *ParseStringConstant();
  139. asCScriptNode *ParseFunctionCall();
  140. asCScriptNode *ParseVariableAccess();
  141. asCScriptNode *ParseConstructCall();
  142. asCScriptNode *ParseToken(int token);
  143. asCScriptNode *ParseOneOf(int *tokens, int num);
  144. asCScriptNode *ParseClass();
  145. asCScriptNode *ParseInitList();
  146. asCScriptNode *ParseInterface();
  147. asCScriptNode *ParseInterfaceMethod();
  148. asCScriptNode *ParseVirtualPropertyDecl(bool isMethod, bool isInterface);
  149. asCScriptNode *ParseCast();
  150. asCScriptNode *ParseEnumeration(); // Parse enumeration enum { X, Y }
  151. asCScriptNode *ParseTypedef(); // Parse named type declaration
  152. void ParseMethodOverrideBehaviors(asCScriptNode *funcNode);
  153. bool IsVarDecl();
  154. bool IsVirtualPropertyDecl();
  155. bool IsFuncDecl(bool isMethod);
  156. bool IsRealType(int tokenType);
  157. bool IsDataType(const sToken &token);
  158. bool IsOperator(int tokenType);
  159. bool IsPreOperator(int tokenType);
  160. bool IsPostOperator(int tokenType);
  161. bool IsConstant(int tokenType);
  162. bool IsAssignOperator(int tokenType);
  163. bool IsFunctionCall();
  164. bool IdentifierIs(const sToken &t, const char *str);
  165. bool CheckTemplateType(sToken &t);
  166. asCString ExpectedToken(const char *token);
  167. asCString ExpectedTokens(const char *token1, const char *token2);
  168. asCString ExpectedOneOf(int *tokens, int count);
  169. asCString ExpectedOneOf(const char **tokens, int count);
  170. bool errorWhileParsing;
  171. bool isSyntaxError;
  172. bool checkValidTypes;
  173. bool isParsingAppInterface;
  174. asCScriptEngine *engine;
  175. asCBuilder *builder;
  176. asCScriptCode *script;
  177. asCScriptNode *scriptNode;
  178. size_t sourcePos;
  179. };
  180. END_AS_NAMESPACE
  181. #endif