scan.l 38 KB

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