Class: Hilbert::Lexer::Base

Inherits:
Object
  • Object
show all
Includes:
Tokens
Defined in:
lib/hilbert/lexer/base.rb

Direct Known Subclasses

FormulaLexer, MainLexer, WorldLexer

Constant Summary

Constants included from Tokens

Tokens::ANYSP, Tokens::ANYSTR, Tokens::BICO, Tokens::BRCS, Tokens::BRCT, Tokens::CLN, Tokens::CMA, Tokens::COND, Tokens::CONJ, Tokens::DEFLOGIC, Tokens::DIFF_SYM, Tokens::DISJ, Tokens::DIV, Tokens::E, Tokens::EMBEDDED_FUNC, Tokens::EQL, Tokens::EVALOGIC, Tokens::EXP, Tokens::FLO, Tokens::FORMULA, Tokens::FUNCCN, Tokens::FUNCCV, Tokens::FUNCCVN, Tokens::FUNCV, Tokens::INF, Tokens::INT, Tokens::INTE_SYM, Tokens::LBRCS, Tokens::LBRCT, Tokens::LDARW, Tokens::LIM_SYM, Tokens::LPRN, Tokens::LSARW, Tokens::MUL, Tokens::NEGA, Tokens::NLIN, Tokens::NONL, Tokens::NUM, Tokens::NUMS_BY_CMA, Tokens::NUMS_BY_SP, Tokens::NUMS_BY_SP_BY_SCLN_OR_NELN, Tokens::OPE, Tokens::PI, Tokens::PLS, Tokens::PRN, Tokens::PROVAR, Tokens::RBRCS, Tokens::RBRCT, Tokens::RDARW, Tokens::RPRN, Tokens::RSARW, Tokens::SCLN, Tokens::SCLN_OR_NELN, Tokens::SGM_SYM, Tokens::SPC, Tokens::SPCS, Tokens::SUB, Tokens::USER_FUNC, Tokens::VAR, Tokens::VARNUM, Tokens::VARNUMS_BY_CMA, Tokens::VARS_BY_CMA

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Base

Returns a new instance of Base.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hilbert/lexer/base.rb', line 27

def initialize(str)
  ss = StringScanner.new(str)
  @lexeds = []
  until ss.eos?
    scan_rslt, ss = scan(ss)
    if scan_rslt
      @lexeds << scan_rslt unless scan_rslt[:token] == :NULL
    else
      fail <<-ERROR
I'm so sorry, something wrong. Please feel free to report this. [DEBUG CODE30]

========== DEBUG INFO ==========
str:       #{str}
scan_rslt: #{scan_rslt}
@lexeds:   #{@lexeds}

ERROR
    end
  end
end

Class Attribute Details

.token_rule_hashObject (readonly)

Returns the value of attribute token_rule_hash.



10
11
12
# File 'lib/hilbert/lexer/base.rb', line 10

def token_rule_hash
  @token_rule_hash
end

Instance Attribute Details

#lexedsObject

Returns the value of attribute lexeds.



7
8
9
# File 'lib/hilbert/lexer/base.rb', line 7

def lexeds
  @lexeds
end

Class Method Details

.clear!Object



18
19
20
# File 'lib/hilbert/lexer/base.rb', line 18

def clear!
  @token_rule_hash = {}
end

.execute(str) ⇒ Object



22
23
24
# File 'lib/hilbert/lexer/base.rb', line 22

def execute(str)
  new(str).lexeds
end

.rule(pattern, &token) ⇒ Object



12
13
14
15
16
# File 'lib/hilbert/lexer/base.rb', line 12

def rule(pattern, &token)
  token ||= proc { :NULL }
  @token_rule_hash ||= {}
  @token_rule_hash[pattern] = token.call
end

Instance Method Details

#get_els(num) ⇒ Object



70
71
72
73
# File 'lib/hilbert/lexer/base.rb', line 70

def get_els(num)
  num = num.to_i
  @lexeds[num][:els]
end

#get_value(num) ⇒ Object

Accessor GET(without side effect)



65
66
67
68
# File 'lib/hilbert/lexer/base.rb', line 65

def get_value(num)
  num = num.to_i
  @lexeds[num][:value]
end

#parsed!(parsed, target) ⇒ Object

POST(with side effect, without idempotence.)



84
85
86
87
88
89
90
91
# File 'lib/hilbert/lexer/base.rb', line 84

def parsed!(parsed, target)
  case target
  when Range
    parsed_between!((target.first.to_i)..(target.last.to_i), parsed)
  else
    parsed_at!(target.to_i, parsed)
  end
end

#scan(ss) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hilbert/lexer/base.rb', line 48

def scan(ss)
  scan_rslt = nil
  token_rule_hash.each do |pattern, token|
    if ss.scan(pattern)
      scan_rslt = {
        token: token,
        value: ss[0],
        els: 4.times.inject([]) { |s,i|s << ss[i+1] }.compact
      }
      break
    end
  end
  [scan_rslt, ss]
end

#squash!(range, opts = { token: :CONT }) ⇒ Object

squash!(range, token: :CONT)



94
95
96
97
98
99
100
# File 'lib/hilbert/lexer/base.rb', line 94

def squash!(range, opts = { token: :CONT })
  token = opts[:token]
  range = (range.first.to_i)..(range.last.to_i)
  value = values[range].join
  range.count.times { @lexeds.delete_at(range.first) }
  @lexeds.insert(range.first, { token: token, value: value })
end

#token_rule_hashObject



79
80
81
# File 'lib/hilbert/lexer/base.rb', line 79

def token_rule_hash
  self.class.token_rule_hash
end

#token_strObject



75
76
77
# File 'lib/hilbert/lexer/base.rb', line 75

def token_str
  @lexeds.map.with_index { |lexed, i| ":#{lexed[:token]}#{i}" }.join
end

#valuesObject

Legacy Accessor



103
104
105
# File 'lib/hilbert/lexer/base.rb', line 103

def values
  @lexeds.map { |lexed| lexed[:value] }
end