Class: Dentaku::TokenScanner
- Inherits:
-
Object
- Object
- Dentaku::TokenScanner
- Defined in:
- lib/dentaku/token_scanner.rb
Class Method Summary collapse
- .combinator ⇒ Object
- .comparator ⇒ Object
- .double_quoted_string ⇒ Object
- .function ⇒ Object
- .grouping ⇒ Object
- .identifier ⇒ Object
- .numeric ⇒ Object
- .operator ⇒ Object
- .scanners ⇒ Object
- .single_quoted_string ⇒ Object
- .whitespace ⇒ Object
Instance Method Summary collapse
-
#initialize(category, regexp, converter = nil) ⇒ TokenScanner
constructor
A new instance of TokenScanner.
- #scan(string) ⇒ Object
Constructor Details
#initialize(category, regexp, converter = nil) ⇒ TokenScanner
Returns a new instance of TokenScanner.
5 6 7 8 9 |
# File 'lib/dentaku/token_scanner.rb', line 5 def initialize(category, regexp, converter=nil) @category = category @regexp = %r{\A(#{ regexp })}i @converter = converter end |
Class Method Details
.combinator ⇒ Object
72 73 74 |
# File 'lib/dentaku/token_scanner.rb', line 72 def combinator new(:combinator, '(and|or)\b', lambda { |raw| raw.strip.downcase.to_sym }) end |
.comparator ⇒ Object
66 67 68 69 70 |
# File 'lib/dentaku/token_scanner.rb', line 66 def comparator names = { le: '<=', ge: '>=', ne: '!=', lt: '<', gt: '>', eq: '=' }.invert alternate = { ne: '<>', eq: '==' }.invert new(:comparator, '<=|>=|!=|<>|<|>|==|=', lambda { |raw| names[raw] || alternate[raw] }) end |
.double_quoted_string ⇒ Object
48 49 50 |
# File 'lib/dentaku/token_scanner.rb', line 48 def double_quoted_string new(:string, '"[^"]*"', lambda { |raw| raw.gsub(/^"|"$/, '') }) end |
.function ⇒ Object
76 77 78 79 80 81 |
# File 'lib/dentaku/token_scanner.rb', line 76 def function new(:function, '\w+\s*\(', lambda do |raw| function_name = raw.gsub('(', '') [Token.new(:function, function_name.strip.downcase.to_sym, function_name), Token.new(:grouping, :fopen, '(')] end) end |
.grouping ⇒ Object
61 62 63 64 |
# File 'lib/dentaku/token_scanner.rb', line 61 def grouping names = { open: '(', close: ')', comma: ',' }.invert new(:grouping, '\(|\)|,', lambda { |raw| names[raw] }) end |
.identifier ⇒ Object
83 84 85 |
# File 'lib/dentaku/token_scanner.rb', line 83 def identifier new(:identifier, '\w+\b', lambda { |raw| raw.strip.downcase.to_s }) end |
.numeric ⇒ Object
44 45 46 |
# File 'lib/dentaku/token_scanner.rb', line 44 def numeric new(:numeric, '(\d+(\.\d+)?|\.\d+)\b', lambda { |raw| raw =~ /\./ ? BigDecimal.new(raw) : raw.to_i }) end |
.operator ⇒ Object
56 57 58 59 |
# File 'lib/dentaku/token_scanner.rb', line 56 def operator names = { pow: '^', add: '+', subtract: '-', multiply: '*', divide: '/', mod: '%' }.invert new(:operator, '\^|\+|-|\*|\/|%', lambda { |raw| names[raw] }) end |
.scanners ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dentaku/token_scanner.rb', line 25 def scanners @scanners ||= [ whitespace, numeric, double_quoted_string, single_quoted_string, operator, grouping, comparator, combinator, function, identifier ] end |
.single_quoted_string ⇒ Object
52 53 54 |
# File 'lib/dentaku/token_scanner.rb', line 52 def single_quoted_string new(:string, "'[^']*'", lambda { |raw| raw.gsub(/^'|'$/, '') }) end |
.whitespace ⇒ Object
40 41 42 |
# File 'lib/dentaku/token_scanner.rb', line 40 def whitespace new(:whitespace, '\s+') end |
Instance Method Details
#scan(string) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dentaku/token_scanner.rb', line 11 def scan(string) if m = @regexp.match(string) value = raw = m.to_s value = @converter.call(raw) if @converter return Array(value).map do |v| Token === v ? v : Token.new(@category, v, raw) end end false end |