scan.l 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. 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, h2pconst, scanbase;
  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. localp: presobject;
  228. localp1: presobject;
  229. currentlevel : integer;
  230. begin
  231. localp:=p;
  232. currentlevel:=0;
  233. while assigned(localp) do
  234. begin
  235. WriteLn('Entry[',ttypstr[localp^.typ],']',localp^.str);
  236. case localp^.typ of
  237. { Some arguments sharing the same type }
  238. t_arglist:
  239. begin
  240. localp1:=localp;
  241. while assigned(localp1) do
  242. begin
  243. writeentry(localp1,currentlevel);
  244. localp1:=localp1^.p1;
  245. end;
  246. end;
  247. end;
  248. localp:=localp^.next;
  249. end;
  250. end;
  251. procedure internalerror(i : integer);
  252. begin
  253. writeln('Internal error ',i,' in line ',yylineno);
  254. halt(1);
  255. end;
  256. procedure commenteof;
  257. begin
  258. writeln('unexpected EOF inside comment at line ',yylineno);
  259. end;
  260. procedure copy_until_eol;
  261. begin
  262. c:=get_char;
  263. while c<>newline do
  264. begin
  265. write(outfile,c);
  266. c:=get_char;
  267. end;
  268. end;
  269. procedure skip_until_eol;
  270. begin
  271. c:=get_char;
  272. while c<>newline do
  273. c:=get_char;
  274. end;
  275. function strpnew(const s : string) : pchar;
  276. var
  277. p : pchar;
  278. begin
  279. getmem(p,length(s)+1);
  280. strpcopy(p,s);
  281. strpnew:=p;
  282. end;
  283. function NotInCPlusBlock : Boolean; inline;
  284. begin
  285. NotInCPlusBlock := cplusblocklevel < 1;
  286. end;
  287. constructor tresobject.init_preop(const s : string;_p1 : presobject);
  288. begin
  289. typ:=t_preop;
  290. p:=strpnew(s);
  291. p1:=_p1;
  292. p2:=nil;
  293. p3:=nil;
  294. next:=nil;
  295. intname:=false;
  296. end;
  297. constructor tresobject.init_bop(const s : string;_p1,_p2 : presobject);
  298. begin
  299. typ:=t_bop;
  300. p:=strpnew(s);
  301. p1:=_p1;
  302. p2:=_p2;
  303. p3:=nil;
  304. next:=nil;
  305. intname:=false;
  306. end;
  307. constructor tresobject.init_id(const s : string);
  308. begin
  309. typ:=t_id;
  310. p:=strpnew(s);
  311. p1:=nil;
  312. p2:=nil;
  313. p3:=nil;
  314. next:=nil;
  315. intname:=false;
  316. end;
  317. constructor tresobject.init_intid(const s : string);
  318. begin
  319. typ:=t_id;
  320. p:=strpnew(s);
  321. p1:=nil;
  322. p2:=nil;
  323. p3:=nil;
  324. next:=nil;
  325. intname:=true;
  326. end;
  327. constructor tresobject.init_two(t : ttyp;_p1,_p2 : presobject);
  328. begin
  329. typ:=t;
  330. p1:=_p1;
  331. p2:=_p2;
  332. p3:=nil;
  333. p:=nil;
  334. next:=nil;
  335. intname:=false;
  336. end;
  337. constructor tresobject.init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  338. begin
  339. typ:=t;
  340. p1:=_p1;
  341. p2:=_p2;
  342. p3:=_p3;
  343. p:=nil;
  344. next:=nil;
  345. intname:=false;
  346. end;
  347. constructor tresobject.init_one(t : ttyp;_p1 : presobject);
  348. begin
  349. typ:=t;
  350. p1:=_p1;
  351. p2:=nil;
  352. p3:=nil;
  353. next:=nil;
  354. p:=nil;
  355. intname:=false;
  356. end;
  357. constructor tresobject.init_no(t : ttyp);
  358. begin
  359. typ:=t;
  360. p:=nil;
  361. p1:=nil;
  362. p2:=nil;
  363. p3:=nil;
  364. next:=nil;
  365. intname:=false;
  366. end;
  367. procedure tresobject.setstr(const s : string);
  368. begin
  369. if assigned(p) then
  370. strdispose(p);
  371. p:=strpnew(s);
  372. end;
  373. function tresobject.str : string;
  374. begin
  375. str:=strpas(p);
  376. end;
  377. function tresobject.strlength : byte;
  378. begin
  379. if assigned(p) then
  380. strlength:=strlen(p)
  381. else
  382. strlength:=0;
  383. end;
  384. { can this ve considered as a constant ? }
  385. function tresobject.is_const : boolean;
  386. begin
  387. case typ of
  388. t_id,t_void :
  389. is_const:=true;
  390. t_preop :
  391. is_const:= ((str='-') or (str=' not ')) and p1^.is_const;
  392. t_bop :
  393. is_const:= p2^.is_const and p1^.is_const;
  394. else
  395. is_const:=false;
  396. end;
  397. end;
  398. function tresobject.get_copy : presobject;
  399. var
  400. newres : presobject;
  401. begin
  402. newres:=new(presobject,init_no(typ));
  403. newres^.intname:=intname;
  404. if assigned(p) then
  405. newres^.p:=strnew(p);
  406. if assigned(p1) then
  407. newres^.p1:=p1^.get_copy;
  408. if assigned(p2) then
  409. newres^.p2:=p2^.get_copy;
  410. if assigned(p3) then
  411. newres^.p3:=p3^.get_copy;
  412. if assigned(next) then
  413. newres^.next:=next^.get_copy;
  414. get_copy:=newres;
  415. end;
  416. destructor tresobject.done;
  417. begin
  418. (* writeln('disposing ',byte(typ)); *)
  419. if assigned(p)then strdispose(p);
  420. if assigned(p1) then
  421. dispose(p1,done);
  422. if assigned(p2) then
  423. dispose(p2,done);
  424. if assigned(p3) then
  425. dispose(p3,done);
  426. if assigned(next) then
  427. dispose(next,done);
  428. end;
  429. %}
  430. D [0-9]
  431. %%
  432. "/*" if NotInCPlusBlock then
  433. begin
  434. if not stripcomment then
  435. write(outfile,aktspace,'{');
  436. repeat
  437. c:=get_char;
  438. case c of
  439. '*' :
  440. begin
  441. c:=get_char;
  442. if c='/' then
  443. begin
  444. if not stripcomment then
  445. write(outfile,' }');
  446. c:=get_char;
  447. if c=newline then
  448. writeln(outfile);
  449. unget_char(c);
  450. flush(outfile);
  451. exit;
  452. end
  453. else
  454. begin
  455. if not stripcomment then
  456. write(outfile,'*');
  457. unget_char(c)
  458. end;
  459. end;
  460. newline :
  461. begin
  462. if not stripcomment then
  463. begin
  464. writeln(outfile);
  465. write(outfile,aktspace);
  466. end;
  467. end;
  468. { Don't write this thing out, to
  469. avoid nested comments.
  470. }
  471. '{','}' :
  472. begin
  473. end;
  474. #0 :
  475. commenteof;
  476. else
  477. if not stripcomment then
  478. write(outfile,c);
  479. end;
  480. until false;
  481. flush(outfile);
  482. end
  483. else
  484. skip_until_eol;
  485. "//" if NotInCPlusBlock then
  486. begin
  487. commentstr:='';
  488. if (in_define) and not (stripcomment) then
  489. begin
  490. commentstr:='{';
  491. end
  492. else
  493. If not stripcomment then
  494. write(outfile,aktspace,'{');
  495. repeat
  496. c:=get_char;
  497. case c of
  498. newline :
  499. begin
  500. unget_char(c);
  501. if not stripcomment then
  502. begin
  503. if in_define then
  504. begin
  505. commentstr:=commentstr+' }';
  506. end
  507. else
  508. begin
  509. write(outfile,' }');
  510. writeln(outfile);
  511. end;
  512. end;
  513. flush(outfile);
  514. exit;
  515. end;
  516. { Don't write this comment out,
  517. to avoid nested comment problems
  518. }
  519. '{','}' :
  520. begin
  521. end;
  522. #0 :
  523. commenteof;
  524. else
  525. if not stripcomment then
  526. begin
  527. if in_define then
  528. begin
  529. commentstr:=commentstr+c;
  530. end
  531. else
  532. write(outfile,c);
  533. end;
  534. end;
  535. until false;
  536. flush(outfile);
  537. end
  538. else
  539. skip_until_eol;
  540. \"[^\"]*\" if NotInCPlusBlock then return(CSTRING) else skip_until_eol;
  541. \'[^\']*\' if NotInCPlusBlock then return(CSTRING) else skip_until_eol;
  542. "L"\"[^\"]*\" if NotInCPlusBlock then
  543. begin
  544. if win32headers then
  545. return(CSTRING)
  546. else
  547. return(256);
  548. end
  549. else skip_until_eol;
  550. "L"\'[^\']*\' if NotInCPlusBlock then
  551. begin
  552. if win32headers then
  553. return(CSTRING)
  554. else
  555. return(256);
  556. end
  557. else
  558. skip_until_eol;
  559. {D}+[Uu]?[Ll]?[Ll]? if NotInCPlusBlock then
  560. begin
  561. if yytext[1]='0' then
  562. begin
  563. delete(yytext,1,1);
  564. yytext:='&'+yytext;
  565. end;
  566. while yytext[length(yytext)] in ['L','U','l','u'] do
  567. Delete(yytext,length(yytext),1);
  568. return(NUMBER);
  569. end
  570. else skip_until_eol;
  571. "0x"[0-9A-Fa-f]*[Uu]?[Ll]?[Ll]?
  572. if NotInCPlusBlock then
  573. begin
  574. (* handle pre- and postfixes *)
  575. if copy(yytext,1,2)='0x' then
  576. begin
  577. delete(yytext,1,2);
  578. yytext:='$'+yytext;
  579. end;
  580. while yytext[length(yytext)] in ['L','U','l','u'] do
  581. Delete(yytext,length(yytext),1);
  582. return(NUMBER);
  583. end
  584. else
  585. skip_until_eol;
  586. {D}+(\.{D}+)?([Ee][+-]?{D}+)?
  587. if NotInCPlusBlock then
  588. begin
  589. return(NUMBER);
  590. end
  591. else
  592. skip_until_eol;
  593. "->" if NotInCPlusBlock then
  594. begin
  595. if in_define then
  596. return(DEREF)
  597. else
  598. return(256);
  599. end
  600. else
  601. skip_until_eol;
  602. "-" if NotInCPlusBlock then return(MINUS) else skip_until_eol;
  603. "==" if NotInCPlusBlock then return(EQUAL) else skip_until_eol;
  604. "!=" if NotInCPlusBlock then return(UNEQUAL) else skip_until_eol;
  605. ">=" if NotInCPlusBlock then return(GTE) else skip_until_eol;
  606. "<=" if NotInCPlusBlock then return(LTE) else skip_until_eol;
  607. ">>" if NotInCPlusBlock then return(_SHR) else skip_until_eol;
  608. "##" if NotInCPlusBlock then return(STICK) else skip_until_eol;
  609. "<<" if NotInCPlusBlock then return(_SHL) else skip_until_eol;
  610. ">" if NotInCPlusBlock then return(GT) else skip_until_eol;
  611. "<" if NotInCPlusBlock then return(LT) else skip_until_eol;
  612. "|" if NotInCPlusBlock then return(_OR) else skip_until_eol;
  613. "&" if NotInCPlusBlock then return(_AND) else skip_until_eol;
  614. "~" if NotInCPlusBlock then return(_NOT) else skip_until_eol; (* inverse, but handled as not operation *)
  615. "!" if NotInCPlusBlock then return(_NOT) else skip_until_eol;
  616. "/" if NotInCPlusBlock then return(_SLASH) else skip_until_eol;
  617. "+" if NotInCPlusBlock then return(_PLUS) else skip_until_eol;
  618. "?" if NotInCPlusBlock then return(QUESTIONMARK) else skip_until_eol;
  619. ":" if NotInCPlusBlock then return(COLON) else skip_until_eol;
  620. "," if NotInCPlusBlock then return(COMMA) else skip_until_eol;
  621. "[" if NotInCPlusBlock then return(LECKKLAMMER) else skip_until_eol;
  622. "]" if NotInCPlusBlock then return(RECKKLAMMER) else skip_until_eol;
  623. "(" if NotInCPlusBlock then
  624. begin
  625. inc(arglevel);
  626. return(LKLAMMER);
  627. end
  628. else
  629. skip_until_eol;
  630. ")" if NotInCPlusBlock then
  631. begin
  632. dec(arglevel);
  633. return(RKLAMMER);
  634. end
  635. else
  636. skip_until_eol;
  637. "*" if NotInCPlusBlock then return(STAR) else skip_until_eol;
  638. "..." if NotInCPlusBlock then return(ELLIPSIS) else skip_until_eol;
  639. "." if NotInCPlusBlock then
  640. if in_define then
  641. return(POINT)
  642. else
  643. return(256);
  644. "=" if NotInCPlusBlock then return(_ASSIGN) else skip_until_eol;
  645. "extern" if NotInCPlusBlock then return(EXTERN) else skip_until_eol;
  646. "STDCALL" if NotInCPlusBlock then
  647. begin
  648. if Win32headers then
  649. return(STDCALL)
  650. else
  651. return(ID);
  652. end
  653. else
  654. begin
  655. skip_until_eol;
  656. end;
  657. "CDECL" if NotInCPlusBlock then
  658. begin
  659. if not Win32headers then
  660. return(ID)
  661. else
  662. return(CDECL);
  663. end
  664. else
  665. begin
  666. skip_until_eol;
  667. end;
  668. "PASCAL" if NotInCPlusBlock then
  669. begin
  670. if not Win32headers then
  671. return(ID)
  672. else
  673. return(PASCAL);
  674. end
  675. else
  676. begin
  677. skip_until_eol;
  678. end;
  679. "PACKED" if NotInCPlusBlock then
  680. begin
  681. if not Win32headers then
  682. return(ID)
  683. else
  684. return(_PACKED);
  685. end
  686. else
  687. begin
  688. skip_until_eol;
  689. end;
  690. "WINAPI" if NotInCPlusBlock then
  691. begin
  692. if not Win32headers then
  693. return(ID)
  694. else
  695. return(WINAPI);
  696. end
  697. else
  698. begin
  699. skip_until_eol;
  700. end;
  701. "SYS_TRAP" if NotInCPlusBlock then
  702. begin
  703. if not palmpilot then
  704. return(ID)
  705. else
  706. return(SYS_TRAP);
  707. end
  708. else
  709. begin
  710. skip_until_eol;
  711. end;
  712. "WINGDIAPI" if NotInCPlusBlock then
  713. begin
  714. if not Win32headers then
  715. return(ID)
  716. else
  717. return(WINGDIAPI);
  718. end
  719. else
  720. begin
  721. skip_until_eol;
  722. end;
  723. "CALLBACK" if NotInCPlusBlock then
  724. begin
  725. if not Win32headers then
  726. return(ID)
  727. else
  728. return(CALLBACK);
  729. end
  730. else
  731. begin
  732. skip_until_eol;
  733. end;
  734. "EXPENTRY" if NotInCPlusBlock then
  735. begin
  736. if not Win32headers then
  737. return(ID)
  738. else
  739. return(CALLBACK);
  740. end
  741. else
  742. begin
  743. skip_until_eol;
  744. end;
  745. "void" if NotInCPlusBlock then return(VOID) else skip_until_eol;
  746. "VOID" if NotInCPlusBlock then return(VOID) else skip_until_eol;
  747. "#ifdef"[ \t]*"__cplusplus"[ \t]*\n"extern \"C\" {"\n"#endif"
  748. begin
  749. if not stripinfo then
  750. writeln(outfile,'{ C++ extern C conditionnal removed }');
  751. end;
  752. "#ifdef"[ \t]*"cplusplus"[ \t]*\n"extern \"C\" {"\n"#endif"
  753. begin
  754. if not stripinfo then
  755. writeln(outfile,'{ C++ extern C conditionnal removed }');
  756. end;
  757. "#ifdef"[ \t]*"__cplusplus"[ \t]*\n"}"\n"#endif"
  758. begin
  759. if not stripinfo then
  760. writeln(outfile,'{ C++ end of extern C conditionnal removed }');
  761. end;
  762. "#ifdef"[ \t]*"cplusplus"[ \t]*\n"}"\n"#endif"
  763. begin
  764. if not stripinfo then
  765. writeln(outfile,'{ C++ end of extern C conditionnal removed }');
  766. end;
  767. "#ifdef"[ \t]*"cplusplus"[ \t]*
  768. begin
  769. Inc(cplusblocklevel);
  770. end;
  771. "#ifdef"[ \t]*"__cplusplus"[ \t]*
  772. begin
  773. Inc(cplusblocklevel);
  774. end;
  775. "#ifdef"[ \t]
  776. begin
  777. if cplusblocklevel > 0 then
  778. Inc(cplusblocklevel)
  779. else
  780. begin
  781. if cplusblocklevel < 0 then
  782. Dec(cplusblocklevel);
  783. write(outfile,'{$ifdef ');
  784. copy_until_eol;
  785. writeln(outfile,'}');
  786. flush(outfile);
  787. end;
  788. end;
  789. "#"[ \t]*"else" begin
  790. if cplusblocklevel < -1 then
  791. begin
  792. writeln(outfile,'{$else}');
  793. block_type:=bt_no;
  794. flush(outfile);
  795. end
  796. else
  797. case cplusblocklevel of
  798. 0 :
  799. begin
  800. writeln(outfile,'{$else}');
  801. block_type:=bt_no;
  802. flush(outfile);
  803. end;
  804. 1 : cplusblocklevel := -1;
  805. -1 : cplusblocklevel := 1;
  806. end;
  807. end;
  808. "#"[ \t]*"endif" begin
  809. if cplusblocklevel > 0 then
  810. begin
  811. Dec(cplusblocklevel);
  812. end
  813. else
  814. begin
  815. case cplusblocklevel of
  816. 0 : begin
  817. writeln(outfile,'{$endif}');
  818. block_type:=bt_no;
  819. flush(outfile);
  820. end;
  821. -1 : begin
  822. cplusblocklevel :=0;
  823. end
  824. else
  825. inc(cplusblocklevel);
  826. end;
  827. end;
  828. end;
  829. "#"[ \t]*"elif" begin
  830. if cplusblocklevel < -1 then
  831. begin
  832. if not stripinfo then
  833. write(outfile,'(*** was #elif ****)');
  834. write(outfile,'{$else');
  835. copy_until_eol;
  836. writeln(outfile,'}');
  837. block_type:=bt_no;
  838. flush(outfile);
  839. end
  840. else
  841. case cplusblocklevel of
  842. 0 :
  843. begin
  844. if not stripinfo then
  845. write(outfile,'(*** was #elif ****)');
  846. write(outfile,'{$else');
  847. copy_until_eol;
  848. writeln(outfile,'}');
  849. block_type:=bt_no;
  850. flush(outfile);
  851. end;
  852. 1 : cplusblocklevel := -1;
  853. -1 : cplusblocklevel := 1;
  854. end;
  855. end;
  856. "#"[ \t]*"undef" begin
  857. write(outfile,'{$undef');
  858. copy_until_eol;
  859. writeln(outfile,'}');
  860. flush(outfile);
  861. end;
  862. "#"[ \t]*"error" begin
  863. write(outfile,'{$error');
  864. copy_until_eol;
  865. writeln(outfile,'}');
  866. flush(outfile);
  867. end;
  868. "#"[ \t]*"include" if NotInCPlusBlock then
  869. begin
  870. write(outfile,'{$include');
  871. copy_until_eol;
  872. writeln(outfile,'}');
  873. flush(outfile);
  874. block_type:=bt_no;
  875. end
  876. else
  877. skip_until_eol;
  878. "#"[ \t]*"if" begin
  879. if cplusblocklevel > 0 then
  880. Inc(cplusblocklevel)
  881. else
  882. begin
  883. if cplusblocklevel < 0 then
  884. Dec(cplusblocklevel);
  885. write(outfile,'{$if');
  886. copy_until_eol;
  887. writeln(outfile,'}');
  888. flush(outfile);
  889. block_type:=bt_no;
  890. end;
  891. end;
  892. "# "[0-9]+" " if NotInCPlusBlock then
  893. (* preprocessor line info *)
  894. repeat
  895. c:=get_char;
  896. case c of
  897. newline :
  898. begin
  899. unget_char(c);
  900. exit;
  901. end;
  902. #0 :
  903. commenteof;
  904. end;
  905. until false
  906. else
  907. skip_until_eol;
  908. "#"[ \t]*"pragma" begin
  909. if not stripinfo then
  910. begin
  911. write(outfile,'(** unsupported pragma');
  912. write(outfile,'#pragma');
  913. copy_until_eol;
  914. writeln(outfile,'*)');
  915. flush(outfile);
  916. end
  917. else
  918. skip_until_eol;
  919. block_type:=bt_no;
  920. end;
  921. "#"[ \t]*"define" if NotInCPlusBlock then
  922. begin
  923. commentstr:='';
  924. in_define:=true;
  925. in_space_define:=1;
  926. return(DEFINE);
  927. end
  928. else
  929. skip_until_eol;
  930. "char" if NotInCPlusBlock then return(_CHAR) else skip_until_eol;
  931. "union" if NotInCPlusBlock then return(UNION) else skip_until_eol;
  932. "enum" if NotInCPlusBlock then return(ENUM) else skip_until_eol;
  933. "struct" if NotInCPlusBlock then return(STRUCT) else skip_until_eol;
  934. "{" if NotInCPlusBlock then return(LGKLAMMER) else skip_until_eol;
  935. "}" if NotInCPlusBlock then return(RGKLAMMER) else skip_until_eol;
  936. "typedef" if NotInCPlusBlock then return(TYPEDEF) else skip_until_eol;
  937. "int" if NotInCPlusBlock then return(INT) else skip_until_eol;
  938. "short" if NotInCPlusBlock then return(SHORT) else skip_until_eol;
  939. "long" if NotInCPlusBlock then return(LONG) else skip_until_eol;
  940. "signed" if NotInCPlusBlock then return(SIGNED) else skip_until_eol;
  941. "unsigned" if NotInCPlusBlock then return(UNSIGNED) 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. "int8" if NotInCPlusBlock then return(INT8) else skip_until_eol;
  947. "int16" if NotInCPlusBlock then return(INT16) else skip_until_eol;
  948. "int32" if NotInCPlusBlock then return(INT32) else skip_until_eol;
  949. "int64" if NotInCPlusBlock then return(INT64) else skip_until_eol;
  950. "float" if NotInCPlusBlock then return(FLOAT) else skip_until_eol;
  951. "const" if NotInCPlusBlock then return(_CONST) else skip_until_eol;
  952. "CONST" if NotInCPlusBlock then return(_CONST) else skip_until_eol;
  953. "FAR" if NotInCPlusBlock then return(_FAR) else skip_until_eol;
  954. "far" if NotInCPlusBlock then return(_FAR) else skip_until_eol;
  955. "NEAR" if NotInCPlusBlock then return(_NEAR) else skip_until_eol;
  956. "near" if NotInCPlusBlock then return(_NEAR) else skip_until_eol;
  957. "HUGE" if NotInCPlusBlock then return(_HUGE) else skip_until_eol;
  958. "huge" if NotInCPlusBlock then return(_HUGE) else skip_until_eol;
  959. "while" if NotInCPlusBlock then return(_WHILE) else skip_until_eol;
  960. [A-Za-z_][A-Za-z0-9_]* if NotInCPlusBlock then
  961. begin
  962. if in_space_define=1 then
  963. in_space_define:=2;
  964. return(ID);
  965. end
  966. else
  967. skip_until_eol;
  968. ";" if NotInCPlusBlock then return(SEMICOLON) else skip_until_eol;
  969. [ \f\t] if NotInCPlusBlock then
  970. begin
  971. if (arglevel=0) and (in_space_define=2) then
  972. begin
  973. in_space_define:=0;
  974. return(SPACE_DEFINE);
  975. end;
  976. end
  977. else
  978. skip_until_eol;
  979. \n begin
  980. if in_define then
  981. begin
  982. in_space_define:=0;
  983. if cont_line then
  984. begin
  985. cont_line:=false;
  986. end
  987. else
  988. begin
  989. in_define:=false;
  990. if NotInCPlusBlock then
  991. return(NEW_LINE)
  992. else
  993. skip_until_eol
  994. end;
  995. end;
  996. end;
  997. \\$ begin
  998. if in_define then
  999. begin
  1000. cont_line:=true;
  1001. end
  1002. else
  1003. begin
  1004. writeln('Unexpected wrap of line ',yylineno);
  1005. writeln('"',yyline,'"');
  1006. return(256);
  1007. end;
  1008. end;
  1009. . begin
  1010. writeln('Illegal character in line ',yylineno);
  1011. writeln('"',yyline,'"');
  1012. return(256);
  1013. end;
  1014. %%
  1015. function act_token : string;
  1016. begin
  1017. act_token:=yytext;
  1018. end;
  1019. end.