scanner.pas 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  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. }
  17. unit scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,version,tokens,
  23. verbose,comphook,
  24. finput,
  25. widestr;
  26. const
  27. max_include_nesting=32;
  28. max_macro_nesting=16;
  29. preprocbufsize=32*1024;
  30. type
  31. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  32. tscannerfile = class;
  33. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  34. tpreprocstack = class
  35. typ : preproctyp;
  36. accept : boolean;
  37. next : tpreprocstack;
  38. name : stringid;
  39. line_nb : longint;
  40. owner : tscannerfile;
  41. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  42. end;
  43. tdirectiveproc=procedure;
  44. tdirectiveitem = class(TNamedIndexItem)
  45. public
  46. is_conditional : boolean;
  47. proc : tdirectiveproc;
  48. constructor Create(const n:string;p:tdirectiveproc);
  49. constructor CreateCond(const n:string;p:tdirectiveproc);
  50. end;
  51. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  52. tscannerfile = class
  53. public
  54. inputfile : tinputfile; { current inputfile list }
  55. inputfilecount : longint;
  56. inputbuffer, { input buffer }
  57. inputpointer : pchar;
  58. inputstart : longint;
  59. line_no, { line }
  60. lastlinepos : longint;
  61. lasttokenpos : longint; { token }
  62. lasttoken,
  63. nexttoken : ttoken;
  64. comment_level,
  65. yylexcount : longint;
  66. lastasmgetchar : char;
  67. ignoredirectives : tstringlist; { ignore directives, used to give warnings only once }
  68. preprocstack : tpreprocstack;
  69. in_asm_string : boolean;
  70. preproc_pattern : string;
  71. preproc_token : ttoken;
  72. constructor Create(const fn:string);
  73. destructor Destroy;override;
  74. { File buffer things }
  75. function openinputfile:boolean;
  76. procedure closeinputfile;
  77. function tempopeninputfile:boolean;
  78. procedure tempcloseinputfile;
  79. procedure saveinputfile;
  80. procedure restoreinputfile;
  81. procedure firstfile;
  82. procedure nextfile;
  83. procedure addfile(hp:tinputfile);
  84. procedure reload;
  85. procedure insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  86. { Scanner things }
  87. procedure gettokenpos;
  88. procedure inc_comment_level;
  89. procedure dec_comment_level;
  90. procedure illegal_char(c:char);
  91. procedure end_of_file;
  92. procedure checkpreprocstack;
  93. procedure poppreprocstack;
  94. procedure ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  95. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  96. procedure elsepreprocstack;
  97. procedure handleconditional(p:tdirectiveitem);
  98. procedure handledirectives;
  99. procedure linebreak;
  100. procedure readchar;
  101. procedure readstring;
  102. procedure readnumber;
  103. function readid:string;
  104. function readval:longint;
  105. function readval_asstring:string;
  106. function readcomment:string;
  107. function readquotedstring:string;
  108. function readstate:char;
  109. function readstatedefault:char;
  110. procedure skipspace;
  111. procedure skipuntildirective;
  112. procedure skipcomment;
  113. procedure skipdelphicomment;
  114. procedure skipoldtpcomment;
  115. procedure readtoken;
  116. function readpreproc:ttoken;
  117. function asmgetcharstart : char;
  118. function asmgetchar:char;
  119. end;
  120. {$ifdef PREPROCWRITE}
  121. tpreprocfile=class
  122. f : text;
  123. buf : pointer;
  124. spacefound,
  125. eolfound : boolean;
  126. constructor create(const fn:string);
  127. destructor destroy;
  128. procedure Add(const s:string);
  129. procedure AddSpace;
  130. end;
  131. {$endif PREPROCWRITE}
  132. var
  133. { read strings }
  134. c : char;
  135. orgpattern,
  136. pattern : string;
  137. patternw : pcompilerwidestring;
  138. { token }
  139. token, { current token being parsed }
  140. idtoken : ttoken; { holds the token if the pattern is a known word }
  141. current_scanner : tscannerfile; { current scanner in use }
  142. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  143. {$ifdef PREPROCWRITE}
  144. preprocfile : tpreprocfile; { used with only preprocessing }
  145. {$endif PREPROCWRITE}
  146. type
  147. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  148. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  149. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  150. procedure InitScanner;
  151. procedure DoneScanner;
  152. {To be called when the language mode is finally determined}
  153. procedure ConsolidateMode;
  154. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  155. implementation
  156. uses
  157. dos,
  158. cutils,
  159. systems,
  160. switches,
  161. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  162. fmodule;
  163. var
  164. { dictionaries with the supported directives }
  165. turbo_scannerdirectives : tdictionary; { for other modes }
  166. mac_scannerdirectives : tdictionary; { for mode mac }
  167. {*****************************************************************************
  168. Helper routines
  169. *****************************************************************************}
  170. const
  171. { use any special name that is an invalid file name to avoid problems }
  172. preprocstring : array [preproctyp] of string[7]
  173. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  174. function is_keyword(const s:string):boolean;
  175. var
  176. low,high,mid : longint;
  177. begin
  178. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  179. not (s[1] in ['a'..'z','A'..'Z']) then
  180. begin
  181. is_keyword:=false;
  182. exit;
  183. end;
  184. low:=ord(tokenidx^[length(s),s[1]].first);
  185. high:=ord(tokenidx^[length(s),s[1]].last);
  186. while low<high do
  187. begin
  188. mid:=(high+low+1) shr 1;
  189. if pattern<tokeninfo^[ttoken(mid)].str then
  190. high:=mid-1
  191. else
  192. low:=mid;
  193. end;
  194. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  195. (tokeninfo^[ttoken(high)].keyword in aktmodeswitches);
  196. end;
  197. {To be called when the language mode is finally determined}
  198. procedure ConsolidateMode;
  199. begin
  200. if m_mac in aktmodeswitches then
  201. if current_module.is_unit and not assigned(current_module.globalmacrosymtable) then
  202. begin
  203. current_module.globalmacrosymtable:= tmacrosymtable.create(true);
  204. current_module.globalmacrosymtable.next:= current_module.localmacrosymtable;
  205. macrosymtablestack:=current_module.globalmacrosymtable;
  206. end;
  207. end;
  208. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  209. var
  210. b : boolean;
  211. oldaktmodeswitches : tmodeswitches;
  212. begin
  213. oldaktmodeswitches:=aktmodeswitches;
  214. b:=true;
  215. if s='DEFAULT' then
  216. aktmodeswitches:=initmodeswitches
  217. else
  218. if s='DELPHI' then
  219. aktmodeswitches:=delphimodeswitches
  220. else
  221. if s='TP' then
  222. aktmodeswitches:=tpmodeswitches
  223. else
  224. if s='FPC' then
  225. aktmodeswitches:=fpcmodeswitches
  226. else
  227. if s='OBJFPC' then
  228. aktmodeswitches:=objfpcmodeswitches
  229. else
  230. if s='GPC' then
  231. aktmodeswitches:=gpcmodeswitches
  232. else
  233. if s='MACPAS' then
  234. aktmodeswitches:=macmodeswitches
  235. else
  236. b:=false;
  237. if b and changeInit then
  238. initmodeswitches := aktmodeswitches;
  239. if b then
  240. begin
  241. { resolve all postponed switch changes }
  242. if localswitcheschanged then
  243. begin
  244. aktlocalswitches:=nextaktlocalswitches;
  245. localswitcheschanged:=false;
  246. end;
  247. { turn ansistrings on by default ? }
  248. if (m_delphi in aktmodeswitches) then
  249. begin
  250. include(aktlocalswitches,cs_ansistrings);
  251. if changeinit then
  252. include(initlocalswitches,cs_ansistrings);
  253. end
  254. else
  255. begin
  256. exclude(aktlocalswitches,cs_ansistrings);
  257. if changeinit then
  258. exclude(initlocalswitches,cs_ansistrings);
  259. end;
  260. { Default enum packing for delphi/tp7 }
  261. if (m_tp7 in aktmodeswitches) or
  262. (m_delphi in aktmodeswitches) then
  263. aktpackenum:=1
  264. else if (m_mac in aktmodeswitches) then
  265. { compatible with Metrowerks Pascal }
  266. aktpackenum:=2
  267. else
  268. aktpackenum:=4;
  269. if changeinit then
  270. initpackenum:=aktpackenum;
  271. {$ifdef i386}
  272. { Default to intel assembler for delphi/tp7 on i386 }
  273. if (m_delphi in aktmodeswitches) or
  274. (m_tp7 in aktmodeswitches) then
  275. aktasmmode:=asmmode_i386_intel;
  276. if changeinit then
  277. initasmmode:=aktasmmode;
  278. {$endif i386}
  279. { Undefine old symbol }
  280. if (m_delphi in oldaktmodeswitches) then
  281. undef_system_macro('FPC_DELPHI')
  282. else if (m_tp7 in oldaktmodeswitches) then
  283. undef_system_macro('FPC_TP')
  284. else if (m_objfpc in oldaktmodeswitches) then
  285. undef_system_macro('FPC_OBJFPC')
  286. else if (m_gpc in oldaktmodeswitches) then
  287. undef_system_macro('FPC_GPC')
  288. else if (m_mac in oldaktmodeswitches) then
  289. undef_system_macro('FPC_MACPAS');
  290. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  291. if (m_delphi in aktmodeswitches) then
  292. def_system_macro('FPC_DELPHI')
  293. else if (m_tp7 in aktmodeswitches) then
  294. def_system_macro('FPC_TP')
  295. else if (m_objfpc in aktmodeswitches) then
  296. def_system_macro('FPC_OBJFPC')
  297. else if (m_gpc in aktmodeswitches) then
  298. def_system_macro('FPC_GPC')
  299. else if (m_mac in aktmodeswitches) then
  300. def_system_macro('FPC_MACPAS');
  301. end;
  302. SetCompileMode:=b;
  303. end;
  304. {*****************************************************************************
  305. Conditional Directives
  306. *****************************************************************************}
  307. procedure dir_else;
  308. begin
  309. current_scanner.elsepreprocstack;
  310. end;
  311. procedure dir_endif;
  312. begin
  313. current_scanner.poppreprocstack;
  314. end;
  315. function isdef(var valuedescr: String): Boolean;
  316. var
  317. hs : string;
  318. mac : tmacro;
  319. begin
  320. current_scanner.skipspace;
  321. hs:=current_scanner.readid;
  322. valuedescr:= hs;
  323. if hs='' then
  324. Message(scan_e_error_in_preproc_expr);
  325. mac:=tmacro(search_macro(hs));
  326. if assigned(mac) then
  327. mac.is_used:=true;
  328. isdef:= assigned(mac) and mac.defined;
  329. end;
  330. procedure dir_ifdef;
  331. begin
  332. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  333. end;
  334. function isnotdef(var valuedescr: String): Boolean;
  335. var
  336. hs : string;
  337. mac : tmacro;
  338. begin
  339. current_scanner.skipspace;
  340. hs:=current_scanner.readid;
  341. valuedescr:= hs;
  342. if hs='' then
  343. Message(scan_e_error_in_preproc_expr);
  344. mac:=tmacro(search_macro(hs));
  345. if assigned(mac) then
  346. mac.is_used:=true;
  347. isnotdef:= not (assigned(mac) and mac.defined);
  348. end;
  349. procedure dir_ifndef;
  350. begin
  351. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  352. end;
  353. function opt_check(var valuedescr: String): Boolean;
  354. var
  355. hs : string;
  356. state : char;
  357. begin
  358. opt_check:= false;
  359. current_scanner.skipspace;
  360. hs:=current_scanner.readid;
  361. valuedescr:= hs;
  362. if (length(hs)>1) then
  363. Message1(scan_w_illegal_switch,hs)
  364. else
  365. begin
  366. state:=current_scanner.ReadState;
  367. if state in ['-','+'] then
  368. opt_check:=CheckSwitch(hs[1],state)
  369. else
  370. Message(scan_e_error_in_preproc_expr);
  371. end;
  372. end;
  373. procedure dir_ifopt;
  374. begin
  375. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  376. end;
  377. procedure dir_libprefix;
  378. var
  379. s : string;
  380. begin
  381. current_scanner.skipspace;
  382. if c <> '''' then
  383. Message2(scan_f_syn_expected, '''', c);
  384. s := current_scanner.readquotedstring;
  385. stringdispose(outputprefix);
  386. outputprefix := stringdup(s);
  387. with current_module do
  388. setfilename(paramfn^, paramallowoutput);
  389. end;
  390. procedure dir_libsuffix;
  391. var
  392. s : string;
  393. begin
  394. current_scanner.skipspace;
  395. if c <> '''' then
  396. Message2(scan_f_syn_expected, '''', c);
  397. s := current_scanner.readquotedstring;
  398. stringdispose(outputsuffix);
  399. outputsuffix := stringdup(s);
  400. with current_module do
  401. setfilename(paramfn^, paramallowoutput);
  402. end;
  403. procedure dir_extension;
  404. var
  405. s : string;
  406. begin
  407. current_scanner.skipspace;
  408. if c <> '''' then
  409. Message2(scan_f_syn_expected, '''', c);
  410. s := current_scanner.readquotedstring;
  411. outputextension := '.'+s;
  412. with current_module do
  413. setfilename(paramfn^, paramallowoutput);
  414. end;
  415. {
  416. Compile time expression type check
  417. ----------------------------------
  418. Each subexpression returns its type to the caller, which then can
  419. do type check. Since data types of compile time expressions is
  420. not well defined, the type system does a best effort. The drawback is
  421. that some errors might not be detected.
  422. Instead of returning a particular data type, a set of possible data types
  423. are returned. This way ambigouos types can be handled. For instance a
  424. value of 1 can be both a boolean and and integer.
  425. Booleans
  426. --------
  427. The following forms of boolean values are supported:
  428. * C coded, that is 0 is false, non-zero is true.
  429. * TRUE/FALSE for mac style compile time variables
  430. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  431. When a compile time expression is evaluated, they are then translated
  432. to C coded booleans (0/1), to simplify for the expression evaluator.
  433. Note that this scheme then also of support mac compile time variables which
  434. are 0/1 but with a boolean meaning.
  435. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  436. means that units which is not recompiled, and thus stores
  437. compile time variables as the old format (0/1), continue to work.
  438. Short circuit evaluation
  439. ------------------------
  440. For this to work, the part of a compile time expression which is short
  441. circuited, should not be evaluated, while it still should be parsed.
  442. Therefor there is a parameter eval, telling whether evaluation is needed.
  443. In case not, the value returned can be arbitrary.
  444. }
  445. type
  446. {Compile time expression types}
  447. TCTEType = (ctetBoolean, ctetInteger, ctetString, ctetSet);
  448. TCTETypeSet = set of TCTEType;
  449. const
  450. cteTypeNames : array[TCTEType] of string[10] = (
  451. 'BOOLEAN','INTEGER','STRING','SET');
  452. {Subset of types which can be elements in sets.}
  453. setElementTypes = [ctetBoolean, ctetInteger, ctetString];
  454. function GetCTETypeName(t: TCTETypeSet): String;
  455. var
  456. i: TCTEType;
  457. begin
  458. result:= '';
  459. for i:= Low(TCTEType) to High(TCTEType) do
  460. if i in t then
  461. if result = '' then
  462. result:= cteTypeNames[i]
  463. else
  464. result:= result + ' or ' + cteTypeNames[i];
  465. end;
  466. procedure CTEError(actType, desiredExprType: TCTETypeSet; place: String);
  467. begin
  468. Message3(scan_e_compile_time_typeerror,
  469. GetCTETypeName(desiredExprType),
  470. GetCTETypeName(actType),
  471. place
  472. );
  473. end;
  474. function parse_compiler_expr(var compileExprType: TCTETypeSet):string;
  475. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string; forward;
  476. procedure preproc_consume(t : ttoken);
  477. begin
  478. if t<>current_scanner.preproc_token then
  479. Message(scan_e_preproc_syntax_error);
  480. current_scanner.preproc_token:=current_scanner.readpreproc;
  481. end;
  482. function preproc_substitutedtoken(var macroType: TCTETypeSet; eval : Boolean): string;
  483. { Currently this parses identifiers as well as numbers.
  484. The result from this procedure can either be that the token
  485. itself is a value, or that it is a compile time variable/macro,
  486. which then is substituted for another value (for macros
  487. recursivelly substituted).}
  488. var
  489. hs: string;
  490. mac : tmacro;
  491. macrocount,
  492. len : integer;
  493. numres : longint;
  494. w: word;
  495. begin
  496. result := current_scanner.preproc_pattern;
  497. if not eval then
  498. exit;
  499. mac:= nil;
  500. { Substitue macros and compiler variables with their content/value.
  501. For real macros also do recursive substitution. }
  502. macrocount:=0;
  503. repeat
  504. mac:=tmacro(search_macro(result));
  505. inc(macrocount);
  506. if macrocount>max_macro_nesting then
  507. begin
  508. Message(scan_w_macro_too_deep);
  509. break;
  510. end;
  511. if assigned(mac) and mac.defined then
  512. if assigned(mac.buftext) then
  513. begin
  514. if mac.buflen>255 then
  515. begin
  516. len:=255;
  517. Message(scan_w_macro_cut_after_255_chars);
  518. end
  519. else
  520. len:=mac.buflen;
  521. hs[0]:=char(len);
  522. move(mac.buftext^,hs[1],len);
  523. result:=upcase(hs);
  524. mac.is_used:=true;
  525. end
  526. else
  527. begin
  528. Message1(scan_e_error_macro_lacks_value, result);
  529. break;
  530. end
  531. else
  532. begin
  533. break;
  534. end;
  535. if mac.is_compiler_var then
  536. break;
  537. until false;
  538. { At this point, result do contain the value. Do some decoding and
  539. determine the type.}
  540. val(result,numres,w);
  541. if (w=0) then {It is an integer}
  542. begin
  543. if (numres = 0) or (numres = 1) then
  544. macroType := [ctetInteger, ctetBoolean]
  545. else
  546. macroType := [ctetInteger];
  547. end
  548. else if assigned(mac) and (m_mac in aktmodeswitches) and (result='FALSE') then
  549. begin
  550. result:= '0';
  551. macroType:= [ctetBoolean];
  552. end
  553. else if assigned(mac) and (m_mac in aktmodeswitches) and (result='TRUE') then
  554. begin
  555. result:= '1';
  556. macroType:= [ctetBoolean];
  557. end
  558. else if (m_mac in aktmodeswitches) and
  559. (not assigned(mac) or not mac.defined) and
  560. (macrocount = 1) then
  561. begin
  562. {Errors in mode mac is issued here. For non macpas modes there is
  563. more liberty, but the error will eventually be caught at a later stage.}
  564. Message1(scan_e_error_macro_undefined, result);
  565. macroType:= [ctetString]; {Just to have something}
  566. end
  567. else
  568. macroType:= [ctetString];
  569. end;
  570. function read_factor(var factorType: TCTETypeSet; eval : Boolean) : string;
  571. var
  572. hs : string;
  573. mac: tmacro;
  574. srsym : tsym;
  575. srsymtable : tsymtable;
  576. l : longint;
  577. w : integer;
  578. hasKlammer: Boolean;
  579. setElemType : TCTETypeSet;
  580. begin
  581. if current_scanner.preproc_token=_ID then
  582. begin
  583. if current_scanner.preproc_pattern='DEFINED' then
  584. begin
  585. factorType:= [ctetBoolean];
  586. preproc_consume(_ID);
  587. current_scanner.skipspace;
  588. if current_scanner.preproc_token =_LKLAMMER then
  589. begin
  590. preproc_consume(_LKLAMMER);
  591. current_scanner.skipspace;
  592. hasKlammer:= true;
  593. end
  594. else if (m_mac in aktmodeswitches) then
  595. hasKlammer:= false
  596. else
  597. Message(scan_e_error_in_preproc_expr);
  598. if current_scanner.preproc_token =_ID then
  599. begin
  600. hs := current_scanner.preproc_pattern;
  601. mac := tmacro(search_macro(hs));
  602. if assigned(mac) and mac.defined then
  603. begin
  604. hs := '1';
  605. mac.is_used:=true;
  606. end
  607. else
  608. hs := '0';
  609. read_factor := hs;
  610. preproc_consume(_ID);
  611. current_scanner.skipspace;
  612. end
  613. else
  614. Message(scan_e_error_in_preproc_expr);
  615. if hasKlammer then
  616. if current_scanner.preproc_token =_RKLAMMER then
  617. preproc_consume(_RKLAMMER)
  618. else
  619. Message(scan_e_error_in_preproc_expr);
  620. end
  621. else
  622. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  623. begin
  624. factorType:= [ctetBoolean];
  625. preproc_consume(_ID);
  626. current_scanner.skipspace;
  627. if current_scanner.preproc_token =_ID then
  628. begin
  629. hs := current_scanner.preproc_pattern;
  630. mac := tmacro(search_macro(hs));
  631. if assigned(mac) then
  632. begin
  633. hs := '0';
  634. mac.is_used:=true;
  635. end
  636. else
  637. hs := '1';
  638. read_factor := hs;
  639. preproc_consume(_ID);
  640. current_scanner.skipspace;
  641. end
  642. else
  643. Message(scan_e_error_in_preproc_expr);
  644. end
  645. else
  646. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='OPTION') then
  647. begin
  648. factorType:= [ctetBoolean];
  649. preproc_consume(_ID);
  650. current_scanner.skipspace;
  651. if current_scanner.preproc_token =_LKLAMMER then
  652. begin
  653. preproc_consume(_LKLAMMER);
  654. current_scanner.skipspace;
  655. end
  656. else
  657. Message(scan_e_error_in_preproc_expr);
  658. if not (current_scanner.preproc_token = _ID) then
  659. Message(scan_e_error_in_preproc_expr);
  660. hs:=current_scanner.preproc_pattern;
  661. if (length(hs) > 1) then
  662. {This is allowed in Metrowerks Pascal}
  663. Message(scan_e_error_in_preproc_expr)
  664. else
  665. begin
  666. if CheckSwitch(hs[1],'+') then
  667. read_factor := '1'
  668. else
  669. read_factor := '0';
  670. end;
  671. preproc_consume(_ID);
  672. current_scanner.skipspace;
  673. if current_scanner.preproc_token =_RKLAMMER then
  674. preproc_consume(_RKLAMMER)
  675. else
  676. Message(scan_e_error_in_preproc_expr);
  677. end
  678. else
  679. if current_scanner.preproc_pattern='SIZEOF' then
  680. begin
  681. factorType:= [ctetInteger];
  682. preproc_consume(_ID);
  683. current_scanner.skipspace;
  684. if current_scanner.preproc_token =_LKLAMMER then
  685. begin
  686. preproc_consume(_LKLAMMER);
  687. current_scanner.skipspace;
  688. end
  689. else
  690. Message(scan_e_preproc_syntax_error);
  691. if eval then
  692. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  693. begin
  694. l:=0;
  695. case srsym.typ of
  696. globalvarsym,
  697. localvarsym,
  698. paravarsym :
  699. l:=tabstractvarsym(srsym).getsize;
  700. typedconstsym :
  701. l:=ttypedconstsym(srsym).getsize;
  702. typesym:
  703. l:=ttypesym(srsym).restype.def.size;
  704. else
  705. Message(scan_e_error_in_preproc_expr);
  706. end;
  707. str(l,read_factor);
  708. end
  709. else
  710. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  711. preproc_consume(_ID);
  712. current_scanner.skipspace;
  713. if current_scanner.preproc_token =_RKLAMMER then
  714. preproc_consume(_RKLAMMER)
  715. else
  716. Message(scan_e_preproc_syntax_error);
  717. end
  718. else
  719. if current_scanner.preproc_pattern='DECLARED' then
  720. begin
  721. factorType:= [ctetBoolean];
  722. preproc_consume(_ID);
  723. current_scanner.skipspace;
  724. if current_scanner.preproc_token =_LKLAMMER then
  725. begin
  726. preproc_consume(_LKLAMMER);
  727. current_scanner.skipspace;
  728. end
  729. else
  730. Message(scan_e_error_in_preproc_expr);
  731. if current_scanner.preproc_token =_ID then
  732. begin
  733. hs := upper(current_scanner.preproc_pattern);
  734. if searchsym(hs,srsym,srsymtable) then
  735. hs := '1'
  736. else
  737. hs := '0';
  738. read_factor := hs;
  739. preproc_consume(_ID);
  740. current_scanner.skipspace;
  741. end
  742. else
  743. Message(scan_e_error_in_preproc_expr);
  744. if current_scanner.preproc_token =_RKLAMMER then
  745. preproc_consume(_RKLAMMER)
  746. else
  747. Message(scan_e_error_in_preproc_expr);
  748. end
  749. else
  750. if current_scanner.preproc_pattern='NOT' then
  751. begin
  752. factorType:= [ctetBoolean];
  753. preproc_consume(_ID);
  754. hs:=read_factor(factorType, eval);
  755. if eval then
  756. begin
  757. if not (ctetBoolean in factorType) then
  758. CTEError(factorType, [ctetBoolean], 'NOT');
  759. val(hs,l,w);
  760. if l<>0 then
  761. read_factor:='0'
  762. else
  763. read_factor:='1';
  764. end
  765. else
  766. read_factor:='0'; {Just to have something}
  767. end
  768. else
  769. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='TRUE') then
  770. begin
  771. factorType:= [ctetBoolean];
  772. preproc_consume(_ID);
  773. read_factor:='1';
  774. end
  775. else
  776. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='FALSE') then
  777. begin
  778. factorType:= [ctetBoolean];
  779. preproc_consume(_ID);
  780. read_factor:='0';
  781. end
  782. else
  783. begin
  784. hs:=preproc_substitutedtoken(factorType, eval);
  785. { Default is to return the original symbol }
  786. read_factor:=hs;
  787. if eval and (m_delphi in aktmodeswitches) and (ctetString in factorType) then
  788. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  789. begin
  790. case srsym.typ of
  791. constsym :
  792. begin
  793. with tconstsym(srsym) do
  794. begin
  795. case consttyp of
  796. constord :
  797. begin
  798. case consttype.def.deftype of
  799. orddef:
  800. begin
  801. if is_integer(consttype.def) then
  802. begin
  803. read_factor:=tostr(value.valueord);
  804. factorType:= [ctetInteger];
  805. end
  806. else if is_boolean(consttype.def) then
  807. begin
  808. read_factor:=tostr(value.valueord);
  809. factorType:= [ctetBoolean];
  810. end
  811. else if is_char(consttype.def) then
  812. begin
  813. read_factor:=chr(value.valueord);
  814. factorType:= [ctetString];
  815. end
  816. end;
  817. enumdef:
  818. begin
  819. read_factor:=tostr(value.valueord);
  820. factorType:= [ctetInteger];
  821. end;
  822. end;
  823. end;
  824. conststring :
  825. begin
  826. read_factor := upper(pchar(value.valueptr));
  827. factorType:= [ctetString];
  828. end;
  829. constset :
  830. begin
  831. hs:=',';
  832. for l:=0 to 255 do
  833. if l in pconstset(tconstsym(srsym).value.valueptr)^ then
  834. hs:=hs+tostr(l)+',';
  835. read_factor := hs;
  836. factorType:= [ctetSet];
  837. end;
  838. end;
  839. end;
  840. end;
  841. enumsym :
  842. begin
  843. read_factor:=tostr(tenumsym(srsym).value);
  844. factorType:= [ctetInteger];
  845. end;
  846. end;
  847. end;
  848. preproc_consume(_ID);
  849. current_scanner.skipspace;
  850. end
  851. end
  852. else if current_scanner.preproc_token =_LKLAMMER then
  853. begin
  854. preproc_consume(_LKLAMMER);
  855. read_factor:=read_expr(factorType, eval);
  856. preproc_consume(_RKLAMMER);
  857. end
  858. else if current_scanner.preproc_token = _LECKKLAMMER then
  859. begin
  860. preproc_consume(_LECKKLAMMER);
  861. read_factor := ',';
  862. while current_scanner.preproc_token = _ID do
  863. begin
  864. read_factor := read_factor+read_factor(setElemType, eval)+',';
  865. if current_scanner.preproc_token = _COMMA then
  866. preproc_consume(_COMMA);
  867. end;
  868. // TODO Add check of setElemType
  869. preproc_consume(_RECKKLAMMER);
  870. factorType:= [ctetSet];
  871. end
  872. else
  873. Message(scan_e_error_in_preproc_expr);
  874. end;
  875. function read_term(var termType: TCTETypeSet; eval : Boolean) : string;
  876. var
  877. hs1,hs2 : string;
  878. l1,l2 : longint;
  879. w : integer;
  880. termType2: TCTETypeSet;
  881. begin
  882. hs1:=read_factor(termType, eval);
  883. repeat
  884. if (current_scanner.preproc_token<>_ID) then
  885. break;
  886. if current_scanner.preproc_pattern<>'AND' then
  887. break;
  888. val(hs1,l1,w);
  889. if l1=0 then
  890. eval:= false; {Short circuit evaluation of OR}
  891. if eval then
  892. begin
  893. {Check if first expr is boolean. Must be done here, after we know
  894. it is an AND expression.}
  895. if not (ctetBoolean in termType) then
  896. CTEError(termType, [ctetBoolean], 'AND');
  897. termType:= [ctetBoolean];
  898. end;
  899. preproc_consume(_ID);
  900. hs2:=read_factor(termType2, eval);
  901. if eval then
  902. begin
  903. if not (ctetBoolean in termType2) then
  904. CTEError(termType2, [ctetBoolean], 'AND');
  905. val(hs2,l2,w);
  906. if (l1<>0) and (l2<>0) then
  907. hs1:='1'
  908. else
  909. hs1:='0';
  910. end;
  911. until false;
  912. read_term:=hs1;
  913. end;
  914. function read_simple_expr(var simpleExprType: TCTETypeSet; eval : Boolean) : string;
  915. var
  916. hs1,hs2 : string;
  917. l1,l2 : longint;
  918. w : integer;
  919. simpleExprType2: TCTETypeSet;
  920. begin
  921. hs1:=read_term(simpleExprType, eval);
  922. repeat
  923. if (current_scanner.preproc_token<>_ID) then
  924. break;
  925. if current_scanner.preproc_pattern<>'OR' then
  926. break;
  927. val(hs1,l1,w);
  928. if l1<>0 then
  929. eval:= false; {Short circuit evaluation of OR}
  930. if eval then
  931. begin
  932. {Check if first expr is boolean. Must be done here, after we know
  933. it is an OR expression.}
  934. if not (ctetBoolean in simpleExprType) then
  935. CTEError(simpleExprType, [ctetBoolean], 'OR');
  936. simpleExprType:= [ctetBoolean];
  937. end;
  938. preproc_consume(_ID);
  939. hs2:=read_term(simpleExprType2, eval);
  940. if eval then
  941. begin
  942. if not (ctetBoolean in simpleExprType2) then
  943. CTEError(simpleExprType2, [ctetBoolean], 'OR');
  944. val(hs2,l2,w);
  945. if (l1<>0) or (l2<>0) then
  946. hs1:='1'
  947. else
  948. hs1:='0';
  949. end;
  950. until false;
  951. read_simple_expr:=hs1;
  952. end;
  953. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string;
  954. var
  955. hs1,hs2 : string;
  956. b : boolean;
  957. op : ttoken;
  958. w : integer;
  959. l1,l2 : longint;
  960. exprType2: TCTETypeSet;
  961. begin
  962. hs1:=read_simple_expr(exprType, eval);
  963. op:=current_scanner.preproc_token;
  964. if (op = _ID) and (current_scanner.preproc_pattern = 'IN') then
  965. op := _IN;
  966. if not (op in [_IN,_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  967. begin
  968. read_expr:=hs1;
  969. exit;
  970. end;
  971. if (op = _IN) then
  972. preproc_consume(_ID)
  973. else
  974. preproc_consume(op);
  975. hs2:=read_simple_expr(exprType2, eval);
  976. if eval then
  977. begin
  978. if op = _IN then
  979. begin
  980. if exprType2 <> [ctetSet] then
  981. CTEError(exprType2, [ctetSet], 'IN');
  982. if exprType = [ctetSet] then
  983. CTEError(exprType, setElementTypes, 'IN');
  984. if is_number(hs1) and is_number(hs2) then
  985. Message(scan_e_preproc_syntax_error)
  986. else if hs2[1] = ',' then
  987. b:=pos(','+hs1+',', hs2) > 0 { TODO For integer sets, perhaps check for numeric equivalence so that 0 = 00 }
  988. else
  989. Message(scan_e_preproc_syntax_error);
  990. end
  991. else
  992. begin
  993. if (exprType * exprType2) = [] then
  994. CTEError(exprType2, exprType, tokeninfo^[op].str);
  995. if is_number(hs1) and is_number(hs2) then
  996. begin
  997. val(hs1,l1,w);
  998. val(hs2,l2,w);
  999. case op of
  1000. _EQUAL :
  1001. b:=l1=l2;
  1002. _UNEQUAL :
  1003. b:=l1<>l2;
  1004. _LT :
  1005. b:=l1<l2;
  1006. _GT :
  1007. b:=l1>l2;
  1008. _GTE :
  1009. b:=l1>=l2;
  1010. _LTE :
  1011. b:=l1<=l2;
  1012. end;
  1013. end
  1014. else
  1015. begin
  1016. case op of
  1017. _EQUAL :
  1018. b:=hs1=hs2;
  1019. _UNEQUAL :
  1020. b:=hs1<>hs2;
  1021. _LT :
  1022. b:=hs1<hs2;
  1023. _GT :
  1024. b:=hs1>hs2;
  1025. _GTE :
  1026. b:=hs1>=hs2;
  1027. _LTE :
  1028. b:=hs1<=hs2;
  1029. end;
  1030. end;
  1031. end;
  1032. end
  1033. else
  1034. b:= false; {Just to have something}
  1035. if b then
  1036. read_expr:='1'
  1037. else
  1038. read_expr:='0';
  1039. exprType:= [ctetBoolean];
  1040. end;
  1041. begin
  1042. current_scanner.skipspace;
  1043. { start preproc expression scanner }
  1044. current_scanner.preproc_token:=current_scanner.readpreproc;
  1045. parse_compiler_expr:=read_expr(compileExprType, true);
  1046. end;
  1047. function boolean_compile_time_expr(var valuedescr: String): Boolean;
  1048. var
  1049. hs : string;
  1050. exprType: TCTETypeSet;
  1051. begin
  1052. hs:=parse_compiler_expr(exprType);
  1053. if (exprType * [ctetBoolean]) = [] then
  1054. CTEError(exprType, [ctetBoolean], 'IF or ELSEIF');
  1055. boolean_compile_time_expr:= hs <> '0';
  1056. valuedescr:= hs;
  1057. end;
  1058. procedure dir_if;
  1059. begin
  1060. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1061. end;
  1062. procedure dir_elseif;
  1063. begin
  1064. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1065. end;
  1066. procedure dir_define_impl(macstyle: boolean);
  1067. var
  1068. hs : string;
  1069. bracketcount : longint;
  1070. mac : tmacro;
  1071. macropos : longint;
  1072. macrobuffer : pmacrobuffer;
  1073. begin
  1074. current_scanner.skipspace;
  1075. hs:=current_scanner.readid;
  1076. mac:=tmacro(search_macro(hs));
  1077. if not assigned(mac) or (mac.owner <> macrosymtablestack) then
  1078. begin
  1079. mac:=tmacro.create(hs);
  1080. mac.defined:=true;
  1081. Message1(parser_c_macro_defined,mac.name);
  1082. macrosymtablestack.insert(mac);
  1083. end
  1084. else
  1085. begin
  1086. Message1(parser_c_macro_defined,mac.name);
  1087. mac.defined:=true;
  1088. mac.is_compiler_var:=false;
  1089. { delete old definition }
  1090. if assigned(mac.buftext) then
  1091. begin
  1092. freemem(mac.buftext,mac.buflen);
  1093. mac.buftext:=nil;
  1094. end;
  1095. end;
  1096. mac.is_used:=true;
  1097. if (cs_support_macro in aktmoduleswitches) then
  1098. begin
  1099. { !!!!!! handle macro params, need we this? }
  1100. current_scanner.skipspace;
  1101. if not macstyle then
  1102. begin
  1103. { may be a macro? }
  1104. if c <> ':' then
  1105. exit;
  1106. current_scanner.readchar;
  1107. if c <> '=' then
  1108. exit;
  1109. current_scanner.readchar;
  1110. current_scanner.skipspace;
  1111. end;
  1112. { key words are never substituted }
  1113. if is_keyword(hs) then
  1114. Message(scan_e_keyword_cant_be_a_macro);
  1115. new(macrobuffer);
  1116. macropos:=0;
  1117. { parse macro, brackets are counted so it's possible
  1118. to have a $ifdef etc. in the macro }
  1119. bracketcount:=0;
  1120. repeat
  1121. case c of
  1122. '}' :
  1123. if (bracketcount=0) then
  1124. break
  1125. else
  1126. dec(bracketcount);
  1127. '{' :
  1128. inc(bracketcount);
  1129. #10,#13 :
  1130. current_scanner.linebreak;
  1131. #26 :
  1132. current_scanner.end_of_file;
  1133. end;
  1134. macrobuffer^[macropos]:=c;
  1135. inc(macropos);
  1136. if macropos>=maxmacrolen then
  1137. Message(scan_f_macro_buffer_overflow);
  1138. current_scanner.readchar;
  1139. until false;
  1140. { free buffer of macro ?}
  1141. if assigned(mac.buftext) then
  1142. freemem(mac.buftext,mac.buflen);
  1143. { get new mem }
  1144. getmem(mac.buftext,macropos);
  1145. mac.buflen:=macropos;
  1146. { copy the text }
  1147. move(macrobuffer^,mac.buftext^,macropos);
  1148. dispose(macrobuffer);
  1149. end
  1150. else
  1151. begin
  1152. { check if there is an assignment, then we need to give a
  1153. warning }
  1154. current_scanner.skipspace;
  1155. if c=':' then
  1156. begin
  1157. current_scanner.readchar;
  1158. if c='=' then
  1159. Message(scan_w_macro_support_turned_off);
  1160. end;
  1161. end;
  1162. end;
  1163. procedure dir_define;
  1164. begin
  1165. dir_define_impl(false);
  1166. end;
  1167. procedure dir_definec;
  1168. begin
  1169. dir_define_impl(true);
  1170. end;
  1171. procedure dir_setc;
  1172. var
  1173. hs : string;
  1174. mac : tmacro;
  1175. exprType: TCTETypeSet;
  1176. l : longint;
  1177. w : integer;
  1178. begin
  1179. current_scanner.skipspace;
  1180. hs:=current_scanner.readid;
  1181. mac:=tmacro(search_macro(hs));
  1182. if not assigned(mac) or (mac.owner <> macrosymtablestack) then
  1183. begin
  1184. mac:=tmacro.create(hs);
  1185. mac.defined:=true;
  1186. mac.is_compiler_var:=true;
  1187. Message1(parser_c_macro_defined,mac.name);
  1188. macrosymtablestack.insert(mac);
  1189. end
  1190. else
  1191. begin
  1192. mac.defined:=true;
  1193. mac.is_compiler_var:=true;
  1194. { delete old definition }
  1195. if assigned(mac.buftext) then
  1196. begin
  1197. freemem(mac.buftext,mac.buflen);
  1198. mac.buftext:=nil;
  1199. end;
  1200. end;
  1201. mac.is_used:=true;
  1202. { key words are never substituted }
  1203. if is_keyword(hs) then
  1204. Message(scan_e_keyword_cant_be_a_macro);
  1205. { !!!!!! handle macro params, need we this? }
  1206. current_scanner.skipspace;
  1207. { may be a macro? }
  1208. { assignment can be both := and = }
  1209. if c=':' then
  1210. current_scanner.readchar;
  1211. if c='=' then
  1212. begin
  1213. current_scanner.readchar;
  1214. hs:= parse_compiler_expr(exprType);
  1215. if (exprType * [ctetBoolean, ctetInteger]) = [] then
  1216. CTEError(exprType, [ctetBoolean, ctetInteger], 'SETC');
  1217. if length(hs) <> 0 then
  1218. begin
  1219. {If we are absolutely shure it is boolean, translate
  1220. to TRUE/FALSE to increase possibility to do future type check}
  1221. if exprType = [ctetBoolean] then
  1222. begin
  1223. val(hs,l,w);
  1224. if l<>0 then
  1225. hs:='TRUE'
  1226. else
  1227. hs:='FALSE';
  1228. end;
  1229. Message2(parser_c_macro_set_to,mac.name,hs);
  1230. { free buffer of macro ?}
  1231. if assigned(mac.buftext) then
  1232. freemem(mac.buftext,mac.buflen);
  1233. { get new mem }
  1234. getmem(mac.buftext,length(hs));
  1235. mac.buflen:=length(hs);
  1236. { copy the text }
  1237. move(hs[1],mac.buftext^,mac.buflen);
  1238. end
  1239. else
  1240. Message(scan_e_preproc_syntax_error);
  1241. end
  1242. else
  1243. Message(scan_e_preproc_syntax_error);
  1244. end;
  1245. procedure dir_undef;
  1246. var
  1247. hs : string;
  1248. mac : tmacro;
  1249. begin
  1250. current_scanner.skipspace;
  1251. hs:=current_scanner.readid;
  1252. mac:=tmacro(search_macro(hs));
  1253. if not assigned(mac) or (mac.owner <> macrosymtablestack) then
  1254. begin
  1255. mac:=tmacro.create(hs);
  1256. Message1(parser_c_macro_undefined,mac.name);
  1257. mac.defined:=false;
  1258. macrosymtablestack.insert(mac);
  1259. end
  1260. else
  1261. begin
  1262. Message1(parser_c_macro_undefined,mac.name);
  1263. mac.defined:=false;
  1264. mac.is_compiler_var:=false;
  1265. { delete old definition }
  1266. if assigned(mac.buftext) then
  1267. begin
  1268. freemem(mac.buftext,mac.buflen);
  1269. mac.buftext:=nil;
  1270. end;
  1271. end;
  1272. mac.is_used:=true;
  1273. end;
  1274. procedure dir_include;
  1275. function findincludefile(const path,name,ext:string;var foundfile:string):boolean;
  1276. var
  1277. found : boolean;
  1278. hpath : string;
  1279. begin
  1280. (* look for the include file
  1281. If path was specified as part of {$I } then
  1282. 1. specified path (expanded with path of inputfile if relative)
  1283. else
  1284. 1. path of current inputfile,current dir
  1285. 2. local includepath
  1286. 3. global includepath *)
  1287. found:=false;
  1288. foundfile:='';
  1289. hpath:='';
  1290. if path<>'' then
  1291. begin
  1292. if not path_absolute(path) then
  1293. hpath:=current_scanner.inputfile.path^+path
  1294. else
  1295. hpath:=path;
  1296. found:=FindFile(name+ext, hpath,foundfile);
  1297. end
  1298. else
  1299. begin
  1300. hpath:=current_scanner.inputfile.path^+';'+CurDirRelPath(source_info);
  1301. found:=FindFile(name+ext, hpath,foundfile);
  1302. if not found then
  1303. found:=current_module.localincludesearchpath.FindFile(name+ext,foundfile);
  1304. if not found then
  1305. found:=includesearchpath.FindFile(name+ext,foundfile);
  1306. end;
  1307. findincludefile:=found;
  1308. end;
  1309. var
  1310. args,
  1311. foundfile,
  1312. hs : string;
  1313. path : dirstr;
  1314. name : namestr;
  1315. ext : extstr;
  1316. hp : tinputfile;
  1317. found : boolean;
  1318. begin
  1319. current_scanner.skipspace;
  1320. args:=current_scanner.readcomment;
  1321. hs:=GetToken(args,' ');
  1322. if hs='' then
  1323. exit;
  1324. if (hs[1]='%') then
  1325. begin
  1326. { case insensitive }
  1327. hs:=upper(hs);
  1328. { remove %'s }
  1329. Delete(hs,1,1);
  1330. if hs[length(hs)]='%' then
  1331. Delete(hs,length(hs),1);
  1332. { save old }
  1333. path:=hs;
  1334. { first check for internal macros }
  1335. if hs='TIME' then
  1336. hs:=gettimestr
  1337. else
  1338. if hs='DATE' then
  1339. hs:=getdatestr
  1340. else
  1341. if hs='FILE' then
  1342. hs:=current_module.sourcefiles.get_file_name(aktfilepos.fileindex)
  1343. else
  1344. if hs='LINE' then
  1345. hs:=tostr(aktfilepos.line)
  1346. else
  1347. if hs='FPCVERSION' then
  1348. hs:=version_string
  1349. else
  1350. if hs='FPCDATE' then
  1351. hs:=date_string
  1352. else
  1353. if hs='FPCTARGET' then
  1354. hs:=target_cpu_string
  1355. else
  1356. if hs='FPCTARGETCPU' then
  1357. hs:=target_cpu_string
  1358. else
  1359. if hs='FPCTARGETOS' then
  1360. hs:=target_info.shortname
  1361. else
  1362. hs:=getenv(hs);
  1363. if hs='' then
  1364. Message1(scan_w_include_env_not_found,path);
  1365. { make it a stringconst }
  1366. hs:=''''+hs+'''';
  1367. current_scanner.insertmacro(path,@hs[1],length(hs),
  1368. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1369. end
  1370. else
  1371. begin
  1372. hs:=FixFileName(hs);
  1373. fsplit(hs,path,name,ext);
  1374. { try to find the file }
  1375. found:=findincludefile(path,name,ext,foundfile);
  1376. if (ext='') then
  1377. begin
  1378. { try default extensions .inc , .pp and .pas }
  1379. if (not found) then
  1380. found:=findincludefile(path,name,'.inc',foundfile);
  1381. if (not found) then
  1382. found:=findincludefile(path,name,sourceext,foundfile);
  1383. if (not found) then
  1384. found:=findincludefile(path,name,pasext,foundfile);
  1385. end;
  1386. if current_scanner.inputfilecount<max_include_nesting then
  1387. begin
  1388. inc(current_scanner.inputfilecount);
  1389. { we need to reread the current char }
  1390. dec(current_scanner.inputpointer);
  1391. { shutdown current file }
  1392. current_scanner.tempcloseinputfile;
  1393. { load new file }
  1394. hp:=do_openinputfile(foundfile);
  1395. current_scanner.addfile(hp);
  1396. current_module.sourcefiles.register_file(hp);
  1397. if (not found) then
  1398. Message1(scan_f_cannot_open_includefile,hs);
  1399. if (not current_scanner.openinputfile) then
  1400. Message1(scan_f_cannot_open_includefile,hs);
  1401. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  1402. current_scanner.reload;
  1403. end
  1404. else
  1405. Message(scan_f_include_deep_ten);
  1406. end;
  1407. end;
  1408. {*****************************************************************************
  1409. Preprocessor writting
  1410. *****************************************************************************}
  1411. {$ifdef PREPROCWRITE}
  1412. constructor tpreprocfile.create(const fn:string);
  1413. begin
  1414. { open outputfile }
  1415. assign(f,fn);
  1416. {$I-}
  1417. rewrite(f);
  1418. {$I+}
  1419. if ioresult<>0 then
  1420. Comment(V_Fatal,'can''t create file '+fn);
  1421. getmem(buf,preprocbufsize);
  1422. settextbuf(f,buf^,preprocbufsize);
  1423. { reset }
  1424. eolfound:=false;
  1425. spacefound:=false;
  1426. end;
  1427. destructor tpreprocfile.destroy;
  1428. begin
  1429. close(f);
  1430. freemem(buf,preprocbufsize);
  1431. end;
  1432. procedure tpreprocfile.add(const s:string);
  1433. begin
  1434. write(f,s);
  1435. end;
  1436. procedure tpreprocfile.addspace;
  1437. begin
  1438. if eolfound then
  1439. begin
  1440. writeln(f,'');
  1441. eolfound:=false;
  1442. spacefound:=false;
  1443. end
  1444. else
  1445. if spacefound then
  1446. begin
  1447. write(f,' ');
  1448. spacefound:=false;
  1449. end;
  1450. end;
  1451. {$endif PREPROCWRITE}
  1452. {*****************************************************************************
  1453. TPreProcStack
  1454. *****************************************************************************}
  1455. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1456. begin
  1457. accept:=a;
  1458. typ:=atyp;
  1459. next:=n;
  1460. end;
  1461. {*****************************************************************************
  1462. TDirectiveItem
  1463. *****************************************************************************}
  1464. constructor TDirectiveItem.Create(const n:string;p:tdirectiveproc);
  1465. begin
  1466. inherited CreateName(n);
  1467. is_conditional:=false;
  1468. proc:=p;
  1469. end;
  1470. constructor TDirectiveItem.CreateCond(const n:string;p:tdirectiveproc);
  1471. begin
  1472. inherited CreateName(n);
  1473. is_conditional:=true;
  1474. proc:=p;
  1475. end;
  1476. {****************************************************************************
  1477. TSCANNERFILE
  1478. ****************************************************************************}
  1479. constructor tscannerfile.create(const fn:string);
  1480. begin
  1481. inputfile:=do_openinputfile(fn);
  1482. if assigned(current_module) then
  1483. current_module.sourcefiles.register_file(inputfile);
  1484. { reset localinput }
  1485. inputbuffer:=nil;
  1486. inputpointer:=nil;
  1487. inputstart:=0;
  1488. { reset scanner }
  1489. preprocstack:=nil;
  1490. comment_level:=0;
  1491. yylexcount:=0;
  1492. block_type:=bt_general;
  1493. line_no:=0;
  1494. lastlinepos:=0;
  1495. lasttokenpos:=0;
  1496. lasttoken:=NOTOKEN;
  1497. nexttoken:=NOTOKEN;
  1498. lastasmgetchar:=#0;
  1499. ignoredirectives:=TStringList.Create;
  1500. in_asm_string:=false;
  1501. end;
  1502. procedure tscannerfile.firstfile;
  1503. begin
  1504. { load block }
  1505. if not openinputfile then
  1506. Message1(scan_f_cannot_open_input,inputfile.name^);
  1507. reload;
  1508. end;
  1509. destructor tscannerfile.destroy;
  1510. begin
  1511. if assigned(current_module) and
  1512. (current_module.state=ms_compiled) and
  1513. (status.errorcount=0) then
  1514. checkpreprocstack
  1515. else
  1516. begin
  1517. while assigned(preprocstack) do
  1518. poppreprocstack;
  1519. end;
  1520. if not inputfile.closed then
  1521. closeinputfile;
  1522. ignoredirectives.free;
  1523. end;
  1524. function tscannerfile.openinputfile:boolean;
  1525. begin
  1526. openinputfile:=inputfile.open;
  1527. { load buffer }
  1528. inputbuffer:=inputfile.buf;
  1529. inputpointer:=inputfile.buf;
  1530. inputstart:=inputfile.bufstart;
  1531. { line }
  1532. line_no:=0;
  1533. lastlinepos:=0;
  1534. lasttokenpos:=0;
  1535. end;
  1536. procedure tscannerfile.closeinputfile;
  1537. begin
  1538. inputfile.close;
  1539. { reset buffer }
  1540. inputbuffer:=nil;
  1541. inputpointer:=nil;
  1542. inputstart:=0;
  1543. { reset line }
  1544. line_no:=0;
  1545. lastlinepos:=0;
  1546. lasttokenpos:=0;
  1547. end;
  1548. function tscannerfile.tempopeninputfile:boolean;
  1549. begin
  1550. if inputfile.is_macro then
  1551. exit;
  1552. tempopeninputfile:=inputfile.tempopen;
  1553. { reload buffer }
  1554. inputbuffer:=inputfile.buf;
  1555. inputpointer:=inputfile.buf;
  1556. inputstart:=inputfile.bufstart;
  1557. end;
  1558. procedure tscannerfile.tempcloseinputfile;
  1559. begin
  1560. if inputfile.closed or inputfile.is_macro then
  1561. exit;
  1562. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1563. inputfile.tempclose;
  1564. { reset buffer }
  1565. inputbuffer:=nil;
  1566. inputpointer:=nil;
  1567. inputstart:=0;
  1568. end;
  1569. procedure tscannerfile.saveinputfile;
  1570. begin
  1571. inputfile.saveinputpointer:=inputpointer;
  1572. inputfile.savelastlinepos:=lastlinepos;
  1573. inputfile.saveline_no:=line_no;
  1574. end;
  1575. procedure tscannerfile.restoreinputfile;
  1576. begin
  1577. inputpointer:=inputfile.saveinputpointer;
  1578. lastlinepos:=inputfile.savelastlinepos;
  1579. line_no:=inputfile.saveline_no;
  1580. if not inputfile.is_macro then
  1581. parser_current_file:=inputfile.name^;
  1582. end;
  1583. procedure tscannerfile.nextfile;
  1584. var
  1585. to_dispose : tinputfile;
  1586. begin
  1587. if assigned(inputfile.next) then
  1588. begin
  1589. if inputfile.is_macro then
  1590. to_dispose:=inputfile
  1591. else
  1592. begin
  1593. to_dispose:=nil;
  1594. dec(inputfilecount);
  1595. end;
  1596. { we can allways close the file, no ? }
  1597. inputfile.close;
  1598. inputfile:=inputfile.next;
  1599. if assigned(to_dispose) then
  1600. to_dispose.free;
  1601. restoreinputfile;
  1602. end;
  1603. end;
  1604. procedure tscannerfile.addfile(hp:tinputfile);
  1605. begin
  1606. saveinputfile;
  1607. { add to list }
  1608. hp.next:=inputfile;
  1609. inputfile:=hp;
  1610. { load new inputfile }
  1611. restoreinputfile;
  1612. end;
  1613. procedure tscannerfile.reload;
  1614. begin
  1615. with inputfile do
  1616. begin
  1617. { when nothing more to read then leave immediatly, so we
  1618. don't change the aktfilepos and leave it point to the last
  1619. char }
  1620. if (c=#26) and (not assigned(next)) then
  1621. exit;
  1622. repeat
  1623. { still more to read?, then change the #0 to a space so its seen
  1624. as a seperator, this can't be used for macro's which can change
  1625. the place of the #0 in the buffer with tempopen }
  1626. if (c=#0) and (bufsize>0) and
  1627. not(inputfile.is_macro) and
  1628. (inputpointer-inputbuffer<bufsize) then
  1629. begin
  1630. c:=' ';
  1631. inc(inputpointer);
  1632. exit;
  1633. end;
  1634. { can we read more from this file ? }
  1635. if (c<>#26) and (not endoffile) then
  1636. begin
  1637. readbuf;
  1638. inputpointer:=buf;
  1639. inputbuffer:=buf;
  1640. inputstart:=bufstart;
  1641. { first line? }
  1642. if line_no=0 then
  1643. begin
  1644. c:=inputpointer^;
  1645. { eat utf-8 signature? }
  1646. if (ord(inputpointer^)=$ef) and
  1647. (ord((inputpointer+1)^)=$bb) and
  1648. (ord((inputpointer+2)^)=$bf) then
  1649. begin
  1650. inc(inputpointer,3);
  1651. message(scan_c_switching_to_utf8);
  1652. aktsourcecodepage:='utf8';
  1653. end;
  1654. line_no:=1;
  1655. if cs_asm_source in aktglobalswitches then
  1656. inputfile.setline(line_no,bufstart);
  1657. end;
  1658. end
  1659. else
  1660. begin
  1661. { load eof position in tokenpos/aktfilepos }
  1662. gettokenpos;
  1663. { close file }
  1664. closeinputfile;
  1665. { no next module, than EOF }
  1666. if not assigned(inputfile.next) then
  1667. begin
  1668. c:=#26;
  1669. exit;
  1670. end;
  1671. { load next file and reopen it }
  1672. nextfile;
  1673. tempopeninputfile;
  1674. { status }
  1675. Message1(scan_t_back_in,inputfile.name^);
  1676. end;
  1677. { load next char }
  1678. c:=inputpointer^;
  1679. inc(inputpointer);
  1680. until c<>#0; { if also end, then reload again }
  1681. end;
  1682. end;
  1683. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  1684. var
  1685. hp : tinputfile;
  1686. begin
  1687. { save old postion }
  1688. dec(inputpointer);
  1689. tempcloseinputfile;
  1690. { create macro 'file' }
  1691. { use special name to dispose after !! }
  1692. hp:=do_openinputfile('_Macro_.'+macname);
  1693. addfile(hp);
  1694. with inputfile do
  1695. begin
  1696. setmacro(p,len);
  1697. { local buffer }
  1698. inputbuffer:=buf;
  1699. inputpointer:=buf;
  1700. inputstart:=bufstart;
  1701. ref_index:=fileindex;
  1702. end;
  1703. { reset line }
  1704. line_no:=line;
  1705. lastlinepos:=0;
  1706. lasttokenpos:=0;
  1707. { load new c }
  1708. c:=inputpointer^;
  1709. inc(inputpointer);
  1710. end;
  1711. procedure tscannerfile.gettokenpos;
  1712. { load the values of tokenpos and lasttokenpos }
  1713. begin
  1714. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1715. akttokenpos.line:=line_no;
  1716. akttokenpos.column:=lasttokenpos-lastlinepos;
  1717. akttokenpos.fileindex:=inputfile.ref_index;
  1718. aktfilepos:=akttokenpos;
  1719. end;
  1720. procedure tscannerfile.inc_comment_level;
  1721. var
  1722. oldaktfilepos : tfileposinfo;
  1723. begin
  1724. if (m_nested_comment in aktmodeswitches) then
  1725. inc(comment_level)
  1726. else
  1727. comment_level:=1;
  1728. if (comment_level>1) then
  1729. begin
  1730. oldaktfilepos:=aktfilepos;
  1731. gettokenpos; { update for warning }
  1732. Message1(scan_w_comment_level,tostr(comment_level));
  1733. aktfilepos:=oldaktfilepos;
  1734. end;
  1735. end;
  1736. procedure tscannerfile.dec_comment_level;
  1737. begin
  1738. if (m_nested_comment in aktmodeswitches) then
  1739. dec(comment_level)
  1740. else
  1741. comment_level:=0;
  1742. end;
  1743. procedure tscannerfile.linebreak;
  1744. var
  1745. cur : char;
  1746. oldtokenpos,
  1747. oldaktfilepos : tfileposinfo;
  1748. begin
  1749. with inputfile do
  1750. begin
  1751. if (byte(inputpointer^)=0) and not(endoffile) then
  1752. begin
  1753. cur:=c;
  1754. reload;
  1755. if byte(cur)+byte(c)<>23 then
  1756. dec(inputpointer);
  1757. end
  1758. else
  1759. begin
  1760. { Support all combination of #10 and #13 as line break }
  1761. if (byte(inputpointer^)+byte(c)=23) then
  1762. inc(inputpointer);
  1763. end;
  1764. { Always return #10 as line break }
  1765. c:=#10;
  1766. { increase line counters }
  1767. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1768. inc(line_no);
  1769. { update linebuffer }
  1770. if cs_asm_source in aktglobalswitches then
  1771. inputfile.setline(line_no,lastlinepos);
  1772. { update for status and call the show status routine,
  1773. but don't touch aktfilepos ! }
  1774. oldaktfilepos:=aktfilepos;
  1775. oldtokenpos:=akttokenpos;
  1776. gettokenpos; { update for v_status }
  1777. inc(status.compiledlines);
  1778. ShowStatus;
  1779. aktfilepos:=oldaktfilepos;
  1780. akttokenpos:=oldtokenpos;
  1781. end;
  1782. end;
  1783. procedure tscannerfile.illegal_char(c:char);
  1784. var
  1785. s : string;
  1786. begin
  1787. if c in [#32..#255] then
  1788. s:=''''+c+''''
  1789. else
  1790. s:='#'+tostr(ord(c));
  1791. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  1792. end;
  1793. procedure tscannerfile.end_of_file;
  1794. begin
  1795. checkpreprocstack;
  1796. Message(scan_f_end_of_file);
  1797. end;
  1798. {-------------------------------------------
  1799. IF Conditional Handling
  1800. -------------------------------------------}
  1801. procedure tscannerfile.checkpreprocstack;
  1802. begin
  1803. { check for missing ifdefs }
  1804. while assigned(preprocstack) do
  1805. begin
  1806. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  1807. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  1808. poppreprocstack;
  1809. end;
  1810. end;
  1811. procedure tscannerfile.poppreprocstack;
  1812. var
  1813. hp : tpreprocstack;
  1814. begin
  1815. if assigned(preprocstack) then
  1816. begin
  1817. Message1(scan_c_endif_found,preprocstack.name);
  1818. hp:=preprocstack.next;
  1819. preprocstack.free;
  1820. preprocstack:=hp;
  1821. end
  1822. else
  1823. Message(scan_e_endif_without_if);
  1824. end;
  1825. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  1826. var
  1827. condition: Boolean;
  1828. valuedescr: String;
  1829. begin
  1830. if (preprocstack=nil) or preprocstack.accept then
  1831. condition:= compile_time_predicate(valuedescr)
  1832. else
  1833. begin
  1834. condition:= false;
  1835. valuedescr:= '';
  1836. end;
  1837. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  1838. preprocstack.name:=valuedescr;
  1839. preprocstack.line_nb:=line_no;
  1840. preprocstack.owner:=self;
  1841. if preprocstack.accept then
  1842. Message2(messid,preprocstack.name,'accepted')
  1843. else
  1844. Message2(messid,preprocstack.name,'rejected');
  1845. end;
  1846. procedure tscannerfile.elsepreprocstack;
  1847. begin
  1848. if assigned(preprocstack) and
  1849. (preprocstack.typ<>pp_else) then
  1850. begin
  1851. if (preprocstack.typ=pp_elseif) then
  1852. preprocstack.accept:=false
  1853. else
  1854. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  1855. preprocstack.accept:=not preprocstack.accept;
  1856. preprocstack.typ:=pp_else;
  1857. preprocstack.line_nb:=line_no;
  1858. if preprocstack.accept then
  1859. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1860. else
  1861. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1862. end
  1863. else
  1864. Message(scan_e_endif_without_if);
  1865. end;
  1866. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  1867. var
  1868. valuedescr: String;
  1869. begin
  1870. if assigned(preprocstack) and
  1871. (preprocstack.typ in [pp_if,pp_elseif]) then
  1872. begin
  1873. { when the branch is accepted we use pp_elseif so we know that
  1874. all the next branches need to be rejected. when this branch is still
  1875. not accepted then leave it at pp_if }
  1876. if (preprocstack.typ=pp_elseif) then
  1877. preprocstack.accept:=false
  1878. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  1879. begin
  1880. preprocstack.accept:=false;
  1881. preprocstack.typ:=pp_elseif;
  1882. end
  1883. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  1884. and compile_time_predicate(valuedescr) then
  1885. begin
  1886. preprocstack.name:=valuedescr;
  1887. preprocstack.accept:=true;
  1888. preprocstack.typ:=pp_elseif;
  1889. end;
  1890. preprocstack.line_nb:=line_no;
  1891. if preprocstack.accept then
  1892. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1893. else
  1894. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1895. end
  1896. else
  1897. Message(scan_e_endif_without_if);
  1898. end;
  1899. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  1900. var
  1901. oldaktfilepos : tfileposinfo;
  1902. begin
  1903. oldaktfilepos:=aktfilepos;
  1904. repeat
  1905. current_scanner.gettokenpos;
  1906. p.proc();
  1907. { accept the text ? }
  1908. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  1909. break
  1910. else
  1911. begin
  1912. current_scanner.gettokenpos;
  1913. Message(scan_c_skipping_until);
  1914. repeat
  1915. current_scanner.skipuntildirective;
  1916. if not (m_mac in aktmodeswitches) then
  1917. p:=tdirectiveitem(turbo_scannerdirectives.search(current_scanner.readid))
  1918. else
  1919. p:=tdirectiveitem(mac_scannerdirectives.search(current_scanner.readid));
  1920. until assigned(p) and (p.is_conditional);
  1921. current_scanner.gettokenpos;
  1922. Message1(scan_d_handling_switch,'$'+p.name);
  1923. end;
  1924. until false;
  1925. aktfilepos:=oldaktfilepos;
  1926. end;
  1927. procedure tscannerfile.handledirectives;
  1928. var
  1929. t : tdirectiveitem;
  1930. hs : string;
  1931. begin
  1932. gettokenpos;
  1933. readchar; {Remove the $}
  1934. hs:=readid;
  1935. {$ifdef PREPROCWRITE}
  1936. if parapreprocess then
  1937. begin
  1938. t:=Get_Directive(hs);
  1939. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  1940. begin
  1941. preprocfile^.AddSpace;
  1942. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  1943. exit;
  1944. end;
  1945. end;
  1946. {$endif PREPROCWRITE}
  1947. { skip this directive? }
  1948. if (ignoredirectives.find(hs)<>nil) then
  1949. begin
  1950. if (comment_level>0) then
  1951. readcomment;
  1952. { we've read the whole comment }
  1953. aktcommentstyle:=comment_none;
  1954. exit;
  1955. end;
  1956. if hs='' then
  1957. begin
  1958. Message1(scan_w_illegal_switch,'$'+hs);
  1959. end;
  1960. { Check for compiler switches }
  1961. while (length(hs)=1) and (c in ['-','+']) do
  1962. begin
  1963. HandleSwitch(hs[1],c);
  1964. current_scanner.readchar; {Remove + or -}
  1965. if c=',' then
  1966. begin
  1967. current_scanner.readchar; {Remove , }
  1968. { read next switch, support $v+,$+}
  1969. hs:=current_scanner.readid;
  1970. if (hs='') then
  1971. begin
  1972. if (c='$') and (m_fpc in aktmodeswitches) then
  1973. begin
  1974. current_scanner.readchar; { skip $ }
  1975. hs:=current_scanner.readid;
  1976. end;
  1977. if (hs='') then
  1978. Message1(scan_w_illegal_directive,'$'+c);
  1979. end
  1980. else
  1981. Message1(scan_d_handling_switch,'$'+hs);
  1982. end
  1983. else
  1984. hs:='';
  1985. end;
  1986. { directives may follow switches after a , }
  1987. if hs<>'' then
  1988. begin
  1989. if not (m_mac in aktmodeswitches) then
  1990. t:=tdirectiveitem(turbo_scannerdirectives.search(hs))
  1991. else
  1992. t:=tdirectiveitem(mac_scannerdirectives.search(hs));
  1993. if assigned(t) then
  1994. begin
  1995. if t.is_conditional then
  1996. handleconditional(t)
  1997. else
  1998. begin
  1999. Message1(scan_d_handling_switch,'$'+hs);
  2000. t.proc();
  2001. end;
  2002. end
  2003. else
  2004. begin
  2005. current_scanner.ignoredirectives.insert(hs);
  2006. Message1(scan_w_illegal_directive,'$'+hs);
  2007. end;
  2008. { conditionals already read the comment }
  2009. if (current_scanner.comment_level>0) then
  2010. current_scanner.readcomment;
  2011. { we've read the whole comment }
  2012. aktcommentstyle:=comment_none;
  2013. end;
  2014. end;
  2015. procedure tscannerfile.readchar;
  2016. begin
  2017. c:=inputpointer^;
  2018. if c=#0 then
  2019. reload
  2020. else
  2021. inc(inputpointer);
  2022. end;
  2023. procedure tscannerfile.readstring;
  2024. var
  2025. i : longint;
  2026. err : boolean;
  2027. begin
  2028. err:=false;
  2029. i:=0;
  2030. repeat
  2031. case c of
  2032. '_',
  2033. '0'..'9',
  2034. 'A'..'Z' :
  2035. begin
  2036. if i<255 then
  2037. begin
  2038. inc(i);
  2039. orgpattern[i]:=c;
  2040. pattern[i]:=c;
  2041. end
  2042. else
  2043. begin
  2044. if not err then
  2045. begin
  2046. Message(scan_e_string_exceeds_255_chars);
  2047. err:=true;
  2048. end;
  2049. end;
  2050. c:=inputpointer^;
  2051. inc(inputpointer);
  2052. end;
  2053. 'a'..'z' :
  2054. begin
  2055. if i<255 then
  2056. begin
  2057. inc(i);
  2058. orgpattern[i]:=c;
  2059. pattern[i]:=chr(ord(c)-32)
  2060. end
  2061. else
  2062. begin
  2063. if not err then
  2064. begin
  2065. Message(scan_e_string_exceeds_255_chars);
  2066. err:=true;
  2067. end;
  2068. end;
  2069. c:=inputpointer^;
  2070. inc(inputpointer);
  2071. end;
  2072. #0 :
  2073. reload;
  2074. else
  2075. break;
  2076. end;
  2077. until false;
  2078. orgpattern[0]:=chr(i);
  2079. pattern[0]:=chr(i);
  2080. end;
  2081. procedure tscannerfile.readnumber;
  2082. var
  2083. base,
  2084. i : longint;
  2085. begin
  2086. case c of
  2087. '%' :
  2088. begin
  2089. readchar;
  2090. base:=2;
  2091. pattern[1]:='%';
  2092. i:=1;
  2093. end;
  2094. '&' :
  2095. begin
  2096. readchar;
  2097. base:=8;
  2098. pattern[1]:='&';
  2099. i:=1;
  2100. end;
  2101. '$' :
  2102. begin
  2103. readchar;
  2104. base:=16;
  2105. pattern[1]:='$';
  2106. i:=1;
  2107. end;
  2108. else
  2109. begin
  2110. base:=10;
  2111. i:=0;
  2112. end;
  2113. end;
  2114. while ((base>=10) and (c in ['0'..'9'])) or
  2115. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  2116. ((base=8) and (c in ['0'..'7'])) or
  2117. ((base=2) and (c in ['0'..'1'])) do
  2118. begin
  2119. if i<255 then
  2120. begin
  2121. inc(i);
  2122. pattern[i]:=c;
  2123. end;
  2124. readchar;
  2125. end;
  2126. pattern[0]:=chr(i);
  2127. end;
  2128. function tscannerfile.readid:string;
  2129. begin
  2130. readstring;
  2131. readid:=pattern;
  2132. end;
  2133. function tscannerfile.readval:longint;
  2134. var
  2135. l : longint;
  2136. w : integer;
  2137. begin
  2138. readnumber;
  2139. val(pattern,l,w);
  2140. readval:=l;
  2141. end;
  2142. function tscannerfile.readval_asstring:string;
  2143. begin
  2144. readnumber;
  2145. readval_asstring:=pattern;
  2146. end;
  2147. function tscannerfile.readcomment:string;
  2148. var
  2149. i : longint;
  2150. begin
  2151. i:=0;
  2152. repeat
  2153. case c of
  2154. '{' :
  2155. begin
  2156. if aktcommentstyle=comment_tp then
  2157. inc_comment_level;
  2158. end;
  2159. '}' :
  2160. begin
  2161. if aktcommentstyle=comment_tp then
  2162. begin
  2163. readchar;
  2164. dec_comment_level;
  2165. if comment_level=0 then
  2166. break
  2167. else
  2168. continue;
  2169. end;
  2170. end;
  2171. '*' :
  2172. begin
  2173. if aktcommentstyle=comment_oldtp then
  2174. begin
  2175. readchar;
  2176. if c=')' then
  2177. begin
  2178. readchar;
  2179. dec_comment_level;
  2180. break;
  2181. end
  2182. else
  2183. { Add both characters !!}
  2184. if (i<255) then
  2185. begin
  2186. inc(i);
  2187. readcomment[i]:='*';
  2188. if (i<255) then
  2189. begin
  2190. inc(i);
  2191. readcomment[i]:='*';
  2192. end;
  2193. end;
  2194. end
  2195. else
  2196. { Not old TP comment, so add...}
  2197. begin
  2198. if (i<255) then
  2199. begin
  2200. inc(i);
  2201. readcomment[i]:='*';
  2202. end;
  2203. end;
  2204. end;
  2205. #10,#13 :
  2206. linebreak;
  2207. #26 :
  2208. end_of_file;
  2209. else
  2210. begin
  2211. if (i<255) then
  2212. begin
  2213. inc(i);
  2214. readcomment[i]:=c;
  2215. end;
  2216. end;
  2217. end;
  2218. readchar;
  2219. until false;
  2220. readcomment[0]:=chr(i);
  2221. end;
  2222. function tscannerfile.readquotedstring:string;
  2223. var
  2224. i : longint;
  2225. msgwritten : boolean;
  2226. begin
  2227. i:=0;
  2228. msgwritten:=false;
  2229. if (c='''') then
  2230. begin
  2231. repeat
  2232. readchar;
  2233. case c of
  2234. #26 :
  2235. end_of_file;
  2236. #10,#13 :
  2237. Message(scan_f_string_exceeds_line);
  2238. '''' :
  2239. begin
  2240. readchar;
  2241. if c<>'''' then
  2242. break;
  2243. end;
  2244. end;
  2245. if i<255 then
  2246. begin
  2247. inc(i);
  2248. result[i]:=c;
  2249. end
  2250. else
  2251. begin
  2252. if not msgwritten then
  2253. begin
  2254. Message(scan_e_string_exceeds_255_chars);
  2255. msgwritten:=true;
  2256. end;
  2257. end;
  2258. until false;
  2259. end;
  2260. result[0]:=chr(i);
  2261. end;
  2262. function tscannerfile.readstate:char;
  2263. var
  2264. state : char;
  2265. begin
  2266. state:=' ';
  2267. if c=' ' then
  2268. begin
  2269. current_scanner.skipspace;
  2270. current_scanner.readid;
  2271. if pattern='ON' then
  2272. state:='+'
  2273. else
  2274. if pattern='OFF' then
  2275. state:='-';
  2276. end
  2277. else
  2278. state:=c;
  2279. if not (state in ['+','-']) then
  2280. Message(scan_e_wrong_switch_toggle);
  2281. readstate:=state;
  2282. end;
  2283. function tscannerfile.readstatedefault:char;
  2284. var
  2285. state : char;
  2286. begin
  2287. state:=' ';
  2288. if c=' ' then
  2289. begin
  2290. current_scanner.skipspace;
  2291. current_scanner.readid;
  2292. if pattern='ON' then
  2293. state:='+'
  2294. else
  2295. if pattern='OFF' then
  2296. state:='-'
  2297. else
  2298. if pattern='DEFAULT' then
  2299. state:='*';
  2300. end
  2301. else
  2302. state:=c;
  2303. if not (state in ['+','-','*']) then
  2304. Message(scan_e_wrong_switch_toggle_default);
  2305. readstatedefault:=state;
  2306. end;
  2307. procedure tscannerfile.skipspace;
  2308. begin
  2309. repeat
  2310. case c of
  2311. #26 :
  2312. begin
  2313. reload;
  2314. if (c=#26) and not assigned(inputfile.next) then
  2315. break;
  2316. continue;
  2317. end;
  2318. #10,
  2319. #13 :
  2320. linebreak;
  2321. #9,#11,#12,' ' :
  2322. ;
  2323. else
  2324. break;
  2325. end;
  2326. readchar;
  2327. until false;
  2328. end;
  2329. procedure tscannerfile.skipuntildirective;
  2330. var
  2331. found : longint;
  2332. next_char_loaded : boolean;
  2333. begin
  2334. found:=0;
  2335. next_char_loaded:=false;
  2336. repeat
  2337. case c of
  2338. #10,
  2339. #13 :
  2340. linebreak;
  2341. #26 :
  2342. begin
  2343. reload;
  2344. if (c=#26) and not assigned(inputfile.next) then
  2345. end_of_file;
  2346. continue;
  2347. end;
  2348. '{' :
  2349. begin
  2350. if (aktcommentstyle in [comment_tp,comment_none]) then
  2351. begin
  2352. aktcommentstyle:=comment_tp;
  2353. if (comment_level=0) then
  2354. found:=1;
  2355. inc_comment_level;
  2356. end;
  2357. end;
  2358. '*' :
  2359. begin
  2360. if (aktcommentstyle=comment_oldtp) then
  2361. begin
  2362. readchar;
  2363. if c=')' then
  2364. begin
  2365. dec_comment_level;
  2366. found:=0;
  2367. aktcommentstyle:=comment_none;
  2368. end
  2369. else
  2370. next_char_loaded:=true;
  2371. end
  2372. else
  2373. found := 0;
  2374. end;
  2375. '}' :
  2376. begin
  2377. if (aktcommentstyle=comment_tp) then
  2378. begin
  2379. dec_comment_level;
  2380. if (comment_level=0) then
  2381. aktcommentstyle:=comment_none;
  2382. found:=0;
  2383. end;
  2384. end;
  2385. '$' :
  2386. begin
  2387. if found=1 then
  2388. found:=2;
  2389. end;
  2390. '''' :
  2391. if (aktcommentstyle=comment_none) then
  2392. begin
  2393. repeat
  2394. readchar;
  2395. case c of
  2396. #26 :
  2397. end_of_file;
  2398. #10,#13 :
  2399. break;
  2400. '''' :
  2401. begin
  2402. readchar;
  2403. if c<>'''' then
  2404. begin
  2405. next_char_loaded:=true;
  2406. break;
  2407. end;
  2408. end;
  2409. end;
  2410. until false;
  2411. end;
  2412. '(' :
  2413. begin
  2414. if (aktcommentstyle=comment_none) then
  2415. begin
  2416. readchar;
  2417. if c='*' then
  2418. begin
  2419. readchar;
  2420. if c='$' then
  2421. begin
  2422. found:=2;
  2423. inc_comment_level;
  2424. aktcommentstyle:=comment_oldtp;
  2425. end
  2426. else
  2427. begin
  2428. skipoldtpcomment;
  2429. next_char_loaded:=true;
  2430. end;
  2431. end
  2432. else
  2433. next_char_loaded:=true;
  2434. end
  2435. else
  2436. found:=0;
  2437. end;
  2438. '/' :
  2439. begin
  2440. if (aktcommentstyle=comment_none) then
  2441. begin
  2442. readchar;
  2443. if c='/' then
  2444. skipdelphicomment;
  2445. next_char_loaded:=true;
  2446. end
  2447. else
  2448. found:=0;
  2449. end;
  2450. else
  2451. found:=0;
  2452. end;
  2453. if next_char_loaded then
  2454. next_char_loaded:=false
  2455. else
  2456. readchar;
  2457. until (found=2);
  2458. end;
  2459. {****************************************************************************
  2460. Comment Handling
  2461. ****************************************************************************}
  2462. procedure tscannerfile.skipcomment;
  2463. begin
  2464. aktcommentstyle:=comment_tp;
  2465. readchar;
  2466. inc_comment_level;
  2467. { handle compiler switches }
  2468. if (c='$') then
  2469. handledirectives;
  2470. { handle_switches can dec comment_level, }
  2471. while (comment_level>0) do
  2472. begin
  2473. case c of
  2474. '{' :
  2475. inc_comment_level;
  2476. '}' :
  2477. dec_comment_level;
  2478. #10,#13 :
  2479. linebreak;
  2480. #26 :
  2481. begin
  2482. reload;
  2483. if (c=#26) and not assigned(inputfile.next) then
  2484. end_of_file;
  2485. continue;
  2486. end;
  2487. end;
  2488. readchar;
  2489. end;
  2490. aktcommentstyle:=comment_none;
  2491. end;
  2492. procedure tscannerfile.skipdelphicomment;
  2493. begin
  2494. aktcommentstyle:=comment_delphi;
  2495. inc_comment_level;
  2496. readchar;
  2497. { this is not supported }
  2498. if c='$' then
  2499. Message(scan_w_wrong_styled_switch);
  2500. { skip comment }
  2501. while not (c in [#10,#13,#26]) do
  2502. readchar;
  2503. dec_comment_level;
  2504. aktcommentstyle:=comment_none;
  2505. end;
  2506. procedure tscannerfile.skipoldtpcomment;
  2507. var
  2508. found : longint;
  2509. begin
  2510. aktcommentstyle:=comment_oldtp;
  2511. inc_comment_level;
  2512. { only load a char if last already processed,
  2513. was cause of bug1634 PM }
  2514. if c=#0 then
  2515. readchar;
  2516. { this is now supported }
  2517. if (c='$') then
  2518. handledirectives;
  2519. { skip comment }
  2520. while (comment_level>0) do
  2521. begin
  2522. found:=0;
  2523. repeat
  2524. case c of
  2525. #26 :
  2526. begin
  2527. reload;
  2528. if (c=#26) and not assigned(inputfile.next) then
  2529. end_of_file;
  2530. continue;
  2531. end;
  2532. #10,#13 :
  2533. linebreak;
  2534. '*' :
  2535. begin
  2536. if found=3 then
  2537. found:=4
  2538. else
  2539. found:=1;
  2540. end;
  2541. ')' :
  2542. begin
  2543. if found in [1,4] then
  2544. begin
  2545. dec_comment_level;
  2546. if comment_level=0 then
  2547. found:=2
  2548. else
  2549. found:=0;
  2550. end;
  2551. end;
  2552. '(' :
  2553. begin
  2554. if found=4 then
  2555. inc_comment_level;
  2556. found:=3;
  2557. end;
  2558. else
  2559. begin
  2560. if found=4 then
  2561. inc_comment_level;
  2562. found:=0;
  2563. end;
  2564. end;
  2565. readchar;
  2566. until (found=2);
  2567. end;
  2568. aktcommentstyle:=comment_none;
  2569. end;
  2570. {****************************************************************************
  2571. Token Scanner
  2572. ****************************************************************************}
  2573. procedure tscannerfile.readtoken;
  2574. var
  2575. code : integer;
  2576. len,
  2577. low,high,mid : longint;
  2578. w : word;
  2579. m : longint;
  2580. mac : tmacro;
  2581. asciinr : string[6];
  2582. msgwritten,
  2583. iswidestring : boolean;
  2584. label
  2585. exit_label;
  2586. begin
  2587. if localswitcheschanged then
  2588. begin
  2589. aktlocalswitches:=nextaktlocalswitches;
  2590. localswitcheschanged:=false;
  2591. end;
  2592. { was there already a token read, then return that token }
  2593. if nexttoken<>NOTOKEN then
  2594. begin
  2595. token:=nexttoken;
  2596. nexttoken:=NOTOKEN;
  2597. goto exit_label;
  2598. end;
  2599. { Skip all spaces and comments }
  2600. repeat
  2601. case c of
  2602. '{' :
  2603. skipcomment;
  2604. #26 :
  2605. begin
  2606. reload;
  2607. if (c=#26) and not assigned(inputfile.next) then
  2608. break;
  2609. end;
  2610. ' ',#9..#13 :
  2611. begin
  2612. {$ifdef PREPROCWRITE}
  2613. if parapreprocess then
  2614. begin
  2615. if c=#10 then
  2616. preprocfile.eolfound:=true
  2617. else
  2618. preprocfile.spacefound:=true;
  2619. end;
  2620. {$endif PREPROCWRITE}
  2621. skipspace;
  2622. end
  2623. else
  2624. break;
  2625. end;
  2626. until false;
  2627. { Save current token position, for EOF its already loaded }
  2628. if c<>#26 then
  2629. gettokenpos;
  2630. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  2631. if c in ['A'..'Z','a'..'z','_'] then
  2632. begin
  2633. readstring;
  2634. token:=_ID;
  2635. idtoken:=_ID;
  2636. { keyword or any other known token,
  2637. pattern is always uppercased }
  2638. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  2639. begin
  2640. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  2641. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  2642. while low<high do
  2643. begin
  2644. mid:=(high+low+1) shr 1;
  2645. if pattern<tokeninfo^[ttoken(mid)].str then
  2646. high:=mid-1
  2647. else
  2648. low:=mid;
  2649. end;
  2650. with tokeninfo^[ttoken(high)] do
  2651. if pattern=str then
  2652. begin
  2653. if keyword in aktmodeswitches then
  2654. if op=NOTOKEN then
  2655. token:=ttoken(high)
  2656. else
  2657. token:=op;
  2658. idtoken:=ttoken(high);
  2659. end;
  2660. end;
  2661. { Only process identifiers and not keywords }
  2662. if token=_ID then
  2663. begin
  2664. { this takes some time ... }
  2665. if (cs_support_macro in aktmoduleswitches) then
  2666. begin
  2667. mac:=tmacro(search_macro(pattern));
  2668. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  2669. begin
  2670. if yylexcount<max_macro_nesting then
  2671. begin
  2672. mac.is_used:=true;
  2673. inc(yylexcount);
  2674. insertmacro(pattern,mac.buftext,mac.buflen,
  2675. mac.fileinfo.line,mac.fileinfo.fileindex);
  2676. { handle empty macros }
  2677. if c=#0 then
  2678. reload;
  2679. readtoken;
  2680. { that's all folks }
  2681. dec(yylexcount);
  2682. exit;
  2683. end
  2684. else
  2685. Message(scan_w_macro_too_deep);
  2686. end;
  2687. end;
  2688. end;
  2689. { return token }
  2690. goto exit_label;
  2691. end
  2692. else
  2693. begin
  2694. idtoken:=_NOID;
  2695. case c of
  2696. '$' :
  2697. begin
  2698. readnumber;
  2699. token:=_INTCONST;
  2700. goto exit_label;
  2701. end;
  2702. '%' :
  2703. begin
  2704. if not(m_fpc in aktmodeswitches) then
  2705. Illegal_Char(c)
  2706. else
  2707. begin
  2708. readnumber;
  2709. token:=_INTCONST;
  2710. goto exit_label;
  2711. end;
  2712. end;
  2713. '&' :
  2714. begin
  2715. if m_fpc in aktmodeswitches then
  2716. begin
  2717. readnumber;
  2718. token:=_INTCONST;
  2719. goto exit_label;
  2720. end
  2721. else if m_mac in aktmodeswitches then
  2722. begin
  2723. readchar;
  2724. token:=_AMPERSAND;
  2725. goto exit_label;
  2726. end
  2727. else
  2728. Illegal_Char(c);
  2729. end;
  2730. '0'..'9' :
  2731. begin
  2732. readnumber;
  2733. if (c in ['.','e','E']) then
  2734. begin
  2735. { first check for a . }
  2736. if c='.' then
  2737. begin
  2738. readchar;
  2739. { is it a .. from a range? }
  2740. case c of
  2741. '.' :
  2742. begin
  2743. readchar;
  2744. token:=_INTCONST;
  2745. nexttoken:=_POINTPOINT;
  2746. goto exit_label;
  2747. end;
  2748. ')' :
  2749. begin
  2750. readchar;
  2751. token:=_INTCONST;
  2752. nexttoken:=_RECKKLAMMER;
  2753. goto exit_label;
  2754. end;
  2755. end;
  2756. { insert the number after the . }
  2757. pattern:=pattern+'.';
  2758. while c in ['0'..'9'] do
  2759. begin
  2760. pattern:=pattern+c;
  2761. readchar;
  2762. end;
  2763. end;
  2764. { E can also follow after a point is scanned }
  2765. if c in ['e','E'] then
  2766. begin
  2767. pattern:=pattern+'E';
  2768. readchar;
  2769. if c in ['-','+'] then
  2770. begin
  2771. pattern:=pattern+c;
  2772. readchar;
  2773. end;
  2774. if not(c in ['0'..'9']) then
  2775. Illegal_Char(c);
  2776. while c in ['0'..'9'] do
  2777. begin
  2778. pattern:=pattern+c;
  2779. readchar;
  2780. end;
  2781. end;
  2782. token:=_REALNUMBER;
  2783. goto exit_label;
  2784. end;
  2785. token:=_INTCONST;
  2786. goto exit_label;
  2787. end;
  2788. ';' :
  2789. begin
  2790. readchar;
  2791. token:=_SEMICOLON;
  2792. goto exit_label;
  2793. end;
  2794. '[' :
  2795. begin
  2796. readchar;
  2797. token:=_LECKKLAMMER;
  2798. goto exit_label;
  2799. end;
  2800. ']' :
  2801. begin
  2802. readchar;
  2803. token:=_RECKKLAMMER;
  2804. goto exit_label;
  2805. end;
  2806. '(' :
  2807. begin
  2808. readchar;
  2809. case c of
  2810. '*' :
  2811. begin
  2812. c:=#0;{Signal skipoldtpcomment to reload a char }
  2813. skipoldtpcomment;
  2814. readtoken;
  2815. exit;
  2816. end;
  2817. '.' :
  2818. begin
  2819. readchar;
  2820. token:=_LECKKLAMMER;
  2821. goto exit_label;
  2822. end;
  2823. end;
  2824. token:=_LKLAMMER;
  2825. goto exit_label;
  2826. end;
  2827. ')' :
  2828. begin
  2829. readchar;
  2830. token:=_RKLAMMER;
  2831. goto exit_label;
  2832. end;
  2833. '+' :
  2834. begin
  2835. readchar;
  2836. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2837. begin
  2838. readchar;
  2839. token:=_PLUSASN;
  2840. goto exit_label;
  2841. end;
  2842. token:=_PLUS;
  2843. goto exit_label;
  2844. end;
  2845. '-' :
  2846. begin
  2847. readchar;
  2848. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2849. begin
  2850. readchar;
  2851. token:=_MINUSASN;
  2852. goto exit_label;
  2853. end;
  2854. token:=_MINUS;
  2855. goto exit_label;
  2856. end;
  2857. ':' :
  2858. begin
  2859. readchar;
  2860. if c='=' then
  2861. begin
  2862. readchar;
  2863. token:=_ASSIGNMENT;
  2864. goto exit_label;
  2865. end;
  2866. token:=_COLON;
  2867. goto exit_label;
  2868. end;
  2869. '*' :
  2870. begin
  2871. readchar;
  2872. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2873. begin
  2874. readchar;
  2875. token:=_STARASN;
  2876. end
  2877. else
  2878. if c='*' then
  2879. begin
  2880. readchar;
  2881. token:=_STARSTAR;
  2882. end
  2883. else
  2884. token:=_STAR;
  2885. goto exit_label;
  2886. end;
  2887. '/' :
  2888. begin
  2889. readchar;
  2890. case c of
  2891. '=' :
  2892. begin
  2893. if (cs_support_c_operators in aktmoduleswitches) then
  2894. begin
  2895. readchar;
  2896. token:=_SLASHASN;
  2897. goto exit_label;
  2898. end;
  2899. end;
  2900. '/' :
  2901. begin
  2902. skipdelphicomment;
  2903. readtoken;
  2904. exit;
  2905. end;
  2906. end;
  2907. token:=_SLASH;
  2908. goto exit_label;
  2909. end;
  2910. '|' :
  2911. if m_mac in aktmodeswitches then
  2912. begin
  2913. readchar;
  2914. token:=_PIPE;
  2915. goto exit_label;
  2916. end
  2917. else
  2918. Illegal_Char(c);
  2919. '=' :
  2920. begin
  2921. readchar;
  2922. token:=_EQUAL;
  2923. goto exit_label;
  2924. end;
  2925. '.' :
  2926. begin
  2927. readchar;
  2928. case c of
  2929. '.' :
  2930. begin
  2931. readchar;
  2932. case c of
  2933. '.' :
  2934. begin
  2935. readchar;
  2936. token:=_POINTPOINTPOINT;
  2937. goto exit_label;
  2938. end;
  2939. else
  2940. begin
  2941. token:=_POINTPOINT;
  2942. goto exit_label;
  2943. end;
  2944. end;
  2945. end;
  2946. ')' :
  2947. begin
  2948. readchar;
  2949. token:=_RECKKLAMMER;
  2950. goto exit_label;
  2951. end;
  2952. end;
  2953. token:=_POINT;
  2954. goto exit_label;
  2955. end;
  2956. '@' :
  2957. begin
  2958. readchar;
  2959. token:=_KLAMMERAFFE;
  2960. goto exit_label;
  2961. end;
  2962. ',' :
  2963. begin
  2964. readchar;
  2965. token:=_COMMA;
  2966. goto exit_label;
  2967. end;
  2968. '''','#','^' :
  2969. begin
  2970. len:=0;
  2971. msgwritten:=false;
  2972. pattern:='';
  2973. iswidestring:=false;
  2974. if c='^' then
  2975. begin
  2976. readchar;
  2977. c:=upcase(c);
  2978. if (block_type=bt_type) or
  2979. (lasttoken=_ID) or (lasttoken=_NIL) or
  2980. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  2981. begin
  2982. token:=_CARET;
  2983. goto exit_label;
  2984. end
  2985. else
  2986. begin
  2987. inc(len);
  2988. if c<#64 then
  2989. pattern[len]:=chr(ord(c)+64)
  2990. else
  2991. pattern[len]:=chr(ord(c)-64);
  2992. readchar;
  2993. end;
  2994. end;
  2995. repeat
  2996. case c of
  2997. '#' :
  2998. begin
  2999. readchar; { read # }
  3000. if c='$' then
  3001. begin
  3002. readchar; { read leading $ }
  3003. asciinr:='$';
  3004. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  3005. begin
  3006. asciinr:=asciinr+c;
  3007. readchar;
  3008. end;
  3009. end
  3010. else
  3011. begin
  3012. asciinr:='';
  3013. while (c in ['0'..'9']) and (length(asciinr)<6) do
  3014. begin
  3015. asciinr:=asciinr+c;
  3016. readchar;
  3017. end;
  3018. end;
  3019. val(asciinr,m,code);
  3020. if (asciinr='') or (code<>0) then
  3021. Message(scan_e_illegal_char_const)
  3022. else if (m<0) or (m>255) or (length(asciinr)>3) then
  3023. begin
  3024. if (m>=0) and (m<=65535) then
  3025. begin
  3026. if not iswidestring then
  3027. begin
  3028. ascii2unicode(@pattern[1],len,patternw);
  3029. iswidestring:=true;
  3030. len:=0;
  3031. end;
  3032. concatwidestringchar(patternw,tcompilerwidechar(m));
  3033. end
  3034. else
  3035. Message(scan_e_illegal_char_const)
  3036. end
  3037. else if iswidestring then
  3038. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  3039. else
  3040. begin
  3041. if len<255 then
  3042. begin
  3043. inc(len);
  3044. pattern[len]:=chr(m);
  3045. end
  3046. else
  3047. begin
  3048. if not msgwritten then
  3049. begin
  3050. Message(scan_e_string_exceeds_255_chars);
  3051. msgwritten:=true;
  3052. end;
  3053. end;
  3054. end;
  3055. end;
  3056. '''' :
  3057. begin
  3058. repeat
  3059. readchar;
  3060. case c of
  3061. #26 :
  3062. end_of_file;
  3063. #10,#13 :
  3064. Message(scan_f_string_exceeds_line);
  3065. '''' :
  3066. begin
  3067. readchar;
  3068. if c<>'''' then
  3069. break;
  3070. end;
  3071. end;
  3072. { interpret as utf-8 string? }
  3073. if (ord(c)>=$80) and (aktsourcecodepage='utf8') then
  3074. begin
  3075. { convert existing string to an utf-8 string }
  3076. if not iswidestring then
  3077. begin
  3078. ascii2unicode(@pattern[1],len,patternw);
  3079. iswidestring:=true;
  3080. len:=0;
  3081. end;
  3082. { four or more chars aren't handled }
  3083. if (ord(c) and $f0)=$f0 then
  3084. message(scan_e_utf8_bigger_than_65535)
  3085. { three chars }
  3086. else if (ord(c) and $e0)=$e0 then
  3087. begin
  3088. w:=ord(c) and $f;
  3089. readchar;
  3090. if (ord(c) and $c0)<>$80 then
  3091. message(scan_e_utf8_malformed);
  3092. w:=(w shl 6) or (ord(c) and $3f);
  3093. readchar;
  3094. if (ord(c) and $c0)<>$80 then
  3095. message(scan_e_utf8_malformed);
  3096. w:=(w shl 6) or (ord(c) and $3f);
  3097. concatwidestringchar(patternw,w);
  3098. end
  3099. { two chars }
  3100. else if (ord(c) and $c0)<>0 then
  3101. begin
  3102. w:=ord(c) and $1f;
  3103. readchar;
  3104. if (ord(c) and $c0)<>$80 then
  3105. message(scan_e_utf8_malformed);
  3106. w:=(w shl 6) or (ord(c) and $3f);
  3107. concatwidestringchar(patternw,w);
  3108. end
  3109. { illegal }
  3110. else if (ord(c) and $80)<>0 then
  3111. message(scan_e_utf8_malformed)
  3112. else
  3113. concatwidestringchar(patternw,tcompilerwidechar(c))
  3114. end
  3115. else if iswidestring then
  3116. begin
  3117. if aktsourcecodepage='utf8' then
  3118. concatwidestringchar(patternw,ord(c))
  3119. else
  3120. concatwidestringchar(patternw,asciichar2unicode(c))
  3121. end
  3122. else
  3123. begin
  3124. if len<255 then
  3125. begin
  3126. inc(len);
  3127. pattern[len]:=c;
  3128. end
  3129. else
  3130. begin
  3131. if not msgwritten then
  3132. begin
  3133. Message(scan_e_string_exceeds_255_chars);
  3134. msgwritten:=true;
  3135. end;
  3136. end;
  3137. end;
  3138. until false;
  3139. end;
  3140. '^' :
  3141. begin
  3142. readchar;
  3143. c:=upcase(c);
  3144. if c<#64 then
  3145. c:=chr(ord(c)+64)
  3146. else
  3147. c:=chr(ord(c)-64);
  3148. if iswidestring then
  3149. concatwidestringchar(patternw,asciichar2unicode(c))
  3150. else
  3151. begin
  3152. if len<255 then
  3153. begin
  3154. inc(len);
  3155. pattern[len]:=c;
  3156. end
  3157. else
  3158. begin
  3159. if not msgwritten then
  3160. begin
  3161. Message(scan_e_string_exceeds_255_chars);
  3162. msgwritten:=true;
  3163. end;
  3164. end;
  3165. end;
  3166. readchar;
  3167. end;
  3168. else
  3169. break;
  3170. end;
  3171. until false;
  3172. { strings with length 1 become const chars }
  3173. if iswidestring then
  3174. begin
  3175. if patternw^.len=1 then
  3176. token:=_CWCHAR
  3177. else
  3178. token:=_CWSTRING;
  3179. end
  3180. else
  3181. begin
  3182. pattern[0]:=chr(len);
  3183. if len=1 then
  3184. token:=_CCHAR
  3185. else
  3186. token:=_CSTRING;
  3187. end;
  3188. goto exit_label;
  3189. end;
  3190. '>' :
  3191. begin
  3192. readchar;
  3193. case c of
  3194. '=' :
  3195. begin
  3196. readchar;
  3197. token:=_GTE;
  3198. goto exit_label;
  3199. end;
  3200. '>' :
  3201. begin
  3202. readchar;
  3203. token:=_OP_SHR;
  3204. goto exit_label;
  3205. end;
  3206. '<' :
  3207. begin { >< is for a symetric diff for sets }
  3208. readchar;
  3209. token:=_SYMDIF;
  3210. goto exit_label;
  3211. end;
  3212. end;
  3213. token:=_GT;
  3214. goto exit_label;
  3215. end;
  3216. '<' :
  3217. begin
  3218. readchar;
  3219. case c of
  3220. '>' :
  3221. begin
  3222. readchar;
  3223. token:=_UNEQUAL;
  3224. goto exit_label;
  3225. end;
  3226. '=' :
  3227. begin
  3228. readchar;
  3229. token:=_LTE;
  3230. goto exit_label;
  3231. end;
  3232. '<' :
  3233. begin
  3234. readchar;
  3235. token:=_OP_SHL;
  3236. goto exit_label;
  3237. end;
  3238. end;
  3239. token:=_LT;
  3240. goto exit_label;
  3241. end;
  3242. #26 :
  3243. begin
  3244. token:=_EOF;
  3245. checkpreprocstack;
  3246. goto exit_label;
  3247. end;
  3248. else
  3249. Illegal_Char(c);
  3250. end;
  3251. end;
  3252. exit_label:
  3253. lasttoken:=token;
  3254. end;
  3255. function tscannerfile.readpreproc:ttoken;
  3256. begin
  3257. skipspace;
  3258. case c of
  3259. '_',
  3260. 'A'..'Z',
  3261. 'a'..'z' :
  3262. begin
  3263. current_scanner.preproc_pattern:=readid;
  3264. readpreproc:=_ID;
  3265. end;
  3266. '0'..'9' :
  3267. begin
  3268. current_scanner.preproc_pattern:=readval_asstring;
  3269. { realnumber? }
  3270. if c='.' then
  3271. begin
  3272. readchar;
  3273. while c in ['0'..'9'] do
  3274. begin
  3275. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  3276. readchar;
  3277. end;
  3278. end;
  3279. readpreproc:=_ID;
  3280. end;
  3281. '$','%','&' :
  3282. begin
  3283. current_scanner.preproc_pattern:=readval_asstring;
  3284. readpreproc:=_ID;
  3285. end;
  3286. ',' :
  3287. begin
  3288. readchar;
  3289. readpreproc:=_COMMA;
  3290. end;
  3291. '}' :
  3292. begin
  3293. readpreproc:=_END;
  3294. end;
  3295. '(' :
  3296. begin
  3297. readchar;
  3298. readpreproc:=_LKLAMMER;
  3299. end;
  3300. ')' :
  3301. begin
  3302. readchar;
  3303. readpreproc:=_RKLAMMER;
  3304. end;
  3305. '[' :
  3306. begin
  3307. readchar;
  3308. readpreproc:=_LECKKLAMMER;
  3309. end;
  3310. ']' :
  3311. begin
  3312. readchar;
  3313. readpreproc:=_RECKKLAMMER;
  3314. end;
  3315. '+' :
  3316. begin
  3317. readchar;
  3318. readpreproc:=_PLUS;
  3319. end;
  3320. '-' :
  3321. begin
  3322. readchar;
  3323. readpreproc:=_MINUS;
  3324. end;
  3325. '*' :
  3326. begin
  3327. readchar;
  3328. readpreproc:=_STAR;
  3329. end;
  3330. '/' :
  3331. begin
  3332. readchar;
  3333. readpreproc:=_SLASH;
  3334. end;
  3335. '=' :
  3336. begin
  3337. readchar;
  3338. readpreproc:=_EQUAL;
  3339. end;
  3340. '>' :
  3341. begin
  3342. readchar;
  3343. if c='=' then
  3344. begin
  3345. readchar;
  3346. readpreproc:=_GTE;
  3347. end
  3348. else
  3349. readpreproc:=_GT;
  3350. end;
  3351. '<' :
  3352. begin
  3353. readchar;
  3354. case c of
  3355. '>' :
  3356. begin
  3357. readchar;
  3358. readpreproc:=_UNEQUAL;
  3359. end;
  3360. '=' :
  3361. begin
  3362. readchar;
  3363. readpreproc:=_LTE;
  3364. end;
  3365. else
  3366. readpreproc:=_LT;
  3367. end;
  3368. end;
  3369. #26 :
  3370. begin
  3371. readpreproc:=_EOF;
  3372. checkpreprocstack;
  3373. end;
  3374. else
  3375. Illegal_Char(c);
  3376. end;
  3377. end;
  3378. function tscannerfile.asmgetcharstart : char;
  3379. begin
  3380. { return first the character already
  3381. available in c }
  3382. lastasmgetchar:=c;
  3383. result:=asmgetchar;
  3384. end;
  3385. function tscannerfile.asmgetchar : char;
  3386. begin
  3387. if lastasmgetchar<>#0 then
  3388. begin
  3389. c:=lastasmgetchar;
  3390. lastasmgetchar:=#0;
  3391. end
  3392. else
  3393. readchar;
  3394. if in_asm_string then
  3395. begin
  3396. asmgetchar:=c;
  3397. exit;
  3398. end;
  3399. repeat
  3400. case c of
  3401. {$ifndef arm}
  3402. // the { ... } is used in ARM assembler to define register sets, so we can't used
  3403. // it as comment, either (* ... *), /* ... */ or // ... should be used instead
  3404. '{' :
  3405. skipcomment;
  3406. {$endif arm}
  3407. #10,#13 :
  3408. begin
  3409. linebreak;
  3410. asmgetchar:=c;
  3411. exit;
  3412. end;
  3413. #26 :
  3414. begin
  3415. reload;
  3416. if (c=#26) and not assigned(inputfile.next) then
  3417. end_of_file;
  3418. continue;
  3419. end;
  3420. '/' :
  3421. begin
  3422. readchar;
  3423. if c='/' then
  3424. skipdelphicomment
  3425. else
  3426. begin
  3427. asmgetchar:='/';
  3428. lastasmgetchar:=c;
  3429. exit;
  3430. end;
  3431. end;
  3432. '(' :
  3433. begin
  3434. readchar;
  3435. if c='*' then
  3436. begin
  3437. c:=#0;{Signal skipoldtpcomment to reload a char }
  3438. skipoldtpcomment;
  3439. end
  3440. else
  3441. begin
  3442. asmgetchar:='(';
  3443. lastasmgetchar:=c;
  3444. exit;
  3445. end;
  3446. end;
  3447. else
  3448. begin
  3449. asmgetchar:=c;
  3450. exit;
  3451. end;
  3452. end;
  3453. until false;
  3454. end;
  3455. {*****************************************************************************
  3456. Helpers
  3457. *****************************************************************************}
  3458. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3459. begin
  3460. if dm in [directive_all, directive_turbo] then
  3461. turbo_scannerdirectives.insert(tdirectiveitem.create(s,p));
  3462. if dm in [directive_all, directive_mac] then
  3463. mac_scannerdirectives.insert(tdirectiveitem.create(s,p));
  3464. end;
  3465. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3466. begin
  3467. if dm in [directive_all, directive_turbo] then
  3468. turbo_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  3469. if dm in [directive_all, directive_mac] then
  3470. mac_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  3471. end;
  3472. {*****************************************************************************
  3473. Initialization
  3474. *****************************************************************************}
  3475. procedure InitScanner;
  3476. begin
  3477. InitWideString(patternw);
  3478. turbo_scannerdirectives:=TDictionary.Create;
  3479. mac_scannerdirectives:=TDictionary.Create;
  3480. { Common directives and conditionals }
  3481. AddDirective('I',directive_all, @dir_include);
  3482. AddDirective('DEFINE',directive_all, @dir_define);
  3483. AddDirective('UNDEF',directive_all, @dir_undef);
  3484. AddConditional('IF',directive_all, @dir_if);
  3485. AddConditional('IFDEF',directive_all, @dir_ifdef);
  3486. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  3487. AddConditional('ELSE',directive_all, @dir_else);
  3488. AddConditional('ELSEIF',directive_all, @dir_elseif);
  3489. AddConditional('ENDIF',directive_all, @dir_endif);
  3490. { Directives and conditionals for all modes except mode macpas}
  3491. AddDirective('INCLUDE',directive_turbo, @dir_include);
  3492. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  3493. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  3494. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  3495. AddConditional('IFEND',directive_turbo, @dir_endif);
  3496. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  3497. { Directives and conditionals for mode macpas: }
  3498. AddDirective('SETC',directive_mac, @dir_setc);
  3499. AddDirective('DEFINEC',directive_mac, @dir_definec);
  3500. AddDirective('UNDEFC',directive_mac, @dir_undef);
  3501. AddConditional('IFC',directive_mac, @dir_if);
  3502. AddConditional('ELSEC',directive_mac, @dir_else);
  3503. AddConditional('ELIFC',directive_mac, @dir_elseif);
  3504. AddConditional('ENDC',directive_mac, @dir_endif);
  3505. end;
  3506. procedure DoneScanner;
  3507. begin
  3508. turbo_scannerdirectives.Free;
  3509. mac_scannerdirectives.Free;
  3510. DoneWideString(patternw);
  3511. end;
  3512. end.