StringReader.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /**
  2. *
  3. * @author seagle
  4. * @date 2024-7-10
  5. * @Description 从字符串中读取关键字
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace MECF.Framework.UI.Core.DxfScript
  13. {
  14. public class StringReader
  15. {
  16. private static readonly char CH_ROUND_LEFT = '(';
  17. private static readonly char CH_ROUND_RIGHT = ')';
  18. private static readonly char CH_BRACE_LEFT = '{';
  19. private static readonly char CH_BRACE_RIGHT = '}';
  20. private static readonly char CH_PLUS = '+';
  21. private static readonly char CH_MINUS = '-';
  22. private static readonly char CH_MULTIPLE = '*';
  23. private static readonly char CH_DEVIDE = '/';
  24. private static readonly char CH_CHANGE = '\\';
  25. private static readonly char CH_EQUAL = '=';
  26. private static readonly char CH_EXCLAM = '!';
  27. private static readonly char CH_WAVE = '~';
  28. private static readonly char CH_GT = '>';
  29. private static readonly char CH_LT = '<';
  30. private static readonly char CH_DOT = '.';
  31. private static readonly char CH_AND = '&';
  32. private static readonly char CH_OR = '|';
  33. private static readonly char CH_COMMA = ',';
  34. private static readonly char CH_SEP = ';';
  35. private static readonly char CH_QUOT = '"';
  36. private string scriptData;
  37. private KeywordType lastKwType;
  38. public int __line__;
  39. private int scriptLength = 0;
  40. private int scriptOffset = 0;
  41. private string buffer;
  42. public StringReader(string scriptData)
  43. {
  44. this.scriptData = scriptData;
  45. if( scriptData == null)
  46. {
  47. this.scriptData = "";
  48. }
  49. __line__ = 1;
  50. scriptLength = scriptData.Length;
  51. scriptOffset = 0;
  52. buffer = "";
  53. lastKwType = KeywordType.KW_INVALID;
  54. }
  55. private static char EOF = (char)0xffff;
  56. private char Getc()
  57. {
  58. if (scriptData == null)
  59. {
  60. return EOF;
  61. }
  62. if(scriptOffset >= scriptLength)
  63. {
  64. return EOF;
  65. }
  66. char ch = scriptData[scriptOffset++];
  67. return ch;
  68. }
  69. private void UnGetc()
  70. {
  71. if (scriptData == null)
  72. {
  73. return ;
  74. }
  75. if (scriptOffset > scriptLength)
  76. {
  77. return ;
  78. }
  79. if (scriptOffset <=0)
  80. {
  81. return;
  82. }
  83. scriptOffset--;
  84. }
  85. private bool Eof()
  86. {
  87. if (scriptData == null) return true;
  88. return scriptOffset >= scriptLength;
  89. }
  90. private bool isAlpha(char ch)
  91. {
  92. if (ch >= 'a' && ch <= 'z')
  93. {
  94. return true;
  95. }
  96. else if (ch >= 'A' && ch <= 'Z')
  97. {
  98. return true;
  99. }
  100. else if (ch >= '0' && ch <= '9')
  101. {
  102. return true;
  103. }
  104. else if (ch == '_')
  105. {
  106. return true;
  107. }
  108. return false;
  109. }
  110. public void rollback(KeywordType kwType)
  111. {
  112. lastKwType = kwType;
  113. }
  114. public KeywordType Next()
  115. {
  116. if (lastKwType != KeywordType.KW_INVALID)
  117. {
  118. KeywordType retKwType = lastKwType;
  119. lastKwType = KeywordType.KW_INVALID;
  120. return retKwType;
  121. }
  122. if (Eof())
  123. {
  124. return KeywordType.KW_EOF;
  125. }
  126. char ch;
  127. while ((ch = Getc()) != EOF)
  128. {
  129. if (ch == ' ' || ch == '\t' || ch == 0x0d)
  130. {
  131. continue;
  132. }
  133. if (ch == 0x0a)
  134. {
  135. __line__++;
  136. continue;
  137. }
  138. if (ch == EOF)
  139. {
  140. return KeywordType.KW_EOF;
  141. }
  142. else if (ch == CH_DEVIDE)
  143. {
  144. char ch2 = Getc();
  145. if (ch2 == CH_DEVIDE)
  146. {
  147. char ch3;
  148. while ((ch3 = Getc()) != EOF)
  149. {
  150. if (ch3 == 0x0a)
  151. {
  152. __line__++;
  153. break;
  154. }
  155. }
  156. if (ch3 == EOF)
  157. {
  158. return KeywordType.KW_EOF;
  159. }
  160. else
  161. {
  162. continue;
  163. }
  164. }
  165. else if (ch2 == CH_MULTIPLE)
  166. {
  167. char ch3;
  168. while ((ch3 = Getc()) != EOF)
  169. {
  170. if (ch3 == 0x0a)
  171. {
  172. __line__++;
  173. }
  174. else if (ch3 == CH_MULTIPLE)
  175. {
  176. char ch4 = Getc();
  177. if (ch4 == CH_DEVIDE)
  178. {
  179. break;
  180. }
  181. else if (ch4 == 0x0a)
  182. {
  183. __line__++;
  184. }
  185. }
  186. }
  187. if (ch3 == EOF)
  188. {
  189. return KeywordType.KW_EOF;
  190. }
  191. else
  192. {
  193. continue;
  194. }
  195. }
  196. else
  197. {
  198. //real devide
  199. UnGetc();
  200. buffer = CH_DEVIDE.ToString();
  201. return KeywordType.KW_OPERATOR_DEVIDE;
  202. }
  203. }
  204. else if (ch == CH_SEP)
  205. {
  206. buffer = CH_SEP.ToString();
  207. return KeywordType.KW_SEP;
  208. }
  209. else if (ch == CH_QUOT)
  210. {
  211. buffer = "";
  212. char ch2;
  213. while ((ch2 = Getc())!= EOF)
  214. {
  215. if (ch2 == CH_QUOT)
  216. {
  217. return KeywordType.KW_CONST_STRING;
  218. }
  219. else if (ch2 == CH_CHANGE)
  220. {
  221. char ch3 = Getc();
  222. if (ch3 == EOF)
  223. {
  224. return KeywordType.KW_INVALID;
  225. }
  226. else if (ch3 == CH_QUOT)
  227. {
  228. buffer+= ch3.ToString();
  229. }
  230. else if (ch3 == 'n')
  231. {
  232. buffer += "\n";
  233. }
  234. else if (ch3 == 't')
  235. {
  236. buffer+= '\t';
  237. }
  238. else if (ch3 == '\\')
  239. {
  240. buffer+= ch3.ToString();
  241. }
  242. else
  243. {
  244. return KeywordType.KW_INVALID;
  245. }
  246. }
  247. else if (ch2 == 0x0a)
  248. {
  249. return KeywordType.KW_INVALID;
  250. }
  251. else
  252. {
  253. buffer+= ch2.ToString();
  254. }
  255. }
  256. return KeywordType.KW_INVALID;
  257. }
  258. else if (ch == CH_PLUS)
  259. {
  260. buffer = ch.ToString();
  261. return KeywordType.KW_OPERATOR_PLUS;
  262. }
  263. else if (ch == CH_MULTIPLE)
  264. {
  265. buffer = ch.ToString();
  266. return KeywordType.KW_OPERATOR_MULTIPLE;
  267. }
  268. else if (ch == CH_MINUS)
  269. {
  270. char ch_last = buffer[0];
  271. if (ch_last == CH_ROUND_RIGHT || isAlpha(ch_last))
  272. {
  273. buffer= ch.ToString();
  274. return KeywordType.KW_OPERATOR_MINUS;
  275. }
  276. else
  277. {
  278. buffer = ch.ToString();
  279. return KeywordType.KW_OPERATOR_NEGATIVE;
  280. }
  281. }
  282. else if (ch == CH_COMMA)
  283. {
  284. buffer = ch.ToString();
  285. return KeywordType.KW_OPERATOR_COMMA;
  286. }
  287. else if (ch >= '0' && ch <= '9')
  288. {
  289. char ch2;
  290. bool hexFlag = false;
  291. bool dotFlag = false;
  292. buffer = ch.ToString();
  293. while ((ch2 = Getc()) != EOF)
  294. {
  295. if (ch2 == 'x' || ch2 == 'X')
  296. {
  297. if (buffer.Equals("0") && !hexFlag)
  298. {
  299. hexFlag = true;
  300. buffer="";
  301. }
  302. else
  303. {
  304. return KeywordType.KW_INVALID;
  305. }
  306. }
  307. else if (ch2 == CH_DOT)
  308. {
  309. if (!dotFlag)
  310. {
  311. dotFlag = true;
  312. buffer += ch2.ToString();
  313. }
  314. else
  315. {
  316. return KeywordType.KW_INVALID;
  317. }
  318. }
  319. else if (ch2 >= '0' && ch2 <= '9')
  320. {
  321. buffer += ch2.ToString();
  322. }
  323. else if (ch2 >= 'a' && ch2 <= 'f')
  324. {
  325. if (hexFlag)
  326. {
  327. buffer+= ch2.ToString();
  328. }
  329. else
  330. {
  331. return KeywordType.KW_INVALID;
  332. }
  333. }
  334. else if (ch2 >= 'A' && ch2 <= 'F')
  335. {
  336. if (hexFlag)
  337. {
  338. buffer += ch2.ToString();
  339. }
  340. else
  341. {
  342. return KeywordType.KW_INVALID;
  343. }
  344. }
  345. else
  346. {
  347. UnGetc();
  348. if (hexFlag && dotFlag)
  349. {
  350. return KeywordType.KW_INVALID;
  351. }
  352. else if (dotFlag)
  353. {
  354. return KeywordType.KW_CONST_DECIMAL;
  355. }
  356. else
  357. {
  358. if (hexFlag && buffer.Length<3)
  359. {
  360. return KeywordType.KW_INVALID;
  361. }
  362. if (hexFlag)
  363. {
  364. buffer = ""+int.Parse(buffer, System.Globalization.NumberStyles.HexNumber);
  365. }
  366. return KeywordType.KW_CONST_INTEGER;
  367. }
  368. }
  369. }
  370. return KeywordType.KW_INVALID;
  371. }
  372. else if (isAlpha(ch))
  373. {
  374. char ch2;
  375. buffer = ch.ToString();
  376. bool dotFlag = false;
  377. while ((ch2 = Getc()) != EOF)
  378. {
  379. if (isAlpha(ch2))
  380. {
  381. buffer+= ch2.ToString();
  382. }
  383. else if (ch2 == CH_DOT)
  384. {
  385. buffer += ch2.ToString();
  386. dotFlag = true;
  387. }
  388. else
  389. {
  390. UnGetc();
  391. if (buffer.ToLower().Equals("on") ||
  392. buffer.ToLower().Equals("true"))
  393. {
  394. return KeywordType.KW_CONST_TRUE;
  395. }
  396. else if (buffer.ToLower().Equals("off") ||
  397. buffer.ToLower().Equals("false"))
  398. {
  399. return KeywordType.KW_CONST_FALSE;
  400. }
  401. else if (buffer.ToLower().Equals("if"))
  402. {
  403. return KeywordType.KW_IF;
  404. }
  405. else if (buffer.ToLower().Equals("else"))
  406. {
  407. return KeywordType.KW_ELSE;
  408. }else if(buffer.ToLower().Equals("return"))
  409. {
  410. return KeywordType.KW_RETURN;
  411. }
  412. else if (buffer.ToLower().Equals( "when"))
  413. {
  414. return KeywordType.KW_WHEN;
  415. }
  416. else if (buffer.ToLower().Equals( "for"))
  417. {
  418. return KeywordType.KW_FOR;
  419. }
  420. else if (ch2 == CH_ROUND_LEFT)
  421. {
  422. return KeywordType.KW_FUNCTION;
  423. }
  424. else
  425. {
  426. return KeywordType.KW_VARIABLE;
  427. }
  428. }
  429. }
  430. return KeywordType.KW_INVALID;
  431. }
  432. else if (ch == CH_GT)
  433. {
  434. char ch2 = Getc();
  435. if (ch2 == EOF)
  436. {
  437. return KeywordType.KW_INVALID;
  438. }
  439. else if (ch2 == CH_GT)
  440. {
  441. buffer = ">>";
  442. return KeywordType.KW_OPERATOR_MOVE_RIGHT;
  443. }
  444. else if (ch2 == CH_EQUAL)
  445. {
  446. buffer = ">=";
  447. return KeywordType.KW_OPERATOR_GE;
  448. }
  449. else
  450. {
  451. buffer = ">";
  452. UnGetc();
  453. return KeywordType.KW_OPERATOR_GT;
  454. }
  455. }
  456. else if (ch == CH_LT)
  457. {
  458. char ch2 = Getc();
  459. if (ch2 == EOF)
  460. {
  461. return KeywordType.KW_INVALID;
  462. }
  463. else if (ch2 == CH_LT)
  464. {
  465. buffer = "<<";
  466. return KeywordType.KW_OPERATOR_MOVE_LEFT;
  467. }
  468. else if (ch2 == CH_EQUAL)
  469. {
  470. buffer = "<=";
  471. return KeywordType.KW_OPERATOR_LE;
  472. }
  473. else
  474. {
  475. buffer = "<";
  476. UnGetc();
  477. return KeywordType.KW_OPERATOR_LT;
  478. }
  479. }
  480. else if (ch == CH_EXCLAM)
  481. {
  482. char ch2 = Getc();
  483. if (ch2 == EOF)
  484. {
  485. return KeywordType.KW_INVALID;
  486. }
  487. else if (ch2 == CH_EQUAL)
  488. {
  489. buffer = "!=";
  490. return KeywordType.KW_OPERATOR_NOT_EQUAL;
  491. }
  492. else
  493. {
  494. buffer = "!";
  495. return KeywordType.KW_OPERATOR_NOT;
  496. }
  497. }
  498. else if (ch == CH_EQUAL)
  499. {
  500. char ch2 = Getc();
  501. if (ch2 == EOF)
  502. {
  503. return KeywordType.KW_INVALID;
  504. }
  505. else if (ch2 == CH_EQUAL)
  506. {
  507. buffer = "==";
  508. return KeywordType.KW_OPERATOR_EQUAL;
  509. }
  510. else
  511. {
  512. buffer = "=";
  513. UnGetc();
  514. return KeywordType.KW_SET;
  515. }
  516. }
  517. else if (ch == CH_AND)
  518. {
  519. char ch2 = Getc();
  520. if (ch2 == EOF)
  521. {
  522. return KeywordType.KW_INVALID;
  523. }
  524. else if (ch2 == CH_AND)
  525. {
  526. buffer = "&&";
  527. return KeywordType.KW_OPERATOR_AND;
  528. }
  529. else
  530. {
  531. buffer = "&";
  532. return KeywordType.KW_OPERATOR_BIT_AND;
  533. }
  534. }
  535. else if (ch == CH_OR)
  536. {
  537. char ch2 = Getc();
  538. if (ch2 == EOF)
  539. {
  540. return KeywordType.KW_INVALID;
  541. }
  542. else if (ch2 == CH_OR)
  543. {
  544. buffer = "||";
  545. return KeywordType.KW_OPERATOR_OR;
  546. }
  547. else
  548. {
  549. buffer = "|";
  550. UnGetc();
  551. return KeywordType.KW_OPERATOR_BIT_OR;
  552. }
  553. }
  554. else if (ch == CH_WAVE)
  555. {
  556. buffer = CH_WAVE.ToString();
  557. return KeywordType.KW_OPERATOR_BIT_NOT;
  558. }
  559. else if (ch == CH_ROUND_LEFT)
  560. {
  561. buffer = CH_ROUND_LEFT.ToString ();
  562. return KeywordType.KW_ROUND_LEFT;
  563. }
  564. else if (ch == CH_ROUND_RIGHT)
  565. {
  566. buffer= CH_ROUND_RIGHT.ToString();
  567. return KeywordType.KW_ROUND_RIGHT;
  568. }
  569. else if (ch == CH_BRACE_LEFT)
  570. {
  571. buffer= CH_BRACE_LEFT.ToString();
  572. return KeywordType.KW_BRACE_LEFT;
  573. }
  574. else if (ch == CH_BRACE_RIGHT)
  575. {
  576. buffer= CH_BRACE_RIGHT.ToString();
  577. return KeywordType.KW_BRACE_RIGHT;
  578. }
  579. else
  580. {
  581. return KeywordType.KW_INVALID;
  582. }
  583. break;
  584. }
  585. return KeywordType.KW_EOF;
  586. }
  587. public string Data()
  588. {
  589. return buffer;
  590. }
  591. }
  592. }