Class: Kanocc::Token
- Inherits:
-
Object
- Object
- Kanocc::Token
- Defined in:
- lib/kanocc/token.rb
Constant Summary collapse
- @@patterns =
Hash.new
Instance Attribute Summary collapse
-
#m ⇒ Object
Returns the value of attribute m.
Class Method Summary collapse
- .is_a_kanocc_grammarsymbol? ⇒ Boolean
- .match(string_scanner) ⇒ Object
- .method(regexp) ⇒ Object
- .pattern(reg, &block) ⇒ Object
- .patterns ⇒ Object
- .regexps ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#m ⇒ Object
Returns the value of attribute m.
20 21 22 |
# File 'lib/kanocc/token.rb', line 20 def m @m end |
Class Method Details
.is_a_kanocc_grammarsymbol? ⇒ Boolean
58 59 60 |
# File 'lib/kanocc/token.rb', line 58 def Token.is_a_kanocc_grammarsymbol? return true end |
.match(string_scanner) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/kanocc/token.rb', line 62 def Token.match(string_scanner) max_match_len = 0 matching_regexp = nil regexps.each do |regexp| if (len = string_scanner.match?(regexp)) and len > max_match_len max_match_len = len matching_regexp = regexp end end if matching_regexp return max_match_len, matching_regexp else return 0, nil end end |
.method(regexp) ⇒ Object
50 51 52 |
# File 'lib/kanocc/token.rb', line 50 def Token.method(regexp) patterns.find {|pattern| pattern[:regexp] == regexp}[:method_name] end |
.pattern(reg, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/kanocc/token.rb', line 28 def Token.pattern(reg, &block) raise "pattern must be given a Regexp as it's first argument" unless reg.is_a?(Regexp) @@patterns[self] = [] unless @@patterns[self] if block_given? method_name = ("pattern " + reg.inspect).to_sym define_method(method_name, &block) else method_name = nil end @@patterns[self] << {:token => self, :regexp => reg, :method_name=>method_name} end |
.patterns ⇒ Object
42 43 44 |
# File 'lib/kanocc/token.rb', line 42 def Token.patterns return @@patterns[self] || [] end |
.regexps ⇒ Object
46 47 48 |
# File 'lib/kanocc/token.rb', line 46 def Token.regexps return (@@patterns[self] || []).map {|pattern| pattern[:regexp]} end |
Instance Method Details
#===(klass) ⇒ Object
24 25 26 |
# File 'lib/kanocc/token.rb', line 24 def ===(klass) self.class == klass end |
#inspect ⇒ Object
78 79 80 |
# File 'lib/kanocc/token.rb', line 78 def inspect self.class.name end |
#is_a_kanocc_token? ⇒ Boolean
54 55 56 |
# File 'lib/kanocc/token.rb', line 54 def is_a_kanocc_token? return true end |