scan.l 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. {$GOTO ON}
  19. interface
  20. uses
  21. strings,
  22. h2plexlib,h2pyacclib;
  23. const
  24. version = '1.0.0';
  25. type
  26. Char=system.char;
  27. ttyp = (
  28. t_id,
  29. { p contains the string }
  30. t_arraydef,
  31. { }
  32. t_pointerdef,
  33. { p1 contains the definition
  34. if in type overrider
  35. or nothing for args
  36. }
  37. t_addrdef,
  38. t_void,
  39. { no field }
  40. t_dec,
  41. { }
  42. t_declist,
  43. { p1 is t_dec
  44. next if exists }
  45. t_memberdec,
  46. { p1 is type specifier
  47. p2 is declarator_list }
  48. t_structdef,
  49. { }
  50. t_memberdeclist,
  51. { p1 is memberdec
  52. next is next if it exist }
  53. t_procdef,
  54. { }
  55. t_uniondef,
  56. { }
  57. t_enumdef,
  58. { }
  59. t_enumlist,
  60. { }
  61. t_preop,
  62. { p contains the operator string
  63. p1 contains the right expr }
  64. t_bop,
  65. { p contains the operator string
  66. p1 contains the left expr
  67. p2 contains the right expr }
  68. t_arrayop,
  69. {
  70. p1 contains the array expr
  71. p2 contains the index expressions }
  72. t_callop,
  73. {
  74. p1 contains the proc expr
  75. p2 contains the index expressions }
  76. t_arg,
  77. {
  78. p1 contain the typedef
  79. p2 the declarator (t_dec)
  80. }
  81. t_arglist,
  82. { }
  83. t_funexprlist,
  84. { }
  85. t_exprlist,
  86. { p1 contains the expr
  87. next contains the next if it exists }
  88. t_ifexpr,
  89. { p1 contains the condition expr
  90. p2 contains the if branch
  91. p3 contains the else branch }
  92. t_funcname,
  93. { p1 contains the function dname
  94. p2 contains the funexprlist
  95. p3 possibly contains the return type }
  96. t_typespec,
  97. { p1 is the type itself
  98. p2 the typecast expr }
  99. t_size_specifier,
  100. { p1 expr for size }
  101. t_default_value,
  102. { p1 expr for value }
  103. t_statement_list,
  104. { p1 is the statement
  105. next is next if it exist }
  106. t_whilenode,
  107. t_fornode,
  108. t_dowhilenode,
  109. t_switchnode,
  110. t_gotonode,
  111. t_continuenode,
  112. t_breaknode
  113. );
  114. const
  115. ttypstr: array[ttyp] of string =
  116. (
  117. 't_id',
  118. 't_arraydef',
  119. 't_pointerdef',
  120. 't_addrdef',
  121. 't_void',
  122. 't_dec',
  123. 't_declist',
  124. 't_memberdec',
  125. 't_structdef',
  126. 't_memberdeclist',
  127. 't_procdef',
  128. 't_uniondef',
  129. 't_enumdef',
  130. 't_enumlist',
  131. 't_preop',
  132. 't_bop',
  133. 't_arrayop',
  134. 't_callop',
  135. 't_arg',
  136. 't_arglist',
  137. 't_funexprlist',
  138. 't_exprlist',
  139. 't_ifexpr',
  140. 't_funcname',
  141. 't_typespec',
  142. 't_size_specifier',
  143. 't_default_value',
  144. 't_statement_list',
  145. 't_whilenode',
  146. 't_fornode',
  147. 't_dowhilenode',
  148. 't_switchnode',
  149. 't_gotonode',
  150. 't_continuenode',
  151. 't_breaknode'
  152. );
  153. type
  154. presobject = ^tresobject;
  155. tresobject = object
  156. typ : ttyp;
  157. p : pchar;
  158. next : presobject;
  159. p1,p2,p3 : presobject;
  160. { name of int/real, then no T prefix is required }
  161. intname : boolean;
  162. constructor init_no(t : ttyp);
  163. constructor init_one(t : ttyp;_p1 : presobject);
  164. constructor init_two(t : ttyp;_p1,_p2 : presobject);
  165. constructor init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  166. constructor init_id(const s : string);
  167. constructor init_intid(const s : string);
  168. constructor init_bop(const s : string;_p1,_p2 : presobject);
  169. constructor init_preop(const s : string;_p1 : presobject);
  170. procedure setstr(const s:string);
  171. function str : string;
  172. function strlength : byte;
  173. function get_copy : presobject;
  174. { can this ve considered as a constant ? }
  175. function is_const : boolean;
  176. destructor done;
  177. end;
  178. tblocktype = (bt_type,bt_const,bt_var,bt_func,bt_no);
  179. var
  180. infile : string;
  181. outfile : text;
  182. c : char;
  183. aktspace : string;
  184. block_type : tblocktype;
  185. commentstr: string;
  186. const
  187. in_define : boolean = false;
  188. { True if define spans to the next line }
  189. cont_line : boolean = false;
  190. { 1 after define; 2 after the ID to print the first separating space }
  191. in_space_define : byte = 0;
  192. arglevel : longint = 0;
  193. {> 1 = ifdef level in a ifdef C++ block
  194. 1 = first level in an ifdef block
  195. 0 = not in an ifdef block
  196. -1 = in else part of ifdef block, process like we weren't in the block
  197. but skip the incoming end.
  198. > -1 = ifdef sublevel in an else block.
  199. }
  200. cplusblocklevel : LongInt = 0;
  201. function yylex : integer;
  202. function act_token : string;
  203. procedure internalerror(i : integer);
  204. function strpnew(const s : string) : pchar;
  205. procedure writetree(p: presobject);
  206. implementation
  207. uses
  208. h2poptions, h2pconst;
  209. const
  210. newline = #10;
  211. procedure writeentry(p: presobject; var currentlevel: integer);
  212. begin
  213. if assigned(p^.p1) then
  214. begin
  215. WriteLn(' Entry p1[',ttypstr[p^.p1^.typ],']',p^.p1^.str);
  216. end;
  217. if assigned(p^.p2) then
  218. begin
  219. WriteLn(' Entry p2[',ttypstr[p^.p2^.typ],']',p^.p2^.str);
  220. end;
  221. if assigned(p^.p3) then
  222. begin
  223. WriteLn(' Entry p3[',ttypstr[p^.p3^.typ],']',p^.p3^.str);
  224. end;
  225. end;
  226. procedure writetree(p: presobject);
  227. var
  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. if yytext[1]='0' then
  563. begin
  564. delete(yytext,1,1);
  565. yytext:='&'+yytext;
  566. end;
  567. while yytext[length(yytext)] in ['L','U','l','u'] do
  568. Delete(yytext,length(yytext),1);
  569. return(NUMBER);
  570. end
  571. else skip_until_eol;
  572. "0x"[0-9A-Fa-f]*[Uu]?[Ll]?[Ll]?
  573. if NotInCPlusBlock then
  574. begin
  575. (* handle pre- and postfixes *)
  576. if copy(yytext,1,2)='0x' then
  577. begin
  578. delete(yytext,1,2);
  579. yytext:='$'+yytext;
  580. end;
  581. while yytext[length(yytext)] in ['L','U','l','u'] do
  582. Delete(yytext,length(yytext),1);
  583. return(NUMBER);
  584. end
  585. else
  586. skip_until_eol;
  587. {D}+(\.{D}+)?([Ee][+-]?{D}+)?
  588. if NotInCPlusBlock then
  589. begin
  590. return(NUMBER);
  591. end
  592. else
  593. skip_until_eol;
  594. "->" if NotInCPlusBlock then
  595. begin
  596. if in_define then
  597. return(DEREF)
  598. else
  599. return(256);
  600. end
  601. else
  602. skip_until_eol;
  603. "-" if NotInCPlusBlock then return(MINUS) else skip_until_eol;
  604. "==" if NotInCPlusBlock then return(EQUAL) else skip_until_eol;
  605. "!=" if NotInCPlusBlock then return(UNEQUAL) else skip_until_eol;
  606. ">=" if NotInCPlusBlock then return(GTE) else skip_until_eol;
  607. "<=" if NotInCPlusBlock then return(LTE) else skip_until_eol;
  608. ">>" if NotInCPlusBlock then return(_SHR) else skip_until_eol;
  609. "##" if NotInCPlusBlock then return(STICK) else skip_until_eol;
  610. "<<" if NotInCPlusBlock then return(_SHL) else skip_until_eol;
  611. ">" if NotInCPlusBlock then return(GT) else skip_until_eol;
  612. "<" if NotInCPlusBlock then return(LT) else skip_until_eol;
  613. "|" if NotInCPlusBlock then return(_OR) else skip_until_eol;
  614. "&" if NotInCPlusBlock then return(_AND) else skip_until_eol;
  615. "~" if NotInCPlusBlock then return(_NOT) else skip_until_eol; (* inverse, but handled as not operation *)
  616. "!" if NotInCPlusBlock then return(_NOT) else skip_until_eol;
  617. "/" if NotInCPlusBlock then return(_SLASH) else skip_until_eol;
  618. "+" if NotInCPlusBlock then return(_PLUS) else skip_until_eol;
  619. "?" if NotInCPlusBlock then return(QUESTIONMARK) else skip_until_eol;
  620. ":" if NotInCPlusBlock then return(COLON) else skip_until_eol;
  621. "," if NotInCPlusBlock then return(COMMA) else skip_until_eol;
  622. "[" if NotInCPlusBlock then return(LECKKLAMMER) else skip_until_eol;
  623. "]" if NotInCPlusBlock then return(RECKKLAMMER) else skip_until_eol;
  624. "(" if NotInCPlusBlock then
  625. begin
  626. inc(arglevel);
  627. return(LKLAMMER);
  628. end
  629. else
  630. skip_until_eol;
  631. ")" if NotInCPlusBlock then
  632. begin
  633. dec(arglevel);
  634. return(RKLAMMER);
  635. end
  636. else
  637. skip_until_eol;
  638. "*" if NotInCPlusBlock then return(STAR) else skip_until_eol;
  639. "..." if NotInCPlusBlock then return(ELLIPSIS) else skip_until_eol;
  640. "." if NotInCPlusBlock then
  641. if in_define then
  642. return(POINT)
  643. else
  644. return(256);
  645. "=" if NotInCPlusBlock then return(_ASSIGN) else skip_until_eol;
  646. "extern" if NotInCPlusBlock then return(EXTERN) else skip_until_eol;
  647. "STDCALL" if NotInCPlusBlock then
  648. begin
  649. if Win32headers then
  650. return(STDCALL)
  651. else
  652. return(ID);
  653. end
  654. else
  655. begin
  656. skip_until_eol;
  657. end;
  658. "CDECL" if NotInCPlusBlock then
  659. begin
  660. if not Win32headers then
  661. return(ID)
  662. else
  663. return(CDECL);
  664. end
  665. else
  666. begin
  667. skip_until_eol;
  668. end;
  669. "PASCAL" if NotInCPlusBlock then
  670. begin
  671. if not Win32headers then
  672. return(ID)
  673. else
  674. return(PASCAL);
  675. end
  676. else
  677. begin
  678. skip_until_eol;
  679. end;
  680. "PACKED" if NotInCPlusBlock then
  681. begin
  682. if not Win32headers then
  683. return(ID)
  684. else
  685. return(_PACKED);
  686. end
  687. else
  688. begin
  689. skip_until_eol;
  690. end;
  691. "WINAPI" if NotInCPlusBlock then
  692. begin
  693. if not Win32headers then
  694. return(ID)
  695. else
  696. return(WINAPI);
  697. end
  698. else
  699. begin
  700. skip_until_eol;
  701. end;
  702. "SYS_TRAP" if NotInCPlusBlock then
  703. begin
  704. if not palmpilot then
  705. return(ID)
  706. else
  707. return(SYS_TRAP);
  708. end
  709. else
  710. begin
  711. skip_until_eol;
  712. end;
  713. "WINGDIAPI" if NotInCPlusBlock then
  714. begin
  715. if not Win32headers then
  716. return(ID)
  717. else
  718. return(WINGDIAPI);
  719. end
  720. else
  721. begin
  722. skip_until_eol;
  723. end;
  724. "CALLBACK" if NotInCPlusBlock then
  725. begin
  726. if not Win32headers then
  727. return(ID)
  728. else
  729. return(CALLBACK);
  730. end
  731. else
  732. begin
  733. skip_until_eol;
  734. end;
  735. "EXPENTRY" if NotInCPlusBlock then
  736. begin
  737. if not Win32headers then
  738. return(ID)
  739. else
  740. return(CALLBACK);
  741. end
  742. else
  743. begin
  744. skip_until_eol;
  745. end;
  746. "void" if NotInCPlusBlock then return(VOID) else skip_until_eol;
  747. "VOID" if NotInCPlusBlock then return(VOID) else skip_until_eol;
  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"extern \"C\" {"\n"#endif"
  754. begin
  755. if not stripinfo then
  756. writeln(outfile,'{ C++ 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]*\n"}"\n"#endif"
  764. begin
  765. if not stripinfo then
  766. writeln(outfile,'{ C++ end of extern C conditionnal removed }');
  767. end;
  768. "#ifdef"[ \t]*"cplusplus"[ \t]*
  769. begin
  770. Inc(cplusblocklevel);
  771. end;
  772. "#ifdef"[ \t]*"__cplusplus"[ \t]*
  773. begin
  774. Inc(cplusblocklevel);
  775. end;
  776. "#ifdef"[ \t]
  777. begin
  778. if cplusblocklevel > 0 then
  779. Inc(cplusblocklevel)
  780. else
  781. begin
  782. if cplusblocklevel < 0 then
  783. Dec(cplusblocklevel);
  784. write(outfile,'{$ifdef ');
  785. copy_until_eol;
  786. writeln(outfile,'}');
  787. flush(outfile);
  788. end;
  789. end;
  790. "#"[ \t]*"else" begin
  791. if cplusblocklevel < -1 then
  792. begin
  793. writeln(outfile,'{$else}');
  794. block_type:=bt_no;
  795. flush(outfile);
  796. end
  797. else
  798. case cplusblocklevel of
  799. 0 :
  800. begin
  801. writeln(outfile,'{$else}');
  802. block_type:=bt_no;
  803. flush(outfile);
  804. end;
  805. 1 : cplusblocklevel := -1;
  806. -1 : cplusblocklevel := 1;
  807. end;
  808. end;
  809. "#"[ \t]*"endif" begin
  810. if cplusblocklevel > 0 then
  811. begin
  812. Dec(cplusblocklevel);
  813. end
  814. else
  815. begin
  816. case cplusblocklevel of
  817. 0 : begin
  818. writeln(outfile,'{$endif}');
  819. block_type:=bt_no;
  820. flush(outfile);
  821. end;
  822. -1 : begin
  823. cplusblocklevel :=0;
  824. end
  825. else
  826. inc(cplusblocklevel);
  827. end;
  828. end;
  829. end;
  830. "#"[ \t]*"elif" begin
  831. if cplusblocklevel < -1 then
  832. begin
  833. if not stripinfo then
  834. write(outfile,'(*** was #elif ****)');
  835. write(outfile,'{$else');
  836. copy_until_eol;
  837. writeln(outfile,'}');
  838. block_type:=bt_no;
  839. flush(outfile);
  840. end
  841. else
  842. case cplusblocklevel of
  843. 0 :
  844. begin
  845. if not stripinfo then
  846. write(outfile,'(*** was #elif ****)');
  847. write(outfile,'{$else');
  848. copy_until_eol;
  849. writeln(outfile,'}');
  850. block_type:=bt_no;
  851. flush(outfile);
  852. end;
  853. 1 : cplusblocklevel := -1;
  854. -1 : cplusblocklevel := 1;
  855. end;
  856. end;
  857. "#"[ \t]*"undef" begin
  858. write(outfile,'{$undef');
  859. copy_until_eol;
  860. writeln(outfile,'}');
  861. flush(outfile);
  862. end;
  863. "#"[ \t]*"error" begin
  864. write(outfile,'{$error');
  865. copy_until_eol;
  866. writeln(outfile,'}');
  867. flush(outfile);
  868. end;
  869. "#"[ \t]*"include" if NotInCPlusBlock then
  870. begin
  871. write(outfile,'{$include');
  872. copy_until_eol;
  873. writeln(outfile,'}');
  874. flush(outfile);
  875. block_type:=bt_no;
  876. end
  877. else
  878. skip_until_eol;
  879. "#"[ \t]*"if" begin
  880. if cplusblocklevel > 0 then
  881. Inc(cplusblocklevel)
  882. else
  883. begin
  884. if cplusblocklevel < 0 then
  885. Dec(cplusblocklevel);
  886. write(outfile,'{$if');
  887. copy_until_eol;
  888. writeln(outfile,'}');
  889. flush(outfile);
  890. block_type:=bt_no;
  891. end;
  892. end;
  893. "# "[0-9]+" " if NotInCPlusBlock then
  894. (* preprocessor line info *)
  895. repeat
  896. c:=get_char;
  897. case c of
  898. newline :
  899. begin
  900. unget_char(c);
  901. exit;
  902. end;
  903. #0 :
  904. commenteof;
  905. end;
  906. until false
  907. else
  908. skip_until_eol;
  909. "#"[ \t]*"pragma" begin
  910. if not stripinfo then
  911. begin
  912. write(outfile,'(** unsupported pragma');
  913. write(outfile,'#pragma');
  914. copy_until_eol;
  915. writeln(outfile,'*)');
  916. flush(outfile);
  917. end
  918. else
  919. skip_until_eol;
  920. block_type:=bt_no;
  921. end;
  922. "#"[ \t]*"define" if NotInCPlusBlock then
  923. begin
  924. commentstr:='';
  925. in_define:=true;
  926. in_space_define:=1;
  927. return(DEFINE);
  928. end
  929. else
  930. skip_until_eol;
  931. "char" if NotInCPlusBlock then return(_CHAR) else skip_until_eol;
  932. "union" if NotInCPlusBlock then return(UNION) else skip_until_eol;
  933. "enum" if NotInCPlusBlock then return(ENUM) else skip_until_eol;
  934. "struct" if NotInCPlusBlock then return(STRUCT) else skip_until_eol;
  935. "{" if NotInCPlusBlock then return(LGKLAMMER) else skip_until_eol;
  936. "}" if NotInCPlusBlock then return(RGKLAMMER) else skip_until_eol;
  937. "typedef" if NotInCPlusBlock then return(TYPEDEF) else skip_until_eol;
  938. "int" if NotInCPlusBlock then return(INT) else skip_until_eol;
  939. "short" if NotInCPlusBlock then return(SHORT) else skip_until_eol;
  940. "long" if NotInCPlusBlock then return(LONG) else skip_until_eol;
  941. "signed" if NotInCPlusBlock then return(SIGNED) else skip_until_eol;
  942. "unsigned" if NotInCPlusBlock then return(UNSIGNED) else skip_until_eol;
  943. "__int8" if NotInCPlusBlock then return(INT8) else skip_until_eol;
  944. "__int16" if NotInCPlusBlock then return(INT16) else skip_until_eol;
  945. "__int32" if NotInCPlusBlock then return(INT32) else skip_until_eol;
  946. "__int64" if NotInCPlusBlock then return(INT64) else skip_until_eol;
  947. "int8" if NotInCPlusBlock then return(INT8) else skip_until_eol;
  948. "int16" if NotInCPlusBlock then return(INT16) else skip_until_eol;
  949. "int32" if NotInCPlusBlock then return(INT32) else skip_until_eol;
  950. "int64" if NotInCPlusBlock then return(INT64) else skip_until_eol;
  951. "float" if NotInCPlusBlock then return(FLOAT) else skip_until_eol;
  952. "const" if NotInCPlusBlock then return(_CONST) else skip_until_eol;
  953. "CONST" if NotInCPlusBlock then return(_CONST) else skip_until_eol;
  954. "FAR" if NotInCPlusBlock then return(_FAR) else skip_until_eol;
  955. "far" if NotInCPlusBlock then return(_FAR) else skip_until_eol;
  956. "NEAR" if NotInCPlusBlock then return(_NEAR) else skip_until_eol;
  957. "near" if NotInCPlusBlock then return(_NEAR) else skip_until_eol;
  958. "HUGE" if NotInCPlusBlock then return(_HUGE) else skip_until_eol;
  959. "huge" if NotInCPlusBlock then return(_HUGE) else skip_until_eol;
  960. "while" if NotInCPlusBlock then return(_WHILE) else skip_until_eol;
  961. [A-Za-z_][A-Za-z0-9_]* if NotInCPlusBlock then
  962. begin
  963. if in_space_define=1 then
  964. in_space_define:=2;
  965. return(ID);
  966. end
  967. else
  968. skip_until_eol;
  969. ";" if NotInCPlusBlock then return(SEMICOLON) else skip_until_eol;
  970. [ \f\t] if NotInCPlusBlock then
  971. begin
  972. if (arglevel=0) and (in_space_define=2) then
  973. begin
  974. in_space_define:=0;
  975. return(SPACE_DEFINE);
  976. end;
  977. end
  978. else
  979. skip_until_eol;
  980. \n begin
  981. if in_define then
  982. begin
  983. in_space_define:=0;
  984. if cont_line then
  985. begin
  986. cont_line:=false;
  987. end
  988. else
  989. begin
  990. in_define:=false;
  991. if NotInCPlusBlock then
  992. return(NEW_LINE)
  993. else
  994. skip_until_eol
  995. end;
  996. end;
  997. end;
  998. \\$ begin
  999. if in_define then
  1000. begin
  1001. cont_line:=true;
  1002. end
  1003. else
  1004. begin
  1005. writeln('Unexpected wrap of line ',yylineno);
  1006. writeln('"',yyline,'"');
  1007. return(256);
  1008. end;
  1009. end;
  1010. . begin
  1011. writeln('Illegal character in line ',yylineno);
  1012. writeln('"',yyline,'"');
  1013. return(256);
  1014. end;
  1015. %%
  1016. function act_token : string;
  1017. begin
  1018. act_token:=yytext;
  1019. end;
  1020. end.