scanner.pas 124 KB

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