PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/qsa-x11-free-1.1.5/src/engine/grammar.y

#
Happy | 723 lines | 628 code | 95 blank | 0 comment | 0 complexity | 970e3465eeea3a9ebcc7a03c7be6db15 MD5 | raw file
Possible License(s): GPL-2.0
  1. %{
  2. /****************************************************************************
  3. ** $Id$
  4. **
  5. ** Copyright (C) 2001-2003 Trolltech AS. All rights reserved.
  6. **
  7. ** This file is part of the Qt Script for Applications framework (QSA).
  8. **
  9. ** This file may be distributed and/or modified under the terms of the
  10. ** GNU General Public License version 2 as published by the Free Software
  11. ** Foundation and appearing in the file LICENSE.GPL included in the
  12. ** packaging of this file.
  13. **
  14. ** Licensees holding a valid Qt Script for Applications license may use
  15. ** this file in accordance with the Qt Script for Applications License
  16. ** Agreement provided with the Software.
  17. **
  18. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  19. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. **
  21. ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
  22. ** information about QSA Commercial License Agreements.
  23. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  24. **
  25. ** Contact info@trolltech.com if any conditions of this licensing are
  26. ** not clear to you.
  27. **
  28. *****************************************************************************/
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include <string.h>
  33. #include "qsengine.h"
  34. #include "qsnodes.h"
  35. #include "qslexer.h"
  36. /* default values for bison */
  37. #define YYDEBUG 0
  38. #define YYMAXDEPTH 0
  39. #ifdef QSDEBUGGER
  40. #define YYERROR_VERBOSE
  41. #define DBG(l, s, e) { l->setLoc(s.first_line, e.last_line); } // location
  42. #else
  43. #undef YYLSP_NEEDED
  44. #define DBG(l, s, e)
  45. #endif
  46. extern int yylex();
  47. static int yyerror (const char *);
  48. static bool automatic();
  49. %}
  50. %union {
  51. int ival;
  52. double dval;
  53. QString *ustr;
  54. QSNode *node;
  55. QSStatementNode *stat;
  56. QSParameterNode *param;
  57. QSFunctionBodyNode *body;
  58. QSFuncDeclNode *func;
  59. QSClassDefNode *clss;
  60. QSProgramNode *prog;
  61. QSAssignExprNode *init;
  62. QSSourceElementNode *src;
  63. QSSourceElementsNode *srcs;
  64. QSStatListNode *slist;
  65. QSArgumentsNode *args;
  66. QSArgumentListNode *alist;
  67. QSCaseBlockNode *cblk;
  68. QSClauseListNode *clist;
  69. QSCaseClauseNode *ccl;
  70. QSElementNode *elm;
  71. QSElisionNode *eli;
  72. QSTypeNode *type;
  73. QSTypedVarNode *tvar;
  74. QSVarBindingNode *vbin;
  75. QSVarBindingListNode *blist;
  76. QSAttributeNode *attrs;
  77. QSAttribute attr;
  78. Operator op;
  79. }
  80. %start Program
  81. /* expect a shift/reduce conflict from the "dangling else" problem
  82. when using bison the warning can be supressed */
  83. // %expect 1
  84. /* literals */
  85. %token NULLTOKEN TRUETOKEN FALSETOKEN
  86. %token STRING NUMBER
  87. /* keywords */
  88. %token BREAK CASE DEFAULT FOR NEW VAR CONTINUE
  89. %token FUNCTION RETURN QS_VOID QS_DELETE
  90. %token IF THIS DO WHILE ELSE QS_IN INSTANCEOF TYPEOF IS
  91. %token SWITCH WITH RESERVED
  92. %token THROW TRY CATCH FINALLY
  93. %token CLASS CONSTRUCTOR EXTENDS
  94. %token ABSTRACT FINAL PRIVATE PUBLIC STATIC QS_CONST
  95. %token PACKAGE IMPORT
  96. /* punctuators */
  97. %token EQEQ NE /* == and != */
  98. %token STREQ STRNEQ /* === and !== */
  99. %token LE GE /* < and > */
  100. %token OR AND /* || and && */
  101. %token PLUSPLUS MINUSMINUS /* ++ and -- */
  102. %token LSHIFT /* << */
  103. %token RSHIFT URSHIFT /* >> and >>> */
  104. %token PLUSEQUAL MINUSEQUAL /* += and -= */
  105. %token MULTEQUAL DIVEQUAL /* *= and /= */
  106. %token LSHIFTEQUAL /* <<= */
  107. %token RSHIFTEQUAL URSHIFTEQUAL /* >>= and >>>= */
  108. %token ANDEQUAL MODEQUAL /* &= and %= */
  109. %token XOREQUAL OREQUAL /* ^= and |= */
  110. /* terminal types */
  111. %token <dval> NUMBER
  112. %token <ustr> STRING
  113. %token <ustr> IDENT
  114. /* non-terminal types */
  115. %type <node> Literal PrimaryExpr Expr MemberExpr FunctionExpr NewExpr CallExpr
  116. %type <node> ArrayLiteral PropertyName PropertyNameAndValueList
  117. %type <node> LeftHandSideExpr PostfixExpr UnaryExpr
  118. %type <node> MultiplicativeExpr AdditiveExpr
  119. %type <node> ShiftExpr RelationalExpr EqualityExpr
  120. %type <node> BitwiseANDExpr BitwiseXORExpr BitwiseORExpr
  121. %type <node> LogicalANDExpr LogicalORExpr
  122. %type <node> ConditionalExpr AssignmentExpr
  123. %type <node> ExprOpt
  124. %type <node> Catch Finally
  125. %type <stat> Statement Block
  126. %type <stat> VariableDefinition EmptyStatement ExprStatement
  127. %type <stat> IfStatement IterationStatement ContinueStatement
  128. %type <stat> BreakStatement ReturnStatement WithStatement
  129. %type <stat> SwitchStatement LabelledStatement
  130. %type <stat> ThrowStatement TryStatement
  131. %type <stat> PackageDefinition ImportStatement
  132. %type <clss> ClassDefinition
  133. %type <slist> StatementList
  134. %type <func> FunctionDeclaration
  135. %type <body> FunctionBody
  136. %type <src> SourceElement
  137. %type <srcs> SourceElements
  138. %type <param> FormalParameterList
  139. %type <op> AssignmentOperator
  140. %type <prog> Program
  141. %type <args> Arguments
  142. %type <alist> ArgumentList
  143. %type <cblk> CaseBlock
  144. %type <ccl> CaseClause DefaultClause
  145. %type <clist> CaseClauses CaseClausesOpt
  146. %type <eli> Elision ElisionOpt
  147. %type <elm> ElementList
  148. %type <ival> VariableDefinitionKind
  149. %type <attr> Attribute
  150. %type <attrs> Attributes
  151. %type <type> TypeExpression ResultSignature
  152. %type <tvar> TypedVariable
  153. %type <vbin> VariableBinding
  154. %type <blist> VariableBindingList
  155. %type <ustr> PackageName
  156. %%
  157. Literal:
  158. NULLTOKEN { $$ = new QSNullNode(); }
  159. | TRUETOKEN { $$ = new QSBooleanNode(true); }
  160. | FALSETOKEN { $$ = new QSBooleanNode(false); }
  161. | NUMBER { $$ = new QSNumberNode($1); }
  162. | STRING { $$ = new QSStringNode($1); delete $1; }
  163. | '/' /* a RegExp ? */ { QSLexer *l = QSLexer::lexer();
  164. if (!l->scanRegExp()) YYABORT;
  165. $$ = new QSRegExpNode(l->pattern,l->flags);}
  166. ;
  167. PrimaryExpr:
  168. THIS { $$ = new QSThisNode(); }
  169. | IDENT { $$ = new QSResolveNode($1);
  170. delete $1; }
  171. | Literal
  172. | ArrayLiteral
  173. | '(' Expr ')' { $$ = new QSGroupNode($2); }
  174. | '{' '}' { $$ = new QSObjectLiteralNode(0L); }
  175. | '{' PropertyNameAndValueList '}' { $$ = new QSObjectLiteralNode($2); }
  176. ;
  177. ArrayLiteral:
  178. '[' ElisionOpt ']' { $$ = new QSArrayNode($2); }
  179. | '[' ElementList ']' { $$ = new QSArrayNode($2); }
  180. | '[' ElementList ',' ElisionOpt ']' { $$ = new QSArrayNode($4, $2); }
  181. ;
  182. ElementList:
  183. ElisionOpt AssignmentExpr { $$ = new QSElementNode($1, $2); }
  184. | ElementList ',' ElisionOpt AssignmentExpr
  185. { $$ = new QSElementNode($1, $3, $4); }
  186. ;
  187. ElisionOpt:
  188. /* nothing */ { $$ = 0L; }
  189. | Elision
  190. ;
  191. Elision:
  192. ',' { $$ = new QSElisionNode(0L); }
  193. | Elision ',' { $$ = new QSElisionNode($1); }
  194. ;
  195. PropertyNameAndValueList:
  196. PropertyName ':' AssignmentExpr { $$ = new QSPropertyValueNode($1, $3); }
  197. | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpr
  198. { $$ = new QSPropertyValueNode($3, $5, $1); }
  199. ;
  200. PropertyName:
  201. IDENT { $$ = new QSPropertyNode($1);
  202. delete $1; }
  203. | STRING { $$ = new QSPropertyNode($1); delete $1; }
  204. | NUMBER { $$ = new QSPropertyNode($1); }
  205. ;
  206. MemberExpr:
  207. PrimaryExpr
  208. | FunctionExpr
  209. | MemberExpr '[' Expr ']' { $$ = new QSAccessorNode1($1, $3); }
  210. | MemberExpr '.' IDENT { $$ = new QSAccessorNode2($1, $3);
  211. delete $3; }
  212. | NEW MemberExpr Arguments { $$ = new QSNewExprNode($2, $3); }
  213. ;
  214. NewExpr:
  215. MemberExpr
  216. | NEW NewExpr { $$ = new QSNewExprNode($2); }
  217. ;
  218. CallExpr:
  219. MemberExpr Arguments { $$ = new QSFunctionCallNode($1, $2); }
  220. | CallExpr Arguments { $$ = new QSFunctionCallNode($1, $2); }
  221. | CallExpr '[' Expr ']' { $$ = new QSAccessorNode1($1, $3); }
  222. | CallExpr '.' IDENT { $$ = new QSAccessorNode2($1, $3);
  223. delete $3; }
  224. ;
  225. Arguments:
  226. '(' ')' { $$ = new QSArgumentsNode(0L); }
  227. | '(' ArgumentList ')' { $$ = new QSArgumentsNode($2); }
  228. ;
  229. ArgumentList:
  230. AssignmentExpr { $$ = new QSArgumentListNode($1); }
  231. | ArgumentList ',' AssignmentExpr { $$ = new QSArgumentListNode($1, $3); }
  232. ;
  233. LeftHandSideExpr:
  234. NewExpr
  235. | CallExpr
  236. ;
  237. PostfixExpr: /* TODO: no line terminator here */
  238. LeftHandSideExpr
  239. | LeftHandSideExpr PLUSPLUS { $$ = new QSPostfixNode($1, OpPlusPlus); }
  240. | LeftHandSideExpr MINUSMINUS { $$ = new QSPostfixNode($1, OpMinusMinus); }
  241. ;
  242. UnaryExpr:
  243. PostfixExpr
  244. | QS_DELETE UnaryExpr { $$ = new QSDeleteNode($2); }
  245. | QS_VOID UnaryExpr { $$ = new QSVoidNode($2); }
  246. | TYPEOF UnaryExpr { $$ = new QSTypeOfNode($2); }
  247. | PLUSPLUS UnaryExpr { $$ = new QSPrefixNode(OpPlusPlus, $2); }
  248. | MINUSMINUS UnaryExpr { $$ = new QSPrefixNode(OpMinusMinus, $2); }
  249. | '+' UnaryExpr { $$ = new QSUnaryPlusNode($2); }
  250. | '-' UnaryExpr { $$ = new QSNegateNode($2); }
  251. | '~' UnaryExpr { $$ = new QSBitwiseNotNode($2); }
  252. | '!' UnaryExpr { $$ = new QSLogicalNotNode($2); }
  253. ;
  254. MultiplicativeExpr:
  255. UnaryExpr
  256. | MultiplicativeExpr '*' UnaryExpr { $$ = new QSMultNode($1, $3, '*'); }
  257. | MultiplicativeExpr '/' UnaryExpr { $$ = new QSMultNode($1, $3, '/'); }
  258. | MultiplicativeExpr '%' UnaryExpr { $$ = new QSMultNode($1,$3,'%'); }
  259. ;
  260. AdditiveExpr:
  261. MultiplicativeExpr
  262. | AdditiveExpr '+' MultiplicativeExpr { $$ = new QSAddNode($1, $3, '+'); }
  263. | AdditiveExpr '-' MultiplicativeExpr { $$ = new QSAddNode($1, $3, '-'); }
  264. ;
  265. ShiftExpr:
  266. AdditiveExpr
  267. | ShiftExpr LSHIFT AdditiveExpr { $$ = new QSShiftNode($1, OpLShift, $3); }
  268. | ShiftExpr RSHIFT AdditiveExpr { $$ = new QSShiftNode($1, OpRShift, $3); }
  269. | ShiftExpr URSHIFT AdditiveExpr { $$ = new QSShiftNode($1, OpURShift, $3); }
  270. ;
  271. RelationalExpr:
  272. ShiftExpr
  273. | RelationalExpr '<' ShiftExpr
  274. { $$ = new QSRelationalNode($1, OpLess, $3); }
  275. | RelationalExpr '>' ShiftExpr
  276. { $$ = new QSRelationalNode($1, OpGreater, $3); }
  277. | RelationalExpr LE ShiftExpr
  278. { $$ = new QSRelationalNode($1, OpLessEq, $3); }
  279. | RelationalExpr GE ShiftExpr
  280. { $$ = new QSRelationalNode($1, OpGreaterEq, $3); }
  281. | RelationalExpr IS ShiftExpr
  282. { $$ = new QSRelationalNode($1, OpIs, $3); }
  283. | RelationalExpr INSTANCEOF ShiftExpr
  284. { $$ = new QSRelationalNode($1, OpInstanceOf, $3); }
  285. | RelationalExpr QS_IN ShiftExpr
  286. { $$ = new QSRelationalNode($1, OpIn, $3); }
  287. ;
  288. EqualityExpr:
  289. RelationalExpr
  290. | EqualityExpr EQEQ RelationalExpr { $$ = new QSEqualNode($1, OpEqEq, $3); }
  291. | EqualityExpr NE RelationalExpr { $$ = new QSEqualNode($1, OpNotEq, $3); }
  292. | EqualityExpr STREQ RelationalExpr { $$ = new QSEqualNode($1, OpStrEq, $3); }
  293. | EqualityExpr STRNEQ RelationalExpr { $$ = new QSEqualNode($1, OpStrNEq, $3);}
  294. ;
  295. BitwiseANDExpr:
  296. EqualityExpr
  297. | BitwiseANDExpr '&' EqualityExpr { $$ = new QSBitOperNode($1, OpBitAnd, $3); }
  298. ;
  299. BitwiseXORExpr:
  300. BitwiseANDExpr
  301. | BitwiseXORExpr '^' BitwiseANDExpr { $$ = new QSBitOperNode($1, OpBitXOr, $3); }
  302. ;
  303. BitwiseORExpr:
  304. BitwiseXORExpr
  305. | BitwiseORExpr '|' BitwiseXORExpr { $$ = new QSBitOperNode($1, OpBitOr, $3); }
  306. ;
  307. LogicalANDExpr:
  308. BitwiseORExpr
  309. | LogicalANDExpr AND BitwiseORExpr
  310. { $$ = new QSBinaryLogicalNode($1, OpAnd, $3); }
  311. ;
  312. LogicalORExpr:
  313. LogicalANDExpr
  314. | LogicalORExpr OR LogicalANDExpr
  315. { $$ = new QSBinaryLogicalNode($1, OpOr, $3); }
  316. ;
  317. ConditionalExpr:
  318. LogicalORExpr
  319. | LogicalORExpr '?' AssignmentExpr ':' AssignmentExpr
  320. { $$ = new QSConditionalNode($1, $3, $5); }
  321. ;
  322. AssignmentExpr:
  323. ConditionalExpr
  324. | LeftHandSideExpr AssignmentOperator AssignmentExpr
  325. { $$ = new QSAssignNode($1, $2, $3);}
  326. ;
  327. AssignmentOperator:
  328. '=' { $$ = OpEqual; }
  329. | PLUSEQUAL { $$ = OpPlusEq; }
  330. | MINUSEQUAL { $$ = OpMinusEq; }
  331. | MULTEQUAL { $$ = OpMultEq; }
  332. | DIVEQUAL { $$ = OpDivEq; }
  333. | LSHIFTEQUAL { $$ = OpLShift; }
  334. | RSHIFTEQUAL { $$ = OpRShift; }
  335. | URSHIFTEQUAL { $$ = OpURShift; }
  336. | ANDEQUAL { $$ = OpAndEq; }
  337. | XOREQUAL { $$ = OpXOrEq; }
  338. | OREQUAL { $$ = OpOrEq; }
  339. | MODEQUAL { $$ = OpModEq; }
  340. ;
  341. Expr:
  342. AssignmentExpr
  343. | Expr ',' AssignmentExpr { $$ = new QSCommaNode($1, $3); }
  344. ;
  345. Statement:
  346. Block
  347. | VariableDefinition
  348. | ClassDefinition { $$ = $1; }
  349. | Attributes ClassDefinition { $2->setAttributes( $1 ); $$ = $2; }
  350. | PackageDefinition
  351. | EmptyStatement
  352. | ExprStatement
  353. | IfStatement
  354. | IterationStatement
  355. | ContinueStatement
  356. | BreakStatement
  357. | ReturnStatement
  358. | WithStatement
  359. | SwitchStatement
  360. | LabelledStatement
  361. | ThrowStatement
  362. | TryStatement
  363. | ImportStatement
  364. ;
  365. Block:
  366. '{' '}' { $$ = new QSBlockNode(0L); DBG($$, @2, @2); }
  367. | '{' StatementList '}' { $$ = new QSBlockNode($2); DBG($$, @3, @3); }
  368. ;
  369. StatementList:
  370. Statement { $$ = new QSStatListNode($1); }
  371. | StatementList Statement { $$ = new QSStatListNode($1, $2); }
  372. ;
  373. EmptyStatement:
  374. ';' { $$ = new QSEmptyStatementNode(); }
  375. ;
  376. ExprStatement:
  377. Expr ';' { $$ = new QSExprStatementNode($1);
  378. DBG($$, @1, @2); }
  379. | Expr error { if (automatic()) {
  380. $$ = new QSExprStatementNode($1);
  381. DBG($$, @1, @1);
  382. } else
  383. YYABORT; }
  384. ;
  385. IfStatement: /* shift/reduce conflict due to dangling else */
  386. IF '(' Expr ')' Statement { $$ = new QSIfNode($3,$5,0L);DBG($$,@1,@4); }
  387. | IF '(' Expr ')' Statement ELSE Statement
  388. { $$ = new QSIfNode($3,$5,$7);DBG($$,@1,@4); }
  389. ;
  390. IterationStatement:
  391. DO Statement WHILE '(' Expr ')' { $$=new QSDoWhileNode($2,$5);DBG($$,@1,@3);}
  392. | WHILE '(' Expr ')' Statement { $$ = new QSWhileNode($3,$5);DBG($$,@1,@4); }
  393. | FOR '(' ExprOpt ';' ExprOpt ';' ExprOpt ')'
  394. Statement { $$ = new QSForNode($3,$5,$7,$9);
  395. DBG($$,@1,@8); }
  396. | FOR '(' VAR VariableBindingList ';' ExprOpt ';' ExprOpt ')'
  397. Statement { $$ = new QSForNode($4,$6,$8,$10);
  398. DBG($$,@1,@9); }
  399. | FOR '(' LeftHandSideExpr QS_IN Expr ')'
  400. Statement { $$ = new QSForInNode($3, $5, $7);
  401. DBG($$,@1,@6); }
  402. | FOR '(' VAR VariableBinding QS_IN Expr ')'
  403. Statement { $$ = new QSForInNode($4,$6,$8);
  404. DBG($$,@1,@7); }
  405. ;
  406. ExprOpt:
  407. /* nothing */ { $$ = 0L; }
  408. | Expr
  409. ;
  410. ContinueStatement:
  411. CONTINUE ';' { $$ = new QSContinueNode(); DBG($$,@1,@2); }
  412. | CONTINUE error { if (automatic()) {
  413. $$ = new QSContinueNode(); DBG($$,@1,@2);
  414. } else
  415. YYABORT; }
  416. | CONTINUE IDENT ';' { $$ = new QSContinueNode($2); DBG($$,@1,@3);
  417. delete $2; }
  418. | CONTINUE IDENT error { if (automatic()) {
  419. $$ = new QSContinueNode($2);DBG($$,@1,@2);
  420. delete $2;
  421. } else
  422. YYABORT; }
  423. ;
  424. BreakStatement:
  425. BREAK ';' { $$ = new QSBreakNode();DBG($$,@1,@2); }
  426. | BREAK error { if (automatic()) {
  427. $$ = new QSBreakNode(); DBG($$,@1,@1);
  428. } else
  429. YYABORT; }
  430. | BREAK IDENT ';' { $$ = new QSBreakNode($2); DBG($$,@1,@3);
  431. delete $2; }
  432. | BREAK IDENT error { if (automatic()) {
  433. $$ = new QSBreakNode($2); DBG($$,@1,@2);
  434. delete $2;
  435. } else
  436. YYABORT;
  437. }
  438. ;
  439. ReturnStatement:
  440. RETURN ';' { $$ = new QSReturnNode(0L); DBG($$,@1,@2); }
  441. | RETURN error { if (automatic()) {
  442. $$ = new QSReturnNode(0L); DBG($$,@1,@1);
  443. } else
  444. YYABORT; }
  445. | RETURN Expr ';' { $$ = new QSReturnNode($2); DBG($$,@1,@2); }
  446. | RETURN Expr error { if (automatic()) {
  447. $$ = new QSReturnNode($2);
  448. DBG($$,@1,@2);
  449. } else {
  450. YYABORT;
  451. }
  452. }
  453. ;
  454. WithStatement:
  455. WITH '(' Expr ')' Statement { $$ = new QSWithNode($3,$5);
  456. DBG($$, @1, @4); }
  457. ;
  458. SwitchStatement:
  459. SWITCH '(' Expr ')' CaseBlock { $$ = new QSSwitchNode($3, $5);
  460. DBG($$, @1, @4); }
  461. ;
  462. CaseBlock:
  463. '{' CaseClausesOpt '}' { $$ = new QSCaseBlockNode($2, 0L, 0L); }
  464. | '{' CaseClausesOpt DefaultClause CaseClausesOpt '}'
  465. { $$ = new QSCaseBlockNode($2, $3, $4); }
  466. ;
  467. CaseClausesOpt:
  468. /* nothing */ { $$ = 0L; }
  469. | CaseClauses
  470. ;
  471. CaseClauses:
  472. CaseClause { $$ = new QSClauseListNode($1); }
  473. | CaseClauses CaseClause { $$ = $1->append($2); }
  474. ;
  475. CaseClause:
  476. CASE Expr ':' { $$ = new QSCaseClauseNode($2, 0L); }
  477. | CASE Expr ':' StatementList { $$ = new QSCaseClauseNode($2, $4); }
  478. ;
  479. DefaultClause:
  480. DEFAULT ':' { $$ = new QSCaseClauseNode(0L, 0L);; }
  481. | DEFAULT ':' StatementList { $$ = new QSCaseClauseNode(0L, $3); }
  482. ;
  483. LabelledStatement:
  484. IDENT ':' Statement { $3->pushLabel($1);
  485. $$ = new QSLabelNode($1, $3);
  486. delete $1; }
  487. ;
  488. ThrowStatement:
  489. THROW Expr ';' { $$ = new QSThrowNode($2); }
  490. ;
  491. TryStatement:
  492. TRY Block Catch { $$ = new QSTryNode($2, $3); }
  493. | TRY Block Finally { $$ = new QSTryNode($2, 0L, $3); }
  494. | TRY Block Catch Finally { $$ = new QSTryNode($2, $3, $4); }
  495. ;
  496. Catch:
  497. CATCH '(' IDENT ')' Block { $$ = new QSCatchNode($3, $5); delete $3; }
  498. ;
  499. Finally:
  500. FINALLY Block { $$ = new QSFinallyNode($2); }
  501. ;
  502. PackageDefinition:
  503. PACKAGE PackageName FunctionBody
  504. { $$ = new QSPackageNode($2, $3);
  505. delete $2; }
  506. ;
  507. ImportStatement:
  508. IMPORT PackageName ';' { $$ = new QSImportNode($2); delete $2; }
  509. ;
  510. PackageName:
  511. IDENT { $$ = $1; delete $1; }
  512. | PackageName '.' IDENT { *$$ += QString::fromLatin1(".") + *$3; delete $3; }
  513. ;
  514. FunctionDeclaration:
  515. FUNCTION IDENT '(' ')' ResultSignature FunctionBody
  516. { $$ = new QSFuncDeclNode($2, 0L, $5, $6); delete $2; }
  517. | FUNCTION IDENT '(' FormalParameterList ')' ResultSignature FunctionBody
  518. { $$ = new QSFuncDeclNode($2, $4, $6, $7); delete $2; }
  519. | Attributes FUNCTION IDENT '(' ')' ResultSignature FunctionBody
  520. { $$ = new QSFuncDeclNode($3, 0L, $6, $7);
  521. $$->setAttributes( $1 ); delete $3; }
  522. | Attributes FUNCTION IDENT '(' FormalParameterList ')' ResultSignature
  523. FunctionBody { $$ = new QSFuncDeclNode($3, $5, $7, $8);
  524. $$->setAttributes( $1 ); delete $3; }
  525. ;
  526. ResultSignature:
  527. { $$ = 0L; }
  528. | ':' TypeExpression { $$ = $2; }
  529. ;
  530. FunctionExpr:
  531. FUNCTION '(' ')' FunctionBody { $$ = new QSFuncExprNode(0L, $4); }
  532. | FUNCTION '(' FormalParameterList ')' FunctionBody
  533. { $$ = new QSFuncExprNode($3, $5); }
  534. ;
  535. FormalParameterList:
  536. IDENT { $$ = new QSParameterNode($1, 0L);
  537. delete $1; }
  538. | IDENT ':' TypeExpression { $$ = new QSParameterNode($1, $3);
  539. delete $1; }
  540. | FormalParameterList ',' IDENT { $$ = $1->append($3, 0L); delete $3; }
  541. | FormalParameterList ',' IDENT ':' TypeExpression
  542. { $$ = $1->append($3, $5); delete $3; }
  543. ;
  544. FunctionBody:
  545. '{' '}' /* TODO: spec ??? */ { $$ = new QSFunctionBodyNode(0L); }
  546. | '{' SourceElements '}' { $$ = new QSFunctionBodyNode($2); }
  547. ;
  548. ClassDefinition:
  549. CLASS IDENT ';' { $$ = new QSClassDefNode($2, 0L, 0L);
  550. delete $2; }
  551. | CLASS IDENT FunctionBody { $$ = new QSClassDefNode($2, 0L, $3);
  552. delete $2; }
  553. | CLASS IDENT EXTENDS TypeExpression FunctionBody
  554. { $$ = new QSClassDefNode($2, $4, $5);
  555. delete $2; }
  556. ;
  557. Program:
  558. /* empty */ { $$ = new QSProgramNode(0L); }
  559. | SourceElements { $$ = new QSProgramNode($1); }
  560. ;
  561. SourceElements:
  562. SourceElement { $$ = new QSSourceElementsNode($1); }
  563. | SourceElements SourceElement { $$ = new QSSourceElementsNode($1, $2); }
  564. ;
  565. SourceElement:
  566. Statement { $$ = new QSSourceElementNode($1); }
  567. | FunctionDeclaration { $$ = new QSSourceElementNode($1); }
  568. ;
  569. Attributes:
  570. Attribute { $$ = new QSAttributeNode($1); }
  571. | Attributes Attribute { $1->add($2); $$ = $1; }
  572. ;
  573. Attribute:
  574. ABSTRACT { $$ = AttributeAbstract; }
  575. | FINAL { $$ = AttributeFinal; }
  576. | PRIVATE { $$ = AttributePrivate; }
  577. | PUBLIC { $$ = AttributePublic; }
  578. | STATIC { $$ = AttributeStatic; }
  579. | TRUETOKEN { $$ = AttributeTrue; }
  580. | FALSETOKEN { $$ = AttributeFalse; }
  581. | CONSTRUCTOR { $$ = AttributeConstructor; }
  582. ;
  583. /************************* Variable Definition ******************************/
  584. VariableDefinition:
  585. VariableDefinitionKind VariableBindingList
  586. { $$ = new QSVarDefNode($1, $2); DBG($$, @1, @2); }
  587. | Attributes VariableDefinitionKind VariableBindingList
  588. { $$ = new QSVarDefNode($2, $3); DBG($$, @1, @3);
  589. ((QSVarDefNode*)$$)->setAttributes( $1 ); }
  590. ;
  591. VariableDefinitionKind:
  592. VAR { $$ = 0; }
  593. | QS_CONST { $$ = 1; }
  594. ;
  595. VariableBindingList:
  596. VariableBinding { $$ = new QSVarBindingListNode(0L, $1); }
  597. | VariableBindingList ',' VariableBinding
  598. { $$ = new QSVarBindingListNode($1, $3); }
  599. ;
  600. VariableBinding:
  601. TypedVariable { $$ = new QSVarBindingNode($1, 0L); }
  602. | TypedVariable '=' AssignmentExpr { $$ = new QSVarBindingNode($1, $3); }
  603. ;
  604. TypedVariable:
  605. IDENT { $$ = new QSTypedVarNode($1, 0L);
  606. delete $1; }
  607. | IDENT ':' TypeExpression { $$ = new QSTypedVarNode($1, $3);
  608. delete $1; }
  609. ;
  610. /************************* Type Expressions *********************************/
  611. TypeExpression:
  612. IDENT { $$ = new QSTypeNode($1); delete $1; }
  613. ;
  614. %%
  615. int yyerror ( const char *errstr ) /* Called by yyparse on error */
  616. {
  617. QSLexer::lexer()->setErrorMessage( errstr );
  618. return 1;
  619. }
  620. /* may we automatically insert a semicolon ? */
  621. bool automatic()
  622. {
  623. if ( yychar == '}' || yychar == 0 )
  624. return true;
  625. else if ( QSLexer::lexer()->prevTerminator() )
  626. return true;
  627. return false;
  628. }