Advertisement
Guest User

dmitrygusev

a guest
Feb 2nd, 2008
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. grammar Expression;
  2.  
  3. options {
  4. output=AST;
  5. language=CSharp;
  6. }
  7.  
  8. tokens {
  9. Minus='-';
  10. Plus='+';
  11. Star='*';
  12. Slash='/';
  13.  
  14. GT='>';
  15. LT='<';
  16. EQ1='=';
  17. EQ2='==';
  18. GE='>=';
  19. LE='<=';
  20. NE1='!=';
  21. NE2='<>';
  22.  
  23. And='\u0438';
  24. Or='\u0438\u043B\u0438';
  25. Not='\u043D\u0435';
  26.  
  27. Function='(';
  28. TimeFrame='{';
  29. TimeFrameSeparator=':';
  30. ControlCell='[';
  31.  
  32. January='\u044F\u043D\u0432\u0430\u0440\u044C';
  33. February='\u0444\u0435\u0432\u0440\u0430\u043B\u044C';
  34. March='\u043C\u0430\u0440\u0442';
  35. April='\u0430\u043F\u0440\u0435\u043B\u044C';
  36. May='\u043C\u0430\u0439';
  37. June='\u0438\u044E\u043D\u044C';
  38. July='\u0438\u044E\u043B\u044C';
  39. August='\u0430\u0432\u0433\u0443\u0441\u0442';
  40. September='\u0441\u0435\u043D\u0442\u044F\u0431\u0440\u044C';
  41. October='\u043E\u043A\u0442\u044F\u0431\u0440\u044C';
  42. November='\u043D\u043E\u044F\u0431\u0440\u044C';
  43. December='\u0434\u0435\u043A\u0430\u0431\u0440\u044C';
  44.  
  45. FirstQuarter='\u043F\u0435\u0440\u0432\u044B\u0439\u043A\u0432\u0430\u0440\u0442\u0430\u043B';
  46. SecondQuarter='\u0432\u0442\u043E\u0440\u043E\u0439\u043A\u0432\u0430\u0440\u0442\u0430\u043B';
  47. ThirdQuarter='\u0442\u0440\u0435\u0442\u0438\u0439\u043A\u0432\u0430\u0440\u0442\u0430\u043B';
  48. FourthQuarter='\u0447\u0435\u0442\u0432\u0435\u0440\u0442\u044B\u0439\u043A\u0432\u0430\u0440\u0442\u0430\u043B';
  49.  
  50. Today='\u0441\u0435\u0433\u043E\u0434\u043D\u044F';
  51.  
  52. CurrentMonth='\u0442\u0435\u043A\u0443\u0449\u0438\u0439\u043C\u0435\u0441\u044F\u0446';
  53. CurrentQuarter='\u0442\u0435\u043A\u0443\u0449\u0438\u0439\u043A\u0432\u0430\u0440\u0442\u0430\u043B';
  54. CurrentYear='\u0442\u0435\u043A\u0443\u0449\u0438\u0439\u0433\u043E\u0434';
  55.  
  56. CurrentTimeFrame='\u0442\u0435\u043A\u0443\u0449\u0438\u0439\u0438\u043D\u0442\u0435\u0440\u0432\u0430\u043B';
  57.  
  58. BeginOfTimeFrame='\u043D\u0430\u0447\u0430\u043B\u043E\u0438\u043D\u0442\u0435\u0440\u0432\u0430\u043B\u0430';
  59. EndOfTimeFrame='\u043A\u043E\u043D\u0435\u0446\u0438\u043D\u0442\u0435\u0440\u0432\u0430\u043B\u0430';
  60.  
  61. FQIdent='\u0438\u0434\u0435\u043D\u0442\u0438\u0444\u0438\u043A\u0430\u0442\u043E\u0440';
  62.  
  63. Year='\u0433\u043E\u0434';
  64. Year2='\u0433\u043E\u0434\u0430';
  65. Year3='\u043B\u0435\u0442';
  66.  
  67. Quarter='\u043A\u0432\u0430\u0440\u0442\u0430\u043B';
  68. Quarter2='\u043A\u0432\u0430\u0440\u0442\u0430\u043B\u0430';
  69. Quarter3='\u043A\u0432\u0430\u0440\u0442\u0430\u043B\u043E\u0432';
  70.  
  71. Month='\u043C\u0435\u0441\u044F\u0446';
  72. Month2='\u043C\u0435\u0441\u044F\u0446\u0430';
  73. Month3='\u043C\u0435\u0441\u044F\u0446\u0435\u0432';
  74.  
  75. Week='\u043D\u0435\u0434\u0435\u043B\u044F';
  76. Week2='\u043D\u0435\u0434\u0435\u043B\u0438';
  77. Week3='\u043D\u0435\u0434\u0435\u043B\u044C';
  78.  
  79. Day='\u0434\u0435\u043D\u044C';
  80. Day2='\u0434\u043D\u044F';
  81. Day3='\u0434\u043D\u0435\u0439';
  82.  
  83. DayNumber='\u0447\u0438\u0441\u043B\u043E';
  84. DayOfWeek='\u0434\u0435\u043D\u044C\u043D\u0435\u0434\u0435\u043B\u0438';
  85. DateFunction='\u0434\u0430\u0442\u0430';
  86. IfFunction='\u0435\u0441\u043B\u0438';
  87.  
  88. True1='\u0438\u0441\u0442\u0438\u043D\u0430';
  89. True2='true';
  90.  
  91. False1='\u043B\u043E\u0436\u044C';
  92. False2='false';
  93. }
  94.  
  95. @namespace {Mosenergo.Asko.Eval}
  96.  
  97. @lexer::header {
  98.  
  99. }
  100.  
  101. @header {
  102. using System.Collections.Generic;
  103. }
  104.  
  105.  
  106.  
  107. @rulecatch {
  108. catch (RecognitionException re) {
  109. RememberError(re);
  110. Recover(input, re);
  111. }
  112. }
  113.  
  114. @members {
  115. private IList<RecognitionException> errors = new List<RecognitionException>();
  116.  
  117. public IList<RecognitionException> GetErrors() {
  118. return errors;
  119. }
  120.  
  121. protected void RememberError(RecognitionException re) {
  122. if (errorRecovery) {
  123. return;
  124. }
  125. errorRecovery = true;
  126. errors.Add(re);
  127. }
  128.  
  129. protected override void Mismatch(IIntStream input, int ttype, BitSet follow) {
  130. MismatchedTokenException mte = new MismatchedTokenException(ttype, input);
  131. RememberError(mte);
  132. RecoverFromMismatchedToken(input, mte, ttype, follow);
  133. }
  134. }
  135.  
  136. expr
  137. : anyExpr EOF
  138. ;
  139. finally {
  140. if ( errors.Count > 0) {
  141. throw errors[0];
  142. }
  143. }
  144.  
  145. anyExpr
  146. : addExpr ((LT | LE | GT | GE | EQ1 | EQ2 | NE1 | NE2)^ addExpr)?
  147. | boolFunction
  148. | boolLiteral
  149. | StringLiteral
  150. ;
  151.  
  152. boolLiteral
  153. : True1
  154. | True2
  155. | False1
  156. | False2
  157. ;
  158.  
  159. addExpr
  160. : mulExpr ((Minus|Plus)^ mulExpr)*
  161. ;
  162.  
  163. mulExpr
  164. : unaryExpr ((Star|Slash)^ unaryExpr)*
  165. ;
  166.  
  167. unaryExpr
  168. : unaryExprNotPlusMinus
  169. | Minus^ unaryExprNotPlusMinus
  170. | Plus^ unaryExprNotPlusMinus
  171. ;
  172.  
  173. unaryExprNotPlusMinus
  174. : primary
  175. ;
  176.  
  177. primary
  178. : valueReference
  179. | functionCall
  180. | literal
  181. | '('! addExpr ')'!
  182. ;
  183.  
  184. valueReference
  185. : ControlCellId (TimeFrame^ timeFrameExpr '}'!)?
  186. ;
  187.  
  188. ControlCellId
  189. : ControlCell
  190. ControlCellIdFragment ':' // Maket
  191. ControlCellIdFragment ':' // Filial
  192. ControlCellIdFragment ':' // Object Group
  193. ControlCellIdFragment ':' // Object Subgroup
  194. (ControlCellIdFragment ':') * // Additional Arguments
  195. ControlCellIdFragment // Indicator
  196. ']'!
  197. ;
  198.  
  199. fragment
  200. ControlCellIdFragment
  201. : ~(':'|']')*
  202. ;
  203.  
  204. timeFrameExpr
  205. :
  206. ( CurrentTimeFrame
  207. | timeFrameFunction
  208. | timeFrameLiteral
  209. | timeFrameExprInternals
  210. ) ((Minus^|Plus^) DecimalLiteral timeFrameOffsetUnit?)?
  211. ;
  212.  
  213. timeFrameExprInternals
  214. : TimeFrame^ timeFrameExpr '}'!
  215. ;
  216.  
  217. functionCall
  218. : DayNumber Function^ dateFunction ')'!
  219. | Month Function^ dateFunction ')'!
  220. | Year Function^ dateFunction ')'!
  221. | DayOfWeek Function^ dateFunction ')'!
  222. | Week Function^ dateFunction ')'!
  223. | Quarter Function^ dateFunction ')'!
  224. | IfFunction Function^ boolArg ','! argument ','! argument ')'!
  225. | id Function^ (argument (','! argument)*)? ')'!
  226. ;
  227.  
  228. boolArg
  229. : comparable (LT | LE | GT | GE | EQ1 | EQ2 | NE1 | NE2)^ comparable
  230. | boolFunction
  231. | boolLiteral
  232. ;
  233.  
  234. comparable
  235. : addExpr
  236. | StringLiteral
  237. ;
  238.  
  239. boolFunction
  240. : andOrFunction
  241. | notFunction
  242. ;
  243.  
  244. notFunction
  245. : Not Function^ boolArg ')'!
  246. ;
  247.  
  248. andOrFunction
  249. : (And|Or) Function^ boolArg (','! boolArg)+ ')'!
  250. ;
  251.  
  252. argument
  253. : timeFrameExpr2
  254. | addExpr
  255. | idFunction
  256. | dateFunction
  257. | StringLiteral
  258. ;
  259.  
  260. timeFrameExpr2
  261. : TimeFrame^ timeFrameExpr2Internals '}'!
  262. ;
  263.  
  264. timeFrameExpr2Internals
  265. :
  266. ( timeFrameFunction
  267. | timeFrameLiteral
  268. | timeFrameExpr2
  269. ) ((Minus^|Plus^) DecimalLiteral timeFrameOffsetUnit?)?
  270. ;
  271.  
  272. timeFrameOffsetUnit
  273. : Year
  274. | Year2
  275. | Year3
  276. | Quarter
  277. | Quarter2
  278. | Quarter3
  279. | Month
  280. | Month2
  281. | Month3
  282. | Week
  283. | Week2
  284. | Week3
  285. | Day
  286. | Day2
  287. | Day3
  288. ;
  289.  
  290. timeFrameFunction
  291. : CurrentTimeFrame Function^ ControlCellId ')'!
  292. ;
  293.  
  294. idFunction
  295. : FQIdent Function^ ControlCellId ')'!
  296. ;
  297.  
  298. timeFrameLiteral
  299. : SingleDayTimeFrame
  300. | Today
  301. | January
  302. | February
  303. | March
  304. | April
  305. | May
  306. | June
  307. | July
  308. | August
  309. | September
  310. | October
  311. | November
  312. | December
  313. | CurrentMonth
  314. | FirstQuarter
  315. | SecondQuarter
  316. | ThirdQuarter
  317. | FourthQuarter
  318. | CurrentQuarter
  319. | CurrentYear
  320. | dateForTimeFrame TimeFrameSeparator^ dateForTimeFrame
  321. ;
  322.  
  323. dateForTimeFrame
  324. : beginOfTimeFrame
  325. | endOfTimeFrame
  326. | SingleDayTimeFrame
  327. | Today
  328. ;
  329.  
  330. dateFunction
  331. : dateForTimeFrame
  332. | createDateFunction
  333. ;
  334.  
  335. createDateFunction
  336. : DateFunction Function^ addExpr ','! addExpr ','! addExpr ')'!
  337. ;
  338.  
  339. beginOfTimeFrame
  340. : BeginOfTimeFrame Function^ timeFrameExpr2 ')'!
  341. ;
  342.  
  343. endOfTimeFrame
  344. : EndOfTimeFrame Function^ timeFrameExpr2 ')'!
  345. ;
  346.  
  347. id
  348. : ID^
  349. ;
  350.  
  351. literal
  352. : DecimalLiteral
  353. | FloatingPointLiteral
  354. ;
  355.  
  356. // $<TimeFrames
  357.  
  358. SingleDayTimeFrame
  359. : DayTimeFrame
  360. ;
  361.  
  362. fragment
  363. DayTimeFrame
  364. : ('0'..'9')? ('0'..'9') '.' MonthTimeFrame
  365. ;
  366.  
  367. fragment
  368. MonthTimeFrame
  369. : ('0'..'9')? ('0'..'9') '.' YearTimeFrame
  370. ;
  371.  
  372. fragment
  373. YearTimeFrame
  374. : (('0'..'9') ('0'..'9'))? ('0'..'9') ('0'..'9')
  375. ;
  376.  
  377. // $>
  378.  
  379. // $<Literals
  380.  
  381. DecimalLiteral
  382. : ('0' | '1'..'9' '0'..'9'*)
  383. ;
  384.  
  385. FloatingPointLiteral
  386. : ('0'..'9')+ '.' ('0'..'9')* Exponent?
  387. | '.' ('0'..'9')+ Exponent?
  388. | ('0'..'9')+ Exponent
  389. ;
  390.  
  391. fragment
  392. Exponent
  393. : ('e'|'E') (Plus|Minus)? ('0'..'9')+
  394. ;
  395.  
  396. StringLiteral
  397. : '"' ( EscapeSequence | ~('\\'|'"') )* '"'
  398. ;
  399.  
  400. fragment
  401. EscapeSequence
  402. : '\\' ('\"'|'\''|'\\')
  403. ;
  404. // $>
  405.  
  406.  
  407. ID : Letter (Letter | '0'..'9' | '\u005f')*
  408. ;
  409.  
  410. fragment
  411. Letter :
  412. '\u0041'..'\u005a' |
  413. '\u0061'..'\u007a' |
  414. '\u00c0'..'\u00d6' |
  415. '\u00d8'..'\u00f6' |
  416. '\u00f8'..'\u00ff' |
  417. '\u0100'..'\u1fff' |
  418. '\u3040'..'\u318f' |
  419. '\u3300'..'\u337f' |
  420. '\u3400'..'\u3d2d' |
  421. '\u4e00'..'\u9fff' |
  422. '\uf900'..'\ufaff'
  423. ;
  424.  
  425. WS : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
  426. ;
  427.  
  428.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement