scan.l 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. %{
  2. {
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. unit scan;
  17. {$H+}
  18. interface
  19. uses
  20. strings,
  21. h2plexlib,h2pyacclib;
  22. const
  23. version = '1.0.0';
  24. type
  25. Char=system.char;
  26. ttyp = (
  27. t_id,
  28. { p contains the string }
  29. t_arraydef,
  30. { }
  31. t_pointerdef,
  32. { p1 contains the definition
  33. if in type overrider
  34. or nothing for args
  35. }
  36. t_addrdef,
  37. t_void,
  38. { no field }
  39. t_dec,
  40. { }
  41. t_declist,
  42. { p1 is t_dec
  43. next if exists }
  44. t_memberdec,
  45. { p1 is type specifier
  46. p2 is declarator_list }
  47. t_structdef,
  48. { }
  49. t_memberdeclist,
  50. { p1 is memberdec
  51. next is next if it exist }
  52. t_procdef,
  53. { }
  54. t_uniondef,
  55. { }
  56. t_enumdef,
  57. { }
  58. t_enumlist,
  59. { }
  60. t_preop,
  61. { p contains the operator string
  62. p1 contains the right expr }
  63. t_bop,
  64. { p contains the operator string
  65. p1 contains the left expr
  66. p2 contains the right expr }
  67. t_arrayop,
  68. {
  69. p1 contains the array expr
  70. p2 contains the index expressions }
  71. t_callop,
  72. {
  73. p1 contains the proc expr
  74. p2 contains the index expressions }
  75. t_arg,
  76. {
  77. p1 contain the typedef
  78. p2 the declarator (t_dec)
  79. }
  80. t_arglist,
  81. { }
  82. t_funexprlist,
  83. { }
  84. t_exprlist,
  85. { p1 contains the expr
  86. next contains the next if it exists }
  87. t_ifexpr,
  88. { p1 contains the condition expr
  89. p2 contains the if branch
  90. p3 contains the else branch }
  91. t_funcname,
  92. { p1 contains the function dname
  93. p2 contains the funexprlist
  94. p3 possibly contains the return type }
  95. t_typespec,
  96. { p1 is the type itself
  97. p2 the typecast expr }
  98. t_size_specifier,
  99. { p1 expr for size }
  100. t_default_value,
  101. { p1 expr for value }
  102. t_statement_list,
  103. { p1 is the statement
  104. next is next if it exist }
  105. t_whilenode,
  106. t_fornode,
  107. t_dowhilenode,
  108. t_switchnode,
  109. t_gotonode,
  110. t_continuenode,
  111. t_breaknode
  112. );
  113. const
  114. ttypstr: array[ttyp] of string =
  115. (
  116. 't_id',
  117. 't_arraydef',
  118. 't_pointerdef',
  119. 't_addrdef',
  120. 't_void',
  121. 't_dec',
  122. 't_declist',
  123. 't_memberdec',
  124. 't_structdef',
  125. 't_memberdeclist',
  126. 't_procdef',
  127. 't_uniondef',
  128. 't_enumdef',
  129. 't_enumlist',
  130. 't_preop',
  131. 't_bop',
  132. 't_arrayop',
  133. 't_callop',
  134. 't_arg',
  135. 't_arglist',
  136. 't_funexprlist',
  137. 't_exprlist',
  138. 't_ifexpr',
  139. 't_funcname',
  140. 't_typespec',
  141. 't_size_specifier',
  142. 't_default_value',
  143. 't_statement_list',
  144. 't_whilenode',
  145. 't_fornode',
  146. 't_dowhilenode',
  147. 't_switchnode',
  148. 't_gotonode',
  149. 't_continuenode',
  150. 't_breaknode'
  151. );
  152. type
  153. presobject = ^tresobject;
  154. tresobject = object
  155. typ : ttyp;
  156. p : pchar;
  157. next : presobject;
  158. p1,p2,p3 : presobject;
  159. { name of int/real, then no T prefix is required }
  160. intname : boolean;
  161. constructor init_no(t : ttyp);
  162. constructor init_one(t : ttyp;_p1 : presobject);
  163. constructor init_two(t : ttyp;_p1,_p2 : presobject);
  164. constructor init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  165. constructor init_id(const s : string);
  166. constructor init_intid(const s : string);
  167. constructor init_bop(const s : string;_p1,_p2 : presobject);
  168. constructor init_preop(const s : string;_p1 : presobject);
  169. procedure setstr(const s:string);
  170. function str : string;
  171. function strlength : byte;
  172. function get_copy : presobject;
  173. { can this ve considered as a constant ? }
  174. function is_const : boolean;
  175. destructor done;
  176. end;
  177. tblocktype = (bt_type,bt_const,bt_var,bt_func,bt_no);
  178. var
  179. infile : string;
  180. outfile : text;
  181. c : char;
  182. aktspace : string;
  183. block_type : tblocktype;
  184. commentstr: string;
  185. const
  186. in_define : boolean = false;
  187. { True if define spans to the next line }
  188. cont_line : boolean = false;
  189. { 1 after define; 2 after the ID to print the first separating space }
  190. in_space_define : byte = 0;
  191. arglevel : longint = 0;
  192. {> 1 = ifdef level in a ifdef C++ block
  193. 1 = first level in an ifdef block
  194. 0 = not in an ifdef block
  195. -1 = in else part of ifdef block, process like we weren't in the block
  196. but skip the incoming end.
  197. > -1 = ifdef sublevel in an else block.
  198. }
  199. cplusblocklevel : LongInt = 0;
  200. function yylex : integer;
  201. function act_token : string;
  202. procedure internalerror(i : integer);
  203. function strpnew(const s : string) : pchar;
  204. procedure writetree(p: presobject);
  205. implementation
  206. uses
  207. h2poptions,converu;
  208. const
  209. newline = #10;
  210. procedure writeentry(p: presobject; var currentlevel: integer);
  211. begin
  212. if assigned(p^.p1) then
  213. begin
  214. WriteLn(' Entry p1[',ttypstr[p^.p1^.typ],']',p^.p1^.str);
  215. end;
  216. if assigned(p^.p2) then
  217. begin
  218. WriteLn(' Entry p2[',ttypstr[p^.p2^.typ],']',p^.p2^.str);
  219. end;
  220. if assigned(p^.p3) then
  221. begin
  222. WriteLn(' Entry p3[',ttypstr[p^.p3^.typ],']',p^.p3^.str);
  223. end;
  224. end;
  225. procedure writetree(p: presobject);
  226. var
  227. i : integer;
  228. localp: presobject;
  229. localp1: presobject;
  230. currentlevel : integer;
  231. begin
  232. localp:=p;
  233. currentlevel:=0;
  234. while assigned(localp) do
  235. begin
  236. WriteLn('Entry[',ttypstr[localp^.typ],']',localp^.str);
  237. case localp^.typ of
  238. { Some arguments sharing the same type }
  239. t_arglist:
  240. begin
  241. localp1:=localp;
  242. while assigned(localp1) do
  243. begin
  244. writeentry(localp1,currentlevel);
  245. localp1:=localp1^.p1;
  246. end;
  247. end;
  248. end;
  249. localp:=localp^.next;
  250. end;
  251. end;
  252. procedure internalerror(i : integer);
  253. begin
  254. writeln('Internal error ',i,' in line ',yylineno);
  255. halt(1);
  256. end;
  257. procedure commenteof;
  258. begin
  259. writeln('unexpected EOF inside comment at line ',yylineno);
  260. end;
  261. procedure copy_until_eol;
  262. begin
  263. c:=get_char;
  264. while c<>newline do
  265. begin
  266. write(outfile,c);
  267. c:=get_char;
  268. end;
  269. end;
  270. procedure skip_until_eol;
  271. begin
  272. c:=get_char;
  273. while c<>newline do
  274. c:=get_char;
  275. end;
  276. function strpnew(const s : string) : pchar;
  277. var
  278. p : pchar;
  279. begin
  280. getmem(p,length(s)+1);
  281. strpcopy(p,s);
  282. strpnew:=p;
  283. end;
  284. function NotInCPlusBlock : Boolean; inline;
  285. begin
  286. NotInCPlusBlock := cplusblocklevel < 1;
  287. end;
  288. constructor tresobject.init_preop(const s : string;_p1 : presobject);
  289. begin
  290. typ:=t_preop;
  291. p:=strpnew(s);
  292. p1:=_p1;
  293. p2:=nil;
  294. p3:=nil;
  295. next:=nil;
  296. intname:=false;
  297. end;
  298. constructor tresobject.init_bop(const s : string;_p1,_p2 : presobject);
  299. begin
  300. typ:=t_bop;
  301. p:=strpnew(s);
  302. p1:=_p1;
  303. p2:=_p2;
  304. p3:=nil;
  305. next:=nil;
  306. intname:=false;
  307. end;
  308. constructor tresobject.init_id(const s : string);
  309. begin
  310. typ:=t_id;
  311. p:=strpnew(s);
  312. p1:=nil;
  313. p2:=nil;
  314. p3:=nil;
  315. next:=nil;
  316. intname:=false;
  317. end;
  318. constructor tresobject.init_intid(const s : string);
  319. begin
  320. typ:=t_id;
  321. p:=strpnew(s);
  322. p1:=nil;
  323. p2:=nil;
  324. p3:=nil;
  325. next:=nil;
  326. intname:=true;
  327. end;
  328. constructor tresobject.init_two(t : ttyp;_p1,_p2 : presobject);
  329. begin
  330. typ:=t;
  331. p1:=_p1;
  332. p2:=_p2;
  333. p3:=nil;
  334. p:=nil;
  335. next:=nil;
  336. intname:=false;
  337. end;
  338. constructor tresobject.init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  339. begin
  340. typ:=t;
  341. p1:=_p1;
  342. p2:=_p2;
  343. p3:=_p3;
  344. p:=nil;
  345. next:=nil;
  346. intname:=false;
  347. end;
  348. constructor tresobject.init_one(t : ttyp;_p1 : presobject);
  349. begin
  350. typ:=t;
  351. p1:=_p1;
  352. p2:=nil;
  353. p3:=nil;
  354. next:=nil;
  355. p:=nil;
  356. intname:=false;
  357. end;
  358. constructor tresobject.init_no(t : ttyp);
  359. begin
  360. typ:=t;
  361. p:=nil;
  362. p1:=nil;
  363. p2:=nil;
  364. p3:=nil;
  365. next:=nil;
  366. intname:=false;
  367. end;
  368. procedure tresobject.setstr(const s : string);
  369. begin
  370. if assigned(p) then
  371. strdispose(p);
  372. p:=strpnew(s);
  373. end;
  374. function tresobject.str : string;
  375. begin
  376. str:=strpas(p);
  377. end;
  378. function tresobject.strlength : byte;
  379. begin
  380. if assigned(p) then
  381. strlength:=strlen(p)
  382. else
  383. strlength:=0;
  384. end;
  385. { can this ve considered as a constant ? }
  386. function tresobject.is_const : boolean;
  387. begin
  388. case typ of
  389. t_id,t_void :
  390. is_const:=true;
  391. t_preop :
  392. is_const:= ((str='-') or (str=' not ')) and p1^.is_const;
  393. t_bop :
  394. is_const:= p2^.is_const and p1^.is_const;
  395. else
  396. is_const:=false;
  397. end;
  398. end;
  399. function tresobject.get_copy : presobject;
  400. var
  401. newres : presobject;
  402. begin
  403. newres:=new(presobject,init_no(typ));
  404. newres^.intname:=intname;
  405. if assigned(p) then
  406. newres^.p:=strnew(p);
  407. if assigned(p1) then
  408. newres^.p1:=p1^.get_copy;
  409. if assigned(p2) then
  410. newres^.p2:=p2^.get_copy;
  411. if assigned(p3) then
  412. newres^.p3:=p3^.get_copy;
  413. if assigned(next) then
  414. newres^.next:=next^.get_copy;
  415. get_copy:=newres;
  416. end;
  417. destructor tresobject.done;
  418. begin
  419. (* writeln('disposing ',byte(typ)); *)
  420. if assigned(p)then strdispose(p);
  421. if assigned(p1) then
  422. dispose(p1,done);
  423. if assigned(p2) then
  424. dispose(p2,done);
  425. if assigned(p3) then
  426. dispose(p3,done);
  427. if assigned(next) then
  428. dispose(next,done);
  429. end;
  430. %}
  431. D [0-9]
  432. %%
  433. "/*" if NotInCPlusBlock then
  434. begin
  435. if not stripcomment then
  436. write(outfile,aktspace,'{');
  437. repeat
  438. c:=get_char;
  439. case c of
  440. '*' :
  441. begin
  442. c:=get_char;
  443. if c='/' then
  444. begin
  445. if not stripcomment then
  446. write(outfile,' }');
  447. c:=get_char;
  448. if c=newline then
  449. writeln(outfile);
  450. unget_char(c);
  451. flush(outfile);
  452. exit;
  453. end
  454. else
  455. begin
  456. if not stripcomment then
  457. write(outfile,'*');
  458. unget_char(c)
  459. end;
  460. end;
  461. newline :
  462. begin
  463. if not stripcomment then
  464. begin
  465. writeln(outfile);
  466. write(outfile,aktspace);
  467. end;
  468. end;
  469. { Don't write this thing out, to
  470. avoid nested comments.
  471. }
  472. '{','}' :
  473. begin
  474. end;
  475. #0 :
  476. commenteof;
  477. else
  478. if not stripcomment then
  479. write(outfile,c);
  480. end;
  481. until false;
  482. flush(outfile);
  483. end
  484. else
  485. skip_until_eol;
  486. "//" if NotInCPlusBlock then
  487. begin
  488. commentstr:='';
  489. if (in_define) and not (stripcomment) then
  490. begin
  491. commentstr:='{';
  492. end
  493. else
  494. If not stripcomment then
  495. write(outfile,aktspace,'{');
  496. repeat
  497. c:=get_char;
  498. case c of
  499. newline :
  500. begin
  501. unget_char(c);
  502. if not stripcomment then
  503. begin
  504. if in_define then
  505. begin
  506. commentstr:=commentstr+' }';
  507. end
  508. else
  509. begin
  510. write(outfile,' }');
  511. writeln(outfile);
  512. end;
  513. end;
  514. flush(outfile);
  515. exit;
  516. end;
  517. { Don't write this comment out,
  518. to avoid nested comment problems
  519. }
  520. '{','}' :
  521. begin
  522. end;
  523. #0 :
  524. commenteof;
  525. else
  526. if not stripcomment then
  527. begin
  528. if in_define then
  529. begin
  530. commentstr:=commentstr+c;
  531. end
  532. else
  533. write(outfile,c);
  534. end;
  535. end;
  536. until false;
  537. flush(outfile);
  538. end
  539. else
  540. skip_until_eol;
  541. \"[^\"]*\" if NotInCPlusBlock then return(CSTRING) else skip_until_eol;
  542. \'[^\']*\' if NotInCPlusBlock then return(CSTRING) else skip_until_eol;
  543. "L"\"[^\"]*\" if NotInCPlusBlock then
  544. begin
  545. if win32headers then
  546. return(CSTRING)
  547. else
  548. return(256);
  549. end
  550. else skip_until_eol;
  551. "L"\'[^\']*\' if NotInCPlusBlock then
  552. begin
  553. if win32headers then
  554. return(CSTRING)
  555. else
  556. return(256);
  557. end
  558. else
  559. skip_until_eol;
  560. {D}+[Uu]?[Ll]?[Ll]? if NotInCPlusBlock then
  561. begin
  562. while yytext[length(yytext)] in ['L','U','l','u'] do
  563. Delete(yytext,length(yytext),1);
  564. return(NUMBER);
  565. end
  566. else skip_until_eol;
  567. "0x"[0-9A-Fa-f]*[Uu]?[Ll]?[Ll]?
  568. if NotInCPlusBlock then
  569. begin
  570. (* handle pre- and postfixes *)
  571. if copy(yytext,1,2)='0x' then
  572. begin
  573. delete(yytext,1,2);
  574. yytext:='$'+yytext;
  575. end;
  576. while yytext[length(yytext)] in ['L','U','l','u'] do
  577. Delete(yytext,length(yytext),1);
  578. return(NUMBER);
  579. end
  580. else
  581. skip_until_eol;
  582. {D}+(\.{D}+)?([Ee][+-]?{D}+)?
  583. if NotInCPlusBlock then
  584. begin
  585. return(NUMBER);
  586. end
  587. else
  588. skip_until_eol;
  589. "->" if NotInCPlusBlock then
  590. begin
  591. if in_define then
  592. return(DEREF)
  593. else
  594. return(256);
  595. end
  596. else
  597. skip_until_eol;
  598. "-" if NotInCPlusBlock then return(MINUS) else skip_until_eol;
  599. "==" if NotInCPlusBlock then return(EQUAL) else skip_until_eol;
  600. "!=" if NotInCPlusBlock then return(UNEQUAL) else skip_until_eol;
  601. ">=" if NotInCPlusBlock then return(GTE) else skip_until_eol;
  602. "<=" if NotInCPlusBlock then return(LTE) else skip_until_eol;
  603. ">>" if NotInCPlusBlock then return(_SHR) else skip_until_eol;
  604. "##" if NotInCPlusBlock then return(STICK) else skip_until_eol;
  605. "<<" if NotInCPlusBlock then return(_SHL) else skip_until_eol;
  606. ">" if NotInCPlusBlock then return(GT) else skip_until_eol;
  607. "<" if NotInCPlusBlock then return(LT) else skip_until_eol;
  608. "|" if NotInCPlusBlock then return(_OR) else skip_until_eol;
  609. "&" if NotInCPlusBlock then return(_AND) else skip_until_eol;
  610. "~" if NotInCPlusBlock then return(_NOT) else skip_until_eol; (* inverse, but handled as not operation *)
  611. "!" if NotInCPlusBlock then return(_NOT) else skip_until_eol;
  612. "/" if NotInCPlusBlock then return(_SLASH) else skip_until_eol;
  613. "+" if NotInCPlusBlock then return(_PLUS) else skip_until_eol;
  614. "?" if NotInCPlusBlock then return(QUESTIONMARK) else skip_until_eol;
  615. ":" if NotInCPlusBlock then return(COLON) else skip_until_eol;
  616. "," if NotInCPlusBlock then return(COMMA) else skip_until_eol;
  617. "[" if NotInCPlusBlock then return(LECKKLAMMER) else skip_until_eol;
  618. "]" if NotInCPlusBlock then return(RECKKLAMMER) else skip_until_eol;
  619. "(" if NotInCPlusBlock then
  620. begin
  621. inc(arglevel);
  622. return(LKLAMMER);
  623. end
  624. else
  625. skip_until_eol;
  626. ")" if NotInCPlusBlock then
  627. begin
  628. dec(arglevel);
  629. return(RKLAMMER);
  630. end
  631. else
  632. skip_until_eol;
  633. "*" if NotInCPlusBlock then return(STAR) else skip_until_eol;
  634. "..." if NotInCPlusBlock then return(ELLIPSIS) else skip_until_eol;
  635. "." if NotInCPlusBlock then
  636. if in_define then
  637. return(POINT)
  638. else
  639. return(256);
  640. "=" if NotInCPlusBlock then return(_ASSIGN) else skip_until_eol;
  641. "extern" if NotInCPlusBlock then return(EXTERN) else skip_until_eol;
  642. "STDCALL" if NotInCPlusBlock then
  643. begin
  644. if Win32headers then
  645. return(STDCALL)
  646. else
  647. return(ID);
  648. end
  649. else
  650. begin
  651. skip_until_eol;
  652. end;
  653. "CDECL" if NotInCPlusBlock then
  654. begin
  655. if not Win32headers then
  656. return(ID)
  657. else
  658. return(CDECL);
  659. end
  660. else
  661. begin
  662. skip_until_eol;
  663. end;
  664. "PASCAL" if NotInCPlusBlock then
  665. begin
  666. if not Win32headers then
  667. return(ID)
  668. else
  669. return(PASCAL);
  670. end
  671. else
  672. begin
  673. skip_until_eol;
  674. end;
  675. "PACKED" if NotInCPlusBlock then
  676. begin
  677. if not Win32headers then
  678. return(ID)
  679. else
  680. return(_PACKED);
  681. end
  682. else
  683. begin
  684. skip_until_eol;
  685. end;
  686. "WINAPI" if NotInCPlusBlock then
  687. begin
  688. if not Win32headers then
  689. return(ID)
  690. else
  691. return(WINAPI);
  692. end
  693. else
  694. begin
  695. skip_until_eol;
  696. end;
  697. "SYS_TRAP" if NotInCPlusBlock then
  698. begin
  699. if not palmpilot then
  700. return(ID)
  701. else
  702. return(SYS_TRAP);
  703. end
  704. else
  705. begin
  706. skip_until_eol;
  707. end;
  708. "WINGDIAPI" if NotInCPlusBlock then
  709. begin
  710. if not Win32headers then
  711. return(ID)
  712. else
  713. return(WINGDIAPI);
  714. end
  715. else
  716. begin
  717. skip_until_eol;
  718. end;
  719. "CALLBACK" if NotInCPlusBlock then
  720. begin
  721. if not Win32headers then
  722. return(ID)
  723. else
  724. return(CALLBACK);
  725. end
  726. else
  727. begin
  728. skip_until_eol;
  729. end;
  730. "EXPENTRY" if NotInCPlusBlock then
  731. begin
  732. if not Win32headers then
  733. return(ID)
  734. else
  735. return(CALLBACK);
  736. end
  737. else
  738. begin
  739. skip_until_eol;
  740. end;
  741. "void" if NotInCPlusBlock then return(VOID) else skip_until_eol;
  742. "VOID" if NotInCPlusBlock then return(VOID) else skip_until_eol;
  743. "#ifdef"[ \t]*"__cplusplus"[ \t]*\n"extern \"C\" {"\n"#endif"
  744. begin
  745. if not stripinfo then
  746. writeln(outfile,'{ C++ extern C conditionnal removed }');
  747. end;
  748. "#ifdef"[ \t]*"cplusplus"[ \t]*\n"extern \"C\" {"\n"#endif"
  749. begin
  750. if not stripinfo then
  751. writeln(outfile,'{ C++ extern C conditionnal removed }');
  752. end;
  753. "#ifdef"[ \t]*"__cplusplus"[ \t]*\n"}"\n"#endif"
  754. begin
  755. if not stripinfo then
  756. writeln(outfile,'{ C++ end of extern C conditionnal removed }');
  757. end;
  758. "#ifdef"[ \t]*"cplusplus"[ \t]*\n"}"\n"#endif"
  759. begin
  760. if not stripinfo then
  761. writeln(outfile,'{ C++ end of extern C conditionnal removed }');
  762. end;
  763. "#ifdef"[ \t]*"cplusplus"[ \t]*
  764. begin
  765. Inc(cplusblocklevel);
  766. end;
  767. "#ifdef"[ \t]*"__cplusplus"[ \t]*
  768. begin
  769. Inc(cplusblocklevel);
  770. end;
  771. "#ifdef"[ \t]
  772. begin
  773. if cplusblocklevel > 0 then
  774. Inc(cplusblocklevel)
  775. else
  776. begin
  777. if cplusblocklevel < 0 then
  778. Dec(cplusblocklevel);
  779. write(outfile,'{$ifdef ');
  780. copy_until_eol;
  781. writeln(outfile,'}');
  782. flush(outfile);
  783. end;
  784. end;
  785. "#"[ \t]*"else" begin
  786. if cplusblocklevel < -1 then
  787. begin
  788. writeln(outfile,'{$else}');
  789. block_type:=bt_no;
  790. flush(outfile);
  791. end
  792. else
  793. case cplusblocklevel of
  794. 0 :
  795. begin
  796. writeln(outfile,'{$else}');
  797. block_type:=bt_no;
  798. flush(outfile);
  799. end;
  800. 1 : cplusblocklevel := -1;
  801. -1 : cplusblocklevel := 1;
  802. end;
  803. end;
  804. "#"[ \t]*"endif" begin
  805. if cplusblocklevel > 0 then
  806. begin
  807. Dec(cplusblocklevel);
  808. end
  809. else
  810. begin
  811. case cplusblocklevel of
  812. 0 : begin
  813. writeln(outfile,'{$endif}');
  814. block_type:=bt_no;
  815. flush(outfile);
  816. end;
  817. -1 : begin
  818. cplusblocklevel :=0;
  819. end
  820. else
  821. inc(cplusblocklevel);
  822. end;
  823. end;
  824. end;
  825. "#"[ \t]*"elif" begin
  826. if cplusblocklevel < -1 then
  827. begin
  828. if not stripinfo then
  829. write(outfile,'(*** was #elif ****)');
  830. write(outfile,'{$else');
  831. copy_until_eol;
  832. writeln(outfile,'}');
  833. block_type:=bt_no;
  834. flush(outfile);
  835. end
  836. else
  837. case cplusblocklevel of
  838. 0 :
  839. begin
  840. if not stripinfo then
  841. write(outfile,'(*** was #elif ****)');
  842. write(outfile,'{$else');
  843. copy_until_eol;
  844. writeln(outfile,'}');
  845. block_type:=bt_no;
  846. flush(outfile);
  847. end;
  848. 1 : cplusblocklevel := -1;
  849. -1 : cplusblocklevel := 1;
  850. end;
  851. end;
  852. "#"[ \t]*"undef" begin
  853. write(outfile,'{$undef');
  854. copy_until_eol;
  855. writeln(outfile,'}');
  856. flush(outfile);
  857. end;
  858. "#"[ \t]*"error" begin
  859. write(outfile,'{$error');
  860. copy_until_eol;
  861. writeln(outfile,'}');
  862. flush(outfile);
  863. end;
  864. "#"[ \t]*"include" if NotInCPlusBlock then
  865. begin
  866. write(outfile,'{$include');
  867. copy_until_eol;
  868. writeln(outfile,'}');
  869. flush(outfile);
  870. block_type:=bt_no;
  871. end
  872. else
  873. skip_until_eol;
  874. "#"[ \t]*"if" begin
  875. if cplusblocklevel > 0 then
  876. Inc(cplusblocklevel)
  877. else
  878. begin
  879. if cplusblocklevel < 0 then
  880. Dec(cplusblocklevel);
  881. write(outfile,'{$if');
  882. copy_until_eol;
  883. writeln(outfile,'}');
  884. flush(outfile);
  885. block_type:=bt_no;
  886. end;
  887. end;
  888. "# "[0-9]+" " if NotInCPlusBlock then
  889. (* preprocessor line info *)
  890. repeat
  891. c:=get_char;
  892. case c of
  893. newline :
  894. begin
  895. unget_char(c);
  896. exit;
  897. end;
  898. #0 :
  899. commenteof;
  900. end;
  901. until false
  902. else
  903. skip_until_eol;
  904. "#"[ \t]*"pragma" begin
  905. if not stripinfo then
  906. begin
  907. write(outfile,'(** unsupported pragma');
  908. write(outfile,'#pragma');
  909. copy_until_eol;
  910. writeln(outfile,'*)');
  911. flush(outfile);
  912. end
  913. else
  914. skip_until_eol;
  915. block_type:=bt_no;
  916. end;
  917. "#"[ \t]*"define" if NotInCPlusBlock then
  918. begin
  919. commentstr:='';
  920. in_define:=true;
  921. in_space_define:=1;
  922. return(DEFINE);
  923. end
  924. else
  925. skip_until_eol;
  926. "char" if NotInCPlusBlock then return(_CHAR) else skip_until_eol;
  927. "union" if NotInCPlusBlock then return(UNION) else skip_until_eol;
  928. "enum" if NotInCPlusBlock then return(ENUM) else skip_until_eol;
  929. "struct" if NotInCPlusBlock then return(STRUCT) else skip_until_eol;
  930. "{" if NotInCPlusBlock then return(LGKLAMMER) else skip_until_eol;
  931. "}" if NotInCPlusBlock then return(RGKLAMMER) else skip_until_eol;
  932. "typedef" if NotInCPlusBlock then return(TYPEDEF) else skip_until_eol;
  933. "int" if NotInCPlusBlock then return(INT) else skip_until_eol;
  934. "short" if NotInCPlusBlock then return(SHORT) else skip_until_eol;
  935. "long" if NotInCPlusBlock then return(LONG) else skip_until_eol;
  936. "signed" if NotInCPlusBlock then return(SIGNED) else skip_until_eol;
  937. "unsigned" if NotInCPlusBlock then return(UNSIGNED) else skip_until_eol;
  938. "__int8" if NotInCPlusBlock then return(INT8) else skip_until_eol;
  939. "__int16" if NotInCPlusBlock then return(INT16) else skip_until_eol;
  940. "__int32" if NotInCPlusBlock then return(INT32) else skip_until_eol;
  941. "__int64" if NotInCPlusBlock then return(INT64) else skip_until_eol;
  942. "int8" if NotInCPlusBlock then return(INT8) else skip_until_eol;
  943. "int16" if NotInCPlusBlock then return(INT16) else skip_until_eol;
  944. "int32" if NotInCPlusBlock then return(INT32) else skip_until_eol;
  945. "int64" if NotInCPlusBlock then return(INT64) else skip_until_eol;
  946. "float" if NotInCPlusBlock then return(FLOAT) else skip_until_eol;
  947. "const" if NotInCPlusBlock then return(_CONST) else skip_until_eol;
  948. "CONST" if NotInCPlusBlock then return(_CONST) else skip_until_eol;
  949. "FAR" if NotInCPlusBlock then return(_FAR) else skip_until_eol;
  950. "far" if NotInCPlusBlock then return(_FAR) else skip_until_eol;
  951. "NEAR" if NotInCPlusBlock then return(_NEAR) else skip_until_eol;
  952. "near" if NotInCPlusBlock then return(_NEAR) else skip_until_eol;
  953. "HUGE" if NotInCPlusBlock then return(_HUGE) else skip_until_eol;
  954. "huge" if NotInCPlusBlock then return(_HUGE) else skip_until_eol;
  955. "while" if NotInCPlusBlock then return(_WHILE) else skip_until_eol;
  956. [A-Za-z_][A-Za-z0-9_]* if NotInCPlusBlock then
  957. begin
  958. if in_space_define=1 then
  959. in_space_define:=2;
  960. return(ID);
  961. end
  962. else
  963. skip_until_eol;
  964. ";" if NotInCPlusBlock then return(SEMICOLON) else skip_until_eol;
  965. [ \f\t] if NotInCPlusBlock then
  966. begin
  967. if (arglevel=0) and (in_space_define=2) then
  968. begin
  969. in_space_define:=0;
  970. return(SPACE_DEFINE);
  971. end;
  972. end
  973. else
  974. skip_until_eol;
  975. \n begin
  976. if in_define then
  977. begin
  978. in_space_define:=0;
  979. if cont_line then
  980. begin
  981. cont_line:=false;
  982. end
  983. else
  984. begin
  985. in_define:=false;
  986. if NotInCPlusBlock then
  987. return(NEW_LINE)
  988. else
  989. skip_until_eol
  990. end;
  991. end;
  992. end;
  993. \\$ begin
  994. if in_define then
  995. begin
  996. cont_line:=true;
  997. end
  998. else
  999. begin
  1000. writeln('Unexpected wrap of line ',yylineno);
  1001. writeln('"',yyline,'"');
  1002. return(256);
  1003. end;
  1004. end;
  1005. . begin
  1006. writeln('Illegal character in line ',yylineno);
  1007. writeln('"',yyline,'"');
  1008. return(256);
  1009. end;
  1010. %%
  1011. function act_token : string;
  1012. begin
  1013. act_token:=yytext;
  1014. end;
  1015. end.