Class: Dentaku::TokenMatcher
- Inherits:
-
Object
- Object
- Dentaku::TokenMatcher
- Defined in:
- lib/dentaku/token_matcher.rb
Instance Attribute Summary collapse
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
- .addsub ⇒ Object
- .anchored_minus ⇒ Object
- .arguments ⇒ Object
- .close ⇒ Object
- .combinator ⇒ Object
- .comma ⇒ Object
- .comp_gt ⇒ Object
- .comp_lt ⇒ Object
- .comparator ⇒ Object
- .datetime ⇒ Object
- .function(name) ⇒ Object
- .if ⇒ Object
- .logical ⇒ Object
- .mod ⇒ Object
- .muldiv ⇒ Object
- .non_close_plus ⇒ Object
- .non_group ⇒ Object
- .non_group_star ⇒ Object
- .not ⇒ Object
- .numeric ⇒ Object
- .open ⇒ Object
- .pow ⇒ Object
- .round ⇒ Object
- .rounddown ⇒ Object
- .roundup ⇒ Object
- .string ⇒ Object
- .subtract ⇒ Object
- .value ⇒ Object
Instance Method Summary collapse
- #==(token) ⇒ Object
- #caret ⇒ Object
- #caret? ⇒ Boolean
-
#initialize(categories = nil, values = nil, children = []) ⇒ TokenMatcher
constructor
A new instance of TokenMatcher.
- #invert ⇒ Object
- #leaf_matcher? ⇒ Boolean
- #leaf_matchers ⇒ Object
- #match(token_stream, offset = 0) ⇒ Object
- #plus ⇒ Object
- #star ⇒ Object
- #|(other_matcher) ⇒ Object
Constructor Details
#initialize(categories = nil, values = nil, children = []) ⇒ TokenMatcher
Returns a new instance of TokenMatcher.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/dentaku/token_matcher.rb', line 7 def initialize(categories = nil, values = nil, children = []) # store categories and values as hash to optimize key lookup, h/t @jan-mangs @categories = [categories].compact.flatten.each_with_object({}) { |c, h| h[c] = 1 } @values = [values].compact.flatten.each_with_object({}) { |v, h| h[v] = 1 } @children = children.compact @invert = false @min = 1 @max = 1 @range = (@min..@max) end |
Instance Attribute Details
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
5 6 7 |
# File 'lib/dentaku/token_matcher.rb', line 5 def categories @categories end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
5 6 7 |
# File 'lib/dentaku/token_matcher.rb', line 5 def children @children end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
5 6 7 |
# File 'lib/dentaku/token_matcher.rb', line 5 def values @values end |
Class Method Details
.addsub ⇒ Object
104 |
# File 'lib/dentaku/token_matcher.rb', line 104 def self.addsub = new(:operator, [:add, :subtract]) |
.anchored_minus ⇒ Object
106 |
# File 'lib/dentaku/token_matcher.rb', line 106 def self.anchored_minus = new(:operator, :subtract).caret |
.arguments ⇒ Object
122 |
# File 'lib/dentaku/token_matcher.rb', line 122 def self.arguments = (value | comma).plus |
.close ⇒ Object
117 |
# File 'lib/dentaku/token_matcher.rb', line 117 def self.close = new(:grouping, :close) |
.combinator ⇒ Object
110 |
# File 'lib/dentaku/token_matcher.rb', line 110 def self.combinator = new(:combinator) |
.comma ⇒ Object
118 |
# File 'lib/dentaku/token_matcher.rb', line 118 def self.comma = new(:grouping, :comma) |
.comp_gt ⇒ Object
113 |
# File 'lib/dentaku/token_matcher.rb', line 113 def self.comp_gt = new(:comparator, [:gt, :ge]) |
.comp_lt ⇒ Object
114 |
# File 'lib/dentaku/token_matcher.rb', line 114 def self.comp_lt = new(:comparator, [:lt, :le]) |
.comparator ⇒ Object
112 |
# File 'lib/dentaku/token_matcher.rb', line 112 def self.comparator = new(:comparator) |
.datetime ⇒ Object
96 |
# File 'lib/dentaku/token_matcher.rb', line 96 def self.datetime = new(:datetime) |
.function(name) ⇒ Object
124 |
# File 'lib/dentaku/token_matcher.rb', line 124 def self.function(name) = new(:function, name) |
.if ⇒ Object
126 |
# File 'lib/dentaku/token_matcher.rb', line 126 def self.if = function(:if) |
.logical ⇒ Object
99 |
# File 'lib/dentaku/token_matcher.rb', line 99 def self.logical = new(:logical) |
.mod ⇒ Object
109 |
# File 'lib/dentaku/token_matcher.rb', line 109 def self.mod = new(:operator, :mod) |
.muldiv ⇒ Object
107 |
# File 'lib/dentaku/token_matcher.rb', line 107 def self.muldiv = new(:operator, [:multiply, :divide]) |
.non_close_plus ⇒ Object
121 |
# File 'lib/dentaku/token_matcher.rb', line 121 def self.non_close_plus = new(:grouping, :close).invert.plus |
.non_group ⇒ Object
119 |
# File 'lib/dentaku/token_matcher.rb', line 119 def self.non_group = new(:grouping).invert |
.non_group_star ⇒ Object
120 |
# File 'lib/dentaku/token_matcher.rb', line 120 def self.non_group_star = new(:grouping).invert.star |
.not ⇒ Object
130 |
# File 'lib/dentaku/token_matcher.rb', line 130 def self.not = function(:not) |
.numeric ⇒ Object
97 |
# File 'lib/dentaku/token_matcher.rb', line 97 def self.numeric = new(:numeric) |
.open ⇒ Object
116 |
# File 'lib/dentaku/token_matcher.rb', line 116 def self.open = new(:grouping, :open) |
.pow ⇒ Object
108 |
# File 'lib/dentaku/token_matcher.rb', line 108 def self.pow = new(:operator, :pow) |
.round ⇒ Object
127 |
# File 'lib/dentaku/token_matcher.rb', line 127 def self.round = function(:round) |
.rounddown ⇒ Object
129 |
# File 'lib/dentaku/token_matcher.rb', line 129 def self.rounddown = function(:rounddown) |
.roundup ⇒ Object
128 |
# File 'lib/dentaku/token_matcher.rb', line 128 def self.roundup = function(:roundup) |
.string ⇒ Object
98 |
# File 'lib/dentaku/token_matcher.rb', line 98 def self.string = new(:string) |
.subtract ⇒ Object
105 |
# File 'lib/dentaku/token_matcher.rb', line 105 def self.subtract = new(:operator, :subtract) |
.value ⇒ Object
100 101 102 |
# File 'lib/dentaku/token_matcher.rb', line 100 def self.value new(:datetime) | new(:numeric) | new(:string) | new(:logical) end |
Instance Method Details
#==(token) ⇒ Object
28 29 30 |
# File 'lib/dentaku/token_matcher.rb', line 28 def ==(token) leaf_matcher? ? matches_token?(token) : any_child_matches_token?(token) end |
#caret ⇒ Object
47 48 49 50 |
# File 'lib/dentaku/token_matcher.rb', line 47 def caret @caret = true self end |
#caret? ⇒ Boolean
52 53 54 |
# File 'lib/dentaku/token_matcher.rb', line 52 def caret? @caret end |
#invert ⇒ Object
23 24 25 26 |
# File 'lib/dentaku/token_matcher.rb', line 23 def invert @invert = ! @invert self end |
#leaf_matcher? ⇒ Boolean
69 70 71 |
# File 'lib/dentaku/token_matcher.rb', line 69 def leaf_matcher? children.empty? end |
#leaf_matchers ⇒ Object
73 74 75 |
# File 'lib/dentaku/token_matcher.rb', line 73 def leaf_matchers leaf_matcher? ? [self] : children end |
#match(token_stream, offset = 0) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dentaku/token_matcher.rb', line 32 def match(token_stream, offset = 0) matched_tokens = [] matched = false while self == token_stream[matched_tokens.length + offset] && matched_tokens.length < @max matched_tokens << token_stream[matched_tokens.length + offset] end if @range.cover?(matched_tokens.length) matched = true end [matched, matched_tokens] end |
#plus ⇒ Object
63 64 65 66 67 |
# File 'lib/dentaku/token_matcher.rb', line 63 def plus @max = Float::INFINITY @range = (@min..@max) self end |
#star ⇒ Object
56 57 58 59 60 61 |
# File 'lib/dentaku/token_matcher.rb', line 56 def star @min = 0 @max = Float::INFINITY @range = (@min..@max) self end |
#|(other_matcher) ⇒ Object
19 20 21 |
# File 'lib/dentaku/token_matcher.rb', line 19 def |(other_matcher) self.class.new(:nomatch, :nomatch, leaf_matchers + other_matcher.leaf_matchers) end |