PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/nip2-7.28.1/src/parse.y

#
Happy | 1687 lines | 1430 code | 257 blank | 0 comment | 0 complexity | 2664b193a7b9936bfd7476b7495ac764 MD5 | raw file
Possible License(s): GPL-2.0
  1. %{
  2. /* Parse ip's macro language.
  3. */
  4. /*
  5. Copyright (C) 1991-2003 The National Gallery
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /*
  19. These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
  20. */
  21. #include "ip.h"
  22. /*
  23. #define DEBUG
  24. */
  25. /* trace text read system
  26. #define DEBUG_CHARACTER
  27. */
  28. /* The lexer from lex.l.
  29. */
  30. int yylex( void );
  31. void yyrestart( FILE *input_file );
  32. /* Declare file-private stuff, shared with the lexer. Bison will put this
  33. * stuff into parse.h, so just declare, don't define. Sadly we can't have
  34. * these things static :(
  35. */
  36. /* Global .. the symbol whose definition we are currently parsing, the symbol
  37. * which all defs in this parse action should be made local to.
  38. */
  39. extern Symbol *current_symbol;
  40. extern Symbol *root_symbol;
  41. /* The current parse context.
  42. */
  43. extern Compile *current_compile;
  44. extern ParseNode *current_parsenode;
  45. /* The kit we are adding new symbols to.
  46. */
  47. extern Toolkit *current_kit;
  48. /* Where it should go in the kit.
  49. */
  50. extern int tool_position;
  51. /* Lineno of start of last top-level def.
  52. */
  53. extern int last_top_lineno;
  54. /* Text we've gathered in this lex.
  55. */
  56. extern char lex_text_buffer[MAX_STRSIZE];
  57. /* Stack of symbols for parser - each represents a new scope level.
  58. */
  59. extern Symbol *scope_stack_symbol[MAX_SSTACK];
  60. extern Compile *scope_stack_compile[MAX_SSTACK];
  61. extern int scope_sp;
  62. /* Use to generate unique ids for anonymouse parse objects (eg. lambdas etc).
  63. */
  64. extern int parse_object_id;
  65. /* Get text for parsed objects.
  66. */
  67. char *input_text( char *out );
  68. void input_reset( void );
  69. void input_push( int n );
  70. void input_backtoch( char ch );
  71. void input_back1( void );
  72. void input_pop( void );
  73. /* Nest and unnest scopes.
  74. */
  75. void scope_push( void );
  76. void scope_pop( void );
  77. void scope_pop_all( void );
  78. void scope_reset( void );
  79. /* Helper functions.
  80. */
  81. void *parse_toplevel_end( Symbol *sym );
  82. void *parse_access_end( Symbol *sym, Symbol *main );
  83. %}
  84. %union {
  85. struct sym_table *yy_symtab;
  86. ParseNode *yy_node;
  87. char *yy_name;
  88. ParseConst yy_const;
  89. UnOp yy_uop;
  90. BinOp yy_binop;
  91. }
  92. %token TK_TAG TK_IDENT TK_CONST TK_DOTDOTDOT TK_LAMBDA TK_FROM TK_TO TK_SUCHTHAT
  93. %token TK_UMINUS TK_UPLUS TK_POW
  94. %token TK_LESS TK_LESSEQ TK_MORE TK_MOREEQ TK_NOTEQ
  95. %token TK_LAND TK_LOR TK_BAND TK_BOR TK_JOIN TK_DIFF
  96. %token TK_IF TK_THEN TK_ELSE
  97. %token TK_CHAR TK_SHORT TK_CLASS TK_SCOPE
  98. %token TK_INT TK_FLOAT TK_DOUBLE TK_SIGNED TK_UNSIGNED TK_COMPLEX
  99. %token TK_SEPARATOR TK_DIALOG TK_LSHIFT TK_RSHIFT
  100. %type <yy_node> expr binop uop rhs list_expression comma_list body
  101. %type <yy_node> simple_pattern complex_pattern list_pattern
  102. %type <yy_node> leaf_pattern
  103. %type <yy_node> crhs cexprlist prhs lambda
  104. %type <yy_const> TK_CONST
  105. %type <yy_name> TK_IDENT TK_TAG
  106. %left TK_SUCHTHAT
  107. %left TK_LAMBDA
  108. %nonassoc TK_IF
  109. %left ','
  110. %left TK_TO
  111. %left TK_LOR
  112. %left TK_LAND '@'
  113. %left TK_BOR
  114. %left '^'
  115. %left TK_BAND
  116. %nonassoc TK_EQ TK_NOTEQ TK_PEQ TK_PNOTEQ
  117. %nonassoc TK_LESS TK_LESSEQ TK_MORE TK_MOREEQ
  118. %left TK_LSHIFT TK_RSHIFT
  119. %left '+' '-'
  120. %left '*' '/' '%'
  121. %left '!' '~' TK_JOIN TK_DIFF TK_UMINUS TK_UPLUS
  122. %right TK_POW ':'
  123. %right TK_CONST '('
  124. %right TK_IDENT TK_TAG TK_SCOPE '['
  125. %right TK_APPLICATION
  126. %left '?' '.'
  127. %start select
  128. /*
  129. Our syntax for list comprehensions is not LALR(1). We have:
  130. simple_pattern '<-' expr ';' |
  131. expr ';'
  132. simple_pattern can be something like
  133. a:x
  134. which is also an expr. We don't know which branch to take until we see a
  135. '<' or a ';'.
  136. Use bison's GLR system to parse this, and ignore the first 13 reduce/reduce
  137. conflicts caused by this ambiguity.
  138. FIXME ... we now depend on bison, but we still have some yacc compatibility
  139. stuff in here, and we don't use all of bison's nice features (eg. for
  140. tracking line numbers in the source file). Fix this up at some stage.
  141. */
  142. %glr-parser
  143. %expect-rr 13
  144. %error-verbose
  145. %%
  146. select:
  147. ',' main |
  148. '^' single_definition |
  149. '*' params_plus_rhs optsemi {
  150. compile_check( current_compile );
  151. } |
  152. prhs {
  153. char buf[MAX_STRSIZE];
  154. current_compile->tree = $1;
  155. /* Junk any old text.
  156. */
  157. IM_FREE( current_compile->text );
  158. IM_FREE( current_compile->prhstext );
  159. IM_FREE( current_compile->rhstext );
  160. /* Set new text.
  161. */
  162. IM_SETSTR( current_compile->rhstext, input_text( buf ) );
  163. compile_check( current_compile );
  164. }
  165. ;
  166. prhs:
  167. TK_BAND expr {
  168. $$ = $2;
  169. } |
  170. '@' cexprlist {
  171. $$ = $2;
  172. }
  173. ;
  174. main:
  175. /* Empty */ |
  176. main single_definition
  177. ;
  178. single_definition:
  179. directive {
  180. tool_position += 1;
  181. } |
  182. toplevel_definition optsemi {
  183. tool_position += 1;
  184. }
  185. ;
  186. directive:
  187. TK_SEPARATOR {
  188. Tool *tool;
  189. if( !is_top( current_symbol ) )
  190. yyerror( _( "not top level" ) );
  191. tool = tool_new_sep( current_kit, tool_position );
  192. tool->lineno = input_state.lineno;
  193. input_reset();
  194. } |
  195. TK_DIALOG TK_CONST TK_CONST {
  196. Tool *tool;
  197. if( !is_top( current_symbol ) )
  198. yyerror( _( "not top level" ) );
  199. /* Should have two strings.
  200. */
  201. if( $2.type != PARSE_CONST_STR || $3.type != PARSE_CONST_STR )
  202. yyerror( _( "not strings" ) );
  203. /* Add tool.
  204. */
  205. tool = tool_new_dia( current_kit, tool_position,
  206. $2.val.str, $3.val.str );
  207. if( !tool )
  208. yyerror( error_get_sub() );
  209. tool->lineno = input_state.lineno;
  210. /* Cast away const here.
  211. */
  212. tree_const_destroy( (ParseConst *) &$2 );
  213. tree_const_destroy( (ParseConst *) &$3 );
  214. input_reset();
  215. }
  216. ;
  217. toplevel_definition:
  218. {
  219. last_top_lineno = input_state.lineno;
  220. scope_reset();
  221. current_compile = root_symbol->expr->compile;
  222. }
  223. definition {
  224. input_reset();
  225. }
  226. ;
  227. /* Parse a new defining occurence. This can be a local or a top-level.
  228. */
  229. definition:
  230. simple_pattern {
  231. Symbol *sym;
  232. /* Two forms: <name pattern-list rhs>, or <pattern rhs>.
  233. * Enforce the no-args-to-pattern-assignment rule in the arg
  234. * pattern parser.
  235. */
  236. if( $1->type == NODE_LEAF ) {
  237. const char *name = IOBJECT( $1->leaf )->name;
  238. /* Make a new defining occurence.
  239. */
  240. sym = symbol_new_defining( current_compile, name );
  241. (void) symbol_user_init( sym );
  242. (void) compile_new_local( sym->expr );
  243. }
  244. else {
  245. char name[256];
  246. /* We have <pattern rhs>. Make an anon symbol for this
  247. * value, then the variables in the pattern become
  248. * toplevels which access that.
  249. */
  250. if( !compile_pattern_has_leaf( $1 ) )
  251. yyerror( _( "left-hand-side pattern "
  252. "contains no identifiers" ) );
  253. im_snprintf( name, 256, "$$pattern_lhs%d",
  254. parse_object_id++ );
  255. sym = symbol_new_defining( current_compile, name );
  256. sym->generated = TRUE;
  257. (void) symbol_user_init( sym );
  258. (void) compile_new_local( sym->expr );
  259. }
  260. /* Note on the enclosing last_sym. Things like the program
  261. * window use this to work out what sym to display after a
  262. * parse. symbol_dispose() is careful to NULL this out.
  263. */
  264. current_compile->last_sym = sym;
  265. /* Initialise symbol parsing variables. Save old current symbol,
  266. * add new one.
  267. */
  268. scope_push();
  269. current_symbol = sym;
  270. current_compile = sym->expr->compile;
  271. g_assert( !current_compile->param );
  272. g_assert( current_compile->nparam == 0 );
  273. /* Junk any old def text.
  274. */
  275. IM_FREE( current_compile->text );
  276. IM_FREE( current_compile->prhstext );
  277. IM_FREE( current_compile->rhstext );
  278. }
  279. params_plus_rhs {
  280. compile_check( current_compile );
  281. /* Link unresolved names into the outer scope.
  282. */
  283. compile_resolve_names( current_compile,
  284. compile_get_parent( current_compile ) );
  285. /* Is this the end of a top-level? Needs extra work to add to
  286. * the enclosing toolkit etc.
  287. */
  288. if( is_scope( symbol_get_parent( current_symbol ) ) )
  289. parse_toplevel_end( current_symbol );
  290. /* Is this a pattern definition? Expand the pattern to a
  291. * set of access defs.
  292. */
  293. if( $1->type != NODE_LEAF ) {
  294. Compile *parent = compile_get_parent( current_compile );
  295. GSList *built_syms;
  296. built_syms = compile_pattern_lhs( parent,
  297. current_symbol, $1 );
  298. if( is_scope( symbol_get_parent( current_symbol ) ) )
  299. slist_map( built_syms,
  300. (SListMapFn) parse_toplevel_end, NULL );
  301. slist_map( built_syms,
  302. (SListMapFn) parse_access_end,
  303. current_symbol );
  304. g_slist_free( built_syms );
  305. }
  306. scope_pop();
  307. }
  308. ;
  309. /* Parse params/body/locals into current_expr
  310. */
  311. params_plus_rhs:
  312. {
  313. input_push( 1 );
  314. /* We've already read the character past the end of the
  315. * identifier (that's why we know the identifier is over).
  316. */
  317. input_back1();
  318. }
  319. params {
  320. input_push( 2 );
  321. input_backtoch( '=' );
  322. }
  323. body {
  324. current_compile->tree = $4;
  325. g_assert( current_compile->tree );
  326. input_push( 4 );
  327. }
  328. locals {
  329. char buf[MAX_STRSIZE];
  330. input_pop();
  331. /* Save body text as rhstext.
  332. */
  333. IM_SETSTR( current_compile->rhstext, input_text( buf ) );
  334. input_pop();
  335. /* Save params '=' body as prhstext.
  336. */
  337. IM_SETSTR( current_compile->prhstext, input_text( buf ) );
  338. input_pop();
  339. /* Save full text of definition.
  340. */
  341. IM_SETSTR( current_compile->text, input_text( buf ) );
  342. #ifdef DEBUG
  343. printf( "%s->compile->text = \"%s\"\n",
  344. IOBJECT( current_compile->sym )->name,
  345. current_compile->text );
  346. printf( "%s->compile->prhstext = \"%s\"\n",
  347. IOBJECT( current_compile->sym )->name,
  348. current_compile->prhstext );
  349. printf( "%s->compile->rhstext = \"%s\"\n",
  350. IOBJECT( current_compile->sym )->name,
  351. current_compile->rhstext );
  352. #endif /*DEBUG*/
  353. }
  354. ;
  355. params:
  356. /* Empty */ |
  357. params simple_pattern {
  358. Symbol *sym;
  359. /* If the pattern is just an identifier, make it a direct
  360. * parameter. Otherwise make an anon param and put the pattern
  361. * in as a local with the same id.
  362. *
  363. * fred [a] = 12;
  364. *
  365. * parses to:
  366. *
  367. * fred $$arg42 = 12 { $$patt42 = [a]; }
  368. *
  369. * A later pass creates the "a = $$arg42?0" definition.
  370. */
  371. if( $2->type == NODE_LEAF ) {
  372. const char *name = IOBJECT( $2->leaf )->name;
  373. /* Make defining occurence.
  374. */
  375. sym = symbol_new_defining( current_compile, name );
  376. (void) symbol_parameter_init( sym );
  377. }
  378. else {
  379. char name[256];
  380. im_snprintf( name, 256, "$$arg%d", parse_object_id );
  381. sym = symbol_new_defining( current_compile, name );
  382. sym->generated = TRUE;
  383. (void) symbol_parameter_init( sym );
  384. im_snprintf( name, 256, "$$patt%d", parse_object_id++ );
  385. sym = symbol_new_defining( current_compile, name );
  386. sym->generated = TRUE;
  387. (void) symbol_user_init( sym );
  388. (void) compile_new_local( sym->expr );
  389. sym->expr->compile->tree = $2;
  390. }
  391. }
  392. ;
  393. body :
  394. '=' TK_CLASS crhs {
  395. $$ = $3;
  396. } |
  397. rhs {
  398. $$ = $1;
  399. }
  400. ;
  401. crhs:
  402. {
  403. ParseNode *pn = tree_class_new( current_compile );
  404. input_push( 3 );
  405. scope_push();
  406. current_symbol = current_compile->super;
  407. current_compile = current_symbol->expr->compile;
  408. current_parsenode = pn;
  409. }
  410. cexprlist {
  411. Compile *parent = compile_get_parent( current_compile );
  412. char buf[MAX_STRSIZE];
  413. int len;
  414. (void) input_text( buf );
  415. /* Always read 1 char too many.
  416. */
  417. if( (len = strlen( buf )) > 0 )
  418. buf[len - 1] = '\0';
  419. IM_SETSTR( current_compile->rhstext, buf );
  420. input_pop();
  421. current_compile->tree = $2;
  422. if( current_compile->tree->elist )
  423. parent->has_super = TRUE;
  424. /* Do some checking.
  425. */
  426. compile_check( current_compile );
  427. /* Link unresolved names.
  428. */
  429. compile_resolve_names( current_compile, parent );
  430. scope_pop();
  431. $$ = current_parsenode;
  432. current_parsenode = NULL;
  433. }
  434. ;
  435. rhs:
  436. '=' expr {
  437. $$ = $2;
  438. } |
  439. '=' expr ',' expr optsemi rhs {
  440. $$ = tree_ifelse_new( current_compile, $4, $2, $6 );
  441. }
  442. ;
  443. locals:
  444. ';' |
  445. '{' deflist '}' |
  446. '{' '}'
  447. ;
  448. optsemi:
  449. /* Empty */ |
  450. ';' optsemi
  451. ;
  452. deflist:
  453. definition {
  454. input_pop();
  455. input_push( 5 );
  456. }
  457. optsemi |
  458. deflist definition {
  459. input_pop();
  460. input_push( 6 );
  461. }
  462. optsemi
  463. ;
  464. cexprlist:
  465. /* Empty */ {
  466. $$ = tree_super_new( current_compile );
  467. } |
  468. cexprlist expr %prec TK_APPLICATION {
  469. $$ = tree_super_extend( current_compile, $1, $2 );
  470. }
  471. ;
  472. expr:
  473. '(' expr ')' {
  474. $$ = $2;
  475. } |
  476. TK_CONST {
  477. $$ = tree_const_new( current_compile, $1 );
  478. } |
  479. TK_IDENT {
  480. $$ = tree_leaf_new( current_compile, $1 );
  481. im_free( $1 );
  482. } |
  483. TK_TAG {
  484. $$ = tree_tag_new( current_compile, $1 );
  485. im_free( $1 );
  486. } |
  487. TK_SCOPE {
  488. $$ = tree_leaf_new( current_compile,
  489. IOBJECT( symbol_get_scope( current_symbol ) )->name );
  490. } |
  491. TK_IF expr TK_THEN expr TK_ELSE expr %prec TK_IF {
  492. $$ = tree_ifelse_new( current_compile, $2, $4, $6 );
  493. } |
  494. expr expr %prec TK_APPLICATION {
  495. $$ = tree_appl_new( current_compile, $1, $2 );
  496. } |
  497. lambda |
  498. list_expression {
  499. $$ = $1;
  500. } |
  501. '(' expr ',' expr ')' {
  502. $$ = tree_binop_new( current_compile, BI_COMMA, $2, $4 );
  503. } |
  504. binop |
  505. uop
  506. ;
  507. lambda:
  508. TK_LAMBDA TK_IDENT %prec TK_LAMBDA {
  509. char name[256];
  510. Symbol *sym;
  511. /* Make an anonymous symbol local to the current sym, compile
  512. * the expr inside that.
  513. */
  514. im_snprintf( name, 256, "$$lambda%d", parse_object_id++ );
  515. sym = symbol_new_defining( current_compile, name );
  516. sym->generated = TRUE;
  517. (void) symbol_user_init( sym );
  518. (void) compile_new_local( sym->expr );
  519. /* Initialise symbol parsing variables. Save old current symbol,
  520. * add new one.
  521. */
  522. scope_push();
  523. current_symbol = sym;
  524. current_compile = sym->expr->compile;
  525. /* Make the parameter.
  526. */
  527. sym = symbol_new_defining( current_compile, $2 );
  528. symbol_parameter_init( sym );
  529. im_free( $2 );
  530. }
  531. expr {
  532. Symbol *sym;
  533. current_compile->tree = $4;
  534. if( !compile_check( current_compile ) )
  535. yyerror( error_get_sub() );
  536. /* Link unresolved names in to the outer scope.
  537. */
  538. compile_resolve_names( current_compile,
  539. compile_get_parent( current_compile ) );
  540. /* The value of the expr is the anon we defined.
  541. */
  542. sym = current_symbol;
  543. scope_pop();
  544. $$ = tree_leafsym_new( current_compile, sym );
  545. }
  546. ;
  547. list_expression:
  548. '[' expr TK_DOTDOTDOT ']' {
  549. $$ = tree_generator_new( current_compile, $2, NULL, NULL );
  550. } |
  551. '[' expr TK_DOTDOTDOT expr ']' {
  552. $$ = tree_generator_new( current_compile, $2, NULL, $4 );
  553. } |
  554. '[' expr ',' expr TK_DOTDOTDOT ']' {
  555. $$ = tree_generator_new( current_compile, $2, $4, NULL );
  556. } |
  557. '[' expr ',' expr TK_DOTDOTDOT expr ']' {
  558. $$ = tree_generator_new( current_compile, $2, $4, $6 );
  559. } |
  560. '[' expr TK_SUCHTHAT {
  561. char name[256];
  562. Symbol *sym;
  563. Compile *enclosing = current_compile;
  564. /* Make an anonymous symbol local to the current sym, copy
  565. * the map expr inside that.
  566. */
  567. im_snprintf( name, 256, "$$lcomp%d", parse_object_id++ );
  568. sym = symbol_new_defining( current_compile, name );
  569. (void) symbol_user_init( sym );
  570. sym->generated = TRUE;
  571. (void) compile_new_local( sym->expr );
  572. /* Push a new scope.
  573. */
  574. scope_push();
  575. current_symbol = sym;
  576. current_compile = sym->expr->compile;
  577. /* Somewhere to save the result expr. We have to copy the
  578. * expr, as we want it to be bound in $$lcomp's context so
  579. * that it can see the generators.
  580. */
  581. sym = symbol_new_defining( current_compile, "$$result" );
  582. sym->generated = TRUE;
  583. sym->placeholder = TRUE;
  584. (void) symbol_user_init( sym );
  585. (void) compile_new_local( sym->expr );
  586. sym->expr->compile->tree = compile_copy_tree( enclosing, $2,
  587. sym->expr->compile );
  588. }
  589. generator frompred_list ']' {
  590. Symbol *sym;
  591. /* The map expr can refer to generator names. Resolve inwards
  592. * so it links to the generators.
  593. */
  594. compile_resolve_names( compile_get_parent( current_compile ),
  595. current_compile );
  596. /* Generate the code for the list comp.
  597. */
  598. compile_lcomp( current_compile );
  599. compile_check( current_compile );
  600. /* Link unresolved names outwards.
  601. */
  602. compile_resolve_names( current_compile,
  603. compile_get_parent( current_compile ) );
  604. /* The value of the expr is the anon we defined.
  605. */
  606. sym = current_symbol;
  607. scope_pop();
  608. $$ = tree_leafsym_new( current_compile, sym );
  609. } |
  610. '[' comma_list ']' {
  611. $$ = $2;
  612. } |
  613. '[' ']' {
  614. ParseConst elist;
  615. elist.type = PARSE_CONST_ELIST;
  616. $$ = tree_const_new( current_compile, elist );
  617. }
  618. ;
  619. frompred_list:
  620. /* Empty */ {
  621. } |
  622. frompred_list ';' frompred {
  623. }
  624. ;
  625. generator:
  626. simple_pattern TK_FROM expr {
  627. char name[256];
  628. Symbol *sym;
  629. im_snprintf( name, 256, "$$pattern%d", parse_object_id );
  630. sym = symbol_new_defining( current_compile, name );
  631. sym->generated = TRUE;
  632. sym->placeholder = TRUE;
  633. (void) symbol_user_init( sym );
  634. (void) compile_new_local( sym->expr );
  635. sym->expr->compile->tree = $1;
  636. im_snprintf( name, 256, "$$generator%d", parse_object_id++ );
  637. sym = symbol_new_defining( current_compile, name );
  638. sym->generated = TRUE;
  639. sym->placeholder = TRUE;
  640. (void) symbol_user_init( sym );
  641. (void) compile_new_local( sym->expr );
  642. sym->expr->compile->tree = $3;
  643. }
  644. ;
  645. frompred:
  646. generator |
  647. expr {
  648. char name[256];
  649. Symbol *sym;
  650. im_snprintf( name, 256, "$$filter%d", parse_object_id++ );
  651. sym = symbol_new_defining( current_compile, name );
  652. sym->generated = TRUE;
  653. sym->placeholder = TRUE;
  654. (void) symbol_user_init( sym );
  655. (void) compile_new_local( sym->expr );
  656. sym->expr->compile->tree = $1;
  657. }
  658. ;
  659. comma_list:
  660. expr ',' comma_list {
  661. $$ = tree_lconst_extend( current_compile, $3, $1 );
  662. } |
  663. expr {
  664. $$ = tree_lconst_new( current_compile, $1 );
  665. }
  666. ;
  667. /* How odd, break the "'+' { BI_ADD } | ..." into a separate production and we
  668. * get reduce/reduce conflits. Copypaste a lot instead.
  669. */
  670. binop:
  671. expr '+' expr {
  672. $$ = tree_binop_new( current_compile, BI_ADD, $1, $3 );
  673. } |
  674. expr ':' expr {
  675. $$ = tree_binop_new( current_compile, BI_CONS, $1, $3 );
  676. } |
  677. expr '-' expr {
  678. $$ = tree_binop_new( current_compile, BI_SUB, $1, $3 );
  679. } |
  680. expr '?' expr {
  681. $$ = tree_binop_new( current_compile, BI_SELECT, $1, $3 );
  682. } |
  683. expr '/' expr {
  684. $$ = tree_binop_new( current_compile, BI_DIV, $1, $3 );
  685. } |
  686. expr '*' expr {
  687. $$ = tree_binop_new( current_compile, BI_MUL, $1, $3 );
  688. } |
  689. expr '%' expr {
  690. $$ = tree_binop_new( current_compile, BI_REM, $1, $3 );
  691. } |
  692. expr TK_JOIN expr {
  693. $$ = tree_binop_new( current_compile, BI_JOIN, $1, $3 );
  694. } |
  695. expr TK_POW expr {
  696. $$ = tree_binop_new( current_compile, BI_POW, $1, $3 );
  697. } |
  698. expr TK_LSHIFT expr {
  699. $$ = tree_binop_new( current_compile, BI_LSHIFT, $1, $3 );
  700. } |
  701. expr TK_RSHIFT expr {
  702. $$ = tree_binop_new( current_compile, BI_RSHIFT, $1, $3 );
  703. } |
  704. expr '^' expr {
  705. $$ = tree_binop_new( current_compile, BI_EOR, $1, $3 );
  706. } |
  707. expr TK_LAND expr {
  708. $$ = tree_binop_new( current_compile, BI_LAND, $1, $3 );
  709. } |
  710. expr TK_BAND expr {
  711. $$ = tree_binop_new( current_compile, BI_BAND, $1, $3 );
  712. } |
  713. expr '@' expr {
  714. $$ = tree_compose_new( current_compile, $1, $3 );
  715. } |
  716. expr TK_LOR expr {
  717. $$ = tree_binop_new( current_compile, BI_LOR, $1, $3 );
  718. } |
  719. expr TK_BOR expr {
  720. $$ = tree_binop_new( current_compile, BI_BOR, $1, $3 );
  721. } |
  722. expr TK_LESS expr {
  723. $$ = tree_binop_new( current_compile, BI_LESS, $1, $3 );
  724. } |
  725. expr TK_LESSEQ expr {
  726. $$ = tree_binop_new( current_compile, BI_LESSEQ, $1, $3 );
  727. } |
  728. expr TK_MORE expr {
  729. $$ = tree_binop_new( current_compile, BI_MORE, $1, $3 );
  730. } |
  731. expr TK_MOREEQ expr {
  732. $$ = tree_binop_new( current_compile, BI_MOREEQ, $1, $3 );
  733. } |
  734. expr TK_EQ expr {
  735. $$ = tree_binop_new( current_compile, BI_EQ, $1, $3 );
  736. } |
  737. expr TK_NOTEQ expr {
  738. $$ = tree_binop_new( current_compile, BI_NOTEQ, $1, $3 );
  739. } |
  740. expr TK_PEQ expr {
  741. $$ = tree_binop_new( current_compile, BI_PEQ, $1, $3 );
  742. } |
  743. expr TK_PNOTEQ expr {
  744. $$ = tree_binop_new( current_compile, BI_PNOTEQ, $1, $3 );
  745. } |
  746. expr '.' expr {
  747. $$ = tree_binop_new( current_compile, BI_DOT, $1, $3 );
  748. } |
  749. expr TK_DIFF expr {
  750. ParseNode *pn1, *pn2;
  751. pn1 = tree_leaf_new( current_compile, "difference" );
  752. pn2 = tree_leaf_new( current_compile, "equal" );
  753. pn1 = tree_appl_new( current_compile, pn1, pn2 );
  754. pn1 = tree_appl_new( current_compile, pn1, $1 );
  755. pn1 = tree_appl_new( current_compile, pn1, $3 );
  756. $$ = pn1;
  757. } |
  758. expr TK_TO expr {
  759. ParseNode *pn;
  760. pn = tree_leaf_new( current_compile, "mknvpair" );
  761. pn = tree_appl_new( current_compile, pn, $1 );
  762. pn = tree_appl_new( current_compile, pn, $3 );
  763. $$ = pn;
  764. }
  765. ;
  766. signed:
  767. /* Nothing */ |
  768. TK_SIGNED
  769. ;
  770. unsigned:
  771. /* Nothing */ |
  772. TK_UNSIGNED
  773. ;
  774. uop:
  775. '(' unsigned TK_CHAR ')' expr %prec TK_UMINUS {
  776. $$ = tree_unop_new( current_compile, UN_CUCHAR, $5 );
  777. } |
  778. '(' TK_SIGNED TK_CHAR ')' expr %prec TK_UMINUS {
  779. $$ = tree_unop_new( current_compile, UN_CSCHAR, $5 );
  780. } |
  781. '(' signed TK_SHORT ')' expr %prec TK_UMINUS {
  782. $$ = tree_unop_new( current_compile, UN_CSSHORT, $5 );
  783. } |
  784. '(' TK_UNSIGNED TK_SHORT ')' expr %prec TK_UMINUS {
  785. $$ = tree_unop_new( current_compile, UN_CUSHORT, $5 );
  786. } |
  787. '(' signed TK_INT ')' expr %prec TK_UMINUS {
  788. $$ = tree_unop_new( current_compile, UN_CSINT, $5 );
  789. } |
  790. '(' TK_UNSIGNED TK_INT ')' expr %prec TK_UMINUS {
  791. $$ = tree_unop_new( current_compile, UN_CUINT, $5 );
  792. } |
  793. '(' TK_FLOAT ')' expr %prec TK_UMINUS {
  794. $$ = tree_unop_new( current_compile, UN_CFLOAT, $4 );
  795. } |
  796. '(' TK_DOUBLE ')' expr %prec TK_UMINUS {
  797. $$ = tree_unop_new( current_compile, UN_CDOUBLE, $4 );
  798. } |
  799. '(' TK_COMPLEX ')' expr %prec TK_UMINUS {
  800. $$ = tree_unop_new( current_compile, UN_CCOMPLEX, $4 );
  801. } |
  802. '(' TK_DOUBLE TK_COMPLEX ')' expr %prec TK_UMINUS {
  803. $$ = tree_unop_new( current_compile, UN_CDCOMPLEX, $5 );
  804. } |
  805. TK_UMINUS expr {
  806. $$ = tree_unop_new( current_compile, UN_MINUS, $2 );
  807. } |
  808. '!' expr {
  809. $$ = tree_unop_new( current_compile, UN_NEG, $2 );
  810. } |
  811. '~' expr {
  812. $$ = tree_unop_new( current_compile, UN_COMPLEMENT, $2 );
  813. } |
  814. TK_UPLUS expr {
  815. $$ = tree_unop_new( current_compile, UN_PLUS, $2 );
  816. }
  817. ;
  818. /* Stuff that can appear on the LHS of an equals, or as a parameter pattern.
  819. */
  820. simple_pattern:
  821. leaf_pattern |
  822. '(' leaf_pattern ',' leaf_pattern ')' {
  823. $$ = tree_binop_new( current_compile, BI_COMMA, $2, $4 );
  824. } |
  825. simple_pattern ':' simple_pattern {
  826. $$ = tree_binop_new( current_compile, BI_CONS, $1, $3 );
  827. } |
  828. '(' complex_pattern ')' {
  829. $$ = $2;
  830. } |
  831. '[' list_pattern ']' {
  832. $$ = $2;
  833. } |
  834. '[' ']' {
  835. ParseConst elist;
  836. elist.type = PARSE_CONST_ELIST;
  837. $$ = tree_const_new( current_compile, elist );
  838. }
  839. ;
  840. /* Stuff that can appear in a complex (a, b) pattern.
  841. */
  842. leaf_pattern:
  843. TK_IDENT {
  844. $$ = tree_leaf_new( current_compile, $1 );
  845. im_free( $1 );
  846. } |
  847. TK_CONST {
  848. $$ = tree_const_new( current_compile, $1 );
  849. }
  850. ;
  851. /* What can appear in round brackets or a comma list.
  852. */
  853. complex_pattern:
  854. TK_IDENT TK_IDENT {
  855. $$ = tree_pattern_class_new( current_compile, $1,
  856. tree_leaf_new( current_compile, $2 ) );
  857. im_free( $1 );
  858. im_free( $2 );
  859. } |
  860. simple_pattern
  861. ;
  862. list_pattern:
  863. complex_pattern ',' list_pattern {
  864. $$ = tree_lconst_extend( current_compile, $3, $1 );
  865. } |
  866. complex_pattern {
  867. $$ = tree_lconst_new( current_compile, $1 );
  868. }
  869. ;
  870. %%
  871. /* Return point on syntax error.
  872. */
  873. jmp_buf parse_error_point;
  874. /* Text we've lexed.
  875. */
  876. char lex_text_buffer[MAX_STRSIZE];
  877. VipsBuf lex_text = VIPS_BUF_STATIC( lex_text_buffer );
  878. /* State of input system.
  879. */
  880. InputState input_state;
  881. /* Defintions for the static decls at the top. We have to put the defs down
  882. * here to mkake sure they don't creep in to the generated parser.h.
  883. */
  884. /* Actually, we can't make these static :-( since they are declared extern at
  885. * the top of the file.
  886. */
  887. Symbol *current_symbol;
  888. Symbol *root_symbol;
  889. Compile *current_compile = NULL;
  890. ParseNode *current_parsenode = NULL;
  891. Toolkit *current_kit;
  892. int tool_position;
  893. int last_top_lineno;
  894. Symbol *scope_stack_symbol[MAX_SSTACK];
  895. Compile *scope_stack_compile[MAX_SSTACK];
  896. int scope_sp = 0;
  897. int parse_object_id = 0;
  898. /* Here for errors in parse.
  899. *
  900. * Bison calls yyerror with only a char* arg. This printf() version is called
  901. * from nip2 in a few places during parse.
  902. */
  903. void
  904. nip2yyerror( const char *sub, ... )
  905. {
  906. va_list ap;
  907. char buf[4096];
  908. va_start( ap, sub );
  909. (void) im_vsnprintf( buf, 4096, sub, ap );
  910. va_end( ap );
  911. error_top( _( "Parse error." ) );
  912. if( current_compile && current_compile->last_sym )
  913. error_sub( _( "Error in %s: %s" ),
  914. IOBJECT( current_compile->last_sym )->name, buf );
  915. else
  916. error_sub( _( "Error: %s" ), buf );
  917. longjmp( parse_error_point, -1 );
  918. }
  919. /* Bison calls this.
  920. */
  921. void
  922. yyerror( const char *msg )
  923. {
  924. nip2yyerror( "%s", msg );
  925. }
  926. /* Attach yyinput to a file.
  927. */
  928. void
  929. attach_input_file( iOpenFile *of )
  930. {
  931. InputState *is = &input_state;
  932. #ifdef DEBUG
  933. printf( "attach_input_file: \"%s\"\n", of->fname );
  934. #endif /*DEBUG*/
  935. /* Need to clear flex/bison's buffers in case we abandoned the
  936. * previous parse.
  937. */
  938. yyrestart( NULL );
  939. is->of = of;
  940. is->str = NULL;
  941. is->strpos = NULL;
  942. is->bwp = 0;
  943. is->bspsp = 0;
  944. is->bsp[is->bspsp] = 0;
  945. is->lineno = 1;
  946. is->charno = 0;
  947. is->pcharno = 0;
  948. is->charpos = 0;
  949. is->oldchar = -1;
  950. /* Init text gatherer.
  951. */
  952. vips_buf_rewind( &lex_text );
  953. }
  954. /* Attach yyinput to a string.
  955. */
  956. void
  957. attach_input_string( const char *str )
  958. {
  959. InputState *is = &input_state;
  960. #ifdef DEBUG
  961. printf( "attach_input_string: \"%s\"\n", str );
  962. #endif /*DEBUG*/
  963. yyrestart( NULL );
  964. is->of = NULL;
  965. is->str = (char *) str;
  966. is->strpos = (char *) str;
  967. is->bwp = 0;
  968. is->bspsp = 0;
  969. is->bsp[is->bspsp] = 0;
  970. is->lineno = 1;
  971. is->charno = 0;
  972. is->pcharno = 0;
  973. is->charpos = 0;
  974. is->oldchar = -1;
  975. /* Init text gatherer.
  976. */
  977. vips_buf_rewind( &lex_text );
  978. }
  979. /* Read a character from the input.
  980. */
  981. int
  982. ip_input( void )
  983. {
  984. InputState *is = &input_state;
  985. int ch;
  986. if( is->oldchar >= 0 ) {
  987. /* From unget buffer.
  988. */
  989. ch = is->oldchar;
  990. is->oldchar = -1;
  991. }
  992. else if( is->of ) {
  993. /* Input from file.
  994. */
  995. if( (ch = getc( is->of->fp )) == EOF )
  996. return( 0 );
  997. }
  998. else {
  999. /* Input from string.
  1000. */
  1001. if( (ch = *is->strpos) )
  1002. is->strpos++;
  1003. else
  1004. /* No counts to update!
  1005. */
  1006. return( 0 );
  1007. }
  1008. /* Update counts.
  1009. */
  1010. if( ch == '\n' ) {
  1011. is->lineno++;
  1012. is->pcharno = is->charno + 1;
  1013. is->charno = 0;
  1014. }
  1015. is->charno++;
  1016. is->charpos++;
  1017. /* Add this character to the characters we have accumulated for this
  1018. * definition.
  1019. */
  1020. if( is->bwp >= MAX_STRSIZE )
  1021. yyerror( _( "definition is too long" ) );
  1022. if( is->bwp >= 0 )
  1023. is->buf[is->bwp] = ch;
  1024. is->bwp++;
  1025. /* Add to lex text buffer.
  1026. */
  1027. if( is->charno > 0 )
  1028. vips_buf_appendc( &lex_text, ch );
  1029. #ifdef DEBUG_CHARACTER
  1030. printf( "ip_input: returning '%c'\n", ch );
  1031. #endif /*DEBUG_CHARACTER*/
  1032. return( ch );
  1033. }
  1034. /* Unget an input character.
  1035. */
  1036. void
  1037. ip_unput( int ch )
  1038. {
  1039. InputState *is = &input_state;
  1040. #ifdef DEBUG_CHARACTER
  1041. printf( "ip_unput: ungetting '%c'\n", ch );
  1042. #endif /*DEBUG_CHARACTER*/
  1043. /* Is lex trying to unget the end-of-file marker? Do nothing if it is.
  1044. */
  1045. if( !ch )
  1046. return;
  1047. if( is->of ) {
  1048. if( ungetc( ch, is->of->fp ) == EOF )
  1049. error( "unget buffer overflow" );
  1050. }
  1051. else
  1052. /* Save extra char here.
  1053. */
  1054. is->oldchar = ch;
  1055. /* Redo counts.
  1056. */
  1057. if( ch == '\n' ) {
  1058. is->lineno--;
  1059. /* Restore previous charno.
  1060. */
  1061. is->charno = is->pcharno;
  1062. is->pcharno = 0;
  1063. }
  1064. is->charno--;
  1065. is->charpos--;
  1066. is->bwp--;
  1067. /* Unget from lex text buffer.
  1068. */
  1069. if( is->charno > 0 )
  1070. vips_buf_removec( &lex_text, ch );
  1071. }
  1072. /* Test for end-of-input.
  1073. */
  1074. gboolean
  1075. is_EOF( void )
  1076. {
  1077. InputState *is = &input_state;
  1078. if( is->of )
  1079. return( feof( is->of->fp ) );
  1080. else
  1081. return( *is->str == '\0' );
  1082. }
  1083. /* Return the text we have accumulated for the current definition. Remove
  1084. * leading and trailing whitespace and spare semicolons. out needs to be
  1085. * MAX_STRSIZE.
  1086. */
  1087. char *
  1088. input_text( char *out )
  1089. {
  1090. InputState *is = &input_state;
  1091. const char *buf = is->buf;
  1092. int start = is->bsp[is->bspsp];
  1093. int end = is->bwp;
  1094. int len;
  1095. int i;
  1096. for( i = start; i < end &&
  1097. (isspace( buf[i] ) || buf[i] == ';'); i++ )
  1098. ;
  1099. start = i;
  1100. for( i = end - 1; i > start &&
  1101. (isspace( buf[i] ) || buf[i] == ';'); i-- )
  1102. ;
  1103. end = i + 1;
  1104. len = end - start;
  1105. g_assert( len < MAX_STRSIZE - 1 );
  1106. im_strncpy( out, buf + start, len + 1 );
  1107. out[len] = '\0';
  1108. #ifdef DEBUG_CHARACTER
  1109. printf( "input_text: level %d, returning \"%s\"\n",
  1110. is->bspsp, out );
  1111. #endif /*DEBUG_CHARACTER*/
  1112. return( out );
  1113. }
  1114. /* Reset/push/pop input stacks.
  1115. */
  1116. void
  1117. input_reset( void )
  1118. {
  1119. InputState *is = &input_state;
  1120. #ifdef DEBUG_CHARACTER
  1121. printf( "input_reset:\n" );
  1122. #endif /*DEBUG_CHARACTER*/
  1123. is->bwp = 0;
  1124. is->bspsp = 0;
  1125. is->bsp[0] = 0;
  1126. vips_buf_rewind( &lex_text );
  1127. }
  1128. void
  1129. input_push( int n )
  1130. {
  1131. InputState *is = &input_state;
  1132. #ifdef DEBUG_CHARACTER
  1133. printf( "input_push(%d): going to level %d, %d bytes into buffer\n",
  1134. n, is->bspsp + 1, is->bwp );
  1135. {
  1136. const int len = IM_MIN( is->bwp, 20 );
  1137. int i;
  1138. for( i = is->bwp - len; i < is->bwp; i++ )
  1139. if( is->buf[i] == '\n' )
  1140. printf( "@" );
  1141. else if( is->buf[i] == ' ' || is->buf[i] == '\t' )
  1142. printf( "_" );
  1143. else
  1144. printf( "%c", is->buf[i] );
  1145. printf( "\n" );
  1146. for( i = 0; i < len; i++ )
  1147. printf( "-" );
  1148. printf( "^\n" );
  1149. }
  1150. #endif /*DEBUG_CHARACTER*/
  1151. is->bspsp += 1;
  1152. if( is->bspsp >= MAX_SSTACK )
  1153. error( "bstack overflow" );
  1154. is->bsp[is->bspsp] = is->bwp;
  1155. }
  1156. /* Yuk! We've just done an input_push() to try to grab the RHS of a
  1157. * definition ... unfortunately, due to token readahead, we've probably
  1158. * already read the start of the RHS.
  1159. *
  1160. * Back up the start point to just after the last ch character.
  1161. */
  1162. void
  1163. input_backtoch( char ch )
  1164. {
  1165. InputState *is = &input_state;
  1166. int i;
  1167. for( i = is->bsp[is->bspsp] - 1; i > 0 && is->buf[i] != ch; i-- )
  1168. ;
  1169. if( is->buf[i] == ch )
  1170. is->bsp[is->bspsp] = i + 1;
  1171. }
  1172. /* Move the last input_push() point back 1 character.
  1173. */
  1174. void
  1175. input_back1( void )
  1176. {
  1177. InputState *is = &input_state;
  1178. if( is->bsp[is->bspsp] > 0 )
  1179. is->bsp[is->bspsp] -= 1;
  1180. }
  1181. void
  1182. input_pop( void )
  1183. {
  1184. InputState *is = &input_state;
  1185. #ifdef DEBUG_CHARACTER
  1186. printf( "input_pop: %d bytes into buffer\n", input_state.bwp );
  1187. #endif /*DEBUG_CHARACTER*/
  1188. if( is->bspsp <= 0 )
  1189. error( "bstack underflow" );
  1190. is->bspsp--;
  1191. }
  1192. void
  1193. scope_push( void )
  1194. {
  1195. if( scope_sp == MAX_SSTACK )
  1196. error( "sstack overflow" );
  1197. scope_stack_symbol[scope_sp] = current_symbol;
  1198. scope_stack_compile[scope_sp] = current_compile;
  1199. scope_sp += 1;
  1200. }
  1201. void
  1202. scope_pop( void )
  1203. {
  1204. if( scope_sp <= 0 )
  1205. error( "sstack underflow" );
  1206. scope_sp -= 1;
  1207. current_symbol = scope_stack_symbol[scope_sp];
  1208. current_compile = scope_stack_compile[scope_sp];
  1209. }
  1210. /* Back to the outermost scope.
  1211. */
  1212. void
  1213. scope_pop_all( void )
  1214. {
  1215. if( scope_sp > 0 ) {
  1216. scope_sp = 0;
  1217. current_symbol = scope_stack_symbol[scope_sp];
  1218. current_compile = scope_stack_compile[scope_sp];
  1219. }
  1220. }
  1221. /* Reset/push/pop parser stacks.
  1222. */
  1223. void
  1224. scope_reset( void )
  1225. {
  1226. scope_sp = 0;
  1227. }
  1228. /* End of top level parse. Fix up the symbol.
  1229. */
  1230. void *
  1231. parse_toplevel_end( Symbol *sym )
  1232. {
  1233. Tool *tool;
  1234. tool = tool_new_sym( current_kit, tool_position, sym );
  1235. tool->lineno = last_top_lineno;
  1236. symbol_made( sym );
  1237. return( NULL );
  1238. }
  1239. /* Built a pattern access definition. Set the various text fragments from the
  1240. * def we are drived from.
  1241. */
  1242. void *
  1243. parse_access_end( Symbol *sym, Symbol *main )
  1244. {
  1245. IM_SETSTR( sym->expr->compile->rhstext,
  1246. main->expr->compile->rhstext );
  1247. IM_SETSTR( sym->expr->compile->prhstext,
  1248. main->expr->compile->prhstext );
  1249. IM_SETSTR( sym->expr->compile->text,
  1250. main->expr->compile->text );
  1251. return( NULL );
  1252. }
  1253. /* Interface to parser.
  1254. */
  1255. static gboolean
  1256. parse_input( int ch, Symbol *sym, Toolkit *kit, int pos )
  1257. {
  1258. current_kit = kit;
  1259. current_symbol = sym;
  1260. root_symbol = sym;
  1261. tool_position = pos;
  1262. scope_reset();
  1263. input_reset();
  1264. /* Signal start nonterminal to parser.
  1265. */
  1266. ip_unput( ch );
  1267. if( setjmp( parse_error_point ) ) {
  1268. /* Restore current_compile.
  1269. */
  1270. scope_pop_all();
  1271. if( current_compile )
  1272. compile_error_set( current_compile );
  1273. return( FALSE );
  1274. }
  1275. yyparse();
  1276. /* All ok.
  1277. */
  1278. return( TRUE );
  1279. }
  1280. /* Parse the input into a set of symbols at a position in a kit.
  1281. * kit may be NULL for no kit.
  1282. */
  1283. gboolean
  1284. parse_toplevel( Toolkit *kit, int pos )
  1285. {
  1286. gboolean result;
  1287. current_compile = NULL;
  1288. result = parse_input( ',', kit->kitg->root, kit, pos );
  1289. iobject_changed( IOBJECT( kit ) );
  1290. return( result );
  1291. }
  1292. /* Parse a single top-level definition.
  1293. */
  1294. gboolean
  1295. parse_onedef( Toolkit *kit, int pos )
  1296. {
  1297. gboolean result;
  1298. current_compile = NULL;
  1299. result = parse_input( '^', kit->kitg->root, kit, pos );
  1300. iobject_changed( IOBJECT( kit ) );
  1301. return( result );
  1302. }
  1303. /* Parse new text into "expr". If params is set, str should be "a b = a+b"
  1304. * (ie. include params), if not, then just rhs (eg. "a+b").
  1305. */
  1306. gboolean
  1307. parse_rhs( Expr *expr, ParseRhsSyntax syntax )
  1308. {
  1309. static const char start_ch_table[] = {
  1310. '&', /* PARSE_RHS */
  1311. '*', /* PARSE_PARAMS */
  1312. '@' /* PARSE_SUPER */
  1313. };
  1314. char start_ch = start_ch_table[(int) syntax];
  1315. Compile *compile = compile_new_local( expr );
  1316. current_compile = compile;
  1317. if( !parse_input( start_ch, expr->sym, NULL, -1 ) ) {
  1318. current_compile = NULL;
  1319. return( FALSE );
  1320. }
  1321. current_compile = NULL;
  1322. #ifdef DEBUG
  1323. printf( "parse_rhs:\n" );
  1324. dump_tree( compile->tree );
  1325. #endif /*DEBUG*/
  1326. /* Resolve any dynamic refs.
  1327. */
  1328. expr_resolve( expr );
  1329. /* Compile.
  1330. */
  1331. if( compile_object( compile ) )
  1332. return( FALSE );
  1333. return( TRUE );
  1334. }
  1335. /* Free any stuff the lexer might have allocated.
  1336. */
  1337. void
  1338. free_lex( int yychar )
  1339. {
  1340. switch( yychar ) {
  1341. case TK_CONST:
  1342. tree_const_destroy( &yylval.yy_const );
  1343. break;
  1344. case TK_IDENT:
  1345. case TK_TAG:
  1346. IM_FREE( yylval.yy_name );
  1347. break;
  1348. default:
  1349. break;
  1350. }
  1351. }
  1352. /* Do we have a string of the form "IDENT = .."? Use the lexer to look along
  1353. * the string checking components, return the IDENT if we do.
  1354. */
  1355. char *
  1356. parse_test_define( void )
  1357. {
  1358. extern int yylex( void );
  1359. int yychar;
  1360. char *ident;
  1361. ident = NULL;
  1362. if( setjmp( parse_error_point ) ) {
  1363. /* Here for yyerror in lex.
  1364. */
  1365. IM_FREE( ident );
  1366. return( NULL );
  1367. }
  1368. if( (yychar = yylex()) != TK_IDENT ) {
  1369. free_lex( yychar );
  1370. yyerror( _( "no leading identifier" ) );
  1371. }
  1372. ident = yylval.yy_name;
  1373. if( (yychar = yylex()) != '=' ) {
  1374. free_lex( yychar );
  1375. yyerror( _( "'=' missing" ) );
  1376. }
  1377. return( ident );
  1378. }
  1379. /* Do we have a string like "Workspaces.untitled.A1 = .."? Check for the
  1380. * symbols as we see them, make the last one and return it. Used by --set.
  1381. */
  1382. Symbol *
  1383. parse_set_symbol( void )
  1384. {
  1385. int yychar;
  1386. extern int yylex( void );
  1387. Compile *compile = symbol_root->expr->compile;
  1388. char *ident;
  1389. Symbol *sym;
  1390. ident = NULL;
  1391. if( setjmp( parse_error_point ) ) {
  1392. /* Here for yyerror in lex.
  1393. */
  1394. IM_FREE( ident );
  1395. return( NULL );
  1396. }
  1397. do {
  1398. if( (yychar = yylex()) != TK_IDENT && yychar != TK_TAG ) {
  1399. free_lex( yychar );
  1400. yyerror( _( "identifier expected" ) );
  1401. }
  1402. ident = yylval.yy_name;
  1403. switch( (yychar = yylex()) ) {
  1404. case '.':
  1405. /* There's a dot, so we expect another identifier to
  1406. * come. Look up this one and move to that context.
  1407. */
  1408. if( !(sym = compile_lookup( compile, ident )) )
  1409. nip2yyerror( _( "'%s' does not exist" ),
  1410. ident );
  1411. if( !sym->expr || !sym->expr->compile )
  1412. nip2yyerror( _( "'%s' has no members" ),
  1413. ident );
  1414. compile = sym->expr->compile;
  1415. IM_FREE( ident );
  1416. break;
  1417. case '=':
  1418. /* This is the final identifier: create the symbol in
  1419. * this context.
  1420. */
  1421. sym = symbol_new_defining( compile, ident );
  1422. IM_FREE( ident );
  1423. break;
  1424. default:
  1425. free_lex( yychar );
  1426. yyerror( _( "'.' or '=' expected" ) );
  1427. }
  1428. } while( yychar != '=' );
  1429. return( sym );
  1430. }