Class: Formula

Inherits:
RubyPeg
  • Object
show all
Defined in:
lib/formulae/parse/formula_peg.rb

Instance Method Summary collapse

Instance Method Details

#any_internal_referenceObject



74
75
76
# File 'lib/formulae/parse/formula_peg.rb', line 74

def any_internal_reference
  table_reference || local_table_reference || quoted_sheet_reference || sheet_reference || sheetless_reference
end

#any_referenceObject



70
71
72
# File 'lib/formulae/parse/formula_peg.rb', line 70

def any_reference
  external_reference || any_internal_reference
end

#areaObject



172
173
174
175
176
# File 'lib/formulae/parse/formula_peg.rb', line 172

def area
  node :area do
    reference && ignore { terminal(":") } && reference
  end
end

#argumentObject



24
25
26
# File 'lib/formulae/parse/formula_peg.rb', line 24

def argument
  expression || null
end

#arithmeticObject



46
47
48
49
50
# File 'lib/formulae/parse/formula_peg.rb', line 46

def arithmetic
  node :arithmetic do
    thing && one_or_more { (space && operator && space && thing) }
  end
end

#booleanObject



196
197
198
# File 'lib/formulae/parse/formula_peg.rb', line 196

def boolean
  boolean_true || boolean_false
end

#boolean_falseObject



206
207
208
209
210
# File 'lib/formulae/parse/formula_peg.rb', line 206

def boolean_false
  node :boolean_false do
    ignore { terminal("FALSE") }
  end
end

#boolean_trueObject



200
201
202
203
204
# File 'lib/formulae/parse/formula_peg.rb', line 200

def boolean_true
  node :boolean_true do
    ignore { terminal("TRUE") }
  end
end

#bracketsObject



34
35
36
37
38
# File 'lib/formulae/parse/formula_peg.rb', line 34

def brackets
  node :brackets do
    ignore { terminal("(") } && space && one_or_more { expression } && space && ignore { terminal(")") }
  end
end

#cellObject



178
179
180
181
182
# File 'lib/formulae/parse/formula_peg.rb', line 178

def cell
  node :cell do
    reference
  end
end

#columnObject



188
189
190
# File 'lib/formulae/parse/formula_peg.rb', line 188

def column
  terminal(/\$?[A-Za-z]{1,3}/)
end

#column_rangeObject



160
161
162
163
164
# File 'lib/formulae/parse/formula_peg.rb', line 160

def column_range
  node :column_range do
    column && ignore { terminal(":") } && column
  end
end

#comparatorObject



58
59
60
61
62
# File 'lib/formulae/parse/formula_peg.rb', line 58

def comparator
  node :comparator do
    terminal(">=") || terminal("<=") || terminal("<>") || terminal(">") || terminal("<") || terminal("=")
  end
end

#comparisonObject



52
53
54
55
56
# File 'lib/formulae/parse/formula_peg.rb', line 52

def comparison
  node :comparison do
    (arithmetic || thing) && space && comparator && space && (arithmetic || thing)
  end
end

#complex_structured_referenceObject



122
123
124
# File 'lib/formulae/parse/formula_peg.rb', line 122

def complex_structured_reference
  terminal(/\[[^\u005d]*\],\[[^\u005d]*\]/)
end

#expressionObject



16
17
18
# File 'lib/formulae/parse/formula_peg.rb', line 16

def expression
  string_join || comparison || arithmetic || thing
end

#external_referenceObject



96
97
98
99
100
# File 'lib/formulae/parse/formula_peg.rb', line 96

def external_reference
  node :external_reference do
    terminal(/\[\d+\]!?/) && any_internal_reference
  end
end

#formulaObject



10
11
12
13
14
# File 'lib/formulae/parse/formula_peg.rb', line 10

def formula
  node :formula do
    optional { space } && one_or_more { expression }
  end
end

#functionObject



28
29
30
31
32
# File 'lib/formulae/parse/formula_peg.rb', line 28

def function
  node :function do
    terminal(/[A-Z]+/) && ignore { terminal("(") } && space && optional { argument } && any_number_of { (space && ignore { terminal(",") } && space && argument) } && space && ignore { terminal(")") }
  end
end

#local_table_referenceObject



108
109
110
111
112
# File 'lib/formulae/parse/formula_peg.rb', line 108

def local_table_reference
  node :local_table_reference do
    ignore { terminal("[") } && (range_structured_reference || complex_structured_reference || overly_structured_reference || simple_structured_reference) && ignore { terminal("]") }
  end
end

#named_referenceObject



134
135
136
137
138
# File 'lib/formulae/parse/formula_peg.rb', line 134

def named_reference
  node :named_reference do
    terminal(/[\p{Word}#][\p{Word}.!]*/)
  end
end

#nullObject



222
223
224
225
226
# File 'lib/formulae/parse/formula_peg.rb', line 222

def null
  node :null do
    followed_by { terminal(",") }
  end
end

#numberObject



84
85
86
87
88
# File 'lib/formulae/parse/formula_peg.rb', line 84

def number
  node :number do
    terminal(/[-+]?[0-9]+\.?[0-9]*([eE][-+]?[0-9]+)?/)
  end
end

#operatorObject



90
91
92
93
94
# File 'lib/formulae/parse/formula_peg.rb', line 90

def operator
  node :operator do
    terminal("+") || terminal("-") || terminal("/") || terminal("*") || terminal("^")
  end
end

#overly_structured_referenceObject



126
127
128
# File 'lib/formulae/parse/formula_peg.rb', line 126

def overly_structured_reference
  ignore { terminal("[") } && simple_structured_reference && ignore { terminal("]") }
end

#percentageObject



78
79
80
81
82
# File 'lib/formulae/parse/formula_peg.rb', line 78

def percentage
  node :percentage do
    terminal(/[-+]?[0-9]+\.?[0-9]*/) && ignore { terminal("%") }
  end
end

#prefixObject



212
213
214
215
216
# File 'lib/formulae/parse/formula_peg.rb', line 212

def prefix
  node :prefix do
    terminal(/[-+]/) && thing
  end
end

#quoted_sheet_referenceObject



140
141
142
143
144
# File 'lib/formulae/parse/formula_peg.rb', line 140

def quoted_sheet_reference
  node :quoted_sheet_reference do
    single_quoted_string && ignore { terminal("!") } && (sheetless_reference || named_reference)
  end
end

#range_structured_referenceObject



118
119
120
# File 'lib/formulae/parse/formula_peg.rb', line 118

def range_structured_reference
  terminal(/\[[^\u005d]*\],\[[^\u005d]*\]:\[[^\u005d]*\]/)
end

#referenceObject



192
193
194
# File 'lib/formulae/parse/formula_peg.rb', line 192

def reference
  terminal(/\$?[A-Za-z]{1,3}\$?[0-9]+(?![0-9A-Za-z_])/)
end

#rootObject



6
7
8
# File 'lib/formulae/parse/formula_peg.rb', line 6

def root
  formula
end

#rowObject



184
185
186
# File 'lib/formulae/parse/formula_peg.rb', line 184

def row
  terminal(/\$?\d+/)
end

#row_rangeObject



166
167
168
169
170
# File 'lib/formulae/parse/formula_peg.rb', line 166

def row_range
  node :row_range do
    row && ignore { terminal(":") } && row
  end
end

#sheet_referenceObject



146
147
148
149
150
# File 'lib/formulae/parse/formula_peg.rb', line 146

def sheet_reference
  node :sheet_reference do
    terminal(/[\p{Word}][\p{Word}_.]+/) && ignore { terminal("!") } && (sheetless_reference || named_reference)
  end
end

#sheetless_referenceObject



156
157
158
# File 'lib/formulae/parse/formula_peg.rb', line 156

def sheetless_reference
  column_range || row_range || area || cell
end

#simple_structured_referenceObject



130
131
132
# File 'lib/formulae/parse/formula_peg.rb', line 130

def simple_structured_reference
  terminal(/[^\u005d]*/)
end

#single_quoted_stringObject



152
153
154
# File 'lib/formulae/parse/formula_peg.rb', line 152

def single_quoted_string
  ignore { terminal("'") } && terminal(/[^']*/) && ignore { terminal("'") }
end

#spaceObject



218
219
220
# File 'lib/formulae/parse/formula_peg.rb', line 218

def space
  ignore { terminal(/[ \n]*/) }
end

#stringObject



64
65
66
67
68
# File 'lib/formulae/parse/formula_peg.rb', line 64

def string
  node :string do
    ignore { terminal("\"") } && terminal(/(""|[^"])*/) && ignore { terminal("\"") }
  end
end

#string_joinObject



40
41
42
43
44
# File 'lib/formulae/parse/formula_peg.rb', line 40

def string_join
  node :string_join do
    (arithmetic || thing) && one_or_more { (space && ignore { terminal("&") } && space && (arithmetic || thing)) }
  end
end

#table_nameObject



114
115
116
# File 'lib/formulae/parse/formula_peg.rb', line 114

def table_name
  terminal(/[.\p{Word}]+/)
end

#table_referenceObject



102
103
104
105
106
# File 'lib/formulae/parse/formula_peg.rb', line 102

def table_reference
  node :table_reference do
    table_name && ignore { terminal("[") } && (range_structured_reference || complex_structured_reference || simple_structured_reference) && ignore { terminal("]") }
  end
end

#thingObject



20
21
22
# File 'lib/formulae/parse/formula_peg.rb', line 20

def thing
  function || brackets || any_reference || string || percentage || number || boolean || prefix || named_reference
end