ExcelFormula.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. /* created on 9/8/2012 9:28:14 AM from peg generator V1.0 using 'ExcelFormula.txt' as input*/
  4. using Peg.Base;
  5. using System;
  6. using System.IO;
  7. using System.Text;
  8. namespace ExcelFormula
  9. {
  10. enum EExcelFormula{Formula= 1, Expression= 2, InfixTerms= 3, PreAndPostTerm= 4,
  11. Term= 5, RefInfixTerms= 6, RefTerm= 7, Constant= 8, RefConstant= 9,
  12. ErrorConstant= 10, LogicalConstant= 11, NumericalConstant= 12,
  13. SignificandPart= 13, WholeNumberPart= 14, FractionalPart= 15,
  14. ExponentPart= 16, StringConstant= 17, StringCharacter= 18, HighCharacter= 19,
  15. ArrayConstant= 20, ConstantListRows= 21, ConstantListRow= 22,
  16. InfixOperator= 23, ValueInfixOperator= 24, RefInfixOperator= 25,
  17. UnionOperator= 26, IntersectionOperator= 27, RangeOperator= 28,
  18. PostfixOperator= 29, PrefixOperator= 30, CellReference= 31, LocalCellReference= 32,
  19. ExternalCellReference= 33, BookPrefix= 34, BangReference= 35,
  20. SheetRangeReference= 36, SingleSheetPrefix= 37, SingleSheetReference= 38,
  21. SingleSheetArea= 39, SingleSheet= 40, SheetRange= 41, WorkbookIndex= 42,
  22. SheetName= 43, SheetNameCharacter= 44, SheetNameSpecial= 45,
  23. SheetNameBaseCharacter= 46, A1Reference= 47, A1Cell= 48, A1Area= 49,
  24. A1Column= 50, A1AbsoluteColumn= 51, A1RelativeColumn= 52, A1Row= 53,
  25. A1AbsoluteRow= 54, A1RelativeRow= 55, CellFunctionCall= 56, UserDefinedFunctionCall= 57,
  26. UserDefinedFunctionName= 58, ArgumentList= 59, Argument= 60,
  27. ArgumentExpression= 61, ArgumentInfixTerms= 62, ArgumentPreAndPostTerm= 63,
  28. ArgumentTerm= 64, ArgumentRefInfixTerms= 65, ArgumentRefTerm= 66,
  29. ArgumentInfixOperator= 67, RefArgumentInfixOperator= 68, NameReference= 69,
  30. ExternalName= 70, BangName= 71, Name= 72, NameStartCharacter= 73,
  31. NameCharacter= 74, StructureReference= 75, TableIdentifier= 76,
  32. TableName= 77, IntraTableReference= 78, InnerReference= 79, Keyword= 80,
  33. KeywordList= 81, ColumnRange= 82, Column= 83, SimpleColumnName= 84,
  34. EscapeColumnCharacter= 85, UnescapedColumnCharacter= 86, AnyNoSpaceColumnCharacter= 87,
  35. SpacedComma= 88, SpacedLBracket= 89, SpacedRBracket= 90, ws= 91};
  36. class ExcelFormula : PegCharParser
  37. {
  38. #region Input Properties
  39. public static EncodingClass encodingClass = EncodingClass.ascii;
  40. public static UnicodeDetection unicodeDetection = UnicodeDetection.notApplicable;
  41. #endregion Input Properties
  42. #region Constructors
  43. public ExcelFormula()
  44. : base()
  45. {
  46. }
  47. public ExcelFormula(string src,TextWriter FerrOut)
  48. : base(src,FerrOut)
  49. {
  50. }
  51. #endregion Constructors
  52. #region Overrides
  53. public override string GetRuleNameFromId(int id)
  54. {
  55. try
  56. {
  57. EExcelFormula ruleEnum = (EExcelFormula)id;
  58. string s= ruleEnum.ToString();
  59. int val;
  60. if( int.TryParse(s,out val) ){
  61. return base.GetRuleNameFromId(id);
  62. }else{
  63. return s;
  64. }
  65. }
  66. catch (Exception)
  67. {
  68. return base.GetRuleNameFromId(id);
  69. }
  70. }
  71. public override void GetProperties(out EncodingClass encoding, out UnicodeDetection detection)
  72. {
  73. encoding = encodingClass;
  74. detection = unicodeDetection;
  75. }
  76. #endregion Overrides
  77. #region Grammar Rules
  78. public bool Formula() /*Formula: Expression (!./FATAL<"end of line expected">);*/
  79. {
  80. return And(()=>
  81. Expression()
  82. && ( Not(()=> Any() ) || Fatal("end of line expected")) );
  83. }
  84. public bool Expression() /*Expression: ws InfixTerms;*/
  85. {
  86. return And(()=> ws() && InfixTerms() );
  87. }
  88. public bool InfixTerms() /*InfixTerms: PreAndPostTerm (InfixOperator ws PreAndPostTerm)*;*/
  89. {
  90. return And(()=>
  91. PreAndPostTerm()
  92. && OptRepeat(()=>
  93. And(()=>
  94. InfixOperator()
  95. && ws()
  96. && PreAndPostTerm() ) ) );
  97. }
  98. public bool PreAndPostTerm() /*PreAndPostTerm: (PrefixOperator ws)* Term (PostfixOperator ws)*;*/
  99. {
  100. return And(()=>
  101. OptRepeat(()=> And(()=> PrefixOperator() && ws() ) )
  102. && Term()
  103. && OptRepeat(()=> And(()=> PostfixOperator() && ws() ) ) );
  104. }
  105. public bool Term() /*Term: (RefInfixTerms / '(' Expression ')' / Constant) ws;*/
  106. {
  107. return And(()=>
  108. (
  109. RefInfixTerms()
  110. || And(()=> Char('(') && Expression() && Char(')') )
  111. || Constant())
  112. && ws() );
  113. }
  114. public bool RefInfixTerms() /*RefInfixTerms: RefTerm (RefInfixOperator ws RefTerm)*;*/
  115. {
  116. return And(()=>
  117. RefTerm()
  118. && OptRepeat(()=>
  119. And(()=> RefInfixOperator() && ws() && RefTerm() ) ) );
  120. }
  121. public bool RefTerm() /*RefTerm: '(' ws RefInfixTerms ')' / RefConstant / CellFunctionCall / CellReference / UserDefinedFunctionCall
  122. / NameReference / StructureReference;*/
  123. {
  124. return
  125. And(()=>
  126. Char('(')
  127. && ws()
  128. && RefInfixTerms()
  129. && Char(')') )
  130. || RefConstant()
  131. || CellFunctionCall()
  132. || CellReference()
  133. || UserDefinedFunctionCall()
  134. || NameReference()
  135. || StructureReference();
  136. }
  137. public bool Constant() /*^^Constant: ErrorConstant / LogicalConstant / NumericalConstant / StringConstant / ArrayConstant;*/
  138. {
  139. return TreeNT((int)EExcelFormula.Constant,()=>
  140. ErrorConstant()
  141. || LogicalConstant()
  142. || NumericalConstant()
  143. || StringConstant()
  144. || ArrayConstant() );
  145. }
  146. public bool RefConstant() /*RefConstant: '#REF!';*/
  147. {
  148. return Char('#','R','E','F','!');
  149. }
  150. public bool ErrorConstant() /*ErrorConstant: RefConstant / '#DIV/0!' / '#N/A' / '#NAME?' / '#NULL!' / '#NUM!' / '#VALUE!' / '#GETTING_DATA';*/
  151. {
  152. return
  153. RefConstant()
  154. || Char('#','D','I','V','/','0','!')
  155. || Char('#','N','/','A')
  156. || Char('#','N','A','M','E','?')
  157. || Char('#','N','U','L','L','!')
  158. || Char('#','N','U','M','!')
  159. || Char('#','V','A','L','U','E','!')
  160. || Char("#GETTING_DATA");
  161. }
  162. public bool LogicalConstant() /*LogicalConstant: 'FALSE' / 'TRUE';*/
  163. {
  164. return Char('F','A','L','S','E') || Char('T','R','U','E');
  165. }
  166. public bool NumericalConstant() /*NumericalConstant: '-'? SignificandPart ExponentPart?;*/
  167. {
  168. return And(()=>
  169. Option(()=> Char('-') )
  170. && SignificandPart()
  171. && Option(()=> ExponentPart() ) );
  172. }
  173. public bool SignificandPart() /*SignificandPart: WholeNumberPart FractionalPart? / FractionalPart;*/
  174. {
  175. return
  176. And(()=>
  177. WholeNumberPart()
  178. && Option(()=> FractionalPart() ) )
  179. || FractionalPart();
  180. }
  181. public bool WholeNumberPart() /*WholeNumberPart: [0-9]+;*/
  182. {
  183. return PlusRepeat(()=> In('0','9') );
  184. }
  185. public bool FractionalPart() /*FractionalPart: '.' [0-9]*;*/
  186. {
  187. return And(()=> Char('.') && OptRepeat(()=> In('0','9') ) );
  188. }
  189. public bool ExponentPart() /*ExponentPart: 'E' ('+' / '-')? [0-9]*;*/
  190. {
  191. return And(()=>
  192. Char('E')
  193. && Option(()=> Char('+') || Char('-') )
  194. && OptRepeat(()=> In('0','9') ) );
  195. }
  196. public bool StringConstant() /*StringConstant: '"' ('""'/StringCharacter)* '"';*/
  197. {
  198. return And(()=>
  199. Char('"')
  200. && OptRepeat(()=> Char('"','"') || StringCharacter() )
  201. && Char('"') );
  202. }
  203. public bool StringCharacter() /*StringCharacter: [#-~] / '!' / ' ' / HighCharacter;*/
  204. {
  205. return
  206. In('#','~')
  207. || Char('!')
  208. || Char(' ')
  209. || HighCharacter();
  210. }
  211. public bool HighCharacter() /*HighCharacter: [#x80-#xFFFF];*/
  212. {
  213. return In('\u0080','\uffff');
  214. }
  215. public bool ArrayConstant() /*^^ArrayConstant: '{' ConstantListRows '}';*/
  216. {
  217. return TreeNT((int)EExcelFormula.ArrayConstant,()=>
  218. And(()=> Char('{') && ConstantListRows() && Char('}') ) );
  219. }
  220. public bool ConstantListRows() /*ConstantListRows: ConstantListRow (';' ConstantListRow)*;*/
  221. {
  222. return And(()=>
  223. ConstantListRow()
  224. && OptRepeat(()=>
  225. And(()=> Char(';') && ConstantListRow() ) ) );
  226. }
  227. public bool ConstantListRow() /*^^ConstantListRow: Constant (',' Constant)*;*/
  228. {
  229. return TreeNT((int)EExcelFormula.ConstantListRow,()=>
  230. And(()=>
  231. Constant()
  232. && OptRepeat(()=> And(()=> Char(',') && Constant() ) ) ) );
  233. }
  234. public bool InfixOperator() /*InfixOperator: RefInfixOperator / ValueInfixOperator;*/
  235. {
  236. return RefInfixOperator() || ValueInfixOperator();
  237. }
  238. public bool ValueInfixOperator() /*^^ValueInfixOperator: '<>' / '>=' / '<=' / '^' / '*' / '/' / '+' / '-' / '&' / '=' / '<' / '>';*/
  239. {
  240. return TreeNT((int)EExcelFormula.ValueInfixOperator,()=>
  241. OneOfLiterals(optimizedLiterals0) );
  242. }
  243. public bool RefInfixOperator() /*RefInfixOperator: RangeOperator / UnionOperator / IntersectionOperator;*/
  244. {
  245. return
  246. RangeOperator()
  247. || UnionOperator()
  248. || IntersectionOperator();
  249. }
  250. public bool UnionOperator() /*^^UnionOperator: ',';*/
  251. {
  252. return TreeNT((int)EExcelFormula.UnionOperator,()=>
  253. Char(',') );
  254. }
  255. public bool IntersectionOperator() /*^^IntersectionOperator: ' ';*/
  256. {
  257. return TreeNT((int)EExcelFormula.IntersectionOperator,()=>
  258. Char(' ') );
  259. }
  260. public bool RangeOperator() /*^^RangeOperator: ':';*/
  261. {
  262. return TreeNT((int)EExcelFormula.RangeOperator,()=>
  263. Char(':') );
  264. }
  265. public bool PostfixOperator() /*^^PostfixOperator: '%';*/
  266. {
  267. return TreeNT((int)EExcelFormula.PostfixOperator,()=>
  268. Char('%') );
  269. }
  270. public bool PrefixOperator() /*^^PrefixOperator: '+' / '-';*/
  271. {
  272. return TreeNT((int)EExcelFormula.PrefixOperator,()=>
  273. Char('+') || Char('-') );
  274. }
  275. public bool CellReference() /*CellReference: ExternalCellReference / LocalCellReference;*/
  276. {
  277. return ExternalCellReference() || LocalCellReference();
  278. }
  279. public bool LocalCellReference() /*LocalCellReference: A1Reference;*/
  280. {
  281. return A1Reference();
  282. }
  283. public bool ExternalCellReference() /*ExternalCellReference: BangReference / SheetRangeReference / SingleSheetReference;*/
  284. {
  285. return
  286. BangReference()
  287. || SheetRangeReference()
  288. || SingleSheetReference();
  289. }
  290. public bool BookPrefix() /*BookPrefix: WorkbookIndex '!';*/
  291. {
  292. return And(()=> WorkbookIndex() && Char('!') );
  293. }
  294. public bool BangReference() /*BangReference: '!' (A1Reference / '#REF!');*/
  295. {
  296. return And(()=>
  297. Char('!')
  298. && ( A1Reference() || Char('#','R','E','F','!')) );
  299. }
  300. public bool SheetRangeReference() /*SheetRangeReference: SheetRange '!' A1Reference;*/
  301. {
  302. return And(()=> SheetRange() && Char('!') && A1Reference() );
  303. }
  304. public bool SingleSheetPrefix() /*SingleSheetPrefix: SingleSheet '!';*/
  305. {
  306. return And(()=> SingleSheet() && Char('!') );
  307. }
  308. public bool SingleSheetReference() /*SingleSheetReference: SingleSheetPrefix (A1Reference / '#REF!');*/
  309. {
  310. return And(()=>
  311. SingleSheetPrefix()
  312. && ( A1Reference() || Char('#','R','E','F','!')) );
  313. }
  314. public bool SingleSheetArea() /*SingleSheetArea: SingleSheetPrefix A1Area;*/
  315. {
  316. return And(()=> SingleSheetPrefix() && A1Area() );
  317. }
  318. public bool SingleSheet() /*SingleSheet: WorkbookIndex? SheetName / '\'' WorkbookIndex? SheetNameSpecial '\'';*/
  319. {
  320. return
  321. And(()=>
  322. Option(()=> WorkbookIndex() )
  323. && SheetName() )
  324. || And(()=>
  325. Char('\'')
  326. && Option(()=> WorkbookIndex() )
  327. && SheetNameSpecial()
  328. && Char('\'') );
  329. }
  330. public bool SheetRange() /*SheetRange: WorkbookIndex? SheetName ':' SheetName / '\'' WorkbookIndex? SheetNameSpecial ':' SheetNameSpecial '\'';*/
  331. {
  332. return
  333. And(()=>
  334. Option(()=> WorkbookIndex() )
  335. && SheetName()
  336. && Char(':')
  337. && SheetName() )
  338. || And(()=>
  339. Char('\'')
  340. && Option(()=> WorkbookIndex() )
  341. && SheetNameSpecial()
  342. && Char(':')
  343. && SheetNameSpecial()
  344. && Char('\'') );
  345. }
  346. public bool WorkbookIndex() /*^^WorkbookIndex: '[' WholeNumberPart ']';*/
  347. {
  348. return TreeNT((int)EExcelFormula.WorkbookIndex,()=>
  349. And(()=> Char('[') && WholeNumberPart() && Char(']') ) );
  350. }
  351. public bool SheetName() /*^^SheetName: SheetNameCharacter+;*/
  352. {
  353. return TreeNT((int)EExcelFormula.SheetName,()=>
  354. PlusRepeat(()=> SheetNameCharacter() ) );
  355. }
  356. public bool SheetNameCharacter() /*SheetNameCharacter: [A-Za-z0-9._] / HighCharacter;*/
  357. {
  358. return
  359. (In('A','Z', 'a','z', '0','9')||OneOf("._"))
  360. || HighCharacter();
  361. }
  362. public bool SheetNameSpecial() /*^^SheetNameSpecial: SheetNameBaseCharacter ('\'\''* SheetNameBaseCharacter)*;*/
  363. {
  364. return TreeNT((int)EExcelFormula.SheetNameSpecial,()=>
  365. And(()=>
  366. SheetNameBaseCharacter()
  367. && OptRepeat(()=>
  368. And(()=>
  369. OptRepeat(()=> Char('\'','\'') )
  370. && SheetNameBaseCharacter() ) ) ) );
  371. }
  372. public bool SheetNameBaseCharacter() /*SheetNameBaseCharacter: [A-Za-z0-9!"#$%&()+,-.;<=>@^_`{|}~ ] / HighCharacter;*/
  373. {
  374. return OneOf(optimizedCharset0) || HighCharacter();
  375. }
  376. public bool A1Reference() /*^^A1Reference: (A1Column ':' A1Column) / (A1Row ':' A1Row) / A1Area / A1Cell;*/
  377. {
  378. return TreeNT((int)EExcelFormula.A1Reference,()=>
  379. And(()=> A1Column() && Char(':') && A1Column() )
  380. || And(()=> A1Row() && Char(':') && A1Row() )
  381. || A1Area()
  382. || A1Cell() );
  383. }
  384. public bool A1Cell() /*A1Cell: A1Column A1Row !NameCharacter;*/
  385. {
  386. return And(()=>
  387. A1Column()
  388. && A1Row()
  389. && Not(()=> NameCharacter() ) );
  390. }
  391. public bool A1Area() /*A1Area: A1Cell ':' A1Cell;*/
  392. {
  393. return And(()=> A1Cell() && Char(':') && A1Cell() );
  394. }
  395. public bool A1Column() /*^^A1Column: A1AbsoluteColumn / A1RelativeColumn;*/
  396. {
  397. return TreeNT((int)EExcelFormula.A1Column,()=>
  398. A1AbsoluteColumn() || A1RelativeColumn() );
  399. }
  400. public bool A1AbsoluteColumn() /*A1AbsoluteColumn: '$' A1RelativeColumn;*/
  401. {
  402. return And(()=> Char('$') && A1RelativeColumn() );
  403. }
  404. public bool A1RelativeColumn() /*A1RelativeColumn: 'XF' [A-D] / 'X' [A-E] [A-Z] / [A-W][A-Z][A-Z] / [A-Z][A-Z] / [A-Z];*/
  405. {
  406. return
  407. And(()=> Char('X','F') && In('A','D') )
  408. || And(()=> Char('X') && In('A','E') && In('A','Z') )
  409. || And(()=> In('A','W') && In('A','Z') && In('A','Z') )
  410. || And(()=> In('A','Z') && In('A','Z') )
  411. || In('A','Z');
  412. }
  413. public bool A1Row() /*^^A1Row: A1AbsoluteRow / A1RelativeRow;*/
  414. {
  415. return TreeNT((int)EExcelFormula.A1Row,()=>
  416. A1AbsoluteRow() || A1RelativeRow() );
  417. }
  418. public bool A1AbsoluteRow() /*A1AbsoluteRow: '$' A1RelativeRow;*/
  419. {
  420. return And(()=> Char('$') && A1RelativeRow() );
  421. }
  422. public bool A1RelativeRow() /*A1RelativeRow: [1-9][0-9]*;*/
  423. {
  424. return And(()=> In('1','9') && OptRepeat(()=> In('0','9') ) );
  425. }
  426. public bool CellFunctionCall() /*^^CellFunctionCall: A1Cell '(' ArgumentList ')';*/
  427. {
  428. return TreeNT((int)EExcelFormula.CellFunctionCall,()=>
  429. And(()=>
  430. A1Cell()
  431. && Char('(')
  432. && ArgumentList()
  433. && Char(')') ) );
  434. }
  435. public bool UserDefinedFunctionCall() /*^^UserDefinedFunctionCall: UserDefinedFunctionName '(' ArgumentList ')';*/
  436. {
  437. return TreeNT((int)EExcelFormula.UserDefinedFunctionCall,()=>
  438. And(()=>
  439. UserDefinedFunctionName()
  440. && Char('(')
  441. && ArgumentList()
  442. && Char(')') ) );
  443. }
  444. public bool UserDefinedFunctionName() /*UserDefinedFunctionName: NameReference;*/
  445. {
  446. return NameReference();
  447. }
  448. public bool ArgumentList() /*ArgumentList: Argument (',' Argument)*;*/
  449. {
  450. return And(()=>
  451. Argument()
  452. && OptRepeat(()=> And(()=> Char(',') && Argument() ) ) );
  453. }
  454. public bool Argument() /*Argument: ArgumentExpression / ws;*/
  455. {
  456. return ArgumentExpression() || ws();
  457. }
  458. public bool ArgumentExpression() /*^^ArgumentExpression: ws ArgumentInfixTerms;*/
  459. {
  460. return TreeNT((int)EExcelFormula.ArgumentExpression,()=>
  461. And(()=> ws() && ArgumentInfixTerms() ) );
  462. }
  463. public bool ArgumentInfixTerms() /*ArgumentInfixTerms: ArgumentPreAndPostTerm (ArgumentInfixOperator ws ArgumentPreAndPostTerm)*;*/
  464. {
  465. return And(()=>
  466. ArgumentPreAndPostTerm()
  467. && OptRepeat(()=>
  468. And(()=>
  469. ArgumentInfixOperator()
  470. && ws()
  471. && ArgumentPreAndPostTerm() ) ) );
  472. }
  473. public bool ArgumentPreAndPostTerm() /*ArgumentPreAndPostTerm: (PrefixOperator ws)* ArgumentTerm (PostfixOperator ws)*;*/
  474. {
  475. return And(()=>
  476. OptRepeat(()=> And(()=> PrefixOperator() && ws() ) )
  477. && ArgumentTerm()
  478. && OptRepeat(()=> And(()=> PostfixOperator() && ws() ) ) );
  479. }
  480. public bool ArgumentTerm() /*ArgumentTerm: (ArgumentRefInfixTerms / '(' Expression ')' / Constant) ws;*/
  481. {
  482. return And(()=>
  483. (
  484. ArgumentRefInfixTerms()
  485. || And(()=> Char('(') && Expression() && Char(')') )
  486. || Constant())
  487. && ws() );
  488. }
  489. public bool ArgumentRefInfixTerms() /*ArgumentRefInfixTerms: ArgumentRefTerm (RefArgumentInfixOperator ws ArgumentRefTerm)*;*/
  490. {
  491. return And(()=>
  492. ArgumentRefTerm()
  493. && OptRepeat(()=>
  494. And(()=>
  495. RefArgumentInfixOperator()
  496. && ws()
  497. && ArgumentRefTerm() ) ) );
  498. }
  499. public bool ArgumentRefTerm() /*ArgumentRefTerm: '(' ws RefInfixTerms ')' / RefConstant / CellFunctionCall / CellReference / UserDefinedFunctionCall
  500. / NameReference / StructureReference;*/
  501. {
  502. return
  503. And(()=>
  504. Char('(')
  505. && ws()
  506. && RefInfixTerms()
  507. && Char(')') )
  508. || RefConstant()
  509. || CellFunctionCall()
  510. || CellReference()
  511. || UserDefinedFunctionCall()
  512. || NameReference()
  513. || StructureReference();
  514. }
  515. public bool ArgumentInfixOperator() /*ArgumentInfixOperator: RefArgumentInfixOperator / ValueInfixOperator;*/
  516. {
  517. return RefArgumentInfixOperator() || ValueInfixOperator();
  518. }
  519. public bool RefArgumentInfixOperator() /*RefArgumentInfixOperator: RangeOperator / IntersectionOperator;*/
  520. {
  521. return RangeOperator() || IntersectionOperator();
  522. }
  523. public bool NameReference() /*^^NameReference: (ExternalName / Name) !'[';*/
  524. {
  525. return TreeNT((int)EExcelFormula.NameReference,()=>
  526. And(()=>
  527. ( ExternalName() || Name())
  528. && Not(()=> Char('[') ) ) );
  529. }
  530. public bool ExternalName() /*ExternalName: BangName / (SingleSheetPrefix / BookPrefix) Name;*/
  531. {
  532. return
  533. BangName()
  534. || And(()=>
  535. ( SingleSheetPrefix() || BookPrefix())
  536. && Name() );
  537. }
  538. public bool BangName() /*BangName: '!' Name;*/
  539. {
  540. return And(()=> Char('!') && Name() );
  541. }
  542. public bool Name() /*Name: NameStartCharacter NameCharacter*;*/
  543. {
  544. return And(()=>
  545. NameStartCharacter()
  546. && OptRepeat(()=> NameCharacter() ) );
  547. }
  548. public bool NameStartCharacter() /*NameStartCharacter: [_\\A-Za-z] / HighCharacter;*/
  549. {
  550. return
  551. (In('A','Z', 'a','z')||OneOf("_\\"))
  552. || HighCharacter();
  553. }
  554. public bool NameCharacter() /*NameCharacter: NameStartCharacter / [0-9] / '.' / '?' / HighCharacter;*/
  555. {
  556. return
  557. NameStartCharacter()
  558. || In('0','9')
  559. || Char('.')
  560. || Char('?')
  561. || HighCharacter();
  562. }
  563. public bool StructureReference() /*^^StructureReference: TableIdentifier? IntraTableReference;*/
  564. {
  565. return TreeNT((int)EExcelFormula.StructureReference,()=>
  566. And(()=>
  567. Option(()=> TableIdentifier() )
  568. && IntraTableReference() ) );
  569. }
  570. public bool TableIdentifier() /*TableIdentifier: BookPrefix? TableName;*/
  571. {
  572. return And(()=> Option(()=> BookPrefix() ) && TableName() );
  573. }
  574. public bool TableName() /*TableName: Name;*/
  575. {
  576. return Name();
  577. }
  578. public bool IntraTableReference() /*IntraTableReference: SpacedLBracket InnerReference SpacedRBracket / Keyword / '[' SimpleColumnName ']';*/
  579. {
  580. return
  581. And(()=>
  582. SpacedLBracket()
  583. && InnerReference()
  584. && SpacedRBracket() )
  585. || Keyword()
  586. || And(()=>
  587. Char('[')
  588. && SimpleColumnName()
  589. && Char(']') );
  590. }
  591. public bool InnerReference() /*InnerReference: (KeywordList SpacedComma)? ColumnRange / KeywordList;*/
  592. {
  593. return
  594. And(()=>
  595. Option(()=>
  596. And(()=> KeywordList() && SpacedComma() ) )
  597. && ColumnRange() )
  598. || KeywordList();
  599. }
  600. public bool Keyword() /*Keyword: '[#All]' / '[#Data]' / '[#Headers]' / '[#Totals]' / '[#This Row]';*/
  601. {
  602. return
  603. Char('[','#','A','l','l',']')
  604. || Char('[','#','D','a','t','a',']')
  605. || Char("[#Headers]")
  606. || Char("[#Totals]")
  607. || Char("[#This Row]");
  608. }
  609. public bool KeywordList() /*KeywordList: '[#Headers]' SpacedComma '[#Data]' / '[#Data]' SpacedComma '[#Totals]' / Keyword;*/
  610. {
  611. return
  612. And(()=>
  613. Char("[#Headers]")
  614. && SpacedComma()
  615. && Char('[','#','D','a','t','a',']') )
  616. || And(()=>
  617. Char('[','#','D','a','t','a',']')
  618. && SpacedComma()
  619. && Char("[#Totals]") )
  620. || Keyword();
  621. }
  622. public bool ColumnRange() /*ColumnRange: Column (':' Column)?;*/
  623. {
  624. return And(()=>
  625. Column()
  626. && Option(()=> And(()=> Char(':') && Column() ) ) );
  627. }
  628. public bool Column() /*Column: '[' ws SimpleColumnName ws ']' / SimpleColumnName;*/
  629. {
  630. return
  631. And(()=>
  632. Char('[')
  633. && ws()
  634. && SimpleColumnName()
  635. && ws()
  636. && Char(']') )
  637. || SimpleColumnName();
  638. }
  639. public bool SimpleColumnName() /*SimpleColumnName: AnyNoSpaceColumnCharacter+ (ws AnyNoSpaceColumnCharacter+)*;*/
  640. {
  641. return And(()=>
  642. PlusRepeat(()=> AnyNoSpaceColumnCharacter() )
  643. && OptRepeat(()=>
  644. And(()=>
  645. ws()
  646. && PlusRepeat(()=> AnyNoSpaceColumnCharacter() ) ) ) );
  647. }
  648. public bool EscapeColumnCharacter() /*EscapeColumnCharacter: '\'' / '#' / '[' / ']';*/
  649. {
  650. return Char('\'') || Char('#') || Char('[') || Char(']');
  651. }
  652. public bool UnescapedColumnCharacter() /*UnescapedColumnCharacter: [A-Za-z0-9!"#$%&()*+,-./:;<=>?@\\^_`{|}~] / HighCharacter;*/
  653. {
  654. return OneOf(optimizedCharset1) || HighCharacter();
  655. }
  656. public bool AnyNoSpaceColumnCharacter() /*AnyNoSpaceColumnCharacter: ('\'' EscapeColumnCharacter) / UnescapedColumnCharacter;*/
  657. {
  658. return
  659. And(()=> Char('\'') && EscapeColumnCharacter() )
  660. || UnescapedColumnCharacter();
  661. }
  662. public bool SpacedComma() /*SpacedComma: ' '? ',' ' '?;*/
  663. {
  664. return And(()=>
  665. Option(()=> Char(' ') )
  666. && Char(',')
  667. && Option(()=> Char(' ') ) );
  668. }
  669. public bool SpacedLBracket() /*SpacedLBracket: '[' ' '?;*/
  670. {
  671. return And(()=> Char('[') && Option(()=> Char(' ') ) );
  672. }
  673. public bool SpacedRBracket() /*SpacedRBracket: ' '? ']';*/
  674. {
  675. return And(()=> Option(()=> Char(' ') ) && Char(']') );
  676. }
  677. public bool ws() /*ws: ' '*;*/
  678. {
  679. return OptRepeat(()=> Char(' ') );
  680. }
  681. #endregion Grammar Rules
  682. #region Optimization Data
  683. internal static OptimizedCharset optimizedCharset0;
  684. internal static OptimizedCharset optimizedCharset1;
  685. internal static OptimizedLiterals optimizedLiterals0;
  686. static ExcelFormula()
  687. {
  688. {
  689. OptimizedCharset.Range[] ranges = new OptimizedCharset.Range[]
  690. {new OptimizedCharset.Range('A','Z'),
  691. new OptimizedCharset.Range('a','z'),
  692. new OptimizedCharset.Range('0','9'),
  693. new OptimizedCharset.Range(',','.'),
  694. };
  695. char[] oneOfChars = new char[] {'!','"','#','$','%'
  696. ,'&','(',')','+',';'
  697. ,'<','=','>','@','^'
  698. ,'_','`','{','|','}'
  699. ,'~',' '};
  700. optimizedCharset0= new OptimizedCharset(ranges,oneOfChars);
  701. }
  702. {
  703. OptimizedCharset.Range[] ranges = new OptimizedCharset.Range[]
  704. {new OptimizedCharset.Range('A','Z'),
  705. new OptimizedCharset.Range('a','z'),
  706. new OptimizedCharset.Range('0','9'),
  707. new OptimizedCharset.Range(',','.'),
  708. };
  709. char[] oneOfChars = new char[] {'!','"','#','$','%'
  710. ,'&','(',')','*','+'
  711. ,'/',':',';','<','='
  712. ,'>','?','@','\\','^'
  713. ,'_','`','{','|','}'
  714. ,'~'};
  715. optimizedCharset1= new OptimizedCharset(ranges,oneOfChars);
  716. }
  717. {
  718. string[] literals=
  719. { "<>",">=","<=","^","*","/","+","-",
  720. "&","=","<",">" };
  721. optimizedLiterals0= new OptimizedLiterals(literals);
  722. }
  723. }
  724. #endregion Optimization Data
  725. }
  726. }