scanner.pas 121 KB

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