PegBase.cs 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. /*Author:Martin.Holzherr;Date:20080922;Context:"PEG Support for C#";Licence:CPOL
  4. * <<History>>
  5. * 20080922;V1.0 created
  6. * 20080929;UTF16BE;Added UTF16BE read support to <<FileLoader.LoadFile(out string src)>>
  7. * <</History>>
  8. */
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.IO;
  12. using System.Diagnostics;
  13. using System.Text;
  14. using System;
  15. namespace Peg.Base
  16. {
  17. #region Input File Support
  18. public enum EncodingClass { unicode, utf8, binary, ascii };
  19. public enum UnicodeDetection { notApplicable, BOM, FirstCharIsAscii };
  20. public class FileLoader
  21. {
  22. public enum FileEncoding { none, ascii, binary, utf8, unicode, utf16be, utf16le, utf32le, utf32be, uniCodeBOM };
  23. public FileLoader(EncodingClass encodingClass, UnicodeDetection detection, string path)
  24. {
  25. encoding_ = GetEncoding(encodingClass, detection, path);
  26. path_ = path;
  27. }
  28. public bool IsBinaryFile()
  29. {
  30. return encoding_ == FileEncoding.binary;
  31. }
  32. public bool LoadFile(out byte[] src)
  33. {
  34. src = null;
  35. if (!IsBinaryFile()) return false;
  36. using (BinaryReader brdr = new BinaryReader(File.Open(path_, FileMode.Open,FileAccess.Read)))
  37. {
  38. src = brdr.ReadBytes((int)brdr.BaseStream.Length);
  39. return true;
  40. }
  41. }
  42. public bool LoadFile(out string src)
  43. {
  44. src = null;
  45. Encoding textEncoding = FileEncodingToTextEncoding();
  46. if (textEncoding == null)
  47. {
  48. if (encoding_ == FileEncoding.binary) return false;
  49. using (StreamReader rd = new StreamReader(path_, true))
  50. {
  51. src = rd.ReadToEnd();
  52. return true;
  53. }
  54. }
  55. else
  56. {
  57. if (encoding_ == FileEncoding.utf16be)//UTF16BE
  58. {
  59. using (BinaryReader brdr = new BinaryReader(File.Open(path_, FileMode.Open, FileAccess.Read)))
  60. {
  61. byte[] bytes = brdr.ReadBytes((int)brdr.BaseStream.Length);
  62. StringBuilder s = new StringBuilder();
  63. for (int i = 0; i < bytes.Length; i += 2)
  64. {
  65. char c = (char)(bytes[i] << 8 | bytes[i + 1]);
  66. s.Append(c);
  67. }
  68. src = s.ToString();
  69. return true;
  70. }
  71. }
  72. else
  73. {
  74. using (StreamReader rd = new StreamReader(path_, textEncoding))
  75. {
  76. src = rd.ReadToEnd();
  77. return true;
  78. }
  79. }
  80. }
  81. }
  82. Encoding FileEncodingToTextEncoding()
  83. {
  84. switch (encoding_)
  85. {
  86. case FileEncoding.utf8: return new UTF8Encoding();
  87. case FileEncoding.utf32be:
  88. case FileEncoding.utf32le: return new UTF32Encoding();
  89. case FileEncoding.unicode:
  90. case FileEncoding.utf16be:
  91. case FileEncoding.utf16le: return new UnicodeEncoding();
  92. case FileEncoding.ascii: return new ASCIIEncoding();
  93. case FileEncoding.binary:
  94. case FileEncoding.uniCodeBOM: return null;
  95. default: Debug.Assert(false);
  96. return null;
  97. }
  98. }
  99. static FileEncoding DetermineUnicodeWhenFirstCharIsAscii(string path)
  100. {
  101. using (BinaryReader br = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read)))
  102. {
  103. byte[] startBytes = br.ReadBytes(4);
  104. if (startBytes.Length == 0) return FileEncoding.none;
  105. if (startBytes.Length == 1 || startBytes.Length == 3) return FileEncoding.utf8;
  106. if (startBytes.Length == 2 && startBytes[0] != 0) return FileEncoding.utf16le;
  107. if (startBytes.Length == 2 && startBytes[0] == 0) return FileEncoding.utf16be;
  108. if (startBytes[0] == 0 && startBytes[1] == 0) return FileEncoding.utf32be;
  109. if (startBytes[0] == 0 && startBytes[1] != 0) return FileEncoding.utf16be;
  110. if (startBytes[0] != 0 && startBytes[1] == 0) return FileEncoding.utf16le;
  111. return FileEncoding.utf8;
  112. }
  113. }
  114. FileEncoding GetEncoding(EncodingClass encodingClass, UnicodeDetection detection, string path)
  115. {
  116. switch (encodingClass)
  117. {
  118. case EncodingClass.ascii: return FileEncoding.ascii;
  119. case EncodingClass.unicode:
  120. {
  121. if (detection == UnicodeDetection.FirstCharIsAscii)
  122. {
  123. return DetermineUnicodeWhenFirstCharIsAscii(path);
  124. }
  125. else if (detection == UnicodeDetection.BOM)
  126. {
  127. return FileEncoding.uniCodeBOM;
  128. }
  129. else return FileEncoding.unicode;
  130. }
  131. case EncodingClass.utf8: return FileEncoding.utf8;
  132. case EncodingClass.binary: return FileEncoding.binary;
  133. }
  134. return FileEncoding.none;
  135. }
  136. string path_;
  137. public readonly FileEncoding encoding_;
  138. }
  139. #endregion Input File Support
  140. #region Error handling
  141. public class PegException : System.Exception
  142. {
  143. public PegException()
  144. : base("Fatal parsing error ocurred")
  145. {
  146. }
  147. }
  148. public struct PegError
  149. {
  150. internal SortedList<int, int> lineStarts;
  151. void AddLineStarts(string s, int first, int last, ref int lineNo, out int colNo)
  152. {
  153. colNo = 2;
  154. for (int i = first + 1; i <= last; ++i, ++colNo)
  155. {
  156. if (s[i - 1] == '\n')
  157. {
  158. lineStarts[i] = ++lineNo;
  159. colNo = 1;
  160. }
  161. }
  162. --colNo;
  163. }
  164. public void GetLineAndCol(string s, int pos, out int lineNo, out int colNo)
  165. {
  166. for (int i = lineStarts.Count(); i > 0; --i)
  167. {
  168. KeyValuePair<int, int> curLs = lineStarts.ElementAt(i - 1);
  169. if (curLs.Key == pos)
  170. {
  171. lineNo = curLs.Value;
  172. colNo = 1;
  173. return;
  174. }
  175. if (curLs.Key < pos)
  176. {
  177. lineNo = curLs.Value;
  178. AddLineStarts(s, curLs.Key, pos, ref lineNo, out colNo);
  179. return;
  180. }
  181. }
  182. lineNo = 1;
  183. AddLineStarts(s, 0, pos, ref lineNo, out colNo);
  184. }
  185. }
  186. #endregion Error handling
  187. #region Syntax/Parse-Tree related classes
  188. public enum ESpecialNodes { eFatal = -10001, eAnonymNTNode = -1000, eAnonymASTNode = -1001, eAnonymousNode = -100 }
  189. public enum ECreatorPhase { eCreate, eCreationComplete, eCreateAndComplete }
  190. public struct PegBegEnd//indices into the source string
  191. {
  192. public int Length
  193. {
  194. get { return posEnd_ - posBeg_; }
  195. }
  196. public string GetAsString(string src)
  197. {
  198. Debug.Assert(src.Length >= posEnd_);
  199. return src.Substring(posBeg_, Length);
  200. }
  201. public int posBeg_;
  202. public int posEnd_;
  203. }
  204. public class PegNode : ICloneable
  205. {
  206. #region Constructors
  207. public PegNode(PegNode parent, int id, PegBegEnd match, PegNode child, PegNode next)
  208. {
  209. parent_ = parent; id_ = id; child_ = child; next_ = next;
  210. match_ = match;
  211. }
  212. public PegNode(PegNode parent, int id, PegBegEnd match, PegNode child)
  213. : this(parent, id, match, child, null)
  214. {
  215. }
  216. public PegNode(PegNode parent, int id, PegBegEnd match)
  217. : this(parent, id, match, null, null)
  218. { }
  219. public PegNode(PegNode parent, int id)
  220. : this(parent, id, new PegBegEnd(), null, null)
  221. {
  222. }
  223. #endregion Constructors
  224. #region Public Members
  225. public virtual string GetAsString(string s)
  226. {
  227. return match_.GetAsString(s);
  228. }
  229. public virtual PegNode Clone()
  230. {
  231. PegNode clone= new PegNode(parent_, id_, match_);
  232. CloneSubTrees(clone);
  233. return clone;
  234. }
  235. #endregion Public Members
  236. #region Protected Members
  237. protected void CloneSubTrees(PegNode clone)
  238. {
  239. PegNode child = null, next = null;
  240. if (child_ != null)
  241. {
  242. child = child_.Clone();
  243. child.parent_ = clone;
  244. }
  245. if (next_ != null)
  246. {
  247. next = next_.Clone();
  248. next.parent_ = clone;
  249. }
  250. clone.child_ = child;
  251. clone.next_ = next;
  252. }
  253. #endregion Protected Members
  254. #region Data Members
  255. public int id_;
  256. public PegNode parent_, child_, next_;
  257. public PegBegEnd match_;
  258. #endregion Data Members
  259. #region ICloneable Members
  260. object ICloneable.Clone()
  261. {
  262. return Clone();
  263. }
  264. #endregion
  265. }
  266. internal struct PegTree
  267. {
  268. internal enum AddPolicy { eAddAsChild, eAddAsSibling };
  269. internal PegNode root_;
  270. internal PegNode cur_;
  271. internal AddPolicy addPolicy;
  272. }
  273. public abstract class PrintNode
  274. {
  275. public abstract int LenMaxLine();
  276. public abstract bool IsLeaf(PegNode p);
  277. public virtual bool IsSkip(PegNode p) { return false; }
  278. public abstract void PrintNodeBeg(PegNode p, bool bAlignVertical, ref int nOffsetLineBeg, int nLevel);
  279. public abstract void PrintNodeEnd(PegNode p, bool bAlignVertical, ref int nOffsetLineBeg, int nLevel);
  280. public abstract int LenNodeBeg(PegNode p);
  281. public abstract int LenNodeEnd(PegNode p);
  282. public abstract void PrintLeaf(PegNode p, ref int nOffsetLineBeg, bool bAlignVertical);
  283. public abstract int LenLeaf(PegNode p);
  284. public abstract int LenDistNext(PegNode p, bool bAlignVertical, ref int nOffsetLineBeg, int nLevel);
  285. public abstract void PrintDistNext(PegNode p, bool bAlignVertical, ref int nOffsetLineBeg, int nLevel);
  286. }
  287. public class TreePrint : PrintNode
  288. {
  289. #region Data Members
  290. public delegate string GetNodeName(PegNode node);
  291. string src_;
  292. TextWriter treeOut_;
  293. int nMaxLineLen_;
  294. bool bVerbose_;
  295. GetNodeName GetNodeName_;
  296. #endregion Data Members
  297. #region Methods
  298. public TreePrint(TextWriter treeOut, string src, int nMaxLineLen, GetNodeName GetNodeName, bool bVerbose)
  299. {
  300. treeOut_ = treeOut;
  301. nMaxLineLen_ = nMaxLineLen;
  302. bVerbose_ = bVerbose;
  303. GetNodeName_ = GetNodeName;
  304. src_ = src;
  305. }
  306. public void PrintTree(PegNode parent, int nOffsetLineBeg, int nLevel)
  307. {
  308. if (IsLeaf(parent))
  309. {
  310. PrintLeaf(parent, ref nOffsetLineBeg, false);
  311. treeOut_.Flush();
  312. return;
  313. }
  314. bool bAlignVertical =
  315. DetermineLineLength(parent, nOffsetLineBeg) > LenMaxLine();
  316. PrintNodeBeg(parent, bAlignVertical, ref nOffsetLineBeg, nLevel);
  317. int nOffset = nOffsetLineBeg;
  318. for (PegNode p = parent.child_; p != null; p = p.next_)
  319. {
  320. if (IsSkip(p)) continue;
  321. if (IsLeaf(p))
  322. {
  323. PrintLeaf(p, ref nOffsetLineBeg, bAlignVertical);
  324. }
  325. else
  326. {
  327. PrintTree(p, nOffsetLineBeg, nLevel + 1);
  328. }
  329. if (bAlignVertical)
  330. {
  331. nOffsetLineBeg = nOffset;
  332. }
  333. while (p.next_ != null && IsSkip(p.next_)) p = p.next_;
  334. if (p.next_ != null)
  335. {
  336. PrintDistNext(p, bAlignVertical, ref nOffsetLineBeg, nLevel);
  337. }
  338. }
  339. PrintNodeEnd(parent, bAlignVertical, ref nOffsetLineBeg, nLevel);
  340. treeOut_.Flush();
  341. }
  342. int DetermineLineLength(PegNode parent, int nOffsetLineBeg)
  343. {
  344. int nLen = LenNodeBeg(parent);
  345. PegNode p;
  346. for (p = parent.child_; p != null; p = p.next_)
  347. {
  348. if (IsSkip(p)) continue;
  349. if (IsLeaf(p))
  350. {
  351. nLen += LenLeaf(p);
  352. }
  353. else
  354. {
  355. nLen += DetermineLineLength(p, nOffsetLineBeg);
  356. }
  357. if (nLen + nOffsetLineBeg > LenMaxLine())
  358. {
  359. return nLen + nOffsetLineBeg;
  360. }
  361. }
  362. nLen += LenNodeEnd(p);
  363. return nLen;
  364. }
  365. public override int LenMaxLine() { return nMaxLineLen_; }
  366. public override void
  367. PrintNodeBeg(PegNode p, bool bAlignVertical, ref int nOffsetLineBeg, int nLevel)
  368. {
  369. PrintIdAsName(p);
  370. treeOut_.Write("<");
  371. if (bAlignVertical)
  372. {
  373. treeOut_.WriteLine();
  374. treeOut_.Write(new string(' ', nOffsetLineBeg += 2));
  375. }
  376. else
  377. {
  378. ++nOffsetLineBeg;
  379. }
  380. }
  381. public override void
  382. PrintNodeEnd(PegNode p, bool bAlignVertical, ref int nOffsetLineBeg, int nLevel)
  383. {
  384. if (bAlignVertical)
  385. {
  386. treeOut_.WriteLine();
  387. treeOut_.Write(new string(' ', nOffsetLineBeg -= 2));
  388. }
  389. treeOut_.Write('>');
  390. if (!bAlignVertical)
  391. {
  392. ++nOffsetLineBeg;
  393. }
  394. }
  395. public override int LenNodeBeg(PegNode p) { return LenIdAsName(p) + 1; }
  396. public override int LenNodeEnd(PegNode p) { return 1; }
  397. public override void PrintLeaf(PegNode p, ref int nOffsetLineBeg, bool bAlignVertical)
  398. {
  399. if (bVerbose_)
  400. {
  401. PrintIdAsName(p);
  402. treeOut_.Write('<');
  403. }
  404. int len = p.match_.posEnd_ - p.match_.posBeg_;
  405. treeOut_.Write("'");
  406. if (len > 0)
  407. {
  408. treeOut_.Write(src_.Substring(p.match_.posBeg_, p.match_.posEnd_ - p.match_.posBeg_));
  409. }
  410. treeOut_.Write("'");
  411. if (bVerbose_) treeOut_.Write('>');
  412. }
  413. public override int LenLeaf(PegNode p)
  414. {
  415. int nLen = p.match_.posEnd_ - p.match_.posBeg_ + 2;
  416. if (bVerbose_) nLen += LenIdAsName(p) + 2;
  417. return nLen;
  418. }
  419. public override bool IsLeaf(PegNode p)
  420. {
  421. return p.child_ == null;
  422. }
  423. public override void
  424. PrintDistNext(PegNode p, bool bAlignVertical, ref int nOffsetLineBeg, int nLevel)
  425. {
  426. if (bAlignVertical)
  427. {
  428. treeOut_.WriteLine();
  429. treeOut_.Write(new string(' ', nOffsetLineBeg));
  430. }
  431. else
  432. {
  433. treeOut_.Write(' ');
  434. ++nOffsetLineBeg;
  435. }
  436. }
  437. public override int
  438. LenDistNext(PegNode p, bool bAlignVertical, ref int nOffsetLineBeg, int nLevel)
  439. {
  440. return 1;
  441. }
  442. int LenIdAsName(PegNode p)
  443. {
  444. string name = GetNodeName_(p);
  445. return name.Length;
  446. }
  447. void PrintIdAsName(PegNode p)
  448. {
  449. string name = GetNodeName_(p);
  450. treeOut_.Write(name);
  451. }
  452. #endregion Methods
  453. }
  454. #endregion Syntax/Parse-Tree related classes
  455. #region Parsers
  456. public abstract class PegBaseParser
  457. {
  458. #region Data Types
  459. public delegate bool Matcher();
  460. public delegate PegNode Creator(ECreatorPhase ePhase, PegNode parentOrCreated, int id);
  461. #endregion Data Types
  462. #region Data members
  463. protected int srcLen_;
  464. protected int pos_;
  465. protected bool bMute_;
  466. protected TextWriter errOut_;
  467. protected Creator nodeCreator_;
  468. protected int maxpos_;
  469. PegTree tree;
  470. #endregion Data members
  471. public virtual string GetRuleNameFromId(int id)
  472. {//normally overridden
  473. switch (id)
  474. {
  475. case (int)ESpecialNodes.eFatal: return "FATAL";
  476. case (int)ESpecialNodes.eAnonymNTNode: return "Nonterminal";
  477. case (int)ESpecialNodes.eAnonymASTNode: return "ASTNode";
  478. case (int)ESpecialNodes.eAnonymousNode: return "Node";
  479. default: return id.ToString();
  480. }
  481. }
  482. public virtual void GetProperties(out EncodingClass encoding, out UnicodeDetection detection)
  483. {
  484. encoding = EncodingClass.ascii;
  485. detection = UnicodeDetection.notApplicable;
  486. }
  487. public int GetMaximumPosition()
  488. {
  489. return maxpos_;
  490. }
  491. protected PegNode DefaultNodeCreator(ECreatorPhase phase, PegNode parentOrCreated, int id)
  492. {
  493. if (phase == ECreatorPhase.eCreate || phase == ECreatorPhase.eCreateAndComplete)
  494. return new PegNode(parentOrCreated, id);
  495. else
  496. {
  497. if (parentOrCreated.match_.posEnd_ > maxpos_)
  498. maxpos_ = parentOrCreated.match_.posEnd_;
  499. return null;
  500. }
  501. }
  502. #region Constructors
  503. public PegBaseParser(TextWriter errOut)
  504. {
  505. srcLen_ = pos_ = 0;
  506. errOut_ = errOut;
  507. nodeCreator_ = DefaultNodeCreator;
  508. }
  509. #endregion Constructors
  510. #region Reinitialization, TextWriter access,Tree Access
  511. public void Construct(TextWriter Fout)
  512. {
  513. srcLen_ = pos_ = 0;
  514. bMute_ = false;
  515. SetErrorDestination(Fout);
  516. ResetTree();
  517. }
  518. public void Rewind() { pos_ = 0; }
  519. public void SetErrorDestination(TextWriter errOut)
  520. {
  521. errOut_ = errOut == null ? new StreamWriter(System.Console.OpenStandardError())
  522. : errOut;
  523. }
  524. #endregion Reinitialization, TextWriter access,Tree Access
  525. #region Tree root access, Tree Node generation/display
  526. public PegNode GetRoot() { return tree.root_; }
  527. public void ResetTree()
  528. {
  529. tree.root_ = null;
  530. tree.cur_ = null;
  531. tree.addPolicy = PegTree.AddPolicy.eAddAsChild;
  532. }
  533. void AddTreeNode(int nId, PegTree.AddPolicy newAddPolicy, Creator createNode, ECreatorPhase ePhase)
  534. {
  535. if (bMute_) return;
  536. if (tree.root_ == null)
  537. {
  538. tree.root_ = tree.cur_ = createNode(ePhase, tree.cur_, nId);
  539. }
  540. else if (tree.addPolicy == PegTree.AddPolicy.eAddAsChild)
  541. {
  542. tree.cur_ = tree.cur_.child_ = createNode(ePhase, tree.cur_, nId);
  543. }
  544. else
  545. {
  546. tree.cur_ = tree.cur_.next_ = createNode(ePhase, tree.cur_.parent_, nId);
  547. }
  548. tree.addPolicy = newAddPolicy;
  549. }
  550. void RestoreTree(PegNode prevCur, PegTree.AddPolicy prevPolicy)
  551. {
  552. if (bMute_) return;
  553. if (prevCur == null)
  554. {
  555. tree.root_ = null;
  556. }
  557. else if (prevPolicy == PegTree.AddPolicy.eAddAsChild)
  558. {
  559. prevCur.child_ = null;
  560. }
  561. else
  562. {
  563. prevCur.next_ = null;
  564. }
  565. tree.cur_ = prevCur;
  566. tree.addPolicy = prevPolicy;
  567. }
  568. public bool TreeChars(Matcher toMatch)
  569. {
  570. return TreeCharsWithId((int)ESpecialNodes.eAnonymousNode, toMatch);
  571. }
  572. public bool TreeChars(Creator nodeCreator, Matcher toMatch)
  573. {
  574. return TreeCharsWithId(nodeCreator, (int)ESpecialNodes.eAnonymousNode, toMatch);
  575. }
  576. public bool TreeCharsWithId(int nId, Matcher toMatch)
  577. {
  578. return TreeCharsWithId(nodeCreator_, nId, toMatch);
  579. }
  580. public bool TreeCharsWithId(Creator nodeCreator, int nId, Matcher toMatch)
  581. {
  582. int pos = pos_;
  583. if (toMatch())
  584. {
  585. if (!bMute_)
  586. {
  587. AddTreeNode(nId, PegTree.AddPolicy.eAddAsSibling, nodeCreator, ECreatorPhase.eCreateAndComplete);
  588. tree.cur_.match_.posBeg_ = pos;
  589. tree.cur_.match_.posEnd_ = pos_;
  590. }
  591. return true;
  592. }
  593. return false;
  594. }
  595. public bool TreeNT(int nRuleId, Matcher toMatch)
  596. {
  597. return TreeNT(nodeCreator_, nRuleId, toMatch);
  598. }
  599. public bool TreeNT(Creator nodeCreator, int nRuleId, Matcher toMatch)
  600. {
  601. if (bMute_) return toMatch();
  602. PegNode prevCur = tree.cur_, ruleNode;
  603. PegTree.AddPolicy prevPolicy = tree.addPolicy;
  604. int posBeg = pos_;
  605. AddTreeNode(nRuleId, PegTree.AddPolicy.eAddAsChild, nodeCreator, ECreatorPhase.eCreate);
  606. ruleNode = tree.cur_;
  607. bool bMatches = toMatch();
  608. if (!bMatches) RestoreTree(prevCur, prevPolicy);
  609. else
  610. {
  611. ruleNode.match_.posBeg_ = posBeg;
  612. ruleNode.match_.posEnd_ = pos_;
  613. tree.cur_ = ruleNode;
  614. tree.addPolicy = PegTree.AddPolicy.eAddAsSibling;
  615. nodeCreator(ECreatorPhase.eCreationComplete, ruleNode, nRuleId);
  616. }
  617. return bMatches;
  618. }
  619. public bool TreeAST(int nRuleId, Matcher toMatch)
  620. {
  621. return TreeAST(nodeCreator_, nRuleId, toMatch);
  622. }
  623. public bool TreeAST(Creator nodeCreator, int nRuleId, Matcher toMatch)
  624. {
  625. if (bMute_) return toMatch();
  626. bool bMatches = TreeNT(nodeCreator, nRuleId, toMatch);
  627. if (bMatches)
  628. {
  629. if (tree.cur_.child_ != null && tree.cur_.child_.next_ == null && tree.cur_.parent_ != null)
  630. {
  631. if (tree.cur_.parent_.child_ == tree.cur_)
  632. {
  633. tree.cur_.parent_.child_ = tree.cur_.child_;
  634. tree.cur_.child_.parent_ = tree.cur_.parent_;
  635. tree.cur_ = tree.cur_.child_;
  636. }
  637. else
  638. {
  639. PegNode prev;
  640. for (prev = tree.cur_.parent_.child_; prev != null && prev.next_ != tree.cur_; prev = prev.next_)
  641. {
  642. }
  643. if (prev != null)
  644. {
  645. prev.next_ = tree.cur_.child_;
  646. tree.cur_.child_.parent_ = tree.cur_.parent_;
  647. tree.cur_ = tree.cur_.child_;
  648. }
  649. }
  650. }
  651. }
  652. return bMatches;
  653. }
  654. public bool TreeNT(Matcher toMatch)
  655. {
  656. return TreeNT((int)ESpecialNodes.eAnonymNTNode, toMatch);
  657. }
  658. public bool TreeNT(Creator nodeCreator, Matcher toMatch)
  659. {
  660. return TreeNT(nodeCreator, (int)ESpecialNodes.eAnonymNTNode, toMatch);
  661. }
  662. public bool TreeAST(Matcher toMatch)
  663. {
  664. return TreeAST((int)ESpecialNodes.eAnonymASTNode, toMatch);
  665. }
  666. public bool TreeAST(Creator nodeCreator, Matcher toMatch)
  667. {
  668. return TreeAST(nodeCreator, (int)ESpecialNodes.eAnonymASTNode, toMatch);
  669. }
  670. public virtual string TreeNodeToString(PegNode node)
  671. {
  672. return GetRuleNameFromId(node.id_);
  673. }
  674. public void SetNodeCreator(Creator nodeCreator)
  675. {
  676. Debug.Assert(nodeCreator != null);
  677. nodeCreator_ = nodeCreator;
  678. }
  679. #endregion Tree Node generation
  680. #region PEG e1 e2 .. ; &e1 ; !e1 ; e? ; e* ; e+ ; e{a,b} ; .
  681. public bool And(Matcher pegSequence)
  682. {
  683. PegNode prevCur = tree.cur_;
  684. PegTree.AddPolicy prevPolicy = tree.addPolicy;
  685. int pos0 = pos_;
  686. bool bMatches = pegSequence();
  687. if (!bMatches)
  688. {
  689. pos_ = pos0;
  690. RestoreTree(prevCur, prevPolicy);
  691. }
  692. return bMatches;
  693. }
  694. public bool Peek(Matcher toMatch)
  695. {
  696. int pos0 = pos_;
  697. bool prevMute = bMute_;
  698. bMute_ = true;
  699. bool bMatches = toMatch();
  700. bMute_ = prevMute;
  701. pos_ = pos0;
  702. return bMatches;
  703. }
  704. public bool Not(Matcher toMatch)
  705. {
  706. int pos0 = pos_;
  707. bool prevMute = bMute_;
  708. bMute_ = true;
  709. bool bMatches = toMatch();
  710. bMute_ = prevMute;
  711. pos_ = pos0;
  712. return !bMatches;
  713. }
  714. public bool PlusRepeat(Matcher toRepeat)
  715. {
  716. int i;
  717. for (i = 0; ; ++i)
  718. {
  719. int pos0 = pos_;
  720. if (!toRepeat())
  721. {
  722. pos_ = pos0;
  723. break;
  724. }
  725. }
  726. return i > 0;
  727. }
  728. public bool OptRepeat(Matcher toRepeat)
  729. {
  730. for (; ; )
  731. {
  732. int pos0 = pos_;
  733. if (!toRepeat())
  734. {
  735. pos_ = pos0;
  736. return true;
  737. }
  738. }
  739. }
  740. public bool Option(Matcher toMatch)
  741. {
  742. int pos0 = pos_;
  743. if (!toMatch()) pos_ = pos0;
  744. return true;
  745. }
  746. public bool ForRepeat(int count, Matcher toRepeat)
  747. {
  748. PegNode prevCur = tree.cur_;
  749. PegTree.AddPolicy prevPolicy = tree.addPolicy;
  750. int pos0 = pos_;
  751. int i;
  752. for (i = 0; i < count; ++i)
  753. {
  754. if (!toRepeat())
  755. {
  756. pos_ = pos0;
  757. RestoreTree(prevCur, prevPolicy);
  758. return false;
  759. }
  760. }
  761. return true;
  762. }
  763. public bool ForRepeat(int lower, int upper, Matcher toRepeat)
  764. {
  765. PegNode prevCur = tree.cur_;
  766. PegTree.AddPolicy prevPolicy = tree.addPolicy;
  767. int pos0 = pos_;
  768. int i;
  769. for (i = 0; i < upper; ++i)
  770. {
  771. if (!toRepeat()) break;
  772. }
  773. if (i < lower)
  774. {
  775. pos_ = pos0;
  776. RestoreTree(prevCur, prevPolicy);
  777. return false;
  778. }
  779. return true;
  780. }
  781. public bool Any()
  782. {
  783. if (pos_ < srcLen_)
  784. {
  785. ++pos_;
  786. return true;
  787. }
  788. return false;
  789. }
  790. #endregion PEG e1 e2 .. ; &e1 ; !e1 ; e? ; e* ; e+ ; e{a,b} ; .
  791. }
  792. public class PegByteParser : PegBaseParser
  793. {
  794. #region Data members
  795. protected byte[] src_;
  796. PegError errors;
  797. #endregion Data members
  798. #region PEG optimizations
  799. public sealed class BytesetData
  800. {
  801. public struct Range
  802. {
  803. public Range(byte low, byte high) { this.low = low; this.high = high; }
  804. public byte low;
  805. public byte high;
  806. }
  807. System.Collections.BitArray charSet_;
  808. bool bNegated_;
  809. public BytesetData(System.Collections.BitArray b)
  810. : this(b, false)
  811. {
  812. }
  813. public BytesetData(System.Collections.BitArray b, bool bNegated)
  814. {
  815. charSet_ = new System.Collections.BitArray(b);
  816. bNegated_ = bNegated;
  817. }
  818. public BytesetData(Range[] r, byte[] c)
  819. : this(r, c, false)
  820. {
  821. }
  822. public BytesetData(Range[] r, byte[] c, bool bNegated)
  823. {
  824. int max = 0;
  825. if (r != null) foreach (Range val in r) if (val.high > max) max = val.high;
  826. if (c != null) foreach (int val in c) if (val > max) max = val;
  827. charSet_ = new System.Collections.BitArray(max + 1, false);
  828. if (r != null)
  829. {
  830. foreach (Range val in r)
  831. {
  832. for (int i = val.low; i <= val.high; ++i)
  833. {
  834. charSet_[i] = true;
  835. }
  836. }
  837. }
  838. if (c != null) foreach (int val in c) charSet_[val] = true;
  839. bNegated_ = bNegated;
  840. }
  841. public bool Matches(byte c)
  842. {
  843. bool bMatches = c < charSet_.Length && charSet_[(int)c];
  844. if (bNegated_) return !bMatches;
  845. else return bMatches;
  846. }
  847. }
  848. /* public class BytesetData
  849. {
  850. public struct Range
  851. {
  852. public Range(byte low, byte high) { this.low = low; this.high = high; }
  853. public byte low;
  854. public byte high;
  855. }
  856. protected System.Collections.BitArray charSet_;
  857. bool bNegated_;
  858. public BytesetData(System.Collections.BitArray b, bool bNegated)
  859. {
  860. charSet_ = new System.Collections.BitArray(b);
  861. bNegated_ = bNegated;
  862. }
  863. public BytesetData(byte[] c, bool bNegated)
  864. {
  865. int max = 0;
  866. foreach (int val in c) if (val > max) max = val;
  867. charSet_ = new System.Collections.BitArray(max + 1, false);
  868. foreach (int val in c) charSet_[val] = true;
  869. bNegated_ = bNegated;
  870. }
  871. public BytesetData(Range[] r, byte[] c, bool bNegated)
  872. {
  873. int max = 0;
  874. foreach (Range val in r) if (val.high > max) max = val.high;
  875. foreach (int val in c) if (val > max) max = val;
  876. charSet_ = new System.Collections.BitArray(max + 1, false);
  877. foreach (Range val in r)
  878. {
  879. for (int i = val.low; i <= val.high; ++i)
  880. {
  881. charSet_[i] = true;
  882. }
  883. }
  884. foreach (int val in c) charSet_[val] = true;
  885. }
  886. public bool Matches(byte c)
  887. {
  888. bool bMatches = c < charSet_.Length && charSet_[(int)c];
  889. if (bNegated_) return !bMatches;
  890. else return bMatches;
  891. }
  892. }*/
  893. #endregion PEG optimizations
  894. #region Constructors
  895. public PegByteParser()
  896. : this(null)
  897. {
  898. }
  899. public PegByteParser(byte[] src):base(null)
  900. {
  901. SetSource(src);
  902. }
  903. public PegByteParser(byte[] src, TextWriter errOut):base(errOut)
  904. {
  905. SetSource(src);
  906. }
  907. #endregion Constructors
  908. #region Reinitialization, Source Code access, TextWriter access,Tree Access
  909. public void Construct(byte[] src, TextWriter Fout)
  910. {
  911. base.Construct(Fout);
  912. SetSource(src);
  913. }
  914. public void SetSource(byte[] src)
  915. {
  916. if (src == null) src = new byte[0];
  917. src_ = src; srcLen_ = src.Length;
  918. errors.lineStarts = new SortedList<int, int>();
  919. errors.lineStarts[0] = 1;
  920. }
  921. public byte[] GetSource() { return src_; }
  922. #endregion Reinitialization, Source Code access, TextWriter access,Tree Access
  923. #region Setting host variables
  924. public bool Into(Matcher toMatch,out byte[] into)
  925. {
  926. int pos = pos_;
  927. if (toMatch())
  928. {
  929. int nLen = pos_ - pos;
  930. into= new byte[nLen];
  931. for(int i=0;i<nLen;++i){
  932. into[i] = src_[i+pos];
  933. }
  934. return true;
  935. }
  936. else
  937. {
  938. into = null;
  939. return false;
  940. }
  941. }
  942. public bool Into(Matcher toMatch,out PegBegEnd begEnd)
  943. {
  944. begEnd.posBeg_ = pos_;
  945. bool bMatches = toMatch();
  946. begEnd.posEnd_ = pos_;
  947. return bMatches;
  948. }
  949. public bool Into(Matcher toMatch,out int into)
  950. {
  951. byte[] s;
  952. into = 0;
  953. if (!Into(toMatch,out s)) return false;
  954. into = 0;
  955. for (int i = 0; i < s.Length; ++i)
  956. {
  957. into <<= 8;
  958. into |= s[i];
  959. }
  960. return true;
  961. }
  962. public bool Into(Matcher toMatch,out double into)
  963. {
  964. byte[] s;
  965. into = 0.0;
  966. if (!Into(toMatch,out s)) return false;
  967. System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  968. string sAsString = encoding.GetString(s);
  969. if (!System.Double.TryParse(sAsString, out into)) return false;
  970. return true;
  971. }
  972. public bool BitsInto(int lowBitNo, int highBitNo,out int into)
  973. {
  974. if (pos_ < srcLen_)
  975. {
  976. into = (src_[pos_] >> (lowBitNo - 1)) & ((1 << highBitNo) - 1);
  977. ++pos_;
  978. return true;
  979. }
  980. into = 0;
  981. return false;
  982. }
  983. public bool BitsInto(int lowBitNo, int highBitNo, BytesetData toMatch, out int into)
  984. {
  985. if (pos_ < srcLen_)
  986. {
  987. byte value = (byte)((src_[pos_] >> (lowBitNo - 1)) & ((1 << highBitNo) - 1));
  988. ++pos_;
  989. into = value;
  990. return toMatch.Matches(value);
  991. }
  992. into = 0;
  993. return false;
  994. }
  995. #endregion Setting host variables
  996. #region Error handling
  997. void LogOutMsg(string sErrKind, string sMsg)
  998. {
  999. errOut_.WriteLine("<{0}>{1}:{2}", pos_, sErrKind, sMsg);
  1000. errOut_.Flush();
  1001. }
  1002. public virtual bool Fatal(string sMsg)
  1003. {
  1004. LogOutMsg("FATAL", sMsg);
  1005. throw new PegException();
  1006. }
  1007. public bool Warning(string sMsg)
  1008. {
  1009. LogOutMsg("WARNING", sMsg);
  1010. return true;
  1011. }
  1012. #endregion Error handling
  1013. #region PEG Bit level equivalents for PEG e1 ; &e1 ; !e1; e1:into ;
  1014. public bool Bits(int lowBitNo, int highBitNo, byte toMatch)
  1015. {
  1016. if (pos_ < srcLen_ && ((src_[pos_] >> (lowBitNo - 1)) & ((1 << highBitNo) - 1)) == toMatch)
  1017. {
  1018. ++pos_;
  1019. return true;
  1020. }
  1021. return false;
  1022. }
  1023. public bool Bits(int lowBitNo, int highBitNo,BytesetData toMatch)
  1024. {
  1025. if( pos_ < srcLen_ )
  1026. {
  1027. byte value= (byte)((src_[pos_] >> (lowBitNo - 1)) & ((1 << highBitNo) - 1));
  1028. ++pos_;
  1029. return toMatch.Matches(value);
  1030. }
  1031. return false;
  1032. }
  1033. public bool PeekBits(int lowBitNo, int highBitNo, byte toMatch)
  1034. {
  1035. return pos_ < srcLen_ && ((src_[pos_] >> (lowBitNo - 1)) & ((1 << highBitNo) - 1)) == toMatch;
  1036. }
  1037. public bool NotBits(int lowBitNo, int highBitNo, byte toMatch)
  1038. {
  1039. return !(pos_ < srcLen_ && ((src_[pos_] >> (lowBitNo - 1)) & ((1 << highBitNo) - 1)) == toMatch);
  1040. }
  1041. public bool IntoBits(int lowBitNo,int highBitNo,out int val)
  1042. {
  1043. return BitsInto(lowBitNo,highBitNo,out val);
  1044. }
  1045. public bool IntoBits(int lowBitNo, int highBitNo, BytesetData toMatch, out int val)
  1046. {
  1047. return BitsInto(lowBitNo, highBitNo, out val);
  1048. }
  1049. public bool Bit(int bitNo,byte toMatch)
  1050. {
  1051. if (pos_ < srcLen_ && ((src_[pos_]>>(bitNo-1))&1)==toMatch){
  1052. ++pos_;
  1053. return true;
  1054. }
  1055. return false;
  1056. }
  1057. public bool PeekBit(int bitNo, byte toMatch)
  1058. {
  1059. return pos_ < srcLen_ && ((src_[pos_] >> (bitNo - 1)) & 1) == toMatch;
  1060. }
  1061. public bool NotBit(int bitNo, byte toMatch)
  1062. {
  1063. return !(pos_ < srcLen_ && ((src_[pos_] >> (bitNo - 1)) & 1) == toMatch);
  1064. }
  1065. #endregion PEG Bit level equivalents for PEG e1 ; &e1 ; !e1; e1:into ;
  1066. #region PEG '<Literal>' / '<Literal>'/i / [low1-high1,low2-high2..] / [<CharList>]
  1067. public bool Char(byte c1)
  1068. {
  1069. if (pos_ < srcLen_ && src_[pos_] == c1)
  1070. { ++pos_; return true; }
  1071. return false;
  1072. }
  1073. public bool Char(byte c1, byte c2)
  1074. {
  1075. if (pos_ + 1 < srcLen_
  1076. && src_[pos_] == c1
  1077. && src_[pos_ + 1] == c2)
  1078. { pos_ += 2; return true; }
  1079. return false;
  1080. }
  1081. public bool Char(byte c1, byte c2, byte c3)
  1082. {
  1083. if (pos_ + 2 < srcLen_
  1084. && src_[pos_] == c1
  1085. && src_[pos_ + 1] == c2
  1086. && src_[pos_ + 2] == c3)
  1087. { pos_ += 3; return true; }
  1088. return false;
  1089. }
  1090. public bool Char(byte c1, byte c2, byte c3, byte c4)
  1091. {
  1092. if (pos_ + 3 < srcLen_
  1093. && src_[pos_] == c1
  1094. && src_[pos_ + 1] == c2
  1095. && src_[pos_ + 2] == c3
  1096. && src_[pos_ + 3] == c4)
  1097. { pos_ += 4; return true; }
  1098. return false;
  1099. }
  1100. public bool Char(byte c1, byte c2, byte c3, byte c4, byte c5)
  1101. {
  1102. if (pos_ + 4 < srcLen_
  1103. && src_[pos_] == c1
  1104. && src_[pos_ + 1] == c2
  1105. && src_[pos_ + 2] == c3
  1106. && src_[pos_ + 3] == c4
  1107. && src_[pos_ + 4] == c5)
  1108. { pos_ += 5; return true; }
  1109. return false;
  1110. }
  1111. public bool Char(byte c1, byte c2, byte c3, byte c4, byte c5, byte c6)
  1112. {
  1113. if (pos_ + 5 < srcLen_
  1114. && src_[pos_] == c1
  1115. && src_[pos_ + 1] == c2
  1116. && src_[pos_ + 2] == c3
  1117. && src_[pos_ + 3] == c4
  1118. && src_[pos_ + 4] == c5
  1119. && src_[pos_ + 5] == c6)
  1120. { pos_ += 6; return true; }
  1121. return false;
  1122. }
  1123. public bool Char(byte c1, byte c2, byte c3, byte c4, byte c5, byte c6, byte c7)
  1124. {
  1125. if (pos_ + 6 < srcLen_
  1126. && src_[pos_] == c1
  1127. && src_[pos_ + 1] == c2
  1128. && src_[pos_ + 2] == c3
  1129. && src_[pos_ + 3] == c4
  1130. && src_[pos_ + 4] == c5
  1131. && src_[pos_ + 5] == c6
  1132. && src_[pos_ + 6] == c7)
  1133. { pos_ += 7; return true; }
  1134. return false;
  1135. }
  1136. public bool Char(byte c1, byte c2, byte c3, byte c4, byte c5, byte c6, byte c7, byte c8)
  1137. {
  1138. if (pos_ + 7 < srcLen_
  1139. && src_[pos_] == c1
  1140. && src_[pos_ + 1] == c2
  1141. && src_[pos_ + 2] == c3
  1142. && src_[pos_ + 3] == c4
  1143. && src_[pos_ + 4] == c5
  1144. && src_[pos_ + 5] == c6
  1145. && src_[pos_ + 6] == c7
  1146. && src_[pos_ + 7] == c8)
  1147. { pos_ += 8; return true; }
  1148. return false;
  1149. }
  1150. public bool Char(byte[] s)
  1151. {
  1152. int sLength = s.Length;
  1153. if (pos_ + sLength > srcLen_) return false;
  1154. for (int i = 0; i < sLength; ++i)
  1155. {
  1156. if (s[i] != src_[pos_ + i]) return false;
  1157. }
  1158. pos_ += sLength;
  1159. return true;
  1160. }
  1161. public static byte ToUpper(byte c)
  1162. {
  1163. if (c >= 97 && c <= 122) return (byte)(c - 32); else return c;
  1164. }
  1165. public bool IChar(byte c1)
  1166. {
  1167. if (pos_ < srcLen_ && ToUpper(src_[pos_]) == c1)
  1168. { ++pos_; return true; }
  1169. return false;
  1170. }
  1171. public bool IChar(byte c1, byte c2)
  1172. {
  1173. if (pos_ + 1 < srcLen_
  1174. && ToUpper(src_[pos_]) == ToUpper(c1)
  1175. && ToUpper(src_[pos_ + 1]) == ToUpper(c2))
  1176. { pos_ += 2; return true; }
  1177. return false;
  1178. }
  1179. public bool IChar(byte c1, byte c2, byte c3)
  1180. {
  1181. if (pos_ + 2 < srcLen_
  1182. && ToUpper(src_[pos_]) == ToUpper(c1)
  1183. && ToUpper(src_[pos_ + 1]) == ToUpper(c2)
  1184. && ToUpper(src_[pos_ + 2]) == ToUpper(c3))
  1185. { pos_ += 3; return true; }
  1186. return false;
  1187. }
  1188. public bool IChar(byte c1, byte c2, byte c3, byte c4)
  1189. {
  1190. if (pos_ + 3 < srcLen_
  1191. && ToUpper(src_[pos_]) == ToUpper(c1)
  1192. && ToUpper(src_[pos_ + 1]) == ToUpper(c2)
  1193. && ToUpper(src_[pos_ + 2]) == ToUpper(c3)
  1194. && ToUpper(src_[pos_ + 3]) == ToUpper(c4))
  1195. { pos_ += 4; return true; }
  1196. return false;
  1197. }
  1198. public bool IChar(byte c1, byte c2, byte c3, byte c4, byte c5)
  1199. {
  1200. if (pos_ + 4 < srcLen_
  1201. && ToUpper(src_[pos_]) == ToUpper(c1)
  1202. && ToUpper(src_[pos_ + 1]) == ToUpper(c2)
  1203. && ToUpper(src_[pos_ + 2]) == ToUpper(c3)
  1204. && ToUpper(src_[pos_ + 3]) == ToUpper(c4)
  1205. && ToUpper(src_[pos_ + 4]) == ToUpper(c5))
  1206. { pos_ += 5; return true; }
  1207. return false;
  1208. }
  1209. public bool IChar(byte c1, byte c2, byte c3, byte c4, byte c5, byte c6)
  1210. {
  1211. if (pos_ + 5 < srcLen_
  1212. && ToUpper(src_[pos_]) == ToUpper(c1)
  1213. && ToUpper(src_[pos_ + 1]) == ToUpper(c2)
  1214. && ToUpper(src_[pos_ + 2]) == ToUpper(c3)
  1215. && ToUpper(src_[pos_ + 3]) == ToUpper(c4)
  1216. && ToUpper(src_[pos_ + 4]) == ToUpper(c5)
  1217. && ToUpper(src_[pos_ + 5]) == ToUpper(c6))
  1218. { pos_ += 6; return true; }
  1219. return false;
  1220. }
  1221. public bool IChar(byte c1, byte c2, byte c3, byte c4, byte c5, byte c6, byte c7)
  1222. {
  1223. if (pos_ + 6 < srcLen_
  1224. && ToUpper(src_[pos_]) == ToUpper(c1)
  1225. && ToUpper(src_[pos_ + 1]) == ToUpper(c2)
  1226. && ToUpper(src_[pos_ + 2]) == ToUpper(c3)
  1227. && ToUpper(src_[pos_ + 3]) == ToUpper(c4)
  1228. && ToUpper(src_[pos_ + 4]) == ToUpper(c5)
  1229. && ToUpper(src_[pos_ + 5]) == ToUpper(c6)
  1230. && ToUpper(src_[pos_ + 6]) == ToUpper(c7))
  1231. { pos_ += 7; return true; }
  1232. return false;
  1233. }
  1234. public bool IChar(byte[] s)
  1235. {
  1236. int sLength = s.Length;
  1237. if (pos_ + sLength > srcLen_) return false;
  1238. for (int i = 0; i < sLength; ++i)
  1239. {
  1240. if (s[i] != ToUpper(src_[pos_ + i])) return false;
  1241. }
  1242. pos_ += sLength;
  1243. return true;
  1244. }
  1245. public bool In(byte c0, byte c1)
  1246. {
  1247. if (pos_ < srcLen_
  1248. && src_[pos_] >= c0 && src_[pos_] <= c1)
  1249. {
  1250. ++pos_;
  1251. return true;
  1252. }
  1253. return false;
  1254. }
  1255. public bool In(byte c0, byte c1, byte c2, byte c3)
  1256. {
  1257. if (pos_ < srcLen_)
  1258. {
  1259. byte c = src_[pos_];
  1260. if (c >= c0 && c <= c1
  1261. || c >= c2 && c <= c3)
  1262. {
  1263. ++pos_;
  1264. return true;
  1265. }
  1266. }
  1267. return false;
  1268. }
  1269. public bool In(byte c0, byte c1, byte c2, byte c3, byte c4, byte c5)
  1270. {
  1271. if (pos_ < srcLen_)
  1272. {
  1273. byte c = src_[pos_];
  1274. if (c >= c0 && c <= c1
  1275. || c >= c2 && c <= c3
  1276. || c >= c4 && c <= c5)
  1277. {
  1278. ++pos_;
  1279. return true;
  1280. }
  1281. }
  1282. return false;
  1283. }
  1284. public bool In(byte c0, byte c1, byte c2, byte c3, byte c4, byte c5, byte c6, byte c7)
  1285. {
  1286. if (pos_ < srcLen_)
  1287. {
  1288. byte c = src_[pos_];
  1289. if (c >= c0 && c <= c1
  1290. || c >= c2 && c <= c3
  1291. || c >= c4 && c <= c5
  1292. || c >= c6 && c <= c7)
  1293. {
  1294. ++pos_;
  1295. return true;
  1296. }
  1297. }
  1298. return false;
  1299. }
  1300. public bool In(byte[] s)
  1301. {
  1302. if (pos_ < srcLen_)
  1303. {
  1304. byte c = src_[pos_];
  1305. for (int i = 0; i < s.Length - 1; i += 2)
  1306. {
  1307. if (c >= s[i] && c <= s[i + 1])
  1308. {
  1309. ++pos_;
  1310. return true;
  1311. }
  1312. }
  1313. }
  1314. return false;
  1315. }
  1316. public bool NotIn(byte[] s)
  1317. {
  1318. if (pos_ < srcLen_)
  1319. {
  1320. byte c = src_[pos_];
  1321. for (int i = 0; i < s.Length - 1; i += 2)
  1322. {
  1323. if ( c >= s[i] && c <= s[i + 1] ) return false;
  1324. }
  1325. ++pos_;
  1326. return true;
  1327. }
  1328. return false;
  1329. }
  1330. public bool OneOf(byte c0, byte c1)
  1331. {
  1332. if (pos_ < srcLen_
  1333. && (src_[pos_] == c0 || src_[pos_] == c1))
  1334. {
  1335. ++pos_;
  1336. return true;
  1337. }
  1338. return false;
  1339. }
  1340. public bool OneOf(byte c0, byte c1, byte c2)
  1341. {
  1342. if (pos_ < srcLen_)
  1343. {
  1344. byte c = src_[pos_];
  1345. if (c == c0 || c == c1 || c == c2)
  1346. {
  1347. ++pos_;
  1348. return true;
  1349. }
  1350. }
  1351. return false;
  1352. }
  1353. public bool OneOf(byte c0, byte c1, byte c2, byte c3)
  1354. {
  1355. if (pos_ < srcLen_)
  1356. {
  1357. byte c = src_[pos_];
  1358. if (c == c0 || c == c1 || c == c2 || c == c3)
  1359. {
  1360. ++pos_;
  1361. return true;
  1362. }
  1363. }
  1364. return false;
  1365. }
  1366. public bool OneOf(byte c0, byte c1, byte c2, byte c3, byte c4)
  1367. {
  1368. if (pos_ < srcLen_)
  1369. {
  1370. byte c = src_[pos_];
  1371. if (c == c0 || c == c1 || c == c2 || c == c3 || c == c4)
  1372. {
  1373. ++pos_;
  1374. return true;
  1375. }
  1376. }
  1377. return false;
  1378. }
  1379. public bool OneOf(byte c0, byte c1, byte c2, byte c3, byte c4, byte c5)
  1380. {
  1381. if (pos_ < srcLen_)
  1382. {
  1383. byte c = src_[pos_];
  1384. if (c == c0 || c == c1 || c == c2 || c == c3 || c == c4 || c == c5)
  1385. {
  1386. ++pos_;
  1387. return true;
  1388. }
  1389. }
  1390. return false;
  1391. }
  1392. public bool OneOf(byte c0, byte c1, byte c2, byte c3, byte c4, byte c5, byte c6)
  1393. {
  1394. if (pos_ < srcLen_)
  1395. {
  1396. byte c = src_[pos_];
  1397. if (c == c0 || c == c1 || c == c2 || c == c3 || c == c4 || c == c5 || c == c6)
  1398. {
  1399. ++pos_;
  1400. return true;
  1401. }
  1402. }
  1403. return false;
  1404. }
  1405. public bool OneOf(byte c0, byte c1, byte c2, byte c3, byte c4, byte c5, byte c6, byte c7)
  1406. {
  1407. if (pos_ < srcLen_)
  1408. {
  1409. byte c = src_[pos_];
  1410. if (c == c0 || c == c1 || c == c2 || c == c3 || c == c4 || c == c5 || c == c6 || c == c7)
  1411. {
  1412. ++pos_;
  1413. return true;
  1414. }
  1415. }
  1416. return false;
  1417. }
  1418. public bool OneOf(byte[] s)
  1419. {
  1420. if (pos_ < srcLen_)
  1421. {
  1422. byte c = src_[pos_];
  1423. for (int i = 0; i < s.Length; ++i)
  1424. {
  1425. if (c == s[i]) { ++pos_; return true; }
  1426. }
  1427. }
  1428. return false;
  1429. }
  1430. public bool NotOneOf(byte[] s)
  1431. {
  1432. if (pos_ < srcLen_)
  1433. {
  1434. byte c = src_[pos_];
  1435. for (int i = 0; i < s.Length; ++i)
  1436. {
  1437. if (c == s[i]) { return false; }
  1438. }
  1439. return true;
  1440. }
  1441. return false;
  1442. }
  1443. public bool OneOf(BytesetData bset)
  1444. {
  1445. if(pos_ < srcLen_ && bset.Matches(src_[pos_]))
  1446. {
  1447. ++pos_; return true;
  1448. }
  1449. return false;
  1450. }
  1451. #endregion PEG '<Literal>' / '<Literal>'/i / [low1-high1,low2-high2..] / [<CharList>]
  1452. }
  1453. public class PegCharParser : PegBaseParser
  1454. {
  1455. #region Data members
  1456. protected string src_;
  1457. PegError errors;
  1458. #endregion Data members
  1459. #region PEG optimizations
  1460. public sealed class OptimizedCharset
  1461. {
  1462. public struct Range
  1463. {
  1464. public Range(char low, char high) { this.low = low; this.high = high; }
  1465. public char low;
  1466. public char high;
  1467. }
  1468. System.Collections.BitArray charSet_;
  1469. bool bNegated_;
  1470. public OptimizedCharset(System.Collections.BitArray b)
  1471. : this(b, false)
  1472. {
  1473. }
  1474. public OptimizedCharset(System.Collections.BitArray b, bool bNegated)
  1475. {
  1476. charSet_ = new System.Collections.BitArray(b);
  1477. bNegated_ = bNegated;
  1478. }
  1479. public OptimizedCharset(Range[] r, char[] c)
  1480. : this(r, c, false)
  1481. {
  1482. }
  1483. public OptimizedCharset(Range[] r, char[] c, bool bNegated)
  1484. {
  1485. int max = 0;
  1486. if (r != null) foreach (Range val in r) if (val.high > max) max = val.high;
  1487. if (c != null) foreach (int val in c) if (val > max) max = val;
  1488. charSet_ = new System.Collections.BitArray(max + 1, false);
  1489. if (r != null)
  1490. {
  1491. foreach (Range val in r)
  1492. {
  1493. for (int i = val.low; i <= val.high; ++i)
  1494. {
  1495. charSet_[i] = true;
  1496. }
  1497. }
  1498. }
  1499. if (c != null) foreach (int val in c) charSet_[val] = true;
  1500. bNegated_ = bNegated;
  1501. }
  1502. public bool Matches(char c)
  1503. {
  1504. bool bMatches = c < charSet_.Length && charSet_[(int)c];
  1505. if (bNegated_) return !bMatches;
  1506. else return bMatches;
  1507. }
  1508. }
  1509. public sealed class OptimizedLiterals
  1510. {
  1511. internal class Trie
  1512. {
  1513. internal Trie(char cThis,int nIndex, string[] literals)
  1514. {
  1515. cThis_ = cThis;
  1516. char cMax = char.MinValue;
  1517. cMin_ = char.MaxValue;
  1518. HashSet<char> followChars = new HashSet<char>();
  1519. foreach (string literal in literals)
  1520. {
  1521. if (literal==null || nIndex > literal.Length ) continue;
  1522. if (nIndex == literal.Length)
  1523. {
  1524. bLitEnd_ = true;
  1525. continue;
  1526. }
  1527. char c = literal[nIndex];
  1528. followChars.Add(c);
  1529. if ( c < cMin_) cMin_ = c;
  1530. if ( c > cMax) cMax = c;
  1531. }
  1532. if (followChars.Count == 0)
  1533. {
  1534. children_ = null;
  1535. }
  1536. else
  1537. {
  1538. children_ = new Trie[(cMax - cMin_) + 1];
  1539. foreach (char c in followChars)
  1540. {
  1541. List<string> subLiterals = new List<string>();
  1542. foreach (string s in literals)
  1543. {
  1544. if ( nIndex >= s.Length ) continue;
  1545. if (c == s[nIndex])
  1546. {
  1547. subLiterals.Add(s);
  1548. }
  1549. }
  1550. children_[c - cMin_] = new Trie(c, nIndex + 1, subLiterals.ToArray());
  1551. }
  1552. }
  1553. }
  1554. internal char cThis_; //character stored in this node
  1555. internal bool bLitEnd_; //end of literal
  1556. internal char cMin_; //first valid character in children
  1557. internal Trie[] children_; //contains the successor node of cThis_;
  1558. }
  1559. internal Trie literalsRoot;
  1560. public OptimizedLiterals(string[] litAlternatives)
  1561. {
  1562. literalsRoot = new Trie('\u0000', 0, litAlternatives);
  1563. }
  1564. }
  1565. #endregion PEG optimizations
  1566. #region Constructors
  1567. public PegCharParser():this("")
  1568. {
  1569. }
  1570. public PegCharParser(string src):base(null)
  1571. {
  1572. SetSource(src);
  1573. }
  1574. public PegCharParser(string src, TextWriter errOut):base(errOut)
  1575. {
  1576. SetSource(src);
  1577. nodeCreator_ = DefaultNodeCreator;
  1578. }
  1579. #endregion Constructors
  1580. #region Overrides
  1581. public override string TreeNodeToString(PegNode node)
  1582. {
  1583. string label = base.TreeNodeToString(node);
  1584. if (node.id_ == (int)ESpecialNodes.eAnonymousNode)
  1585. {
  1586. string value = node.GetAsString(src_);
  1587. if (value.Length < 32) label += " <" + value + ">";
  1588. else label += " <" + value.Substring(0, 29) + "...>";
  1589. }
  1590. return label;
  1591. }
  1592. #endregion Overrides
  1593. #region Reinitialization, Source Code access, TextWriter access,Tree Access
  1594. public void Construct(string src, TextWriter Fout)
  1595. {
  1596. base.Construct(Fout);
  1597. SetSource(src);
  1598. }
  1599. public void SetSource(string src)
  1600. {
  1601. if (src == null) src = "";
  1602. src_ = src; srcLen_ = src.Length; pos_ = 0;
  1603. errors.lineStarts = new SortedList<int, int>();
  1604. errors.lineStarts[0] = 1;
  1605. }
  1606. public string GetSource() { return src_; }
  1607. #endregion Reinitialization, Source Code access, TextWriter access,Tree Access
  1608. #region Setting host variables
  1609. public bool Into(Matcher toMatch,out string into)
  1610. {
  1611. int pos = pos_;
  1612. if (toMatch())
  1613. {
  1614. into = src_.Substring(pos, pos_ - pos);
  1615. return true;
  1616. }
  1617. else
  1618. {
  1619. into = "";
  1620. return false;
  1621. }
  1622. }
  1623. public bool Into(Matcher toMatch,out PegBegEnd begEnd)
  1624. {
  1625. begEnd.posBeg_ = pos_;
  1626. bool bMatches = toMatch();
  1627. begEnd.posEnd_ = pos_;
  1628. return bMatches;
  1629. }
  1630. public bool Into(Matcher toMatch,out int into)
  1631. {
  1632. string s;
  1633. into = 0;
  1634. if (!Into(toMatch,out s)) return false;
  1635. if (!System.Int32.TryParse(s, out into)) return false;
  1636. return true;
  1637. }
  1638. public bool Into(Matcher toMatch,out double into)
  1639. {
  1640. string s;
  1641. into = 0.0;
  1642. if (!Into(toMatch,out s)) return false;
  1643. if (!System.Double.TryParse(s, out into)) return false;
  1644. return true;
  1645. }
  1646. #endregion Setting host variables
  1647. #region Error handling
  1648. void LogOutMsg(string sErrKind, string sMsg)
  1649. {
  1650. int lineNo, colNo;
  1651. errors.GetLineAndCol(src_, pos_, out lineNo, out colNo);
  1652. errOut_.WriteLine("<{0},{1},{2}>{3}:{4}", lineNo, colNo, maxpos_, sErrKind, sMsg);
  1653. errOut_.Flush();
  1654. }
  1655. public virtual bool Fatal(string sMsg)
  1656. {
  1657. LogOutMsg("FATAL", sMsg);
  1658. throw new PegException();
  1659. //return false;
  1660. }
  1661. public bool Warning(string sMsg)
  1662. {
  1663. LogOutMsg("WARNING", sMsg);
  1664. return true;
  1665. }
  1666. #endregion Error handling
  1667. #region PEG optimized version of e* ; e+
  1668. public bool OptRepeat(OptimizedCharset charset)
  1669. {
  1670. for (; pos_ < srcLen_ && charset.Matches(src_[pos_]); ++pos_) ;
  1671. return true;
  1672. }
  1673. public bool PlusRepeat(OptimizedCharset charset)
  1674. {
  1675. int pos0 = pos_;
  1676. for (; pos_ < srcLen_ && charset.Matches(src_[pos_]); ++pos_) ;
  1677. return pos_ > pos0;
  1678. }
  1679. #endregion PEG optimized version of e* ; e+
  1680. #region PEG '<Literal>' / '<Literal>'/i / [low1-high1,low2-high2..] / [<CharList>]
  1681. public bool Char(char c1)
  1682. {
  1683. if (pos_ < srcLen_ && src_[pos_] == c1)
  1684. { ++pos_; return true; }
  1685. return false;
  1686. }
  1687. public bool Char(char c1, char c2)
  1688. {
  1689. if (pos_ + 1 < srcLen_
  1690. && src_[pos_] == c1
  1691. && src_[pos_ + 1] == c2)
  1692. { pos_ += 2; return true; }
  1693. return false;
  1694. }
  1695. public bool Char(char c1, char c2, char c3)
  1696. {
  1697. if (pos_ + 2 < srcLen_
  1698. && src_[pos_] == c1
  1699. && src_[pos_ + 1] == c2
  1700. && src_[pos_ + 2] == c3)
  1701. { pos_ += 3; return true; }
  1702. return false;
  1703. }
  1704. public bool Char(char c1, char c2, char c3, char c4)
  1705. {
  1706. if (pos_ + 3 < srcLen_
  1707. && src_[pos_] == c1
  1708. && src_[pos_ + 1] == c2
  1709. && src_[pos_ + 2] == c3
  1710. && src_[pos_ + 3] == c4)
  1711. { pos_ += 4; return true; }
  1712. return false;
  1713. }
  1714. public bool Char(char c1, char c2, char c3, char c4, char c5)
  1715. {
  1716. if (pos_ + 4 < srcLen_
  1717. && src_[pos_] == c1
  1718. && src_[pos_ + 1] == c2
  1719. && src_[pos_ + 2] == c3
  1720. && src_[pos_ + 3] == c4
  1721. && src_[pos_ + 4] == c5)
  1722. { pos_ += 5; return true; }
  1723. return false;
  1724. }
  1725. public bool Char(char c1, char c2, char c3, char c4, char c5, char c6)
  1726. {
  1727. if (pos_ + 5 < srcLen_
  1728. && src_[pos_] == c1
  1729. && src_[pos_ + 1] == c2
  1730. && src_[pos_ + 2] == c3
  1731. && src_[pos_ + 3] == c4
  1732. && src_[pos_ + 4] == c5
  1733. && src_[pos_ + 5] == c6)
  1734. { pos_ += 6; return true; }
  1735. return false;
  1736. }
  1737. public bool Char(char c1, char c2, char c3, char c4, char c5, char c6, char c7)
  1738. {
  1739. if (pos_ + 6 < srcLen_
  1740. && src_[pos_] == c1
  1741. && src_[pos_ + 1] == c2
  1742. && src_[pos_ + 2] == c3
  1743. && src_[pos_ + 3] == c4
  1744. && src_[pos_ + 4] == c5
  1745. && src_[pos_ + 5] == c6
  1746. && src_[pos_ + 6] == c7)
  1747. { pos_ += 7; return true; }
  1748. return false;
  1749. }
  1750. public bool Char(char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8)
  1751. {
  1752. if (pos_ + 7 < srcLen_
  1753. && src_[pos_] == c1
  1754. && src_[pos_ + 1] == c2
  1755. && src_[pos_ + 2] == c3
  1756. && src_[pos_ + 3] == c4
  1757. && src_[pos_ + 4] == c5
  1758. && src_[pos_ + 5] == c6
  1759. && src_[pos_ + 6] == c7
  1760. && src_[pos_ + 7] == c8)
  1761. { pos_ += 8; return true; }
  1762. return false;
  1763. }
  1764. public bool Char(string s)
  1765. {
  1766. int sLength = s.Length;
  1767. if (pos_ + sLength > srcLen_) return false;
  1768. for (int i = 0; i < sLength; ++i)
  1769. {
  1770. if (s[i] != src_[pos_ + i]) return false;
  1771. }
  1772. pos_ += sLength;
  1773. return true;
  1774. }
  1775. public bool IChar(char c1)
  1776. {
  1777. if (pos_ < srcLen_ && System.Char.ToUpper(src_[pos_]) == c1)
  1778. { ++pos_; return true; }
  1779. return false;
  1780. }
  1781. public bool IChar(char c1, char c2)
  1782. {
  1783. if (pos_ + 1 < srcLen_
  1784. && System.Char.ToUpper(src_[pos_]) == System.Char.ToUpper(c1)
  1785. && System.Char.ToUpper(src_[pos_ + 1]) == System.Char.ToUpper(c2))
  1786. { pos_ += 2; return true; }
  1787. return false;
  1788. }
  1789. public bool IChar(char c1, char c2, char c3)
  1790. {
  1791. if (pos_ + 2 < srcLen_
  1792. && System.Char.ToUpper(src_[pos_]) == System.Char.ToUpper(c1)
  1793. && System.Char.ToUpper(src_[pos_ + 1]) == System.Char.ToUpper(c2)
  1794. && System.Char.ToUpper(src_[pos_ + 2]) == System.Char.ToUpper(c3))
  1795. { pos_ += 3; return true; }
  1796. return false;
  1797. }
  1798. public bool IChar(char c1, char c2, char c3, char c4)
  1799. {
  1800. if (pos_ + 3 < srcLen_
  1801. && System.Char.ToUpper(src_[pos_]) == System.Char.ToUpper(c1)
  1802. && System.Char.ToUpper(src_[pos_ + 1]) == System.Char.ToUpper(c2)
  1803. && System.Char.ToUpper(src_[pos_ + 2]) == System.Char.ToUpper(c3)
  1804. && System.Char.ToUpper(src_[pos_ + 3]) == System.Char.ToUpper(c4))
  1805. { pos_ += 4; return true; }
  1806. return false;
  1807. }
  1808. public bool IChar(char c1, char c2, char c3, char c4, char c5)
  1809. {
  1810. if (pos_ + 4 < srcLen_
  1811. && System.Char.ToUpper(src_[pos_]) == System.Char.ToUpper(c1)
  1812. && System.Char.ToUpper(src_[pos_ + 1]) == System.Char.ToUpper(c2)
  1813. && System.Char.ToUpper(src_[pos_ + 2]) == System.Char.ToUpper(c3)
  1814. && System.Char.ToUpper(src_[pos_ + 3]) == System.Char.ToUpper(c4)
  1815. && System.Char.ToUpper(src_[pos_ + 4]) == System.Char.ToUpper(c5))
  1816. { pos_ += 5; return true; }
  1817. return false;
  1818. }
  1819. public bool IChar(char c1, char c2, char c3, char c4, char c5, char c6)
  1820. {
  1821. if (pos_ + 5 < srcLen_
  1822. && System.Char.ToUpper(src_[pos_]) == System.Char.ToUpper(c1)
  1823. && System.Char.ToUpper(src_[pos_ + 1]) == System.Char.ToUpper(c2)
  1824. && System.Char.ToUpper(src_[pos_ + 2]) == System.Char.ToUpper(c3)
  1825. && System.Char.ToUpper(src_[pos_ + 3]) == System.Char.ToUpper(c4)
  1826. && System.Char.ToUpper(src_[pos_ + 4]) == System.Char.ToUpper(c5)
  1827. && System.Char.ToUpper(src_[pos_ + 5]) == System.Char.ToUpper(c6))
  1828. { pos_ += 6; return true; }
  1829. return false;
  1830. }
  1831. public bool IChar(char c1, char c2, char c3, char c4, char c5, char c6, char c7)
  1832. {
  1833. if (pos_ + 6 < srcLen_
  1834. && System.Char.ToUpper(src_[pos_]) == System.Char.ToUpper(c1)
  1835. && System.Char.ToUpper(src_[pos_ + 1]) == System.Char.ToUpper(c2)
  1836. && System.Char.ToUpper(src_[pos_ + 2]) == System.Char.ToUpper(c3)
  1837. && System.Char.ToUpper(src_[pos_ + 3]) == System.Char.ToUpper(c4)
  1838. && System.Char.ToUpper(src_[pos_ + 4]) == System.Char.ToUpper(c5)
  1839. && System.Char.ToUpper(src_[pos_ + 5]) == System.Char.ToUpper(c6)
  1840. && System.Char.ToUpper(src_[pos_ + 6]) == System.Char.ToUpper(c7))
  1841. { pos_ += 7; return true; }
  1842. return false;
  1843. }
  1844. public bool IChar(string s)
  1845. {
  1846. int sLength = s.Length;
  1847. if (pos_ + sLength > srcLen_) return false;
  1848. for (int i = 0; i < sLength; ++i)
  1849. {
  1850. if (s[i] != System.Char.ToUpper(src_[pos_ + i])) return false;
  1851. }
  1852. pos_ += sLength;
  1853. return true;
  1854. }
  1855. public bool In(char c0, char c1)
  1856. {
  1857. if (pos_ < srcLen_
  1858. && src_[pos_] >= c0 && src_[pos_] <= c1)
  1859. {
  1860. ++pos_;
  1861. return true;
  1862. }
  1863. return false;
  1864. }
  1865. public bool In(char c0, char c1, char c2, char c3)
  1866. {
  1867. if (pos_ < srcLen_)
  1868. {
  1869. char c = src_[pos_];
  1870. if (c >= c0 && c <= c1
  1871. || c >= c2 && c <= c3)
  1872. {
  1873. ++pos_;
  1874. return true;
  1875. }
  1876. }
  1877. return false;
  1878. }
  1879. public bool In(char c0, char c1, char c2, char c3, char c4, char c5)
  1880. {
  1881. if (pos_ < srcLen_)
  1882. {
  1883. char c = src_[pos_];
  1884. if (c >= c0 && c <= c1
  1885. || c >= c2 && c <= c3
  1886. || c >= c4 && c <= c5)
  1887. {
  1888. ++pos_;
  1889. return true;
  1890. }
  1891. }
  1892. return false;
  1893. }
  1894. public bool In(char c0, char c1, char c2, char c3, char c4, char c5, char c6, char c7)
  1895. {
  1896. if (pos_ < srcLen_)
  1897. {
  1898. char c = src_[pos_];
  1899. if (c >= c0 && c <= c1
  1900. || c >= c2 && c <= c3
  1901. || c >= c4 && c <= c5
  1902. || c >= c6 && c <= c7)
  1903. {
  1904. ++pos_;
  1905. return true;
  1906. }
  1907. }
  1908. return false;
  1909. }
  1910. public bool In(string s)
  1911. {
  1912. if (pos_ < srcLen_)
  1913. {
  1914. char c = src_[pos_];
  1915. for (int i = 0; i < s.Length - 1; i += 2)
  1916. {
  1917. if (!(c >= s[i] && c <= s[i + 1])) return false;
  1918. }
  1919. ++pos_;
  1920. return true;
  1921. }
  1922. return false;
  1923. }
  1924. public bool NotIn(string s)
  1925. {
  1926. if (pos_ < srcLen_)
  1927. {
  1928. char c = src_[pos_];
  1929. for (int i = 0; i < s.Length - 1; i += 2)
  1930. {
  1931. if ( c >= s[i] && c <= s[i + 1]) return false;
  1932. }
  1933. ++pos_;
  1934. return true;
  1935. }
  1936. return false;
  1937. }
  1938. public bool OneOf(char c0, char c1)
  1939. {
  1940. if (pos_ < srcLen_
  1941. && (src_[pos_] == c0 || src_[pos_] == c1))
  1942. {
  1943. ++pos_;
  1944. return true;
  1945. }
  1946. return false;
  1947. }
  1948. public bool OneOf(char c0, char c1, char c2)
  1949. {
  1950. if (pos_ < srcLen_)
  1951. {
  1952. char c = src_[pos_];
  1953. if (c == c0 || c == c1 || c == c2)
  1954. {
  1955. ++pos_;
  1956. return true;
  1957. }
  1958. }
  1959. return false;
  1960. }
  1961. public bool OneOf(char c0, char c1, char c2, char c3)
  1962. {
  1963. if (pos_ < srcLen_)
  1964. {
  1965. char c = src_[pos_];
  1966. if (c == c0 || c == c1 || c == c2 || c == c3)
  1967. {
  1968. ++pos_;
  1969. return true;
  1970. }
  1971. }
  1972. return false;
  1973. }
  1974. public bool OneOf(char c0, char c1, char c2, char c3, char c4)
  1975. {
  1976. if (pos_ < srcLen_)
  1977. {
  1978. char c = src_[pos_];
  1979. if (c == c0 || c == c1 || c == c2 || c == c3 || c == c4)
  1980. {
  1981. ++pos_;
  1982. return true;
  1983. }
  1984. }
  1985. return false;
  1986. }
  1987. public bool OneOf(char c0, char c1, char c2, char c3, char c4, char c5)
  1988. {
  1989. if (pos_ < srcLen_)
  1990. {
  1991. char c = src_[pos_];
  1992. if (c == c0 || c == c1 || c == c2 || c == c3 || c == c4 || c == c5)
  1993. {
  1994. ++pos_;
  1995. return true;
  1996. }
  1997. }
  1998. return false;
  1999. }
  2000. public bool OneOf(char c0, char c1, char c2, char c3, char c4, char c5, char c6)
  2001. {
  2002. if (pos_ < srcLen_)
  2003. {
  2004. char c = src_[pos_];
  2005. if (c == c0 || c == c1 || c == c2 || c == c3 || c == c4 || c == c5 || c == c6)
  2006. {
  2007. ++pos_;
  2008. return true;
  2009. }
  2010. }
  2011. return false;
  2012. }
  2013. public bool OneOf(char c0, char c1, char c2, char c3, char c4, char c5, char c6, char c7)
  2014. {
  2015. if (pos_ < srcLen_)
  2016. {
  2017. char c = src_[pos_];
  2018. if (c == c0 || c == c1 || c == c2 || c == c3 || c == c4 || c == c5 || c == c6 || c == c7)
  2019. {
  2020. ++pos_;
  2021. return true;
  2022. }
  2023. }
  2024. return false;
  2025. }
  2026. public bool OneOf(string s)
  2027. {
  2028. if (pos_ < srcLen_)
  2029. {
  2030. if (s.IndexOf(src_[pos_]) != -1)
  2031. {
  2032. ++pos_;
  2033. return true;
  2034. }
  2035. }
  2036. return false;
  2037. }
  2038. public bool NotOneOf(string s)
  2039. {
  2040. if (pos_ < srcLen_)
  2041. {
  2042. if (s.IndexOf(src_[pos_]) == -1)
  2043. {
  2044. ++pos_;
  2045. return true;
  2046. }
  2047. }
  2048. return false;
  2049. }
  2050. public bool OneOf(OptimizedCharset cset)
  2051. {
  2052. if (pos_ < srcLen_ && cset.Matches(src_[pos_]))
  2053. {
  2054. ++pos_; return true;
  2055. }
  2056. return false;
  2057. }
  2058. public bool OneOfLiterals(OptimizedLiterals litAlt)
  2059. {
  2060. OptimizedLiterals.Trie node = litAlt.literalsRoot;
  2061. int matchPos = pos_-1;
  2062. for (int pos = pos_; pos < srcLen_ ; ++pos)
  2063. {
  2064. char c = src_[pos];
  2065. if ( node.children_==null
  2066. || c < node.cMin_ || c > node.cMin_ + node.children_.Length - 1
  2067. || node.children_[c - node.cMin_] == null)
  2068. {
  2069. break;
  2070. }
  2071. node = node.children_[c - node.cMin_];
  2072. if (node.bLitEnd_) matchPos = pos + 1;
  2073. }
  2074. if (matchPos >= pos_)
  2075. {
  2076. pos_= matchPos;
  2077. return true;
  2078. }
  2079. else return false;
  2080. }
  2081. #endregion PEG '<Literal>' / '<Literal>'/i / [low1-high1,low2-high2..] / [<CharList>]
  2082. }
  2083. #endregion Parsers
  2084. }