scanner.pas 134 KB

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