scanner.pas 130 KB

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