surpass 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. data/History.txt +4 -0
  2. data/README.txt +3 -20
  3. data/Rakefile +2 -13
  4. data/bin/surpass +8 -0
  5. data/examples/big-16mb.rb +25 -0
  6. data/examples/big-random-strings.rb +28 -0
  7. data/examples/blanks.rb +34 -0
  8. data/examples/blanks.xls +0 -0
  9. data/examples/col_width.rb +16 -0
  10. data/examples/col_width.xls +0 -0
  11. data/examples/dates.rb +31 -0
  12. data/examples/dates.xls +0 -0
  13. data/examples/format.rb +23 -0
  14. data/examples/format.xls +0 -0
  15. data/examples/hello-world.rb +9 -0
  16. data/examples/hello-world.xls +0 -0
  17. data/examples/image.rb +10 -0
  18. data/examples/image.xls +0 -0
  19. data/examples/merged.rb +36 -0
  20. data/examples/merged.xls +0 -0
  21. data/examples/merged0.rb +27 -0
  22. data/examples/merged0.xls +0 -0
  23. data/examples/merged1.rb +99 -0
  24. data/examples/merged1.xls +0 -0
  25. data/examples/num_formats.rb +55 -0
  26. data/examples/num_formats.xls +0 -0
  27. data/examples/numbers.rb +24 -0
  28. data/examples/numbers.xls +0 -0
  29. data/examples/outline.rb +110 -0
  30. data/examples/outline.xls +0 -0
  31. data/examples/panes.rb +48 -0
  32. data/examples/panes.xls +0 -0
  33. data/examples/protection.rb +132 -0
  34. data/examples/protection.xls +0 -0
  35. data/examples/python.bmp +0 -0
  36. data/examples/row_styles.rb +16 -0
  37. data/examples/row_styles.xls +0 -0
  38. data/examples/row_styles_empty.rb +15 -0
  39. data/examples/row_styles_empty.xls +0 -0
  40. data/examples/set_cell_and_range_style.rb +12 -0
  41. data/examples/set_cell_and_range_style.xls +0 -0
  42. data/examples/wrapped-text.rb +13 -0
  43. data/examples/wrapped-text.xls +0 -0
  44. data/examples/write_arrays.rb +22 -0
  45. data/examples/write_arrays.xls +0 -0
  46. data/examples/ws_props.rb +80 -0
  47. data/lib/surpass/ExcelFormula.g +366 -0
  48. data/lib/surpass/ExcelFormula.tokens +30 -0
  49. data/lib/surpass/ExcelFormulaLexer.rb +922 -0
  50. data/lib/surpass/ExcelFormulaParser.rb +602 -0
  51. data/lib/{biff_record.rb → surpass/biff_record.rb} +0 -0
  52. data/lib/{bitmap.rb → surpass/bitmap.rb} +0 -0
  53. data/lib/{cell.rb → surpass/cell.rb} +2 -34
  54. data/lib/{chart.rb → surpass/chart.rb} +0 -0
  55. data/lib/{column.rb → surpass/column.rb} +0 -0
  56. data/lib/{document.rb → surpass/document.rb} +0 -0
  57. data/lib/surpass/excel_formula.rb +23 -0
  58. data/lib/{excel_magic.rb → surpass/excel_magic.rb} +0 -0
  59. data/lib/{formatting.rb → surpass/formatting.rb} +93 -53
  60. data/lib/{row.rb → surpass/row.rb} +0 -0
  61. data/lib/{style.rb → surpass/style.rb} +2 -1
  62. data/lib/surpass/tokens.txt +2 -0
  63. data/lib/{utilities.rb → surpass/utilities.rb} +0 -0
  64. data/lib/{workbook.rb → surpass/workbook.rb} +0 -0
  65. data/lib/{worksheet.rb → surpass/worksheet.rb} +15 -6
  66. data/lib/surpass.rb +1 -1
  67. data/spec/biff_record_spec.rb +268 -0
  68. data/spec/cell_spec.rb +56 -0
  69. data/spec/data/random-strings.txt +10000 -0
  70. data/spec/document_spec.rb +168 -0
  71. data/spec/excel_formula_spec.rb +27 -0
  72. data/spec/formatting_spec.rb +53 -0
  73. data/spec/output/cells-rk.xls +0 -0
  74. data/spec/output/cells.xls +0 -0
  75. data/spec/output/mini.xls +0 -0
  76. data/spec/reference/P-0508-0000507647-3280-5298.xls +0 -0
  77. data/spec/reference/all-cell-styles.bin +0 -0
  78. data/spec/reference/all-number-formats.bin +0 -0
  79. data/spec/reference/all-styles.bin +0 -0
  80. data/spec/reference/mini.xls +0 -0
  81. data/spec/row_spec.rb +19 -0
  82. data/spec/spec_helper.rb +10 -0
  83. data/spec/style_spec.rb +89 -0
  84. data/spec/surpass_spec.rb +7 -0
  85. data/spec/utilities_spec.rb +57 -0
  86. data/spec/workbook_spec.rb +48 -0
  87. data/spec/worksheet_spec.rb +0 -0
  88. data/stats/cloc.txt +8 -0
  89. data/stats/rcov.txt +0 -0
  90. data/stats/specdoc.txt +158 -0
  91. data/surpass.gemspec +40 -0
  92. data/tasks/setup.rb +1 -1
  93. data/tasks/zentest.rake +36 -0
  94. data/webby/README.txt +6 -0
  95. data/webby/Sitefile +43 -0
  96. data/webby/content/css/pygments.txt +6 -0
  97. data/webby/content/css/style.css +279 -0
  98. data/webby/content/examples/autoformat.png +0 -0
  99. data/webby/content/examples/autoformat.rb +32 -0
  100. data/webby/content/examples/autoformat.xls +0 -0
  101. data/webby/content/examples/borders.png +0 -0
  102. data/webby/content/examples/borders.rb +15 -0
  103. data/webby/content/examples/borders.xls +0 -0
  104. data/webby/content/examples/colours.png +0 -0
  105. data/webby/content/examples/colours.rb +23 -0
  106. data/webby/content/examples/colours.xls +0 -0
  107. data/webby/content/examples/data.png +0 -0
  108. data/webby/content/examples/data.rb +11 -0
  109. data/webby/content/examples/data.xls +0 -0
  110. data/webby/content/examples/formatting.png +0 -0
  111. data/webby/content/examples/formatting.rb +78 -0
  112. data/webby/content/examples/formatting.xls +0 -0
  113. data/webby/content/examples/hello-world.png +0 -0
  114. data/webby/content/examples/hello-world.py +8 -0
  115. data/webby/content/examples/hello-world.rb +9 -0
  116. data/webby/content/examples/hello-world.xls +0 -0
  117. data/webby/content/examples/number-format-string.png +0 -0
  118. data/webby/content/examples/number-format-string.rb +14 -0
  119. data/webby/content/examples/number-format-string.xls +0 -0
  120. data/webby/content/examples/patterns.png +0 -0
  121. data/webby/content/examples/patterns.rb +26 -0
  122. data/webby/content/examples/patterns.xls +0 -0
  123. data/webby/content/examples/show-greens.sh +1 -0
  124. data/webby/content/examples/surpass-info.sh +1 -0
  125. data/webby/content/img/Thumbs.db +0 -0
  126. data/webby/content/img/bg_menu.gif +0 -0
  127. data/webby/content/img/bg_t.gif +0 -0
  128. data/webby/content/img/bullet.gif +0 -0
  129. data/webby/content/img/logo.png +0 -0
  130. data/webby/content/img/logo_.jpg +0 -0
  131. data/webby/content/img/top_bg.gif +0 -0
  132. data/webby/content/img/top_bg_.gif +0 -0
  133. data/webby/content/index.txt +16 -0
  134. data/webby/content/installation/index.txt +24 -0
  135. data/webby/content/source/ExcelFormulaLexer.txt +7 -0
  136. data/webby/content/source/ExcelFormulaParser.txt +7 -0
  137. data/webby/content/source/biff_record.txt +7 -0
  138. data/webby/content/source/bitmap.txt +7 -0
  139. data/webby/content/source/cell.txt +7 -0
  140. data/webby/content/source/chart.txt +7 -0
  141. data/webby/content/source/column.txt +7 -0
  142. data/webby/content/source/document.txt +7 -0
  143. data/webby/content/source/excel_formula.txt +7 -0
  144. data/webby/content/source/excel_magic.txt +7 -0
  145. data/webby/content/source/formatting.txt +7 -0
  146. data/webby/content/source/row.txt +7 -0
  147. data/webby/content/source/style.txt +7 -0
  148. data/webby/content/source/utilities.txt +7 -0
  149. data/webby/content/source/workbook.txt +7 -0
  150. data/webby/content/source/worksheet.txt +7 -0
  151. data/webby/content/surpass-manual.erb +198 -0
  152. data/webby/layouts/book.txt +29 -0
  153. data/webby/layouts/default.txt +27 -0
  154. data/webby/layouts/two_column.txt +25 -0
  155. data/webby/layouts/web.txt +66 -0
  156. data/webby/output/.cairn +0 -0
  157. data/webby/output/css/blueprint/ie.css +26 -0
  158. data/webby/output/css/blueprint/plugins/buttons/icons/cross.png +0 -0
  159. data/webby/output/css/blueprint/plugins/buttons/icons/key.png +0 -0
  160. data/webby/output/css/blueprint/plugins/buttons/icons/tick.png +0 -0
  161. data/webby/output/css/blueprint/plugins/buttons/readme.txt +32 -0
  162. data/webby/output/css/blueprint/plugins/buttons/screen.css +97 -0
  163. data/webby/output/css/blueprint/plugins/fancy-type/readme.txt +14 -0
  164. data/webby/output/css/blueprint/plugins/fancy-type/screen.css +71 -0
  165. data/webby/output/css/blueprint/plugins/link-icons/icons/doc.png +0 -0
  166. data/webby/output/css/blueprint/plugins/link-icons/icons/email.png +0 -0
  167. data/webby/output/css/blueprint/plugins/link-icons/icons/external.png +0 -0
  168. data/webby/output/css/blueprint/plugins/link-icons/icons/feed.png +0 -0
  169. data/webby/output/css/blueprint/plugins/link-icons/icons/im.png +0 -0
  170. data/webby/output/css/blueprint/plugins/link-icons/icons/pdf.png +0 -0
  171. data/webby/output/css/blueprint/plugins/link-icons/icons/visited.png +0 -0
  172. data/webby/output/css/blueprint/plugins/link-icons/icons/xls.png +0 -0
  173. data/webby/output/css/blueprint/plugins/link-icons/readme.txt +18 -0
  174. data/webby/output/css/blueprint/plugins/link-icons/screen.css +40 -0
  175. data/webby/output/css/blueprint/plugins/rtl/readme.txt +10 -0
  176. data/webby/output/css/blueprint/plugins/rtl/screen.css +109 -0
  177. data/webby/output/css/blueprint/print.css +30 -0
  178. data/webby/output/css/blueprint/screen.css +251 -0
  179. data/webby/output/css/blueprint/src/forms.css +49 -0
  180. data/webby/output/css/blueprint/src/grid.css +212 -0
  181. data/webby/output/css/blueprint/src/grid.png +0 -0
  182. data/webby/output/css/blueprint/src/ie.css +59 -0
  183. data/webby/output/css/blueprint/src/print.css +85 -0
  184. data/webby/output/css/blueprint/src/reset.css +38 -0
  185. data/webby/output/css/blueprint/src/typography.css +105 -0
  186. data/webby/output/css/pygments.css +59 -0
  187. data/webby/output/css/site.css +62 -0
  188. data/webby/output/css/style.css +279 -0
  189. data/webby/output/examples/autoformat.png +0 -0
  190. data/webby/output/examples/autoformat.rb +32 -0
  191. data/webby/output/examples/autoformat.xls +0 -0
  192. data/webby/output/examples/borders.png +0 -0
  193. data/webby/output/examples/borders.rb +15 -0
  194. data/webby/output/examples/borders.xls +0 -0
  195. data/webby/output/examples/colours.png +0 -0
  196. data/webby/output/examples/colours.rb +23 -0
  197. data/webby/output/examples/colours.xls +0 -0
  198. data/webby/output/examples/data.png +0 -0
  199. data/webby/output/examples/data.rb +11 -0
  200. data/webby/output/examples/data.xls +0 -0
  201. data/webby/output/examples/formatting.png +0 -0
  202. data/webby/output/examples/formatting.rb +78 -0
  203. data/webby/output/examples/formatting.xls +0 -0
  204. data/webby/output/examples/hello-world.png +0 -0
  205. data/webby/output/examples/hello-world.py +8 -0
  206. data/webby/output/examples/hello-world.rb +9 -0
  207. data/webby/output/examples/hello-world.xls +0 -0
  208. data/webby/output/examples/number-format-string.png +0 -0
  209. data/webby/output/examples/number-format-string.rb +14 -0
  210. data/webby/output/examples/number-format-string.xls +0 -0
  211. data/webby/output/examples/patterns.png +0 -0
  212. data/webby/output/examples/patterns.rb +26 -0
  213. data/webby/output/examples/patterns.xls +0 -0
  214. data/webby/output/examples/show-greens.sh +1 -0
  215. data/webby/output/examples/surpass-info.sh +1 -0
  216. data/webby/output/img/Thumbs.db +0 -0
  217. data/webby/output/img/bg_menu.gif +0 -0
  218. data/webby/output/img/bg_t.gif +0 -0
  219. data/webby/output/img/bullet.gif +0 -0
  220. data/webby/output/img/logo.png +0 -0
  221. data/webby/output/img/logo_.jpg +0 -0
  222. data/webby/output/img/top_bg.gif +0 -0
  223. data/webby/output/img/top_bg_.gif +0 -0
  224. data/webby/output/index.html +138 -0
  225. data/webby/output/installation/index.html +64 -0
  226. data/webby/output/installation/installation.html +1 -0
  227. data/webby/output/installation.html +1 -0
  228. data/webby/output/source/ExcelFormulaLexer.html +981 -0
  229. data/webby/output/source/ExcelFormulaParser.html +661 -0
  230. data/webby/output/source/biff_record.html +462 -0
  231. data/webby/output/source/bitmap.html +277 -0
  232. data/webby/output/source/cell.html +241 -0
  233. data/webby/output/source/chart.html +64 -0
  234. data/webby/output/source/column.html +99 -0
  235. data/webby/output/source/document.html +465 -0
  236. data/webby/output/source/excel_formula.html +82 -0
  237. data/webby/output/source/excel_magic.html +1072 -0
  238. data/webby/output/source/formatting.html +664 -0
  239. data/webby/output/source/row.html +169 -0
  240. data/webby/output/source/style.html +158 -0
  241. data/webby/output/source/surpass.html +110 -0
  242. data/webby/output/source/utilities.html +145 -0
  243. data/webby/output/source/workbook.html +265 -0
  244. data/webby/output/source/worksheet.html +331 -0
  245. data/webby/output/surpass-manual-0-0-5.pdf +0 -0
  246. data/webby/output/surpass-manual.aux +50 -0
  247. data/webby/output/surpass-manual.log +366 -0
  248. data/webby/output/surpass-manual.out +20 -0
  249. data/webby/output/surpass-manual.tex +582 -0
  250. data/webby/output/surpass-manual.toc +20 -0
  251. data/webby/rsync-exclude +7 -0
  252. data/webby/tasks/latex.rake +14 -0
  253. data/webby/tasks/screenshots.rb +58 -0
  254. data/webby/templates/article.erb +15 -0
  255. data/webby/templates/book.erb +16 -0
  256. data/webby/templates/page.erb +9 -0
  257. metadata +302 -24
  258. data/bin/surpass-info +0 -20
  259. data/lib/excel_formula.rb +0 -6
@@ -0,0 +1,366 @@
1
+ grammar ExcelFormula;
2
+
3
+ options {
4
+ language = Ruby;
5
+ k = 2;
6
+ }
7
+
8
+ @header {
9
+ RVA_DELTA = {"R" => 0, "V" => 0x20, "A" => 0x40}
10
+ }
11
+
12
+ @init {
13
+ @rpn = ''
14
+ @sheet_references = []
15
+ @xcall_references = []
16
+ }
17
+
18
+ @members {
19
+ attr_accessor :rpn
20
+ }
21
+
22
+ formula
23
+ : expr["V"]
24
+ ;
25
+
26
+ expr[arg_type]
27
+ : prec0_expr[arg_type]
28
+ (
29
+ (
30
+ EQ { op = [PTGEQ].pack('C') }
31
+ /* | NE { op = [PTGNE].pack('C') } */
32
+ /* | GT { op = [PTGGT].pack('C') } */
33
+ /* | LT { op = [PTGLT].pack('C') } */
34
+ /* | GE { op = [PTGGE].pack('C') } */
35
+ /* | LE { op = [PTGLE].pack('C') } */
36
+
37
+ )
38
+ prec0_expr[arg_type] { @rpn += op }
39
+ )*
40
+ ;
41
+
42
+ prec0_expr[arg_type]
43
+ : prec1_expr[arg_type]
44
+ (
45
+ (
46
+ CONCAT { op = [PTGCONCAT].pack('C') }
47
+ )
48
+ prec1_expr[arg_type] { @rpn += op }
49
+ )*
50
+ ;
51
+
52
+ prec1_expr[arg_type]
53
+ : prec2_expr[arg_type]
54
+ (
55
+ (
56
+ ADD { op = [PTGADD].pack('C') }
57
+ | SUB { op = [PTGSUB].pack('C') }
58
+ )
59
+ prec2_expr[arg_type] { @rpn += op }
60
+ )*
61
+ ;
62
+
63
+
64
+ prec2_expr[arg_type]
65
+ : prec3_expr[arg_type]
66
+ (
67
+ (
68
+ MUL { op = [PTGMUL].pack('C') }
69
+ | DIV { op = [PTGDIV].pack('C') }
70
+ )
71
+ prec3_expr[arg_type] { @rpn += op }
72
+ )*
73
+ ;
74
+
75
+ prec3_expr[arg_type]
76
+ : prec5_expr[arg_type]
77
+ (
78
+ (
79
+ POWER { op = [PTGPOWER].pack('C') }
80
+ )
81
+ prec5_expr[arg_type] { @rpn += op }
82
+ )*
83
+ ;
84
+
85
+ prec4_expr[arg_type]
86
+ : prec5_expr[arg_type]
87
+ (
88
+ PERCENT { @rpn += [PTGPERCENT].pack('C') }
89
+ )?
90
+ ;
91
+
92
+ prec5_expr[arg_type]
93
+ : primary[arg_type]
94
+ | SUB primary[arg_type] { @rpn += [PTGUMINUS].pack('C') }
95
+ ;
96
+
97
+ primary[arg_type]
98
+ : TRUE_CONST
99
+ {
100
+ @rpn += [PTGBOOL, 1].pack("C2")
101
+ }
102
+ | FALSE_CONST
103
+ {
104
+ @rpn += [PTGBOOL, 0].pack("C2")
105
+ }
106
+ | str_tok = STR_CONST
107
+ {
108
+ @rpn += [PTGSTR].pack('C') + upack1(str_tok.text[1, -1]) #TODO
109
+ }
110
+ | int_tok = INT_CONST
111
+ {
112
+ int_value = int_tok.text.to_i
113
+ if int_value <= 65535
114
+ @rpn += [PTGINT, int_value].pack("Cv")
115
+ else
116
+ @rpn += [PTGNUM, int_value.to_f].pack("CE")
117
+ end
118
+ }
119
+ | num_tok = NUM_CONST
120
+ {
121
+ @rpn += [ptgNum, num_tok.text.to_f].pack("CE")
122
+ }
123
+ | ref2d_tok = REF2D
124
+ {
125
+ r, c = Utils.cell_to_packed_rowcol(ref2d_tok.text) # TODO
126
+ ptg = PTGREFR + RVA_DELTA[arg_type]
127
+ @rpn += [ptg, r, c].pack("Cv2")
128
+ }
129
+ | ref2d1_tok = REF2D COLON ref2d2_tok = REF2D
130
+ {
131
+ r1, c1 = Utils.cell_to_packed_rowcol(ref2d1_tok.text) #TODO
132
+ r2, c2 = Utils.cell_to_packed_rowcol(ref2d2_tok.text) #TODO
133
+ ptg = PTGAREAR + RVA_DELTA[arg_type]
134
+ self.rpn += struct.pack("Cv4", ptg, r1, r2, c1, c2)
135
+ }
136
+ /* | sheet1 = sheet
137
+ {
138
+ sheet2 = sheet1
139
+ }
140
+ ( COLON sheet2 = sheet )? BANG ref3d_ref2d=REF2D
141
+ {
142
+ ptg = PTGREF3DR + RVA_DELTA[arg_type]
143
+ rpn_ref2d = ""
144
+ r1, c1 = Utils.cell_to_packed_rowcol(ref3d_ref2d.text) #TODO
145
+ rpn_ref2d = [0x0000, r1, c1].pack("v3")
146
+ }
147
+ ( COLON ref3d_ref2d2= REF2D
148
+ {
149
+ ptg = PTGAREA3DR + RVA_DELTA[arg_type]
150
+ r2, c2 = Utils.cell_to_packed_rowcol(ref3d_ref2d2.text) #TODO
151
+ rpn_ref2d = [0x0000, r1, r2, c1, c2].pack("v5")
152
+ }
153
+ )?
154
+ {
155
+ @rpn += [ptg].pack("C")
156
+ @sheet_references << [sheet1, sheet2, @rpn.size]
157
+ @rpn += rpn_ref2d
158
+ }
159
+ | FUNC_IF
160
+ LP expr["V"] (SEMICOLON | COMMA)
161
+ {
162
+ @rpn += [PTGATTR, 0x02, 0].pack("C2v") # tAttrIf
163
+ pos0 = @rpn.size - 2
164
+ }
165
+ expr[arg_type] (SEMICOLON | COMMA)
166
+ {
167
+ @rpn += [PTGATTR, 0x08, 0].pack("C2v") # tAttrSkip
168
+ pos1 = @rpn.size - 2
169
+ @rpn = @rpn[0...pos0] + [pos1-pos0].pack("v") + @rpn[pos0+2...-1] # TODO
170
+ }
171
+ expr[arg_type] RP
172
+ {
173
+ @rpn += [PTGATTR, 0x08, 3].pack("C2v") # tAttrSkip
174
+ @rpn += [PTGFUNCVARR, 3, 1].pack("C2v") # 3 = nargs, 1 = IF func
175
+ pos2 = @rpn.size
176
+ @rpn = @rpn[0...pos1] + [pos2-(pos1+2)-1].pack("v") + @rpn[pos1+2...-1] # TODO
177
+ }
178
+ | FUNC_CHOOSE
179
+ {
180
+ arg_type = "R"
181
+ rpn_chunks = []
182
+ }
183
+ LP expr["V"] // first argument (the selector)
184
+ {
185
+ rpn_start = @rpn.size
186
+ ref_markers = [@sheet_references.size]
187
+ }
188
+ (
189
+ (SEMICOLON | COMMA)
190
+ { mark = @rpn.size }
191
+ (
192
+ expr[arg_type]
193
+ | { @rpn += [PTGMISSARG].pack("C") }
194
+ )
195
+ {
196
+ rpn_chunks.append(@rpn[mark...-1])
197
+ ref_markers.append(@sheet_references.size)
198
+ }
199
+ )*
200
+ RP
201
+ {
202
+ # TODO test this, no idea if it works, just blindly translated
203
+ @rpn = @rpn[0...rpn_start]
204
+ nc = rpn_chunks.size
205
+ chunklens = rpn_chunks.collect {|c| c.size}
206
+ skiplens = [0] * nc
207
+ skiplens[-1] = 3
208
+
209
+ (nc-1).downto(1) do |i|
210
+ skiplens[i-1] = skiplens[i] + chunklens[i] + 4
211
+ end
212
+ jump_pos = [2 * nc + 2]
213
+
214
+ (0...nc).each do |i|
215
+ jump_pos.append(jump_pos[-1] + chunklens[ic] + 4)
216
+ end
217
+ chunk_shift = 2 * nc + 6 # size of tAttrChoose
218
+
219
+ (0...nc).each do |i|
220
+ (ref_markers[i]...ref_markers[i+1]).each do |r|
221
+ ref = @sheet_references[r]
222
+ @sheet_references[r] = [r[0], r[1], r[2] + chunk_shift]
223
+ end
224
+ chunk_shift += 4 # size of tAttrSkip
225
+ end
226
+
227
+ choose_rpn = []
228
+ choose_rpn.append([PTGATTR, 0x04, nc].pack("CCv")) # 0x04 is tAttrChoose
229
+ choose_rpn.append(jump_pos.pack("v*"))
230
+
231
+ (0...nc).each do |i|
232
+ choose_rpn << rpn_chunks[i]
233
+ choose_rpn << [PTGATTR, 0x08, skiplens[i]].pack("CCv") # 0x08 is tAttrSkip
234
+ end
235
+ choose_rpn.append([PTGFUNCVARV, nc+1, 100].pack("CCv")) # 100 is CHOOSE fn
236
+ @rpn += choose_rpn.join
237
+ }
238
+
239
+ | name_tok = NAME
240
+ {
241
+ raise "[formula] found unexpected NAME token #{name_tok.text}"
242
+ # TODO: handle references to defined names here
243
+ }
244
+ | func_tok = NAME
245
+ {
246
+ raise "not implemented"
247
+ # func_toku = func_tok.text.upper()
248
+ # if func_toku in all_funcs_by_name:
249
+ # (opcode,
250
+ # min_argc,
251
+ # max_argc,
252
+ # func_type,
253
+ # arg_type_str) = all_funcs_by_name[func_toku]
254
+ # arg_type_list = list(arg_type_str)
255
+ # else:
256
+ # raise Exception("[formula] unknown function #{func_tok.text}")
257
+ # xcall = opcode < 0
258
+ # if xcall:
259
+ # # The name of the add-in function is passed as the 1st arg
260
+ # # of the hidden XCALL function
261
+ # self.xcall_references.append((func_toku, len(self.rpn) + 1))
262
+ # self.rpn += struct.pack("<BHHH",
263
+ # ptgNameXR,
264
+ # 0xadde, # ##PATCHME## index to REF entry in EXTERNSHEET record
265
+ # 0xefbe, # ##PATCHME## one-based index to EXTERNNAME record
266
+ # 0x0000) # unused
267
+ }
268
+ LP arg_count = expr_list[arg_type_list, min_argc, max_argc] RP
269
+ {
270
+ raise "not implemented"
271
+ # if arg_count > max_argc or arg_count < min_argc:
272
+ # raise Exception, "#{arg_count} parameters for function: #{func_tok.text}"
273
+ # if xcall:
274
+ # func_ptg = ptgFuncVarR + _RVAdelta[func_type]
275
+ # self.rpn += struct.pack("<2BH", func_ptg, arg_count + 1, 255) # 255 is magic XCALL function
276
+ # elif min_argc == max_argc:
277
+ # func_ptg = ptgFuncR + _RVAdelta[func_type]
278
+ # self.rpn += struct.pack("<BH", func_ptg, opcode)
279
+ # elif arg_count == 1 and func_tok.text.upper() == "SUM":
280
+ # self.rpn += struct.pack("<BBH", ptgAttr, 0x10, 0) # tAttrSum
281
+ # else:
282
+ # func_ptg = ptgFuncVarR + _RVAdelta[func_type]
283
+ # self.rpn += struct.pack("<2BH", func_ptg, arg_count, opcode)
284
+ }
285
+ | LP expr[arg_type] RP
286
+ {
287
+ @rpn += [PTGPAREN].pack('C')
288
+ }
289
+ ;
290
+
291
+ expr_list[arg_type_list, min_argc, max_argc] returns [arg_cnt]
292
+ @members{
293
+ arg_cnt = 0
294
+ arg_type = arg_type_list[arg_cnt]
295
+ }
296
+ : expr[arg_type] { arg_cnt += 1 }
297
+ (
298
+ {
299
+ if arg_cnt < arg_type_list.size
300
+ arg_type = arg_type_list[arg_cnt]
301
+ else
302
+ arg_type = arg_type_list[-1]
303
+ end
304
+ if arg_type == "+"
305
+ arg_type = arg_type_list[-2]
306
+ end
307
+ }
308
+ (SEMICOLON | COMMA)
309
+ (
310
+ expr[arg_type]
311
+ | { @rpn += [PTGMISSARG].pack("B") }
312
+ )
313
+ { arg_cnt += 1 }
314
+ )*
315
+ |
316
+ ;
317
+
318
+ sheet returns[ref]
319
+ : sheet_ref_name = NAME
320
+ { ref = sheet_ref_name.text }
321
+ | sheet_ref_int = INT_CONST
322
+ { ref = sheet_ref_int.text }
323
+ | sheet_ref_quote = QUOTENAME
324
+ { ref = sheet_ref_quote.text[1..-1].replace("''", "'") # TODO }
325
+ ;
326
+
327
+ */
328
+
329
+ ;
330
+
331
+ fragment
332
+ EQ: '=';
333
+ LT: '<';
334
+ GT: '>';
335
+ NE: '<>';
336
+ LE: '<=';
337
+ GE: '>=';
338
+
339
+ ADD: '+';
340
+ SUB: '-';
341
+ MUL: '*';
342
+ DIV: '/';
343
+
344
+ COLON: ':';
345
+ SEMICOLON: ';';
346
+ COMMA: ',';
347
+
348
+ LP: '(';
349
+ RP: ')';
350
+ CONCAT: '&';
351
+ PERCENT: '%';
352
+ POWER: '^';
353
+ BANG: '!';
354
+
355
+ DIGIT: '0'..'9';
356
+
357
+ INT_CONST: DIGIT+ ;
358
+ NUM_CONST: DIGIT* '.' DIGIT+ (('E'|'e') ('+'|'-')? DIGIT+)?; // \d*\.\d+(?:[Ee][+-]?\d+)?
359
+ STR_CONST: '"' ~'"' '"'; // TODO add escape recognition
360
+ REF2D: '$'? ('A'..'I')? ('A'..'Z') '$'? DIGIT+;
361
+ TRUE_CONST: ('T'|'t') ('R'|'r') ('U'|'u') ('E'|'e') ;
362
+ FALSE_CONST: ('F'|'f') ('A'|'a') ('L'|'l') ('S'|'s') ('E'|'e') ;
363
+ NAME: '\w[\.\w]*' ;
364
+ QUOTENAME: '\'(?:[^\']|\'\')*\'';
365
+ FUNC_IF: 'IF';
366
+ FUNC_CHOOSE: 'CHOOSE';
@@ -0,0 +1,30 @@
1
+ COMMA=25
2
+ TRUE_CONST=12
3
+ PERCENT=11
4
+ POWER=10
5
+ FUNC_CHOOSE=33
6
+ BANG=28
7
+ EQ=4
8
+ QUOTENAME=31
9
+ LT=19
10
+ NE=21
11
+ GT=20
12
+ FUNC_IF=32
13
+ RP=27
14
+ FALSE_CONST=13
15
+ LP=26
16
+ GE=23
17
+ MUL=8
18
+ NUM_CONST=16
19
+ REF2D=17
20
+ SEMICOLON=24
21
+ CONCAT=5
22
+ LE=22
23
+ STR_CONST=14
24
+ INT_CONST=15
25
+ COLON=18
26
+ DIV=9
27
+ DIGIT=29
28
+ NAME=30
29
+ SUB=7
30
+ ADD=6