Guest User

Untitled

a guest
Oct 23rd, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.49 KB | None | 0 0
  1. unit utokenizer;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8. Classes, SysUtils;
  9.  
  10. const
  11.  
  12. {$WARNINGS OFF}
  13. IdentifierFirstSymbols: set of char = ['A' .. 'Z', 'a' .. 'z'];
  14. IdentifierSymbols: set of char = ['A' .. 'Z', 'a' .. 'z', '0' .. '9'];
  15. Digits: set of char = ['0' .. '9'];
  16. HexDigits: set of char = ['A' .. 'F', 'a' .. 'f', '0' .. '9'];
  17. ExponentDot = '.';
  18. ExponentE: set of char = ['E', 'e'];
  19. Signs: set of char = ['+', '-'];
  20.  
  21. Constants: array [0..3] of string = ('true', 'false', 'iota', 'nil');
  22. Keywords: array [0 .. 24] of string =
  23. ('break', 'default', 'func', 'interface', 'select',
  24. 'case', 'defer', 'go', 'map', 'struct',
  25. 'chan', 'else', 'goto', 'package', 'switch',
  26. 'const', 'fallthrough', 'if', 'range', 'type',
  27. 'continue', 'for', 'import', 'return', 'var');
  28. Operators: array [0..46] of string =
  29. ('+', '&', '+=', '&=', '&&', '==', '!=', '(', ')', '-', '|', '-=', '|=', '||',
  30. '<', '<=', '[', ']', '*', '^', '*=', '^=', '<-', '>', '>=', '{', '}', '/', '<<',
  31. '/=', '<<=', '++', '=', ':=', ',', ';', '%', '>>', '%=', '>>=',
  32. '--', '!', '...', '.', ':', '&^', '&^=');
  33. PredeclaredTypes: array [0..19] of string =
  34. ('bool', 'byte', 'complex64', 'complex128', 'error', 'float32', 'float64',
  35. 'int', 'int8', 'int16', 'int32', 'int64', 'rune', 'string', 'uint', 'uint8',
  36. 'uint16', 'uint32', 'uint64', 'uintptr');
  37. PredeclaredFunctions: array [0..14] of string =
  38. ('append', 'cap', 'close', 'complex', 'copy', 'delete', 'imag', 'len',
  39. 'make', 'new', 'panic', 'print', 'println', 'real', 'recover');
  40. {$WARNINGS ON}
  41. CharLineEnd = #10;
  42. CommentsLine = '//';
  43. CommentsOpen = '/*';
  44. CommentsClose = '*/';
  45.  
  46. type
  47. TStringQuote =
  48. (
  49. sqSingle,
  50. sqDouble,
  51. sqSingleAndDouble
  52. );
  53.  
  54. TTokenKind = (tkILLEGAL, tkEOF, tkCOMMENT, tkIDENT,
  55. tkINT, tkFLOAT, tkIMAG, tkCHAR, tkSTRING,
  56. tkADD, tkSUB, tkMUL, tkQUO, tkREM, tkAND, tkOR, tkXOR, tkSHL, tkSHR,
  57. tkAND_NOT, tkADD_ASSIGN, tkSUB_ASSIGN, tkMUL_ASSIGN, tkQUO_ASSIGN,
  58. tkREM_ASSIGN, tkAND_ASSIGN, tkOR_ASSIGN, tkXOR_ASSIGN, tkSHL_ASSIGN,
  59. tkSHR_ASSIGN, tkAND_NOT_ASSIGN, tkLAND, tkLOR, tkARROW, tkINC, tkDEC,
  60. tkEQL, tkLSS, tkGTR, tkASSIGN, tkNOT, tkNEQ, tkLEQ, tkGEQ, tkDEFINE,
  61. tkELLIPSIS, tkLPAREN, tkLBRACK, tkLBRACE, tkCOMMA, tkPERIOD, tkRPAREN,
  62. tkRBRACK, tkRBRACE, tkSEMICOLON, tkCOLON,
  63. tkBREAK, tkCASE, tkCHAN, tkCONST, tkCONTINUE, tkDEFAULT, tkDEFER,
  64. tkELSE, tkFALLTHROUGH, tkFOR, tkFUNC, tkGO, tkGOTO, tkIF, tkIMPORT,
  65. tkINTERFACE, tkMAP, tkPACKAGE, tkRANGE, tkRETURN, tkSELECT, tkSTRUCT,
  66. tkSWITCH, tkTYPE, tkVAR, tkSTRIDENT,tkSKIPRESULT, tkUnknown);
  67. TPrecedence = (LowestPrec = 0 // non-operators
  68. , UnaryPrec = 6, HighestPrec = 7);
  69.  
  70. const
  71. StringQuote = sqSingleAndDouble;
  72.  
  73. type
  74.  
  75. PToken = ^TToken;
  76.  
  77. TToken = record
  78. Token: string;
  79. TextPos: integer;
  80. LexType: TTokenKind;
  81. end;
  82. TTokens = array of TToken;
  83. //TPTokens = array of PToken;
  84. { TTokenizer }
  85.  
  86. TTokenizer = class(TObject)
  87. private
  88. CurrPos: integer;
  89. CurrTokenPos: integer;
  90. CurrChar: char;
  91. CurrToken: string;
  92. FTokensList: TTokens;
  93. FTokensCount: integer;
  94. FSource: string;
  95.  
  96. procedure GetNextChar;
  97. procedure Add(Token: string; Pos: integer; LType: TTokenKind);
  98. procedure GetNumber;
  99. procedure GetIdentifier;
  100. procedure GetOthers;
  101. function isKeyword(const Ident: string): boolean;
  102. function isType(const Ident: string): boolean;
  103. function IdentToTokenKind(const Ident: string): TTokenKind;
  104. function NextChar: char;
  105. { function GenerateAnsi(const Str: string): string;}
  106. function GetToken(ind: integer): TToken;
  107. procedure SetSource(src: string);
  108. public
  109. constructor Create;
  110. destructor Destroy; override;
  111. procedure Analyze;
  112. function GetNextToken: PToken;
  113. function NextToken: PToken;
  114. function GetCurrentToken: PToken;
  115. function GetPreviousToken(const Step: integer): PToken;
  116. property Token[ind: integer]: TToken read GetToken;
  117. property TokensCount: integer read FTokensCount;
  118. property Source: string read FSource write SetSource;
  119. end;
  120.  
  121. implementation
  122. uses utils;
  123. { TTokenizer }
  124.  
  125. procedure TTokenizer.Add(Token: string; Pos: integer; LType: TTokenKind);
  126. begin
  127. if Token = '' then
  128. Exit;
  129. Inc(FTokensCount);
  130. SetLength(FTokensList, FTokensCount);
  131. FTokensList[FTokensCount - 1].Token := Token;
  132. FTokensList[FTokensCount - 1].TextPos := Pos;
  133. FTokensList[FTokensCount - 1].LexType := LType;
  134. end;
  135.  
  136. procedure TTokenizer.Analyze;
  137. var
  138. tokenType: TTokenKind;
  139. begin
  140. SetLength(FTokensList, 0);
  141. GetNextChar;
  142. while CurrChar <> #0 do
  143. begin
  144. if CurrPos > Length(FSource) then
  145. Break;
  146. case CurrChar of
  147. 'A' .. 'Z', 'a' .. 'z':
  148. begin
  149. CurrToken := '';
  150. GetIdentifier;
  151. if isKeyword(CurrToken) then
  152. tokenType := IdentToTokenKind(CurrToken)
  153. else
  154. if isType(LowerCase(CurrToken)) and (Length(CurrToken) > 2) then
  155. tokenType := IdentToTokenKind(UpperCase(CurrToken))
  156. else
  157. tokenType := tkIdent;
  158. if CurrToken <> '' then
  159. Add(CurrToken, (CurrPos - 1) - Length(CurrToken), tokenType);
  160. CurrToken := '';
  161. end;
  162. '0' .. '9':
  163. begin
  164. CurrToken := '';
  165. GetNumber;
  166. if CurrToken <> '' then
  167. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)), tkInt);
  168. CurrToken := '';
  169. end;
  170. #1 .. #20, ' ':
  171. begin
  172. GetNextChar;
  173. end;
  174. else
  175. begin
  176. CurrToken := '';
  177. GetOthers;
  178. if CurrToken <> '' then
  179. begin
  180. tokenType := IdentToTokenKind(CurrToken);
  181. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)), tokenType);
  182. end
  183. else
  184. GetNextChar;
  185. CurrToken := '';
  186. //GetNextChar;
  187. end;
  188. //GetNextChar();
  189. end;
  190. end;
  191. end;
  192.  
  193. function TTokenizer.GetNextToken: PToken;
  194. begin
  195. Inc(CurrTokenPos);
  196. if CurrTokenPos > FTokensCount - 1 then
  197. result := nil
  198. else
  199. result := @FTokensList[CurrTokenPos];
  200. end;
  201.  
  202. function TTokenizer.NextToken: PToken;
  203. begin
  204. if CurrTokenPos + 1 > FTokensCount - 1 then
  205. Result := nil
  206. else
  207. Result := @FTokensList[CurrTokenPos+1];
  208. end;
  209.  
  210. function TTokenizer.GetCurrentToken: PToken;
  211. begin
  212. if CurrTokenPos > (FTokensCount - 1) then
  213. Exit;
  214. Result := @FTokensList[CurrTokenPos];
  215.  
  216. end;
  217.  
  218. function TTokenizer.GetPreviousToken(const Step: integer): PToken;
  219. begin
  220. if (CurrTokenPos = 0) or ((CurrTokenPos - Step) < 0) then
  221. Exit;
  222. Result := @FTokensList[CurrTokenPos-step];
  223. end;
  224.  
  225. constructor TTokenizer.Create;
  226. begin
  227. inherited;
  228. FTokensList := nil;
  229. FTokensCount := 0;
  230. CurrPos := 0;
  231. CurrTokenPos := 0;
  232. CurrChar := #0;
  233. CurrToken := '';
  234. end;
  235.  
  236. destructor TTokenizer.Destroy;
  237. begin
  238. SetLength(FTokensList, 0);
  239. inherited;
  240. end;
  241.  
  242. procedure TTokenizer.GetIdentifier;
  243. begin
  244. CurrToken := CurrChar;
  245. GetNextChar;
  246. while CharInSet(CurrChar, IdentifierSymbols) do
  247. begin
  248. CurrToken := CurrToken + CurrChar;
  249. GetNextChar;
  250. end;
  251. end;
  252.  
  253. function TTokenizer.GetToken(ind: integer): TToken;
  254. begin
  255. if ind > (FTokensCount - 1) then
  256. Exit;
  257. Result := FTokensList[ind];
  258. end;
  259.  
  260. procedure TTokenizer.GetNextChar;
  261. begin
  262. Inc(CurrPos);
  263. if CurrPos > Length(FSource) then
  264. CurrChar := #0
  265. else
  266. CurrChar := FSource[CurrPos];
  267. end;
  268.  
  269. procedure TTokenizer.GetNumber;
  270. begin
  271. CurrToken := CurrChar;
  272. GetNextChar;
  273.  
  274. while CharInSet(CurrChar, Digits) do
  275. begin
  276. CurrToken := CurrToken + CurrChar;
  277. GetNextChar;
  278. end;
  279. if CurrChar <> ExponentDot then
  280. Exit;
  281.  
  282. if not CharInSet(NextChar, Digits) then
  283. Exit;
  284.  
  285. CurrToken := CurrToken + ExponentDot;
  286. GetNextChar;
  287.  
  288. while CharInSet(CurrChar, Digits) do
  289. begin
  290. CurrToken := CurrToken + CurrChar;
  291. GetNextChar;
  292. end;
  293.  
  294. if not CharInSet(CurrChar, ExponentE) then
  295. Exit;
  296.  
  297. CurrToken := CurrToken + 'E';
  298. GetNextChar;
  299.  
  300. if CharInSet(CurrChar, Signs) then
  301. CurrToken := CurrToken + CurrChar;
  302. GetNextChar;
  303.  
  304. while CharInSet(CurrChar, Digits) do
  305. begin
  306. CurrToken := CurrToken + CurrChar;
  307. GetNextChar;
  308. end;
  309.  
  310. end;
  311.  
  312. procedure TTokenizer.GetOthers;
  313. var
  314. i: integer;
  315. begin
  316. case CurrChar of
  317. ';', '(', ')', '[', ']', ',', '@', '{', '}','_':
  318. begin
  319. CurrToken := CurrChar;
  320. GetNextChar;
  321. Exit;
  322. end;
  323. '!':
  324. begin
  325. CurrToken := CurrChar;
  326. if (NextChar = '=') then
  327. begin
  328. GetNextChar;
  329. CurrToken := CurrToken + CurrChar;
  330. end;
  331. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  332. IdentToTokenKind(CurrToken));
  333. CurrToken := '';
  334. //GetNextChar;
  335. end;
  336. '.':
  337. begin
  338. CurrToken := CurrChar;
  339. if (NextChar = '.') then
  340. begin
  341. while NextChar = '.' do
  342. begin
  343. GetNextChar;
  344. CurrToken := CurrToken + CurrChar;
  345. end;
  346. end;
  347. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  348. IdentToTokenKind(CurrToken));
  349. CurrToken := '';
  350. //GetNextChar;
  351. end;
  352. ':':
  353. begin
  354. CurrToken := CurrChar;
  355. if (NextChar = '=') then
  356. begin
  357. GetNextChar;
  358. CurrToken := CurrToken + CurrChar;
  359. end;
  360. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  361. IdentToTokenKind(CurrToken));
  362. CurrToken := '';
  363. //GetNextChar;
  364. end;
  365. '=':
  366. begin
  367. CurrToken := CurrChar;
  368. if (NextChar = '=') then
  369. begin
  370. GetNextChar;
  371. CurrToken := CurrToken + CurrChar;
  372. end;
  373. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  374. IdentToTokenKind(CurrToken));
  375. CurrToken := '';
  376. //GetNextChar;
  377. end;
  378. '&':
  379. begin
  380. CurrToken := CurrChar;
  381. if (NextChar = '=') or (NextChar = '^') or (NextChar = '&') then
  382. begin
  383. GetNextChar;
  384. CurrToken := CurrToken + CurrChar;
  385. if (NextChar = '=') then
  386. begin
  387. GetNextChar;
  388. CurrToken := CurrToken + CurrChar;
  389. end;
  390. end;
  391. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  392. IdentToTokenKind(CurrToken));
  393. CurrToken := '';
  394. //GetNextChar;
  395. end;
  396. '<':
  397. begin
  398. CurrToken := CurrChar;
  399. if (NextChar = '<') or (NextChar = '-') then
  400. begin
  401. GetNextChar;
  402. CurrToken := CurrToken + CurrChar;
  403. if (NextChar = '=') then
  404. begin
  405. GetNextChar;
  406. CurrToken := CurrToken + CurrChar;
  407. end;
  408. end;
  409. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  410. IdentToTokenKind(CurrToken));
  411. CurrToken := '';
  412. //GetNextChar;
  413. end;
  414. '>':
  415. begin
  416. CurrToken := CurrChar;
  417. if (NextChar = '>') then
  418. begin
  419. GetNextChar;
  420. CurrToken := CurrToken + CurrChar;
  421. if (NextChar = '=') then
  422. begin
  423. GetNextChar;
  424. CurrToken := CurrToken + CurrChar;
  425. end;
  426. end;
  427. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  428. IdentToTokenKind(CurrToken));
  429. CurrToken := '';
  430. //GetNextChar;
  431. end;
  432. '|':
  433. begin
  434. CurrToken := CurrChar;
  435. if (NextChar = '=') or (NextChar = '|') then
  436. begin
  437. GetNextChar;
  438. CurrToken := CurrToken + CurrChar;
  439. end;
  440. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  441. IdentToTokenKind(CurrToken));
  442. CurrToken := '';
  443. //GetNextChar;
  444. end;
  445. '^':
  446. begin
  447. CurrToken := CurrChar;
  448. if (NextChar = '=') then
  449. begin
  450. GetNextChar;
  451. CurrToken := CurrToken + CurrChar;
  452. end;
  453. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  454. IdentToTokenKind(CurrToken));
  455. CurrToken := '';
  456. //GetNextChar;
  457. end;
  458. '%':
  459. begin
  460. CurrToken := CurrChar;
  461. if (NextChar = '=') then
  462. begin
  463. GetNextChar;
  464. CurrToken := CurrToken + CurrChar;
  465. end;
  466. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  467. IdentToTokenKind(CurrToken));
  468. CurrToken := '';
  469. //GetNextChar;
  470. end;
  471. '+':
  472. begin
  473. CurrToken := CurrChar;
  474. if (NextChar = '=') or (NextChar = '+') then
  475. begin
  476. GetNextChar;
  477. CurrToken := CurrToken + CurrChar;
  478. end;
  479. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  480. IdentToTokenKind(CurrToken));
  481. CurrToken := '';
  482. //GetNextChar;
  483. end;
  484. '-':
  485. begin
  486. CurrToken := CurrChar;
  487. if (NextChar = '=') or (NextChar = '-') then
  488. begin
  489. GetNextChar;
  490. CurrToken := CurrToken + CurrChar;
  491. end;
  492. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  493. IdentToTokenKind(CurrToken));
  494. CurrToken := '';
  495. //GetNextChar;
  496. end;
  497. '*':
  498. begin
  499. CurrToken := CurrChar;
  500. if (NextChar = '=') then
  501. begin
  502. GetNextChar;
  503. CurrToken := CurrToken + CurrChar;
  504. end;
  505. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)),
  506. IdentToTokenKind(CurrToken));
  507. CurrToken := '';
  508. //GetNextChar;
  509. end;
  510. '"':
  511. begin
  512. if (StringQuote = sqDouble) or (StringQuote = sqSingleAndDouble) then
  513. begin
  514. Add('"', CurrPos, tkStrIdent);
  515. GetNextChar;
  516. while CurrChar <> '"' do
  517. begin
  518. CurrToken := CurrToken + CurrChar;
  519. GetNextChar;
  520. end;
  521. if CurrToken <> '' then
  522. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)), tkString);
  523. CurrToken := '"';
  524. GetNextChar;
  525. { Add('"', CurrPos, 2);
  526. GetNextChar; }
  527. end;
  528. Exit;
  529. end;
  530. '/':
  531. begin
  532. if NextChar = '*' then
  533. begin
  534. GetNextChar;
  535. CurrToken := '/';
  536. while CurrChar <> '/' do
  537. begin
  538. CurrToken := CurrToken + CurrChar;
  539. GetNextChar;
  540. end;
  541.  
  542. CurrToken := CurrToken + '/';
  543. if CurrToken <> '' then
  544. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)), tkComment);
  545. CurrToken := '';
  546. //GetNextChar;
  547. end
  548. else
  549. if NextChar = '/' then
  550. begin
  551. CurrToken := CurrToken + CurrChar;
  552. GetNextChar;
  553. while CurrChar <> #10 do
  554. begin
  555. CurrToken := CurrToken + CurrChar;
  556. GetNextChar;
  557. end;
  558. if CurrToken <> '' then
  559. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)), tkComment);
  560. CurrToken := '';
  561. //GetNextChar;
  562. end
  563. else
  564. if (NextChar = '=') then
  565. begin
  566. GetNextChar;
  567. CurrToken := CurrToken + CurrChar;
  568. if CurrToken <> '' then
  569. Add(CurrToken, CurrPos - cardinal(Length(CurrToken)), tkComment);
  570. CurrToken := '';
  571. //GetNextChar;
  572. end;
  573.  
  574. end
  575. else
  576. Exit;
  577. end;
  578. end;
  579.  
  580. function TTokenizer.isKeyword(const Ident: string): boolean;
  581. var
  582. i: integer;
  583. begin
  584. Result := False;
  585. for i := low(Keywords) to high(Keywords) do
  586. if eq(Keywords[i], Ident) then
  587. begin
  588. Result := True;
  589. break;
  590. end;
  591.  
  592. end;
  593.  
  594. function TTokenizer.isType(const Ident: string): boolean;
  595. var
  596. i: integer;
  597. begin
  598. Result := False;
  599. for i := low(PredeclaredTypes) to high(PredeclaredTypes) do
  600. if Pos(ident, predeclaredTypes[i]) > 0 then
  601. begin
  602. Result := True;
  603. break;
  604. end;
  605.  
  606. end;
  607.  
  608. function TTokenizer.IdentToTokenKind(const Ident: string): TTokenKind;
  609. begin
  610. Result := tkUnknown;
  611. case ident of
  612. 'ILLEGAL': Result := tkILLEGAL;
  613. 'EOF': Result := tkEOF;
  614. 'COMMENT': Result := tkCOMMENT;
  615. 'IDENT': Result := tkIDENT;
  616. 'INT': Result := tkINT;
  617. 'FLOAT': Result := tkFLOAT;
  618. 'IMAG': Result := tkIMAG;
  619. 'CHAR': Result := tkCHAR;
  620. 'STRING': Result := tkSTRING;
  621. '_': Result := tkSKIPRESULT;
  622. '+': Result := tkADD;
  623. '-': Result := tkSUB;
  624. '*': Result := tkMUL;
  625. '/': Result := tkQUO;
  626. '%': Result := tkREM;
  627. '&': Result := tkAND;
  628. '|': Result := tkOR;
  629. '^': Result := tkXOR;
  630. '<<': Result := tkSHL;
  631. '>>': Result := tkSHR;
  632. '&^': Result := tkAND_NOT;
  633. '+=': Result := tkADD_ASSIGN;
  634. '-=': Result := tkSUB_ASSIGN;
  635. '*=': Result := tkMUL_ASSIGN;
  636. '/=': Result := tkQUO_ASSIGN;
  637. '%=': Result := tkREM_ASSIGN;
  638. '&=': Result := tkAND_ASSIGN;
  639. '|=': Result := tkOR_ASSIGN;
  640. '^=': Result := tkXOR_ASSIGN;
  641. '<<=': Result := tkSHL_ASSIGN;
  642. '>>=': Result := tkSHR_ASSIGN;
  643. '&^=': Result := tkAND_NOT_ASSIGN;
  644. '&&': Result := tkLAND;
  645. '||': Result := tkLOR;
  646. '<-': Result := tkARROW;
  647. '++': Result := tkINC;
  648. '--': Result := tkDEC;
  649. '==': Result := tkEQL;
  650. '<': Result := tkLSS;
  651. '>': Result := tkGTR;
  652. '=': Result := tkASSIGN;
  653. '!': Result := tkNOT;
  654. '!=': Result := tkNEQ;
  655. '<=': Result := tkLEQ;
  656. '>=': Result := tkGEQ;
  657. ':=': Result := tkDEFINE;
  658. '...': Result := tkELLIPSIS;
  659. '(': Result := tkLPAREN;
  660. '[': Result := tkLBRACK;
  661. '{': Result := tkLBRACE;
  662. '.': Result := tkPERIOD;
  663. ',': Result := tkCOMMA;
  664. ')': Result := tkRPAREN;
  665. ']': Result := tkRBRACK;
  666. '}': Result := tkRBRACE;
  667. ';': Result := tkSEMICOLON;
  668. ':': Result := tkCOLON;
  669. '"': Result := tkSTRIDENT;
  670. 'break': Result := tkBREAK;
  671. 'case': Result := tkCASE;
  672. 'chan': Result := tkCHAN;
  673. 'const': Result := tkCONST;
  674. 'continue': Result := tkCONTINUE;
  675. 'default': Result := tkDEFAULT;
  676. 'defer': Result := tkDEFER;
  677. 'else': Result := tkELSE;
  678. 'fallthrough': Result := tkFALLTHROUGH;
  679. 'for': Result := tkFOR;
  680. 'func': Result := tkFUNC;
  681. 'go': Result := tkGO;
  682. 'goto': Result := tkGOTO;
  683. 'if': Result := tkIF;
  684. 'import': Result := tkIMPORT;
  685. 'interface': Result := tkINTERFACE;
  686. 'map': Result := tkMAP;
  687. 'package': Result := tkPACKAGE;
  688. 'range': Result := tkRANGE;
  689. 'return': Result := tkRETURN;
  690. 'select': Result := tkSELECT;
  691. 'struct': Result := tkSTRUCT;
  692. 'switch': Result := tkSWITCH;
  693. 'type': Result := tkTYPE;
  694. 'var': Result := tkVAR;
  695. end;
  696. end;
  697.  
  698. function TTokenizer.NextChar: char;
  699. begin
  700. if CurrPos + 1 > Length(FSource) then
  701. Result := #0
  702. else
  703. Result := FSource[CurrPos + 1];
  704. end;
  705.  
  706. procedure TTokenizer.SetSource(src: string);
  707. var
  708. I, j: integer;
  709. tmp: string;
  710. begin
  711. try
  712. tmp := src;
  713. finally
  714. Self.FSource := tmp;
  715. tmp := '';
  716. end;
  717. end;
  718.  
  719. end.
  720.  
  721.  
Advertisement
Add Comment
Please, Sign In to add comment